Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * INET An implementation of the TCP/IP protocol suite for the LINUX |
| 4 | * operating system. INET is implemented using the BSD Socket |
| 5 | * interface as the means of communication with the user level. |
| 6 | * |
| 7 | * Routing netlink socket interface: protocol independent part. |
| 8 | * |
| 9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 10 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * Fixes: |
Zheng Yongjun | d467d0b | 2021-06-02 14:56:23 +0800 | [diff] [blame] | 12 | * Vitaly E. Lavrov RTA_OK arithmetic was wrong. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | */ |
| 14 | |
Jakub Kicinski | ee5d032 | 2017-06-21 18:25:04 -0700 | [diff] [blame] | 15 | #include <linux/bitops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/errno.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/types.h> |
| 19 | #include <linux/socket.h> |
| 20 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/timer.h> |
| 22 | #include <linux/string.h> |
| 23 | #include <linux/sockios.h> |
| 24 | #include <linux/net.h> |
| 25 | #include <linux/fcntl.h> |
| 26 | #include <linux/mm.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/capability.h> |
| 30 | #include <linux/skbuff.h> |
| 31 | #include <linux/init.h> |
| 32 | #include <linux/security.h> |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 33 | #include <linux/mutex.h> |
Thomas Graf | 1823730 | 2006-08-04 23:04:54 -0700 | [diff] [blame] | 34 | #include <linux/if_addr.h> |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 35 | #include <linux/if_bridge.h> |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 36 | #include <linux/if_vlan.h> |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 37 | #include <linux/pci.h> |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 38 | #include <linux/etherdevice.h> |
Martin KaFai Lau | 5803869 | 2017-06-15 17:29:09 -0700 | [diff] [blame] | 39 | #include <linux/bpf.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 41 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | #include <linux/inet.h> |
| 44 | #include <linux/netdevice.h> |
| 45 | #include <net/ip.h> |
| 46 | #include <net/protocol.h> |
| 47 | #include <net/arp.h> |
| 48 | #include <net/route.h> |
| 49 | #include <net/udp.h> |
Daniel Borkmann | ea69763 | 2015-01-05 23:57:47 +0100 | [diff] [blame] | 50 | #include <net/tcp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include <net/sock.h> |
| 52 | #include <net/pkt_sched.h> |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 53 | #include <net/fib_rules.h> |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 54 | #include <net/rtnetlink.h> |
Johannes Berg | 30ffee8 | 2009-07-10 09:51:35 +0000 | [diff] [blame] | 55 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Nikolay Aleksandrov | a428afe | 2018-11-24 04:34:20 +0200 | [diff] [blame] | 57 | #define RTNL_MAX_TYPE 50 |
Nikolay Aleksandrov | 2dba407 | 2021-01-26 11:35:33 +0200 | [diff] [blame] | 58 | #define RTNL_SLAVE_MAX_TYPE 40 |
Kees Cook | ccf8dbc | 2018-05-30 15:20:52 -0700 | [diff] [blame] | 59 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 60 | struct rtnl_link { |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 61 | rtnl_doit_func doit; |
| 62 | rtnl_dumpit_func dumpit; |
Florian Westphal | e420251 | 2017-12-02 21:44:06 +0100 | [diff] [blame] | 63 | struct module *owner; |
Florian Westphal | 62256f9 | 2017-08-09 20:41:52 +0200 | [diff] [blame] | 64 | unsigned int flags; |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 65 | struct rcu_head rcu; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 68 | static DEFINE_MUTEX(rtnl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | void rtnl_lock(void) |
| 71 | { |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 72 | mutex_lock(&rtnl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 74 | EXPORT_SYMBOL(rtnl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Kirill Tkhai | 79ffdfc | 2018-03-14 22:17:20 +0300 | [diff] [blame] | 76 | int rtnl_lock_killable(void) |
| 77 | { |
| 78 | return mutex_lock_killable(&rtnl_mutex); |
| 79 | } |
| 80 | EXPORT_SYMBOL(rtnl_lock_killable); |
| 81 | |
Eric Dumazet | 1b5c549 | 2016-06-13 20:21:50 -0700 | [diff] [blame] | 82 | static struct sk_buff *defer_kfree_skb_list; |
| 83 | void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail) |
| 84 | { |
| 85 | if (head && tail) { |
| 86 | tail->next = defer_kfree_skb_list; |
| 87 | defer_kfree_skb_list = head; |
| 88 | } |
| 89 | } |
| 90 | EXPORT_SYMBOL(rtnl_kfree_skbs); |
| 91 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 92 | void __rtnl_unlock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
Eric Dumazet | 1b5c549 | 2016-06-13 20:21:50 -0700 | [diff] [blame] | 94 | struct sk_buff *head = defer_kfree_skb_list; |
| 95 | |
| 96 | defer_kfree_skb_list = NULL; |
| 97 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 98 | mutex_unlock(&rtnl_mutex); |
Eric Dumazet | 1b5c549 | 2016-06-13 20:21:50 -0700 | [diff] [blame] | 99 | |
| 100 | while (head) { |
| 101 | struct sk_buff *next = head->next; |
| 102 | |
| 103 | kfree_skb(head); |
| 104 | cond_resched(); |
| 105 | head = next; |
| 106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | void rtnl_unlock(void) |
| 110 | { |
Herbert Xu | 58ec3b4 | 2008-10-07 15:50:03 -0700 | [diff] [blame] | 111 | /* This fellow will unlock it for us. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | netdev_run_todo(); |
| 113 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 114 | EXPORT_SYMBOL(rtnl_unlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 116 | int rtnl_trylock(void) |
| 117 | { |
| 118 | return mutex_trylock(&rtnl_mutex); |
| 119 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 120 | EXPORT_SYMBOL(rtnl_trylock); |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 121 | |
Patrick McHardy | c9c1014 | 2008-04-23 22:10:48 -0700 | [diff] [blame] | 122 | int rtnl_is_locked(void) |
| 123 | { |
| 124 | return mutex_is_locked(&rtnl_mutex); |
| 125 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 126 | EXPORT_SYMBOL(rtnl_is_locked); |
Patrick McHardy | c9c1014 | 2008-04-23 22:10:48 -0700 | [diff] [blame] | 127 | |
Vlad Buslov | 6f99528 | 2018-09-24 19:22:49 +0300 | [diff] [blame] | 128 | bool refcount_dec_and_rtnl_lock(refcount_t *r) |
| 129 | { |
| 130 | return refcount_dec_and_mutex_lock(r, &rtnl_mutex); |
| 131 | } |
| 132 | EXPORT_SYMBOL(refcount_dec_and_rtnl_lock); |
| 133 | |
Paul E. McKenney | a898def | 2010-02-22 17:04:49 -0800 | [diff] [blame] | 134 | #ifdef CONFIG_PROVE_LOCKING |
Yaowei Bai | 0cbf334 | 2015-10-08 21:29:02 +0800 | [diff] [blame] | 135 | bool lockdep_rtnl_is_held(void) |
Paul E. McKenney | a898def | 2010-02-22 17:04:49 -0800 | [diff] [blame] | 136 | { |
| 137 | return lockdep_is_held(&rtnl_mutex); |
| 138 | } |
| 139 | EXPORT_SYMBOL(lockdep_rtnl_is_held); |
| 140 | #endif /* #ifdef CONFIG_PROVE_LOCKING */ |
| 141 | |
Jakub Kicinski | 51e1368 | 2020-12-09 18:16:08 -0800 | [diff] [blame] | 142 | static struct rtnl_link __rcu *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1]; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 143 | |
| 144 | static inline int rtm_msgindex(int msgtype) |
| 145 | { |
| 146 | int msgindex = msgtype - RTM_BASE; |
| 147 | |
| 148 | /* |
| 149 | * msgindex < 0 implies someone tried to register a netlink |
| 150 | * control code. msgindex >= RTM_NR_MSGTYPES may indicate that |
| 151 | * the message type has not been added to linux/rtnetlink.h |
| 152 | */ |
| 153 | BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); |
| 154 | |
| 155 | return msgindex; |
| 156 | } |
| 157 | |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 158 | static struct rtnl_link *rtnl_get_link(int protocol, int msgtype) |
| 159 | { |
Jakub Kicinski | 51e1368 | 2020-12-09 18:16:08 -0800 | [diff] [blame] | 160 | struct rtnl_link __rcu **tab; |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 161 | |
| 162 | if (protocol >= ARRAY_SIZE(rtnl_msg_handlers)) |
| 163 | protocol = PF_UNSPEC; |
| 164 | |
| 165 | tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]); |
| 166 | if (!tab) |
| 167 | tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]); |
| 168 | |
Jakub Kicinski | 51e1368 | 2020-12-09 18:16:08 -0800 | [diff] [blame] | 169 | return rcu_dereference_rtnl(tab[msgtype]); |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 170 | } |
| 171 | |
Florian Westphal | e420251 | 2017-12-02 21:44:06 +0100 | [diff] [blame] | 172 | static int rtnl_register_internal(struct module *owner, |
| 173 | int protocol, int msgtype, |
| 174 | rtnl_doit_func doit, rtnl_dumpit_func dumpit, |
| 175 | unsigned int flags) |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 176 | { |
Florian Westphal | b0e9fe1 | 2017-12-04 22:42:30 +0100 | [diff] [blame] | 177 | struct rtnl_link *link, *old; |
| 178 | struct rtnl_link __rcu **tab; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 179 | int msgindex; |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 180 | int ret = -ENOBUFS; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 181 | |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 182 | BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 183 | msgindex = rtm_msgindex(msgtype); |
| 184 | |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 185 | rtnl_lock(); |
Jakub Kicinski | 51e1368 | 2020-12-09 18:16:08 -0800 | [diff] [blame] | 186 | tab = rtnl_dereference(rtnl_msg_handlers[protocol]); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 187 | if (tab == NULL) { |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 188 | tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL); |
| 189 | if (!tab) |
| 190 | goto unlock; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 191 | |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 192 | /* ensures we see the 0 stores */ |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 193 | rcu_assign_pointer(rtnl_msg_handlers[protocol], tab); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 196 | old = rtnl_dereference(tab[msgindex]); |
| 197 | if (old) { |
| 198 | link = kmemdup(old, sizeof(*old), GFP_KERNEL); |
| 199 | if (!link) |
| 200 | goto unlock; |
| 201 | } else { |
| 202 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
| 203 | if (!link) |
| 204 | goto unlock; |
| 205 | } |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 206 | |
Florian Westphal | e420251 | 2017-12-02 21:44:06 +0100 | [diff] [blame] | 207 | WARN_ON(link->owner && link->owner != owner); |
| 208 | link->owner = owner; |
| 209 | |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 210 | WARN_ON(doit && link->doit && link->doit != doit); |
| 211 | if (doit) |
| 212 | link->doit = doit; |
| 213 | WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit); |
| 214 | if (dumpit) |
| 215 | link->dumpit = dumpit; |
| 216 | |
| 217 | link->flags |= flags; |
| 218 | |
| 219 | /* publish protocol:msgtype */ |
| 220 | rcu_assign_pointer(tab[msgindex], link); |
| 221 | ret = 0; |
| 222 | if (old) |
| 223 | kfree_rcu(old, rcu); |
| 224 | unlock: |
| 225 | rtnl_unlock(); |
| 226 | return ret; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 227 | } |
Florian Westphal | e420251 | 2017-12-02 21:44:06 +0100 | [diff] [blame] | 228 | |
| 229 | /** |
| 230 | * rtnl_register_module - Register a rtnetlink message type |
| 231 | * |
| 232 | * @owner: module registering the hook (THIS_MODULE) |
| 233 | * @protocol: Protocol family or PF_UNSPEC |
| 234 | * @msgtype: rtnetlink message type |
| 235 | * @doit: Function pointer called for each request message |
| 236 | * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message |
Zheng Yongjun | d467d0b | 2021-06-02 14:56:23 +0800 | [diff] [blame] | 237 | * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions |
Florian Westphal | e420251 | 2017-12-02 21:44:06 +0100 | [diff] [blame] | 238 | * |
| 239 | * Like rtnl_register, but for use by removable modules. |
| 240 | */ |
| 241 | int rtnl_register_module(struct module *owner, |
| 242 | int protocol, int msgtype, |
| 243 | rtnl_doit_func doit, rtnl_dumpit_func dumpit, |
| 244 | unsigned int flags) |
| 245 | { |
| 246 | return rtnl_register_internal(owner, protocol, msgtype, |
| 247 | doit, dumpit, flags); |
| 248 | } |
| 249 | EXPORT_SYMBOL_GPL(rtnl_register_module); |
| 250 | |
| 251 | /** |
Florian Westphal | 16feebc | 2017-12-02 21:44:08 +0100 | [diff] [blame] | 252 | * rtnl_register - Register a rtnetlink message type |
Florian Westphal | e420251 | 2017-12-02 21:44:06 +0100 | [diff] [blame] | 253 | * @protocol: Protocol family or PF_UNSPEC |
| 254 | * @msgtype: rtnetlink message type |
| 255 | * @doit: Function pointer called for each request message |
| 256 | * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message |
Zheng Yongjun | d467d0b | 2021-06-02 14:56:23 +0800 | [diff] [blame] | 257 | * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions |
Florian Westphal | e420251 | 2017-12-02 21:44:06 +0100 | [diff] [blame] | 258 | * |
| 259 | * Registers the specified function pointers (at least one of them has |
| 260 | * to be non-NULL) to be called whenever a request message for the |
| 261 | * specified protocol family and message type is received. |
| 262 | * |
| 263 | * The special protocol family PF_UNSPEC may be used to define fallback |
| 264 | * function pointers for the case when no entry for the specific protocol |
| 265 | * family exists. |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 266 | */ |
| 267 | void rtnl_register(int protocol, int msgtype, |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 268 | rtnl_doit_func doit, rtnl_dumpit_func dumpit, |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 269 | unsigned int flags) |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 270 | { |
Florian Westphal | 16feebc | 2017-12-02 21:44:08 +0100 | [diff] [blame] | 271 | int err; |
| 272 | |
| 273 | err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit, |
| 274 | flags); |
| 275 | if (err) |
| 276 | pr_err("Unable to register rtnetlink message handler, " |
| 277 | "protocol = %d, message type = %d\n", protocol, msgtype); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 278 | } |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 279 | |
| 280 | /** |
| 281 | * rtnl_unregister - Unregister a rtnetlink message type |
| 282 | * @protocol: Protocol family or PF_UNSPEC |
| 283 | * @msgtype: rtnetlink message type |
| 284 | * |
| 285 | * Returns 0 on success or a negative error code. |
| 286 | */ |
| 287 | int rtnl_unregister(int protocol, int msgtype) |
| 288 | { |
Jakub Kicinski | 51e1368 | 2020-12-09 18:16:08 -0800 | [diff] [blame] | 289 | struct rtnl_link __rcu **tab; |
| 290 | struct rtnl_link *link; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 291 | int msgindex; |
| 292 | |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 293 | BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 294 | msgindex = rtm_msgindex(msgtype); |
| 295 | |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 296 | rtnl_lock(); |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 297 | tab = rtnl_dereference(rtnl_msg_handlers[protocol]); |
| 298 | if (!tab) { |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 299 | rtnl_unlock(); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 300 | return -ENOENT; |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 301 | } |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 302 | |
Jakub Kicinski | 51e1368 | 2020-12-09 18:16:08 -0800 | [diff] [blame] | 303 | link = rtnl_dereference(tab[msgindex]); |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 304 | rcu_assign_pointer(tab[msgindex], NULL); |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 305 | rtnl_unlock(); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 306 | |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 307 | kfree_rcu(link, rcu); |
| 308 | |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 309 | return 0; |
| 310 | } |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 311 | EXPORT_SYMBOL_GPL(rtnl_unregister); |
| 312 | |
| 313 | /** |
| 314 | * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol |
| 315 | * @protocol : Protocol family or PF_UNSPEC |
| 316 | * |
| 317 | * Identical to calling rtnl_unregster() for all registered message types |
| 318 | * of a certain protocol family. |
| 319 | */ |
| 320 | void rtnl_unregister_all(int protocol) |
| 321 | { |
Jakub Kicinski | 51e1368 | 2020-12-09 18:16:08 -0800 | [diff] [blame] | 322 | struct rtnl_link __rcu **tab; |
| 323 | struct rtnl_link *link; |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 324 | int msgindex; |
Florian Westphal | 019a316 | 2017-08-09 20:41:49 +0200 | [diff] [blame] | 325 | |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 326 | BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 327 | |
Florian Westphal | 019a316 | 2017-08-09 20:41:49 +0200 | [diff] [blame] | 328 | rtnl_lock(); |
Jakub Kicinski | 51e1368 | 2020-12-09 18:16:08 -0800 | [diff] [blame] | 329 | tab = rtnl_dereference(rtnl_msg_handlers[protocol]); |
Sabrina Dubroca | f707ef6 | 2018-08-28 13:40:53 +0200 | [diff] [blame] | 330 | if (!tab) { |
| 331 | rtnl_unlock(); |
| 332 | return; |
| 333 | } |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 334 | RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL); |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 335 | for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) { |
Jakub Kicinski | 51e1368 | 2020-12-09 18:16:08 -0800 | [diff] [blame] | 336 | link = rtnl_dereference(tab[msgindex]); |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 337 | if (!link) |
| 338 | continue; |
| 339 | |
| 340 | rcu_assign_pointer(tab[msgindex], NULL); |
| 341 | kfree_rcu(link, rcu); |
| 342 | } |
Florian Westphal | 019a316 | 2017-08-09 20:41:49 +0200 | [diff] [blame] | 343 | rtnl_unlock(); |
| 344 | |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 345 | synchronize_net(); |
| 346 | |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 347 | kfree(tab); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 348 | } |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 349 | EXPORT_SYMBOL_GPL(rtnl_unregister_all); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 351 | static LIST_HEAD(link_ops); |
| 352 | |
Eric Dumazet | c63044f | 2011-12-13 11:38:00 +0000 | [diff] [blame] | 353 | static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) |
| 354 | { |
| 355 | const struct rtnl_link_ops *ops; |
| 356 | |
| 357 | list_for_each_entry(ops, &link_ops, list) { |
| 358 | if (!strcmp(ops->kind, kind)) |
| 359 | return ops; |
| 360 | } |
| 361 | return NULL; |
| 362 | } |
| 363 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 364 | /** |
| 365 | * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. |
| 366 | * @ops: struct rtnl_link_ops * to register |
| 367 | * |
| 368 | * The caller must hold the rtnl_mutex. This function should be used |
| 369 | * by drivers that create devices during module initialization. It |
| 370 | * must be called before registering the devices. |
| 371 | * |
| 372 | * Returns 0 on success or a negative error code. |
| 373 | */ |
| 374 | int __rtnl_link_register(struct rtnl_link_ops *ops) |
| 375 | { |
Eric Dumazet | c63044f | 2011-12-13 11:38:00 +0000 | [diff] [blame] | 376 | if (rtnl_link_ops_get(ops->kind)) |
| 377 | return -EEXIST; |
| 378 | |
Johannes Berg | 8c713dc | 2021-06-12 10:20:54 +0200 | [diff] [blame] | 379 | /* The check for alloc/setup is here because if ops |
Jiri Pirko | b0ab2fa | 2014-06-26 09:58:25 +0200 | [diff] [blame] | 380 | * does not have that filled up, it is not possible |
| 381 | * to use the ops for creating device. So do not |
| 382 | * fill up dellink as well. That disables rtnl_dellink. |
| 383 | */ |
Johannes Berg | 8c713dc | 2021-06-12 10:20:54 +0200 | [diff] [blame] | 384 | if ((ops->alloc || ops->setup) && !ops->dellink) |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 385 | ops->dellink = unregister_netdevice_queue; |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 386 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 387 | list_add_tail(&ops->list, &link_ops); |
| 388 | return 0; |
| 389 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 390 | EXPORT_SYMBOL_GPL(__rtnl_link_register); |
| 391 | |
| 392 | /** |
| 393 | * rtnl_link_register - Register rtnl_link_ops with rtnetlink. |
| 394 | * @ops: struct rtnl_link_ops * to register |
| 395 | * |
| 396 | * Returns 0 on success or a negative error code. |
| 397 | */ |
| 398 | int rtnl_link_register(struct rtnl_link_ops *ops) |
| 399 | { |
| 400 | int err; |
| 401 | |
Kees Cook | ccf8dbc | 2018-05-30 15:20:52 -0700 | [diff] [blame] | 402 | /* Sanity-check max sizes to avoid stack buffer overflow. */ |
| 403 | if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE || |
| 404 | ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)) |
| 405 | return -EINVAL; |
| 406 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 407 | rtnl_lock(); |
| 408 | err = __rtnl_link_register(ops); |
| 409 | rtnl_unlock(); |
| 410 | return err; |
| 411 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 412 | EXPORT_SYMBOL_GPL(rtnl_link_register); |
| 413 | |
Pavel Emelyanov | 669f87b | 2008-04-16 00:46:52 -0700 | [diff] [blame] | 414 | static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) |
| 415 | { |
| 416 | struct net_device *dev; |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 417 | LIST_HEAD(list_kill); |
| 418 | |
Pavel Emelyanov | 669f87b | 2008-04-16 00:46:52 -0700 | [diff] [blame] | 419 | for_each_netdev(net, dev) { |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 420 | if (dev->rtnl_link_ops == ops) |
| 421 | ops->dellink(dev, &list_kill); |
Pavel Emelyanov | 669f87b | 2008-04-16 00:46:52 -0700 | [diff] [blame] | 422 | } |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 423 | unregister_netdevice_many(&list_kill); |
Pavel Emelyanov | 669f87b | 2008-04-16 00:46:52 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 426 | /** |
| 427 | * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. |
| 428 | * @ops: struct rtnl_link_ops * to unregister |
| 429 | * |
Kirill Tkhai | 554873e | 2018-03-30 19:38:37 +0300 | [diff] [blame] | 430 | * The caller must hold the rtnl_mutex and guarantee net_namespace_list |
| 431 | * integrity (hold pernet_ops_rwsem for writing to close the race |
| 432 | * with setup_net() and cleanup_net()). |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 433 | */ |
| 434 | void __rtnl_link_unregister(struct rtnl_link_ops *ops) |
| 435 | { |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 436 | struct net *net; |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 437 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 438 | for_each_net(net) { |
Pavel Emelyanov | 669f87b | 2008-04-16 00:46:52 -0700 | [diff] [blame] | 439 | __rtnl_kill_links(net, ops); |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 440 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 441 | list_del(&ops->list); |
| 442 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 443 | EXPORT_SYMBOL_GPL(__rtnl_link_unregister); |
| 444 | |
Cong Wang | 200b916 | 2014-05-12 15:11:20 -0700 | [diff] [blame] | 445 | /* Return with the rtnl_lock held when there are no network |
| 446 | * devices unregistering in any network namespace. |
| 447 | */ |
| 448 | static void rtnl_lock_unregistering_all(void) |
| 449 | { |
| 450 | struct net *net; |
| 451 | bool unregistering; |
Peter Zijlstra | ff960a7 | 2014-10-29 17:04:56 +0100 | [diff] [blame] | 452 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
Cong Wang | 200b916 | 2014-05-12 15:11:20 -0700 | [diff] [blame] | 453 | |
Peter Zijlstra | ff960a7 | 2014-10-29 17:04:56 +0100 | [diff] [blame] | 454 | add_wait_queue(&netdev_unregistering_wq, &wait); |
Cong Wang | 200b916 | 2014-05-12 15:11:20 -0700 | [diff] [blame] | 455 | for (;;) { |
Cong Wang | 200b916 | 2014-05-12 15:11:20 -0700 | [diff] [blame] | 456 | unregistering = false; |
| 457 | rtnl_lock(); |
Kirill Tkhai | f0b07bb1 | 2018-03-29 19:20:32 +0300 | [diff] [blame] | 458 | /* We held write locked pernet_ops_rwsem, and parallel |
| 459 | * setup_net() and cleanup_net() are not possible. |
| 460 | */ |
Cong Wang | 200b916 | 2014-05-12 15:11:20 -0700 | [diff] [blame] | 461 | for_each_net(net) { |
| 462 | if (net->dev_unreg_count > 0) { |
| 463 | unregistering = true; |
| 464 | break; |
| 465 | } |
| 466 | } |
| 467 | if (!unregistering) |
| 468 | break; |
| 469 | __rtnl_unlock(); |
Peter Zijlstra | ff960a7 | 2014-10-29 17:04:56 +0100 | [diff] [blame] | 470 | |
| 471 | wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
Cong Wang | 200b916 | 2014-05-12 15:11:20 -0700 | [diff] [blame] | 472 | } |
Peter Zijlstra | ff960a7 | 2014-10-29 17:04:56 +0100 | [diff] [blame] | 473 | remove_wait_queue(&netdev_unregistering_wq, &wait); |
Cong Wang | 200b916 | 2014-05-12 15:11:20 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 476 | /** |
| 477 | * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. |
| 478 | * @ops: struct rtnl_link_ops * to unregister |
| 479 | */ |
| 480 | void rtnl_link_unregister(struct rtnl_link_ops *ops) |
| 481 | { |
Kirill Tkhai | 8518e9b | 2018-03-27 18:02:32 +0300 | [diff] [blame] | 482 | /* Close the race with setup_net() and cleanup_net() */ |
Kirill Tkhai | 4420bf2 | 2018-03-27 18:02:23 +0300 | [diff] [blame] | 483 | down_write(&pernet_ops_rwsem); |
Cong Wang | 200b916 | 2014-05-12 15:11:20 -0700 | [diff] [blame] | 484 | rtnl_lock_unregistering_all(); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 485 | __rtnl_link_unregister(ops); |
| 486 | rtnl_unlock(); |
Kirill Tkhai | 4420bf2 | 2018-03-27 18:02:23 +0300 | [diff] [blame] | 487 | up_write(&pernet_ops_rwsem); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 488 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 489 | EXPORT_SYMBOL_GPL(rtnl_link_unregister); |
| 490 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 491 | static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev) |
| 492 | { |
| 493 | struct net_device *master_dev; |
| 494 | const struct rtnl_link_ops *ops; |
Florian Westphal | 8515ae3 | 2017-08-10 16:52:59 +0200 | [diff] [blame] | 495 | size_t size = 0; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 496 | |
Florian Westphal | 8515ae3 | 2017-08-10 16:52:59 +0200 | [diff] [blame] | 497 | rcu_read_lock(); |
| 498 | |
| 499 | master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev); |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 500 | if (!master_dev) |
Florian Westphal | 8515ae3 | 2017-08-10 16:52:59 +0200 | [diff] [blame] | 501 | goto out; |
| 502 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 503 | ops = master_dev->rtnl_link_ops; |
Fernando Luis Vazquez Cao | 6049f25 | 2014-02-04 19:35:02 +0900 | [diff] [blame] | 504 | if (!ops || !ops->get_slave_size) |
Florian Westphal | 8515ae3 | 2017-08-10 16:52:59 +0200 | [diff] [blame] | 505 | goto out; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 506 | /* IFLA_INFO_SLAVE_DATA + nested data */ |
Florian Westphal | 8515ae3 | 2017-08-10 16:52:59 +0200 | [diff] [blame] | 507 | size = nla_total_size(sizeof(struct nlattr)) + |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 508 | ops->get_slave_size(master_dev, dev); |
Florian Westphal | 8515ae3 | 2017-08-10 16:52:59 +0200 | [diff] [blame] | 509 | |
| 510 | out: |
| 511 | rcu_read_unlock(); |
| 512 | return size; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 513 | } |
| 514 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 515 | static size_t rtnl_link_get_size(const struct net_device *dev) |
| 516 | { |
| 517 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; |
| 518 | size_t size; |
| 519 | |
| 520 | if (!ops) |
| 521 | return 0; |
| 522 | |
Thomas Graf | 369cf77 | 2010-11-11 15:47:59 +0000 | [diff] [blame] | 523 | size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ |
| 524 | nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 525 | |
| 526 | if (ops->get_size) |
| 527 | /* IFLA_INFO_DATA + nested data */ |
Thomas Graf | 369cf77 | 2010-11-11 15:47:59 +0000 | [diff] [blame] | 528 | size += nla_total_size(sizeof(struct nlattr)) + |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 529 | ops->get_size(dev); |
| 530 | |
| 531 | if (ops->get_xstats_size) |
Thomas Graf | 369cf77 | 2010-11-11 15:47:59 +0000 | [diff] [blame] | 532 | /* IFLA_INFO_XSTATS */ |
| 533 | size += nla_total_size(ops->get_xstats_size(dev)); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 534 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 535 | size += rtnl_link_get_slave_info_data_size(dev); |
| 536 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 537 | return size; |
| 538 | } |
| 539 | |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 540 | static LIST_HEAD(rtnl_af_ops); |
| 541 | |
| 542 | static const struct rtnl_af_ops *rtnl_af_lookup(const int family) |
| 543 | { |
| 544 | const struct rtnl_af_ops *ops; |
| 545 | |
Cong Wang | a100243 | 2021-05-08 11:00:33 -0700 | [diff] [blame] | 546 | ASSERT_RTNL(); |
| 547 | |
| 548 | list_for_each_entry(ops, &rtnl_af_ops, list) { |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 549 | if (ops->family == family) |
| 550 | return ops; |
| 551 | } |
| 552 | |
| 553 | return NULL; |
| 554 | } |
| 555 | |
| 556 | /** |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 557 | * rtnl_af_register - Register rtnl_af_ops with rtnetlink. |
| 558 | * @ops: struct rtnl_af_ops * to register |
| 559 | * |
| 560 | * Returns 0 on success or a negative error code. |
| 561 | */ |
stephen hemminger | 3678a9d | 2013-12-30 10:41:32 -0800 | [diff] [blame] | 562 | void rtnl_af_register(struct rtnl_af_ops *ops) |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 563 | { |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 564 | rtnl_lock(); |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 565 | list_add_tail_rcu(&ops->list, &rtnl_af_ops); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 566 | rtnl_unlock(); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 567 | } |
| 568 | EXPORT_SYMBOL_GPL(rtnl_af_register); |
| 569 | |
| 570 | /** |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 571 | * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. |
| 572 | * @ops: struct rtnl_af_ops * to unregister |
| 573 | */ |
| 574 | void rtnl_af_unregister(struct rtnl_af_ops *ops) |
| 575 | { |
| 576 | rtnl_lock(); |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 577 | list_del_rcu(&ops->list); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 578 | rtnl_unlock(); |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 579 | |
| 580 | synchronize_rcu(); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 581 | } |
| 582 | EXPORT_SYMBOL_GPL(rtnl_af_unregister); |
| 583 | |
Arad, Ronen | b1974ed | 2015-10-19 09:23:28 -0700 | [diff] [blame] | 584 | static size_t rtnl_link_get_af_size(const struct net_device *dev, |
| 585 | u32 ext_filter_mask) |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 586 | { |
| 587 | struct rtnl_af_ops *af_ops; |
| 588 | size_t size; |
| 589 | |
| 590 | /* IFLA_AF_SPEC */ |
| 591 | size = nla_total_size(sizeof(struct nlattr)); |
| 592 | |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 593 | rcu_read_lock(); |
| 594 | list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 595 | if (af_ops->get_link_af_size) { |
| 596 | /* AF_* + nested data */ |
| 597 | size += nla_total_size(sizeof(struct nlattr)) + |
Arad, Ronen | b1974ed | 2015-10-19 09:23:28 -0700 | [diff] [blame] | 598 | af_ops->get_link_af_size(dev, ext_filter_mask); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 599 | } |
| 600 | } |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 601 | rcu_read_unlock(); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 602 | |
| 603 | return size; |
| 604 | } |
| 605 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 606 | static bool rtnl_have_link_slave_info(const struct net_device *dev) |
| 607 | { |
| 608 | struct net_device *master_dev; |
Florian Westphal | 4c82a95 | 2017-09-26 13:58:43 +0200 | [diff] [blame] | 609 | bool ret = false; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 610 | |
Florian Westphal | 4c82a95 | 2017-09-26 13:58:43 +0200 | [diff] [blame] | 611 | rcu_read_lock(); |
| 612 | |
| 613 | master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev); |
Jiri Pirko | 813f020 | 2014-01-23 19:19:21 +0100 | [diff] [blame] | 614 | if (master_dev && master_dev->rtnl_link_ops) |
Florian Westphal | 4c82a95 | 2017-09-26 13:58:43 +0200 | [diff] [blame] | 615 | ret = true; |
| 616 | rcu_read_unlock(); |
| 617 | return ret; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | static int rtnl_link_slave_info_fill(struct sk_buff *skb, |
| 621 | const struct net_device *dev) |
| 622 | { |
| 623 | struct net_device *master_dev; |
| 624 | const struct rtnl_link_ops *ops; |
| 625 | struct nlattr *slave_data; |
| 626 | int err; |
| 627 | |
| 628 | master_dev = netdev_master_upper_dev_get((struct net_device *) dev); |
| 629 | if (!master_dev) |
| 630 | return 0; |
| 631 | ops = master_dev->rtnl_link_ops; |
| 632 | if (!ops) |
| 633 | return 0; |
| 634 | if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0) |
| 635 | return -EMSGSIZE; |
| 636 | if (ops->fill_slave_info) { |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 637 | slave_data = nla_nest_start_noflag(skb, IFLA_INFO_SLAVE_DATA); |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 638 | if (!slave_data) |
| 639 | return -EMSGSIZE; |
| 640 | err = ops->fill_slave_info(skb, master_dev, dev); |
| 641 | if (err < 0) |
| 642 | goto err_cancel_slave_data; |
| 643 | nla_nest_end(skb, slave_data); |
| 644 | } |
| 645 | return 0; |
| 646 | |
| 647 | err_cancel_slave_data: |
| 648 | nla_nest_cancel(skb, slave_data); |
| 649 | return err; |
| 650 | } |
| 651 | |
| 652 | static int rtnl_link_info_fill(struct sk_buff *skb, |
| 653 | const struct net_device *dev) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 654 | { |
| 655 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 656 | struct nlattr *data; |
| 657 | int err; |
| 658 | |
| 659 | if (!ops) |
| 660 | return 0; |
| 661 | if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) |
| 662 | return -EMSGSIZE; |
| 663 | if (ops->fill_xstats) { |
| 664 | err = ops->fill_xstats(skb, dev); |
| 665 | if (err < 0) |
| 666 | return err; |
| 667 | } |
| 668 | if (ops->fill_info) { |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 669 | data = nla_nest_start_noflag(skb, IFLA_INFO_DATA); |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 670 | if (data == NULL) |
| 671 | return -EMSGSIZE; |
| 672 | err = ops->fill_info(skb, dev); |
| 673 | if (err < 0) |
| 674 | goto err_cancel_data; |
| 675 | nla_nest_end(skb, data); |
| 676 | } |
| 677 | return 0; |
| 678 | |
| 679 | err_cancel_data: |
| 680 | nla_nest_cancel(skb, data); |
| 681 | return err; |
| 682 | } |
| 683 | |
| 684 | static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) |
| 685 | { |
| 686 | struct nlattr *linkinfo; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 687 | int err = -EMSGSIZE; |
| 688 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 689 | linkinfo = nla_nest_start_noflag(skb, IFLA_LINKINFO); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 690 | if (linkinfo == NULL) |
| 691 | goto out; |
| 692 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 693 | err = rtnl_link_info_fill(skb, dev); |
| 694 | if (err < 0) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 695 | goto err_cancel_link; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 696 | |
| 697 | err = rtnl_link_slave_info_fill(skb, dev); |
| 698 | if (err < 0) |
| 699 | goto err_cancel_link; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 700 | |
| 701 | nla_nest_end(skb, linkinfo); |
| 702 | return 0; |
| 703 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 704 | err_cancel_link: |
| 705 | nla_nest_cancel(skb, linkinfo); |
| 706 | out: |
| 707 | return err; |
| 708 | } |
| 709 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 710 | int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 712 | struct sock *rtnl = net->rtnl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | |
Yajun Deng | cfdf0d9 | 2021-07-15 20:12:57 +0800 | [diff] [blame] | 714 | return nlmsg_notify(rtnl, skb, pid, group, echo, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 717 | int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) |
Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 718 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 719 | struct sock *rtnl = net->rtnl; |
| 720 | |
Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 721 | return nlmsg_unicast(rtnl, skb, pid); |
| 722 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 723 | EXPORT_SYMBOL(rtnl_unicast); |
Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 724 | |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 725 | void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, |
| 726 | struct nlmsghdr *nlh, gfp_t flags) |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 727 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 728 | struct sock *rtnl = net->rtnl; |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 729 | |
Yajun Deng | f9b282b | 2021-07-27 11:41:41 +0800 | [diff] [blame] | 730 | nlmsg_notify(rtnl, skb, pid, group, nlmsg_report(nlh), flags); |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 731 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 732 | EXPORT_SYMBOL(rtnl_notify); |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 733 | |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 734 | void rtnl_set_sk_err(struct net *net, u32 group, int error) |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 735 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 736 | struct sock *rtnl = net->rtnl; |
| 737 | |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 738 | netlink_set_err(rtnl, 0, group, error); |
| 739 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 740 | EXPORT_SYMBOL(rtnl_set_sk_err); |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) |
| 743 | { |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 744 | struct nlattr *mx; |
| 745 | int i, valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | |
David Ahern | c22a133 | 2019-06-21 16:27:16 -0700 | [diff] [blame] | 747 | /* nothing is dumped for dst_default_metrics, so just skip the loop */ |
| 748 | if (metrics == dst_default_metrics.metrics) |
| 749 | return 0; |
| 750 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 751 | mx = nla_nest_start_noflag(skb, RTA_METRICS); |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 752 | if (mx == NULL) |
| 753 | return -ENOBUFS; |
| 754 | |
| 755 | for (i = 0; i < RTAX_MAX; i++) { |
| 756 | if (metrics[i]) { |
Daniel Borkmann | ea69763 | 2015-01-05 23:57:47 +0100 | [diff] [blame] | 757 | if (i == RTAX_CC_ALGO - 1) { |
| 758 | char tmp[TCP_CA_NAME_MAX], *name; |
| 759 | |
| 760 | name = tcp_ca_get_name_by_key(metrics[i], tmp); |
| 761 | if (!name) |
| 762 | continue; |
| 763 | if (nla_put_string(skb, i + 1, name)) |
| 764 | goto nla_put_failure; |
Daniel Borkmann | c3a8d94 | 2015-08-31 15:58:47 +0200 | [diff] [blame] | 765 | } else if (i == RTAX_FEATURES - 1) { |
| 766 | u32 user_features = metrics[i] & RTAX_FEATURE_MASK; |
| 767 | |
Phil Sutter | f8edcd1 | 2016-08-23 13:14:31 +0200 | [diff] [blame] | 768 | if (!user_features) |
| 769 | continue; |
Daniel Borkmann | c3a8d94 | 2015-08-31 15:58:47 +0200 | [diff] [blame] | 770 | BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK); |
| 771 | if (nla_put_u32(skb, i + 1, user_features)) |
| 772 | goto nla_put_failure; |
Daniel Borkmann | ea69763 | 2015-01-05 23:57:47 +0100 | [diff] [blame] | 773 | } else { |
| 774 | if (nla_put_u32(skb, i + 1, metrics[i])) |
| 775 | goto nla_put_failure; |
| 776 | } |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 777 | valid++; |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 778 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | |
David S. Miller | a57d27f | 2006-08-22 22:20:14 -0700 | [diff] [blame] | 781 | if (!valid) { |
| 782 | nla_nest_cancel(skb, mx); |
| 783 | return 0; |
| 784 | } |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 785 | |
| 786 | return nla_nest_end(skb, mx); |
| 787 | |
| 788 | nla_put_failure: |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 789 | nla_nest_cancel(skb, mx); |
| 790 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 792 | EXPORT_SYMBOL(rtnetlink_put_metrics); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 794 | int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, |
David S. Miller | 87a5069 | 2012-07-10 05:06:14 -0700 | [diff] [blame] | 795 | long expires, u32 error) |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 796 | { |
| 797 | struct rta_cacheinfo ci = { |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 798 | .rta_error = error, |
| 799 | .rta_id = id, |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 800 | }; |
| 801 | |
David Ahern | 3940746 | 2018-04-17 17:33:08 -0700 | [diff] [blame] | 802 | if (dst) { |
| 803 | ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse); |
| 804 | ci.rta_used = dst->__use; |
| 805 | ci.rta_clntref = atomic_read(&dst->__refcnt); |
| 806 | } |
Li Wei | 8253947 | 2012-07-29 16:01:30 +0000 | [diff] [blame] | 807 | if (expires) { |
| 808 | unsigned long clock; |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 809 | |
Li Wei | 8253947 | 2012-07-29 16:01:30 +0000 | [diff] [blame] | 810 | clock = jiffies_to_clock_t(abs(expires)); |
| 811 | clock = min_t(unsigned long, clock, INT_MAX); |
| 812 | ci.rta_expires = (expires > 0) ? clock : -clock; |
| 813 | } |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 814 | return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); |
| 815 | } |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 816 | EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 818 | static void set_operstate(struct net_device *dev, unsigned char transition) |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 819 | { |
| 820 | unsigned char operstate = dev->operstate; |
| 821 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 822 | switch (transition) { |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 823 | case IF_OPER_UP: |
| 824 | if ((operstate == IF_OPER_DORMANT || |
Andrew Lunn | eec517cd | 2020-04-20 00:11:50 +0200 | [diff] [blame] | 825 | operstate == IF_OPER_TESTING || |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 826 | operstate == IF_OPER_UNKNOWN) && |
Andrew Lunn | eec517cd | 2020-04-20 00:11:50 +0200 | [diff] [blame] | 827 | !netif_dormant(dev) && !netif_testing(dev)) |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 828 | operstate = IF_OPER_UP; |
| 829 | break; |
| 830 | |
Andrew Lunn | eec517cd | 2020-04-20 00:11:50 +0200 | [diff] [blame] | 831 | case IF_OPER_TESTING: |
| 832 | if (operstate == IF_OPER_UP || |
| 833 | operstate == IF_OPER_UNKNOWN) |
| 834 | operstate = IF_OPER_TESTING; |
| 835 | break; |
| 836 | |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 837 | case IF_OPER_DORMANT: |
| 838 | if (operstate == IF_OPER_UP || |
| 839 | operstate == IF_OPER_UNKNOWN) |
| 840 | operstate = IF_OPER_DORMANT; |
| 841 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 842 | } |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 843 | |
| 844 | if (dev->operstate != operstate) { |
| 845 | write_lock_bh(&dev_base_lock); |
| 846 | dev->operstate = operstate; |
| 847 | write_unlock_bh(&dev_base_lock); |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 848 | netdev_state_change(dev); |
| 849 | } |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 850 | } |
| 851 | |
Jiri Benc | b1beb68 | 2012-07-27 02:58:22 +0000 | [diff] [blame] | 852 | static unsigned int rtnl_dev_get_flags(const struct net_device *dev) |
| 853 | { |
| 854 | return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) | |
| 855 | (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI)); |
| 856 | } |
| 857 | |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 858 | static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, |
| 859 | const struct ifinfomsg *ifm) |
| 860 | { |
| 861 | unsigned int flags = ifm->ifi_flags; |
| 862 | |
| 863 | /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ |
| 864 | if (ifm->ifi_change) |
| 865 | flags = (flags & ifm->ifi_change) | |
Jiri Benc | b1beb68 | 2012-07-27 02:58:22 +0000 | [diff] [blame] | 866 | (rtnl_dev_get_flags(dev) & ~ifm->ifi_change); |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 867 | |
| 868 | return flags; |
| 869 | } |
| 870 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 871 | static void copy_rtnl_link_stats(struct rtnl_link_stats *a, |
Ben Hutchings | be1f3c2 | 2010-06-08 07:19:54 +0000 | [diff] [blame] | 872 | const struct rtnl_link_stats64 *b) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | { |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 874 | a->rx_packets = b->rx_packets; |
| 875 | a->tx_packets = b->tx_packets; |
| 876 | a->rx_bytes = b->rx_bytes; |
| 877 | a->tx_bytes = b->tx_bytes; |
| 878 | a->rx_errors = b->rx_errors; |
| 879 | a->tx_errors = b->tx_errors; |
| 880 | a->rx_dropped = b->rx_dropped; |
| 881 | a->tx_dropped = b->tx_dropped; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 883 | a->multicast = b->multicast; |
| 884 | a->collisions = b->collisions; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 886 | a->rx_length_errors = b->rx_length_errors; |
| 887 | a->rx_over_errors = b->rx_over_errors; |
| 888 | a->rx_crc_errors = b->rx_crc_errors; |
| 889 | a->rx_frame_errors = b->rx_frame_errors; |
| 890 | a->rx_fifo_errors = b->rx_fifo_errors; |
| 891 | a->rx_missed_errors = b->rx_missed_errors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 893 | a->tx_aborted_errors = b->tx_aborted_errors; |
| 894 | a->tx_carrier_errors = b->tx_carrier_errors; |
| 895 | a->tx_fifo_errors = b->tx_fifo_errors; |
| 896 | a->tx_heartbeat_errors = b->tx_heartbeat_errors; |
| 897 | a->tx_window_errors = b->tx_window_errors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 899 | a->rx_compressed = b->rx_compressed; |
| 900 | a->tx_compressed = b->tx_compressed; |
Jarod Wilson | 6e7333d | 2016-02-01 18:51:05 -0500 | [diff] [blame] | 901 | |
| 902 | a->rx_nohandler = b->rx_nohandler; |
Jan Engelhardt | 10708f3 | 2010-03-11 09:57:29 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 905 | /* All VF info */ |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 906 | static inline int rtnl_vfinfo_size(const struct net_device *dev, |
| 907 | u32 ext_filter_mask) |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 908 | { |
Phil Sutter | 9af15c3 | 2017-01-18 14:04:39 +0100 | [diff] [blame] | 909 | if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) { |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 910 | int num_vfs = dev_num_vf(dev->dev.parent); |
Sabrina Dubroca | 7e75f74 | 2016-11-15 10:39:03 +0100 | [diff] [blame] | 911 | size_t size = nla_total_size(0); |
Scott Feldman | 045de01 | 2010-05-28 03:42:43 -0700 | [diff] [blame] | 912 | size += num_vfs * |
Sabrina Dubroca | 7e75f74 | 2016-11-15 10:39:03 +0100 | [diff] [blame] | 913 | (nla_total_size(0) + |
| 914 | nla_total_size(sizeof(struct ifla_vf_mac)) + |
Denis Kirjanov | 75345f88 | 2019-06-17 10:53:41 +0200 | [diff] [blame] | 915 | nla_total_size(sizeof(struct ifla_vf_broadcast)) + |
Sabrina Dubroca | 7e75f74 | 2016-11-15 10:39:03 +0100 | [diff] [blame] | 916 | nla_total_size(sizeof(struct ifla_vf_vlan)) + |
| 917 | nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */ |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 918 | nla_total_size(MAX_VLAN_LIST_LEN * |
| 919 | sizeof(struct ifla_vf_vlan_info)) + |
Sucheta Chakraborty | ed61668 | 2014-05-22 09:59:05 -0400 | [diff] [blame] | 920 | nla_total_size(sizeof(struct ifla_vf_spoofchk)) + |
Sabrina Dubroca | 7e75f74 | 2016-11-15 10:39:03 +0100 | [diff] [blame] | 921 | nla_total_size(sizeof(struct ifla_vf_tx_rate)) + |
Jiri Benc | 945a367 | 2014-08-08 16:44:32 +0200 | [diff] [blame] | 922 | nla_total_size(sizeof(struct ifla_vf_rate)) + |
Vlad Zolotarov | 01a3d79 | 2015-03-30 21:35:23 +0300 | [diff] [blame] | 923 | nla_total_size(sizeof(struct ifla_vf_link_state)) + |
Eran Ben Elisha | 3b766cd | 2015-06-15 17:59:07 +0300 | [diff] [blame] | 924 | nla_total_size(sizeof(struct ifla_vf_rss_query_en)) + |
Sabrina Dubroca | 7e75f74 | 2016-11-15 10:39:03 +0100 | [diff] [blame] | 925 | nla_total_size(0) + /* nest IFLA_VF_STATS */ |
Eran Ben Elisha | 3b766cd | 2015-06-15 17:59:07 +0300 | [diff] [blame] | 926 | /* IFLA_VF_STATS_RX_PACKETS */ |
Nicolas Dichtel | 343a6d8 | 2016-04-25 10:25:14 +0200 | [diff] [blame] | 927 | nla_total_size_64bit(sizeof(__u64)) + |
Eran Ben Elisha | 3b766cd | 2015-06-15 17:59:07 +0300 | [diff] [blame] | 928 | /* IFLA_VF_STATS_TX_PACKETS */ |
Nicolas Dichtel | 343a6d8 | 2016-04-25 10:25:14 +0200 | [diff] [blame] | 929 | nla_total_size_64bit(sizeof(__u64)) + |
Eran Ben Elisha | 3b766cd | 2015-06-15 17:59:07 +0300 | [diff] [blame] | 930 | /* IFLA_VF_STATS_RX_BYTES */ |
Nicolas Dichtel | 343a6d8 | 2016-04-25 10:25:14 +0200 | [diff] [blame] | 931 | nla_total_size_64bit(sizeof(__u64)) + |
Eran Ben Elisha | 3b766cd | 2015-06-15 17:59:07 +0300 | [diff] [blame] | 932 | /* IFLA_VF_STATS_TX_BYTES */ |
Nicolas Dichtel | 343a6d8 | 2016-04-25 10:25:14 +0200 | [diff] [blame] | 933 | nla_total_size_64bit(sizeof(__u64)) + |
Eran Ben Elisha | 3b766cd | 2015-06-15 17:59:07 +0300 | [diff] [blame] | 934 | /* IFLA_VF_STATS_BROADCAST */ |
Nicolas Dichtel | 343a6d8 | 2016-04-25 10:25:14 +0200 | [diff] [blame] | 935 | nla_total_size_64bit(sizeof(__u64)) + |
Eran Ben Elisha | 3b766cd | 2015-06-15 17:59:07 +0300 | [diff] [blame] | 936 | /* IFLA_VF_STATS_MULTICAST */ |
Nicolas Dichtel | 343a6d8 | 2016-04-25 10:25:14 +0200 | [diff] [blame] | 937 | nla_total_size_64bit(sizeof(__u64)) + |
Eugenia Emantayev | c5a9f6f | 2017-07-17 13:47:07 +0300 | [diff] [blame] | 938 | /* IFLA_VF_STATS_RX_DROPPED */ |
| 939 | nla_total_size_64bit(sizeof(__u64)) + |
| 940 | /* IFLA_VF_STATS_TX_DROPPED */ |
| 941 | nla_total_size_64bit(sizeof(__u64)) + |
Hiroshi Shimamoto | dd461d6 | 2015-08-28 06:57:55 +0000 | [diff] [blame] | 942 | nla_total_size(sizeof(struct ifla_vf_trust))); |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 943 | return size; |
| 944 | } else |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 945 | return 0; |
| 946 | } |
| 947 | |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 948 | static size_t rtnl_port_size(const struct net_device *dev, |
| 949 | u32 ext_filter_mask) |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 950 | { |
| 951 | size_t port_size = nla_total_size(4) /* PORT_VF */ |
| 952 | + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 953 | + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ |
| 954 | + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ |
| 955 | + nla_total_size(1) /* PROT_VDP_REQUEST */ |
| 956 | + nla_total_size(2); /* PORT_VDP_RESPONSE */ |
| 957 | size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); |
| 958 | size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) |
| 959 | + port_size; |
| 960 | size_t port_self_size = nla_total_size(sizeof(struct nlattr)) |
| 961 | + port_size; |
| 962 | |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 963 | if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || |
| 964 | !(ext_filter_mask & RTEXT_FILTER_VF)) |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 965 | return 0; |
| 966 | if (dev_num_vf(dev->dev.parent)) |
| 967 | return port_self_size + vf_ports_size + |
| 968 | vf_port_size * dev_num_vf(dev->dev.parent); |
| 969 | else |
| 970 | return port_self_size; |
| 971 | } |
| 972 | |
David S. Miller | b5cdae3 | 2017-04-18 15:36:58 -0400 | [diff] [blame] | 973 | static size_t rtnl_xdp_size(void) |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 974 | { |
Sabrina Dubroca | b3cfaa3 | 2016-11-15 11:16:35 +0100 | [diff] [blame] | 975 | size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */ |
Martin KaFai Lau | 5803869 | 2017-06-15 17:29:09 -0700 | [diff] [blame] | 976 | nla_total_size(1) + /* XDP_ATTACHED */ |
Jakub Kicinski | a25717d | 2018-07-11 20:36:41 -0700 | [diff] [blame] | 977 | nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */ |
Jakub Kicinski | 4f91da2 | 2018-07-11 20:36:38 -0700 | [diff] [blame] | 978 | nla_total_size(4); /* XDP_<mode>_PROG_ID */ |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 979 | |
David S. Miller | b5cdae3 | 2017-04-18 15:36:58 -0400 | [diff] [blame] | 980 | return xdp_size; |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 981 | } |
| 982 | |
Jiri Pirko | 88f4fb0 | 2019-09-30 11:48:17 +0200 | [diff] [blame] | 983 | static size_t rtnl_prop_list_size(const struct net_device *dev) |
| 984 | { |
| 985 | struct netdev_name_node *name_node; |
| 986 | size_t size; |
| 987 | |
| 988 | if (list_empty(&dev->name_node->list)) |
| 989 | return 0; |
| 990 | size = nla_total_size(0); |
| 991 | list_for_each_entry(name_node, &dev->name_node->list, list) |
| 992 | size += nla_total_size(ALTIFNAMSIZ); |
| 993 | return size; |
| 994 | } |
| 995 | |
Roopa Prabhu | 829eb20 | 2020-07-31 17:34:01 -0700 | [diff] [blame] | 996 | static size_t rtnl_proto_down_size(const struct net_device *dev) |
| 997 | { |
| 998 | size_t size = nla_total_size(1); |
| 999 | |
| 1000 | if (dev->proto_down_reason) |
| 1001 | size += nla_total_size(0) + nla_total_size(4); |
| 1002 | |
| 1003 | return size; |
| 1004 | } |
| 1005 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1006 | static noinline size_t if_nlmsg_size(const struct net_device *dev, |
| 1007 | u32 ext_filter_mask) |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 1008 | { |
| 1009 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) |
| 1010 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 1011 | + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 1012 | + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ |
Nicolas Dichtel | 270cb4d | 2016-04-26 10:06:16 +0200 | [diff] [blame] | 1013 | + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap)) |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 1014 | + nla_total_size(sizeof(struct rtnl_link_stats)) |
David S. Miller | 35c5845 | 2016-04-19 19:49:29 -0400 | [diff] [blame] | 1015 | + nla_total_size_64bit(sizeof(struct rtnl_link_stats64)) |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 1016 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ |
| 1017 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ |
| 1018 | + nla_total_size(4) /* IFLA_TXQLEN */ |
| 1019 | + nla_total_size(4) /* IFLA_WEIGHT */ |
| 1020 | + nla_total_size(4) /* IFLA_MTU */ |
| 1021 | + nla_total_size(4) /* IFLA_LINK */ |
| 1022 | + nla_total_size(4) /* IFLA_MASTER */ |
Jiri Pirko | 9a57247 | 2012-12-27 23:49:39 +0000 | [diff] [blame] | 1023 | + nla_total_size(1) /* IFLA_CARRIER */ |
Ben Greear | edbc0bb | 2012-03-29 12:51:30 +0000 | [diff] [blame] | 1024 | + nla_total_size(4) /* IFLA_PROMISCUITY */ |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 1025 | + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */ |
| 1026 | + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */ |
Tobias Klauser | 6919756 | 2016-11-30 14:30:37 +0100 | [diff] [blame] | 1027 | + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */ |
| 1028 | + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */ |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 1029 | + nla_total_size(1) /* IFLA_OPERSTATE */ |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1030 | + nla_total_size(1) /* IFLA_LINKMODE */ |
david decotigny | 2d3b479 | 2014-03-29 09:48:35 -0700 | [diff] [blame] | 1031 | + nla_total_size(4) /* IFLA_CARRIER_CHANGES */ |
Nicolas Dichtel | d37512a | 2015-01-15 15:11:16 +0100 | [diff] [blame] | 1032 | + nla_total_size(4) /* IFLA_LINK_NETNSID */ |
Serhey Popovych | db833d4 | 2017-06-20 14:35:23 +0300 | [diff] [blame] | 1033 | + nla_total_size(4) /* IFLA_GROUP */ |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1034 | + nla_total_size(ext_filter_mask |
| 1035 | & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ |
| 1036 | + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 1037 | + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 1038 | + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ |
Arad, Ronen | b1974ed | 2015-10-19 09:23:28 -0700 | [diff] [blame] | 1039 | + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */ |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1040 | + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */ |
Anuradha Karuppiah | 88d6378 | 2015-07-14 13:43:20 -0700 | [diff] [blame] | 1041 | + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */ |
Nicolas Dichtel | c57c7a9 | 2016-03-31 18:10:31 +0200 | [diff] [blame] | 1042 | + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */ |
David S. Miller | b5cdae3 | 2017-04-18 15:36:58 -0400 | [diff] [blame] | 1043 | + rtnl_xdp_size() /* IFLA_XDP */ |
Vlad Yasevich | 3d3ea5a | 2017-05-27 10:14:34 -0400 | [diff] [blame] | 1044 | + nla_total_size(4) /* IFLA_EVENT */ |
Nicolas Dichtel | 6621dd2 | 2017-10-03 13:53:23 +0200 | [diff] [blame] | 1045 | + nla_total_size(4) /* IFLA_NEW_NETNSID */ |
Nicolas Dichtel | 38e01b3 | 2018-01-25 15:01:39 +0100 | [diff] [blame] | 1046 | + nla_total_size(4) /* IFLA_NEW_IFINDEX */ |
Roopa Prabhu | 829eb20 | 2020-07-31 17:34:01 -0700 | [diff] [blame] | 1047 | + rtnl_proto_down_size(dev) /* proto down */ |
Christian Brauner | 7e4a8d5 | 2018-09-04 21:53:53 +0200 | [diff] [blame] | 1048 | + nla_total_size(4) /* IFLA_TARGET_NETNSID */ |
David Decotigny | b2d3bcf | 2018-01-18 09:59:13 -0800 | [diff] [blame] | 1049 | + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */ |
| 1050 | + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */ |
Stephen Hemminger | 3e7a50c | 2018-07-27 13:43:22 -0700 | [diff] [blame] | 1051 | + nla_total_size(4) /* IFLA_MIN_MTU */ |
| 1052 | + nla_total_size(4) /* IFLA_MAX_MTU */ |
Jiri Pirko | 88f4fb0 | 2019-09-30 11:48:17 +0200 | [diff] [blame] | 1053 | + rtnl_prop_list_size(dev) |
Michal Kubecek | f74877a | 2019-12-11 10:58:14 +0100 | [diff] [blame] | 1054 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */ |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 1055 | + 0; |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 1056 | } |
| 1057 | |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1058 | static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) |
| 1059 | { |
| 1060 | struct nlattr *vf_ports; |
| 1061 | struct nlattr *vf_port; |
| 1062 | int vf; |
| 1063 | int err; |
| 1064 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1065 | vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS); |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1066 | if (!vf_ports) |
| 1067 | return -EMSGSIZE; |
| 1068 | |
| 1069 | for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1070 | vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT); |
Scott Feldman | 8ca9418 | 2010-05-28 03:42:18 -0700 | [diff] [blame] | 1071 | if (!vf_port) |
| 1072 | goto nla_put_failure; |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1073 | if (nla_put_u32(skb, IFLA_PORT_VF, vf)) |
| 1074 | goto nla_put_failure; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1075 | err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); |
Scott Feldman | 8ca9418 | 2010-05-28 03:42:18 -0700 | [diff] [blame] | 1076 | if (err == -EMSGSIZE) |
| 1077 | goto nla_put_failure; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1078 | if (err) { |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1079 | nla_nest_cancel(skb, vf_port); |
| 1080 | continue; |
| 1081 | } |
| 1082 | nla_nest_end(skb, vf_port); |
| 1083 | } |
| 1084 | |
| 1085 | nla_nest_end(skb, vf_ports); |
| 1086 | |
| 1087 | return 0; |
Scott Feldman | 8ca9418 | 2010-05-28 03:42:18 -0700 | [diff] [blame] | 1088 | |
| 1089 | nla_put_failure: |
| 1090 | nla_nest_cancel(skb, vf_ports); |
| 1091 | return -EMSGSIZE; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) |
| 1095 | { |
| 1096 | struct nlattr *port_self; |
| 1097 | int err; |
| 1098 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1099 | port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF); |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1100 | if (!port_self) |
| 1101 | return -EMSGSIZE; |
| 1102 | |
| 1103 | err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); |
| 1104 | if (err) { |
| 1105 | nla_nest_cancel(skb, port_self); |
Scott Feldman | 8ca9418 | 2010-05-28 03:42:18 -0700 | [diff] [blame] | 1106 | return (err == -EMSGSIZE) ? err : 0; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | nla_nest_end(skb, port_self); |
| 1110 | |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 1114 | static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, |
| 1115 | u32 ext_filter_mask) |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1116 | { |
| 1117 | int err; |
| 1118 | |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 1119 | if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || |
| 1120 | !(ext_filter_mask & RTEXT_FILTER_VF)) |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1121 | return 0; |
| 1122 | |
| 1123 | err = rtnl_port_self_fill(skb, dev); |
| 1124 | if (err) |
| 1125 | return err; |
| 1126 | |
| 1127 | if (dev_num_vf(dev->dev.parent)) { |
| 1128 | err = rtnl_vf_ports_fill(skb, dev); |
| 1129 | if (err) |
| 1130 | return err; |
| 1131 | } |
| 1132 | |
| 1133 | return 0; |
| 1134 | } |
| 1135 | |
Jiri Pirko | 66cae9e | 2013-07-29 18:16:50 +0200 | [diff] [blame] | 1136 | static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev) |
| 1137 | { |
| 1138 | int err; |
Jiri Pirko | 02637fc | 2014-11-28 14:34:16 +0100 | [diff] [blame] | 1139 | struct netdev_phys_item_id ppid; |
Jiri Pirko | 66cae9e | 2013-07-29 18:16:50 +0200 | [diff] [blame] | 1140 | |
| 1141 | err = dev_get_phys_port_id(dev, &ppid); |
| 1142 | if (err) { |
| 1143 | if (err == -EOPNOTSUPP) |
| 1144 | return 0; |
| 1145 | return err; |
| 1146 | } |
| 1147 | |
| 1148 | if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id)) |
| 1149 | return -EMSGSIZE; |
| 1150 | |
| 1151 | return 0; |
| 1152 | } |
| 1153 | |
David Ahern | db24a90 | 2015-03-17 20:23:15 -0600 | [diff] [blame] | 1154 | static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev) |
| 1155 | { |
| 1156 | char name[IFNAMSIZ]; |
| 1157 | int err; |
| 1158 | |
| 1159 | err = dev_get_phys_port_name(dev, name, sizeof(name)); |
| 1160 | if (err) { |
| 1161 | if (err == -EOPNOTSUPP) |
| 1162 | return 0; |
| 1163 | return err; |
| 1164 | } |
| 1165 | |
Michal Schmidt | 77ef033 | 2017-05-04 16:48:58 +0200 | [diff] [blame] | 1166 | if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name)) |
David Ahern | db24a90 | 2015-03-17 20:23:15 -0600 | [diff] [blame] | 1167 | return -EMSGSIZE; |
| 1168 | |
| 1169 | return 0; |
| 1170 | } |
| 1171 | |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1172 | static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) |
| 1173 | { |
Florian Fainelli | bccb302 | 2019-02-06 09:45:46 -0800 | [diff] [blame] | 1174 | struct netdev_phys_item_id ppid = { }; |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1175 | int err; |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1176 | |
Florian Fainelli | bccb302 | 2019-02-06 09:45:46 -0800 | [diff] [blame] | 1177 | err = dev_get_port_parent_id(dev, &ppid, false); |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1178 | if (err) { |
| 1179 | if (err == -EOPNOTSUPP) |
| 1180 | return 0; |
| 1181 | return err; |
| 1182 | } |
| 1183 | |
Florian Fainelli | bccb302 | 2019-02-06 09:45:46 -0800 | [diff] [blame] | 1184 | if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id)) |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1185 | return -EMSGSIZE; |
| 1186 | |
| 1187 | return 0; |
| 1188 | } |
| 1189 | |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1190 | static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb, |
| 1191 | struct net_device *dev) |
| 1192 | { |
Roopa Prabhu | 550bce5 | 2016-04-15 20:36:25 -0700 | [diff] [blame] | 1193 | struct rtnl_link_stats64 *sp; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1194 | struct nlattr *attr; |
| 1195 | |
Nicolas Dichtel | 58414d3 | 2016-04-21 18:58:25 +0200 | [diff] [blame] | 1196 | attr = nla_reserve_64bit(skb, IFLA_STATS64, |
| 1197 | sizeof(struct rtnl_link_stats64), IFLA_PAD); |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1198 | if (!attr) |
| 1199 | return -EMSGSIZE; |
| 1200 | |
Roopa Prabhu | 550bce5 | 2016-04-15 20:36:25 -0700 | [diff] [blame] | 1201 | sp = nla_data(attr); |
| 1202 | dev_get_stats(dev, sp); |
| 1203 | |
| 1204 | attr = nla_reserve(skb, IFLA_STATS, |
| 1205 | sizeof(struct rtnl_link_stats)); |
| 1206 | if (!attr) |
| 1207 | return -EMSGSIZE; |
| 1208 | |
| 1209 | copy_rtnl_link_stats(nla_data(attr), sp); |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1210 | |
| 1211 | return 0; |
| 1212 | } |
| 1213 | |
| 1214 | static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, |
| 1215 | struct net_device *dev, |
| 1216 | int vfs_num, |
| 1217 | struct nlattr *vfinfo) |
| 1218 | { |
| 1219 | struct ifla_vf_rss_query_en vf_rss_query_en; |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 1220 | struct nlattr *vf, *vfstats, *vfvlanlist; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1221 | struct ifla_vf_link_state vf_linkstate; |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 1222 | struct ifla_vf_vlan_info vf_vlan_info; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1223 | struct ifla_vf_spoofchk vf_spoofchk; |
| 1224 | struct ifla_vf_tx_rate vf_tx_rate; |
| 1225 | struct ifla_vf_stats vf_stats; |
| 1226 | struct ifla_vf_trust vf_trust; |
| 1227 | struct ifla_vf_vlan vf_vlan; |
| 1228 | struct ifla_vf_rate vf_rate; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1229 | struct ifla_vf_mac vf_mac; |
Denis Kirjanov | 75345f88 | 2019-06-17 10:53:41 +0200 | [diff] [blame] | 1230 | struct ifla_vf_broadcast vf_broadcast; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1231 | struct ifla_vf_info ivi; |
Danit Goldberg | 30aad41 | 2019-11-06 15:30:07 +0200 | [diff] [blame] | 1232 | struct ifla_vf_guid node_guid; |
| 1233 | struct ifla_vf_guid port_guid; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1234 | |
Mintz, Yuval | 0eed9cf | 2017-06-07 21:00:33 +0300 | [diff] [blame] | 1235 | memset(&ivi, 0, sizeof(ivi)); |
| 1236 | |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1237 | /* Not all SR-IOV capable drivers support the |
| 1238 | * spoofcheck and "RSS query enable" query. Preset to |
| 1239 | * -1 so the user space tool can detect that the driver |
| 1240 | * didn't report anything. |
| 1241 | */ |
| 1242 | ivi.spoofchk = -1; |
| 1243 | ivi.rss_query_en = -1; |
| 1244 | ivi.trusted = -1; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1245 | /* The default value for VF link state is "auto" |
| 1246 | * IFLA_VF_LINK_STATE_AUTO which equals zero |
| 1247 | */ |
| 1248 | ivi.linkstate = 0; |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 1249 | /* VLAN Protocol by default is 802.1Q */ |
| 1250 | ivi.vlan_proto = htons(ETH_P_8021Q); |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1251 | if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi)) |
| 1252 | return 0; |
| 1253 | |
Dan Carpenter | 775f4f0 | 2016-10-13 11:45:28 +0300 | [diff] [blame] | 1254 | memset(&vf_vlan_info, 0, sizeof(vf_vlan_info)); |
Leon Romanovsky | 9fbf082 | 2020-01-30 14:59:49 +0200 | [diff] [blame] | 1255 | memset(&node_guid, 0, sizeof(node_guid)); |
| 1256 | memset(&port_guid, 0, sizeof(port_guid)); |
Dan Carpenter | 775f4f0 | 2016-10-13 11:45:28 +0300 | [diff] [blame] | 1257 | |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1258 | vf_mac.vf = |
| 1259 | vf_vlan.vf = |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 1260 | vf_vlan_info.vf = |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1261 | vf_rate.vf = |
| 1262 | vf_tx_rate.vf = |
| 1263 | vf_spoofchk.vf = |
| 1264 | vf_linkstate.vf = |
| 1265 | vf_rss_query_en.vf = |
Danit Goldberg | 9aed6ae | 2019-12-03 17:43:36 +0200 | [diff] [blame] | 1266 | vf_trust.vf = |
| 1267 | node_guid.vf = |
| 1268 | port_guid.vf = ivi.vf; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1269 | |
| 1270 | memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); |
Denis Kirjanov | 75345f88 | 2019-06-17 10:53:41 +0200 | [diff] [blame] | 1271 | memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len); |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1272 | vf_vlan.vlan = ivi.vlan; |
| 1273 | vf_vlan.qos = ivi.qos; |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 1274 | vf_vlan_info.vlan = ivi.vlan; |
| 1275 | vf_vlan_info.qos = ivi.qos; |
| 1276 | vf_vlan_info.vlan_proto = ivi.vlan_proto; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1277 | vf_tx_rate.rate = ivi.max_tx_rate; |
| 1278 | vf_rate.min_tx_rate = ivi.min_tx_rate; |
| 1279 | vf_rate.max_tx_rate = ivi.max_tx_rate; |
| 1280 | vf_spoofchk.setting = ivi.spoofchk; |
| 1281 | vf_linkstate.link_state = ivi.linkstate; |
| 1282 | vf_rss_query_en.setting = ivi.rss_query_en; |
| 1283 | vf_trust.setting = ivi.trusted; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1284 | vf = nla_nest_start_noflag(skb, IFLA_VF_INFO); |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 1285 | if (!vf) |
| 1286 | goto nla_put_vfinfo_failure; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1287 | if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || |
Denis Kirjanov | 75345f88 | 2019-06-17 10:53:41 +0200 | [diff] [blame] | 1288 | nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) || |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1289 | nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || |
| 1290 | nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), |
| 1291 | &vf_rate) || |
| 1292 | nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), |
| 1293 | &vf_tx_rate) || |
| 1294 | nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), |
| 1295 | &vf_spoofchk) || |
| 1296 | nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), |
| 1297 | &vf_linkstate) || |
| 1298 | nla_put(skb, IFLA_VF_RSS_QUERY_EN, |
| 1299 | sizeof(vf_rss_query_en), |
| 1300 | &vf_rss_query_en) || |
| 1301 | nla_put(skb, IFLA_VF_TRUST, |
| 1302 | sizeof(vf_trust), &vf_trust)) |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 1303 | goto nla_put_vf_failure; |
Danit Goldberg | 30aad41 | 2019-11-06 15:30:07 +0200 | [diff] [blame] | 1304 | |
Danit Goldberg | 30aad41 | 2019-11-06 15:30:07 +0200 | [diff] [blame] | 1305 | if (dev->netdev_ops->ndo_get_vf_guid && |
| 1306 | !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid, |
| 1307 | &port_guid)) { |
| 1308 | if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid), |
| 1309 | &node_guid) || |
| 1310 | nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid), |
| 1311 | &port_guid)) |
| 1312 | goto nla_put_vf_failure; |
| 1313 | } |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1314 | vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST); |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 1315 | if (!vfvlanlist) |
| 1316 | goto nla_put_vf_failure; |
| 1317 | if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info), |
| 1318 | &vf_vlan_info)) { |
| 1319 | nla_nest_cancel(skb, vfvlanlist); |
| 1320 | goto nla_put_vf_failure; |
| 1321 | } |
| 1322 | nla_nest_end(skb, vfvlanlist); |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1323 | memset(&vf_stats, 0, sizeof(vf_stats)); |
| 1324 | if (dev->netdev_ops->ndo_get_vf_stats) |
| 1325 | dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, |
| 1326 | &vf_stats); |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1327 | vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS); |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 1328 | if (!vfstats) |
| 1329 | goto nla_put_vf_failure; |
Nicolas Dichtel | 343a6d8 | 2016-04-25 10:25:14 +0200 | [diff] [blame] | 1330 | if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS, |
| 1331 | vf_stats.rx_packets, IFLA_VF_STATS_PAD) || |
| 1332 | nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS, |
| 1333 | vf_stats.tx_packets, IFLA_VF_STATS_PAD) || |
| 1334 | nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES, |
| 1335 | vf_stats.rx_bytes, IFLA_VF_STATS_PAD) || |
| 1336 | nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES, |
| 1337 | vf_stats.tx_bytes, IFLA_VF_STATS_PAD) || |
| 1338 | nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST, |
| 1339 | vf_stats.broadcast, IFLA_VF_STATS_PAD) || |
| 1340 | nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST, |
Eugenia Emantayev | c5a9f6f | 2017-07-17 13:47:07 +0300 | [diff] [blame] | 1341 | vf_stats.multicast, IFLA_VF_STATS_PAD) || |
| 1342 | nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED, |
| 1343 | vf_stats.rx_dropped, IFLA_VF_STATS_PAD) || |
| 1344 | nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED, |
| 1345 | vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) { |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 1346 | nla_nest_cancel(skb, vfstats); |
| 1347 | goto nla_put_vf_failure; |
| 1348 | } |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1349 | nla_nest_end(skb, vfstats); |
| 1350 | nla_nest_end(skb, vf); |
| 1351 | return 0; |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 1352 | |
| 1353 | nla_put_vf_failure: |
| 1354 | nla_nest_cancel(skb, vf); |
| 1355 | nla_put_vfinfo_failure: |
| 1356 | nla_nest_cancel(skb, vfinfo); |
| 1357 | return -EMSGSIZE; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1358 | } |
| 1359 | |
Florian Westphal | 250fc3d | 2017-09-26 13:58:41 +0200 | [diff] [blame] | 1360 | static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb, |
| 1361 | struct net_device *dev, |
| 1362 | u32 ext_filter_mask) |
| 1363 | { |
| 1364 | struct nlattr *vfinfo; |
| 1365 | int i, num_vfs; |
| 1366 | |
| 1367 | if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0)) |
| 1368 | return 0; |
| 1369 | |
| 1370 | num_vfs = dev_num_vf(dev->dev.parent); |
| 1371 | if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs)) |
| 1372 | return -EMSGSIZE; |
| 1373 | |
| 1374 | if (!dev->netdev_ops->ndo_get_vf_config) |
| 1375 | return 0; |
| 1376 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1377 | vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST); |
Florian Westphal | 250fc3d | 2017-09-26 13:58:41 +0200 | [diff] [blame] | 1378 | if (!vfinfo) |
| 1379 | return -EMSGSIZE; |
| 1380 | |
| 1381 | for (i = 0; i < num_vfs; i++) { |
| 1382 | if (rtnl_fill_vfinfo(skb, dev, i, vfinfo)) |
| 1383 | return -EMSGSIZE; |
| 1384 | } |
| 1385 | |
| 1386 | nla_nest_end(skb, vfinfo); |
| 1387 | return 0; |
| 1388 | } |
| 1389 | |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1390 | static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev) |
| 1391 | { |
Kangjie Lu | 5f8e447 | 2016-05-03 16:46:24 -0400 | [diff] [blame] | 1392 | struct rtnl_link_ifmap map; |
| 1393 | |
| 1394 | memset(&map, 0, sizeof(map)); |
| 1395 | map.mem_start = dev->mem_start; |
| 1396 | map.mem_end = dev->mem_end; |
| 1397 | map.base_addr = dev->base_addr; |
| 1398 | map.irq = dev->irq; |
| 1399 | map.dma = dev->dma; |
| 1400 | map.port = dev->if_port; |
| 1401 | |
Nicolas Dichtel | 270cb4d | 2016-04-26 10:06:16 +0200 | [diff] [blame] | 1402 | if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD)) |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1403 | return -EMSGSIZE; |
| 1404 | |
| 1405 | return 0; |
| 1406 | } |
| 1407 | |
Jakub Kicinski | a25717d | 2018-07-11 20:36:41 -0700 | [diff] [blame] | 1408 | static u32 rtnl_xdp_prog_skb(struct net_device *dev) |
Daniel Borkmann | d67b9cd | 2017-05-12 01:04:46 +0200 | [diff] [blame] | 1409 | { |
Martin KaFai Lau | 5803869 | 2017-06-15 17:29:09 -0700 | [diff] [blame] | 1410 | const struct bpf_prog *generic_xdp_prog; |
Daniel Borkmann | d67b9cd | 2017-05-12 01:04:46 +0200 | [diff] [blame] | 1411 | |
| 1412 | ASSERT_RTNL(); |
| 1413 | |
Martin KaFai Lau | 5803869 | 2017-06-15 17:29:09 -0700 | [diff] [blame] | 1414 | generic_xdp_prog = rtnl_dereference(dev->xdp_prog); |
Jakub Kicinski | a25717d | 2018-07-11 20:36:41 -0700 | [diff] [blame] | 1415 | if (!generic_xdp_prog) |
| 1416 | return 0; |
| 1417 | return generic_xdp_prog->aux->id; |
| 1418 | } |
Daniel Borkmann | d67b9cd | 2017-05-12 01:04:46 +0200 | [diff] [blame] | 1419 | |
Jakub Kicinski | a25717d | 2018-07-11 20:36:41 -0700 | [diff] [blame] | 1420 | static u32 rtnl_xdp_prog_drv(struct net_device *dev) |
| 1421 | { |
Andrii Nakryiko | 7f0a838 | 2020-07-21 23:45:55 -0700 | [diff] [blame] | 1422 | return dev_xdp_prog_id(dev, XDP_MODE_DRV); |
Jakub Kicinski | a25717d | 2018-07-11 20:36:41 -0700 | [diff] [blame] | 1423 | } |
Jakub Kicinski | 118b4aa | 2017-12-01 15:08:55 -0800 | [diff] [blame] | 1424 | |
Jakub Kicinski | a25717d | 2018-07-11 20:36:41 -0700 | [diff] [blame] | 1425 | static u32 rtnl_xdp_prog_hw(struct net_device *dev) |
| 1426 | { |
Andrii Nakryiko | 7f0a838 | 2020-07-21 23:45:55 -0700 | [diff] [blame] | 1427 | return dev_xdp_prog_id(dev, XDP_MODE_HW); |
Jakub Kicinski | a25717d | 2018-07-11 20:36:41 -0700 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev, |
| 1431 | u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr, |
| 1432 | u32 (*get_prog_id)(struct net_device *dev)) |
| 1433 | { |
| 1434 | u32 curr_id; |
| 1435 | int err; |
| 1436 | |
| 1437 | curr_id = get_prog_id(dev); |
| 1438 | if (!curr_id) |
| 1439 | return 0; |
| 1440 | |
| 1441 | *prog_id = curr_id; |
| 1442 | err = nla_put_u32(skb, attr, curr_id); |
| 1443 | if (err) |
| 1444 | return err; |
| 1445 | |
| 1446 | if (*mode != XDP_ATTACHED_NONE) |
| 1447 | *mode = XDP_ATTACHED_MULTI; |
| 1448 | else |
| 1449 | *mode = tgt_mode; |
| 1450 | |
| 1451 | return 0; |
Daniel Borkmann | d67b9cd | 2017-05-12 01:04:46 +0200 | [diff] [blame] | 1452 | } |
| 1453 | |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1454 | static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev) |
| 1455 | { |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1456 | struct nlattr *xdp; |
Martin KaFai Lau | 5803869 | 2017-06-15 17:29:09 -0700 | [diff] [blame] | 1457 | u32 prog_id; |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1458 | int err; |
Jakub Kicinski | 4f91da2 | 2018-07-11 20:36:38 -0700 | [diff] [blame] | 1459 | u8 mode; |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1460 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1461 | xdp = nla_nest_start_noflag(skb, IFLA_XDP); |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1462 | if (!xdp) |
| 1463 | return -EMSGSIZE; |
David S. Miller | b5cdae3 | 2017-04-18 15:36:58 -0400 | [diff] [blame] | 1464 | |
Jakub Kicinski | a25717d | 2018-07-11 20:36:41 -0700 | [diff] [blame] | 1465 | prog_id = 0; |
| 1466 | mode = XDP_ATTACHED_NONE; |
Jakub Kicinski | 202aabe | 2018-07-16 19:08:50 -0700 | [diff] [blame] | 1467 | err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB, |
| 1468 | IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb); |
| 1469 | if (err) |
Jakub Kicinski | a25717d | 2018-07-11 20:36:41 -0700 | [diff] [blame] | 1470 | goto err_cancel; |
Jakub Kicinski | 202aabe | 2018-07-16 19:08:50 -0700 | [diff] [blame] | 1471 | err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV, |
| 1472 | IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv); |
| 1473 | if (err) |
Jakub Kicinski | a25717d | 2018-07-11 20:36:41 -0700 | [diff] [blame] | 1474 | goto err_cancel; |
Jakub Kicinski | 202aabe | 2018-07-16 19:08:50 -0700 | [diff] [blame] | 1475 | err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW, |
| 1476 | IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw); |
| 1477 | if (err) |
Jakub Kicinski | a25717d | 2018-07-11 20:36:41 -0700 | [diff] [blame] | 1478 | goto err_cancel; |
| 1479 | |
Jakub Kicinski | 4f91da2 | 2018-07-11 20:36:38 -0700 | [diff] [blame] | 1480 | err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode); |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1481 | if (err) |
| 1482 | goto err_cancel; |
| 1483 | |
Jakub Kicinski | a25717d | 2018-07-11 20:36:41 -0700 | [diff] [blame] | 1484 | if (prog_id && mode != XDP_ATTACHED_MULTI) { |
Martin KaFai Lau | 5803869 | 2017-06-15 17:29:09 -0700 | [diff] [blame] | 1485 | err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id); |
| 1486 | if (err) |
| 1487 | goto err_cancel; |
| 1488 | } |
| 1489 | |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1490 | nla_nest_end(skb, xdp); |
| 1491 | return 0; |
| 1492 | |
| 1493 | err_cancel: |
| 1494 | nla_nest_cancel(skb, xdp); |
| 1495 | return err; |
| 1496 | } |
| 1497 | |
Vlad Yasevich | 3d3ea5a | 2017-05-27 10:14:34 -0400 | [diff] [blame] | 1498 | static u32 rtnl_get_event(unsigned long event) |
| 1499 | { |
| 1500 | u32 rtnl_event_type = IFLA_EVENT_NONE; |
| 1501 | |
| 1502 | switch (event) { |
| 1503 | case NETDEV_REBOOT: |
| 1504 | rtnl_event_type = IFLA_EVENT_REBOOT; |
| 1505 | break; |
| 1506 | case NETDEV_FEAT_CHANGE: |
| 1507 | rtnl_event_type = IFLA_EVENT_FEATURES; |
| 1508 | break; |
| 1509 | case NETDEV_BONDING_FAILOVER: |
| 1510 | rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER; |
| 1511 | break; |
| 1512 | case NETDEV_NOTIFY_PEERS: |
| 1513 | rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS; |
| 1514 | break; |
| 1515 | case NETDEV_RESEND_IGMP: |
| 1516 | rtnl_event_type = IFLA_EVENT_IGMP_RESEND; |
| 1517 | break; |
| 1518 | case NETDEV_CHANGEINFODATA: |
| 1519 | rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS; |
| 1520 | break; |
| 1521 | default: |
| 1522 | break; |
| 1523 | } |
| 1524 | |
| 1525 | return rtnl_event_type; |
| 1526 | } |
| 1527 | |
Florian Westphal | 79110a0 | 2017-09-26 13:58:40 +0200 | [diff] [blame] | 1528 | static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev) |
| 1529 | { |
| 1530 | const struct net_device *upper_dev; |
| 1531 | int ret = 0; |
| 1532 | |
| 1533 | rcu_read_lock(); |
| 1534 | |
| 1535 | upper_dev = netdev_master_upper_dev_get_rcu(dev); |
| 1536 | if (upper_dev) |
| 1537 | ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex); |
| 1538 | |
| 1539 | rcu_read_unlock(); |
| 1540 | return ret; |
| 1541 | } |
| 1542 | |
Sabrina Dubroca | feadc4b | 2019-05-14 15:12:19 +0200 | [diff] [blame] | 1543 | static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev, |
| 1544 | bool force) |
Florian Westphal | 79110a0 | 2017-09-26 13:58:40 +0200 | [diff] [blame] | 1545 | { |
| 1546 | int ifindex = dev_get_iflink(dev); |
| 1547 | |
Sabrina Dubroca | feadc4b | 2019-05-14 15:12:19 +0200 | [diff] [blame] | 1548 | if (force || dev->ifindex != ifindex) |
| 1549 | return nla_put_u32(skb, IFLA_LINK, ifindex); |
Florian Westphal | 79110a0 | 2017-09-26 13:58:40 +0200 | [diff] [blame] | 1550 | |
Sabrina Dubroca | feadc4b | 2019-05-14 15:12:19 +0200 | [diff] [blame] | 1551 | return 0; |
Florian Westphal | 79110a0 | 2017-09-26 13:58:40 +0200 | [diff] [blame] | 1552 | } |
| 1553 | |
Florian Westphal | 6c55700 | 2017-10-02 23:50:05 +0200 | [diff] [blame] | 1554 | static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb, |
| 1555 | struct net_device *dev) |
| 1556 | { |
| 1557 | char buf[IFALIASZ]; |
| 1558 | int ret; |
| 1559 | |
| 1560 | ret = dev_get_alias(dev, buf, sizeof(buf)); |
| 1561 | return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0; |
| 1562 | } |
| 1563 | |
Florian Westphal | b1e66b9 | 2017-09-26 13:58:42 +0200 | [diff] [blame] | 1564 | static int rtnl_fill_link_netnsid(struct sk_buff *skb, |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 1565 | const struct net_device *dev, |
Guillaume Nault | d4e4fdf | 2019-10-23 18:39:04 +0200 | [diff] [blame] | 1566 | struct net *src_net, gfp_t gfp) |
Florian Westphal | b1e66b9 | 2017-09-26 13:58:42 +0200 | [diff] [blame] | 1567 | { |
Sabrina Dubroca | feadc4b | 2019-05-14 15:12:19 +0200 | [diff] [blame] | 1568 | bool put_iflink = false; |
| 1569 | |
Florian Westphal | b1e66b9 | 2017-09-26 13:58:42 +0200 | [diff] [blame] | 1570 | if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) { |
| 1571 | struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); |
| 1572 | |
| 1573 | if (!net_eq(dev_net(dev), link_net)) { |
Guillaume Nault | d4e4fdf | 2019-10-23 18:39:04 +0200 | [diff] [blame] | 1574 | int id = peernet2id_alloc(src_net, link_net, gfp); |
Florian Westphal | b1e66b9 | 2017-09-26 13:58:42 +0200 | [diff] [blame] | 1575 | |
| 1576 | if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) |
| 1577 | return -EMSGSIZE; |
Sabrina Dubroca | feadc4b | 2019-05-14 15:12:19 +0200 | [diff] [blame] | 1578 | |
| 1579 | put_iflink = true; |
Florian Westphal | b1e66b9 | 2017-09-26 13:58:42 +0200 | [diff] [blame] | 1580 | } |
| 1581 | } |
| 1582 | |
Sabrina Dubroca | feadc4b | 2019-05-14 15:12:19 +0200 | [diff] [blame] | 1583 | return nla_put_iflink(skb, dev, put_iflink); |
Florian Westphal | b1e66b9 | 2017-09-26 13:58:42 +0200 | [diff] [blame] | 1584 | } |
| 1585 | |
Florian Westphal | 070cbf5b | 2017-10-16 15:44:35 +0200 | [diff] [blame] | 1586 | static int rtnl_fill_link_af(struct sk_buff *skb, |
| 1587 | const struct net_device *dev, |
| 1588 | u32 ext_filter_mask) |
| 1589 | { |
| 1590 | const struct rtnl_af_ops *af_ops; |
| 1591 | struct nlattr *af_spec; |
| 1592 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1593 | af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC); |
Florian Westphal | 070cbf5b | 2017-10-16 15:44:35 +0200 | [diff] [blame] | 1594 | if (!af_spec) |
| 1595 | return -EMSGSIZE; |
| 1596 | |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 1597 | list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { |
Florian Westphal | 070cbf5b | 2017-10-16 15:44:35 +0200 | [diff] [blame] | 1598 | struct nlattr *af; |
| 1599 | int err; |
| 1600 | |
| 1601 | if (!af_ops->fill_link_af) |
| 1602 | continue; |
| 1603 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1604 | af = nla_nest_start_noflag(skb, af_ops->family); |
Florian Westphal | 070cbf5b | 2017-10-16 15:44:35 +0200 | [diff] [blame] | 1605 | if (!af) |
| 1606 | return -EMSGSIZE; |
| 1607 | |
| 1608 | err = af_ops->fill_link_af(skb, dev, ext_filter_mask); |
| 1609 | /* |
| 1610 | * Caller may return ENODATA to indicate that there |
| 1611 | * was no data to be dumped. This is not an error, it |
| 1612 | * means we should trim the attribute header and |
| 1613 | * continue. |
| 1614 | */ |
| 1615 | if (err == -ENODATA) |
| 1616 | nla_nest_cancel(skb, af); |
| 1617 | else if (err < 0) |
| 1618 | return -EMSGSIZE; |
| 1619 | |
| 1620 | nla_nest_end(skb, af); |
| 1621 | } |
| 1622 | |
| 1623 | nla_nest_end(skb, af_spec); |
| 1624 | return 0; |
| 1625 | } |
| 1626 | |
Jiri Pirko | 88f4fb0 | 2019-09-30 11:48:17 +0200 | [diff] [blame] | 1627 | static int rtnl_fill_alt_ifnames(struct sk_buff *skb, |
| 1628 | const struct net_device *dev) |
| 1629 | { |
| 1630 | struct netdev_name_node *name_node; |
| 1631 | int count = 0; |
| 1632 | |
| 1633 | list_for_each_entry(name_node, &dev->name_node->list, list) { |
| 1634 | if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name)) |
| 1635 | return -EMSGSIZE; |
| 1636 | count++; |
| 1637 | } |
| 1638 | return count; |
| 1639 | } |
| 1640 | |
| 1641 | static int rtnl_fill_prop_list(struct sk_buff *skb, |
| 1642 | const struct net_device *dev) |
| 1643 | { |
| 1644 | struct nlattr *prop_list; |
| 1645 | int ret; |
| 1646 | |
| 1647 | prop_list = nla_nest_start(skb, IFLA_PROP_LIST); |
| 1648 | if (!prop_list) |
| 1649 | return -EMSGSIZE; |
| 1650 | |
| 1651 | ret = rtnl_fill_alt_ifnames(skb, dev); |
| 1652 | if (ret <= 0) |
| 1653 | goto nest_cancel; |
| 1654 | |
| 1655 | nla_nest_end(skb, prop_list); |
| 1656 | return 0; |
| 1657 | |
| 1658 | nest_cancel: |
| 1659 | nla_nest_cancel(skb, prop_list); |
| 1660 | return ret; |
| 1661 | } |
| 1662 | |
Roopa Prabhu | 829eb20 | 2020-07-31 17:34:01 -0700 | [diff] [blame] | 1663 | static int rtnl_fill_proto_down(struct sk_buff *skb, |
| 1664 | const struct net_device *dev) |
| 1665 | { |
| 1666 | struct nlattr *pr; |
| 1667 | u32 preason; |
| 1668 | |
| 1669 | if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down)) |
| 1670 | goto nla_put_failure; |
| 1671 | |
| 1672 | preason = dev->proto_down_reason; |
| 1673 | if (!preason) |
| 1674 | return 0; |
| 1675 | |
| 1676 | pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON); |
| 1677 | if (!pr) |
| 1678 | return -EMSGSIZE; |
| 1679 | |
| 1680 | if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) { |
| 1681 | nla_nest_cancel(skb, pr); |
| 1682 | goto nla_put_failure; |
| 1683 | } |
| 1684 | |
| 1685 | nla_nest_end(skb, pr); |
| 1686 | return 0; |
| 1687 | |
| 1688 | nla_put_failure: |
| 1689 | return -EMSGSIZE; |
| 1690 | } |
| 1691 | |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 1692 | static int rtnl_fill_ifinfo(struct sk_buff *skb, |
| 1693 | struct net_device *dev, struct net *src_net, |
Patrick McHardy | 575c3e2 | 2007-05-22 17:00:49 -0700 | [diff] [blame] | 1694 | int type, u32 pid, u32 seq, u32 change, |
Vlad Yasevich | 3d3ea5a | 2017-05-27 10:14:34 -0400 | [diff] [blame] | 1695 | unsigned int flags, u32 ext_filter_mask, |
Nicolas Dichtel | 38e01b3 | 2018-01-25 15:01:39 +0100 | [diff] [blame] | 1696 | u32 event, int *new_nsid, int new_ifindex, |
Guillaume Nault | d4e4fdf | 2019-10-23 18:39:04 +0200 | [diff] [blame] | 1697 | int tgt_netnsid, gfp_t gfp) |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1698 | { |
| 1699 | struct ifinfomsg *ifm; |
| 1700 | struct nlmsghdr *nlh; |
| 1701 | |
Eric Dumazet | 2907c35 | 2011-05-25 07:34:04 +0000 | [diff] [blame] | 1702 | ASSERT_RTNL(); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1703 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); |
| 1704 | if (nlh == NULL) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 1705 | return -EMSGSIZE; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1706 | |
| 1707 | ifm = nlmsg_data(nlh); |
| 1708 | ifm->ifi_family = AF_UNSPEC; |
| 1709 | ifm->__ifi_pad = 0; |
| 1710 | ifm->ifi_type = dev->type; |
| 1711 | ifm->ifi_index = dev->ifindex; |
| 1712 | ifm->ifi_flags = dev_get_flags(dev); |
| 1713 | ifm->ifi_change = change; |
| 1714 | |
Christian Brauner | 7e4a8d5 | 2018-09-04 21:53:53 +0200 | [diff] [blame] | 1715 | if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid)) |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 1716 | goto nla_put_failure; |
| 1717 | |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1718 | if (nla_put_string(skb, IFLA_IFNAME, dev->name) || |
| 1719 | nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || |
| 1720 | nla_put_u8(skb, IFLA_OPERSTATE, |
| 1721 | netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || |
| 1722 | nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || |
| 1723 | nla_put_u32(skb, IFLA_MTU, dev->mtu) || |
Stephen Hemminger | 3e7a50c | 2018-07-27 13:43:22 -0700 | [diff] [blame] | 1724 | nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) || |
| 1725 | nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) || |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1726 | nla_put_u32(skb, IFLA_GROUP, dev->group) || |
Ben Greear | edbc0bb | 2012-03-29 12:51:30 +0000 | [diff] [blame] | 1727 | nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 1728 | nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || |
Eric Dumazet | c70ce02 | 2016-03-21 09:55:10 -0700 | [diff] [blame] | 1729 | nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) || |
| 1730 | nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) || |
Mark A. Greer | 1d69c2b | 2012-07-20 13:35:13 +0000 | [diff] [blame] | 1731 | #ifdef CONFIG_RPS |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 1732 | nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || |
Mark A. Greer | 1d69c2b | 2012-07-20 13:35:13 +0000 | [diff] [blame] | 1733 | #endif |
Florian Westphal | 79110a0 | 2017-09-26 13:58:40 +0200 | [diff] [blame] | 1734 | put_master_ifindex(skb, dev) || |
Jiri Pirko | 9a57247 | 2012-12-27 23:49:39 +0000 | [diff] [blame] | 1735 | nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1736 | (dev->qdisc && |
| 1737 | nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) || |
Florian Westphal | 6c55700 | 2017-10-02 23:50:05 +0200 | [diff] [blame] | 1738 | nla_put_ifalias(skb, dev) || |
david decotigny | 2d3b479 | 2014-03-29 09:48:35 -0700 | [diff] [blame] | 1739 | nla_put_u32(skb, IFLA_CARRIER_CHANGES, |
David Decotigny | b2d3bcf | 2018-01-18 09:59:13 -0800 | [diff] [blame] | 1740 | atomic_read(&dev->carrier_up_count) + |
| 1741 | atomic_read(&dev->carrier_down_count)) || |
David Decotigny | b2d3bcf | 2018-01-18 09:59:13 -0800 | [diff] [blame] | 1742 | nla_put_u32(skb, IFLA_CARRIER_UP_COUNT, |
| 1743 | atomic_read(&dev->carrier_up_count)) || |
| 1744 | nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT, |
| 1745 | atomic_read(&dev->carrier_down_count))) |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1746 | goto nla_put_failure; |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 1747 | |
Roopa Prabhu | 829eb20 | 2020-07-31 17:34:01 -0700 | [diff] [blame] | 1748 | if (rtnl_fill_proto_down(skb, dev)) |
| 1749 | goto nla_put_failure; |
| 1750 | |
Vlad Yasevich | 3d3ea5a | 2017-05-27 10:14:34 -0400 | [diff] [blame] | 1751 | if (event != IFLA_EVENT_NONE) { |
| 1752 | if (nla_put_u32(skb, IFLA_EVENT, event)) |
| 1753 | goto nla_put_failure; |
| 1754 | } |
| 1755 | |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1756 | if (rtnl_fill_link_ifmap(skb, dev)) |
| 1757 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | |
| 1759 | if (dev->addr_len) { |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1760 | if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || |
| 1761 | nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) |
| 1762 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | } |
| 1764 | |
Jiri Pirko | 66cae9e | 2013-07-29 18:16:50 +0200 | [diff] [blame] | 1765 | if (rtnl_phys_port_id_fill(skb, dev)) |
| 1766 | goto nla_put_failure; |
| 1767 | |
David Ahern | db24a90 | 2015-03-17 20:23:15 -0600 | [diff] [blame] | 1768 | if (rtnl_phys_port_name_fill(skb, dev)) |
| 1769 | goto nla_put_failure; |
| 1770 | |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1771 | if (rtnl_phys_switch_id_fill(skb, dev)) |
| 1772 | goto nla_put_failure; |
| 1773 | |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1774 | if (rtnl_fill_stats(skb, dev)) |
Pavel Emelyanov | 96e7408 | 2008-05-21 14:12:46 -0700 | [diff] [blame] | 1775 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | |
Florian Westphal | 250fc3d | 2017-09-26 13:58:41 +0200 | [diff] [blame] | 1777 | if (rtnl_fill_vf(skb, dev, ext_filter_mask)) |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1778 | goto nla_put_failure; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1779 | |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 1780 | if (rtnl_port_fill(skb, dev, ext_filter_mask)) |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1781 | goto nla_put_failure; |
| 1782 | |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1783 | if (rtnl_xdp_fill(skb, dev)) |
| 1784 | goto nla_put_failure; |
| 1785 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 1786 | if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1787 | if (rtnl_link_fill(skb, dev) < 0) |
| 1788 | goto nla_put_failure; |
| 1789 | } |
| 1790 | |
Guillaume Nault | d4e4fdf | 2019-10-23 18:39:04 +0200 | [diff] [blame] | 1791 | if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp)) |
Florian Westphal | b1e66b9 | 2017-09-26 13:58:42 +0200 | [diff] [blame] | 1792 | goto nla_put_failure; |
Nicolas Dichtel | d37512a | 2015-01-15 15:11:16 +0100 | [diff] [blame] | 1793 | |
Nicolas Dichtel | 6621dd2 | 2017-10-03 13:53:23 +0200 | [diff] [blame] | 1794 | if (new_nsid && |
| 1795 | nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0) |
| 1796 | goto nla_put_failure; |
Nicolas Dichtel | 38e01b3 | 2018-01-25 15:01:39 +0100 | [diff] [blame] | 1797 | if (new_ifindex && |
| 1798 | nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0) |
| 1799 | goto nla_put_failure; |
| 1800 | |
Michal Kubecek | f74877a | 2019-12-11 10:58:14 +0100 | [diff] [blame] | 1801 | if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) && |
| 1802 | nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr)) |
| 1803 | goto nla_put_failure; |
Nicolas Dichtel | 6621dd2 | 2017-10-03 13:53:23 +0200 | [diff] [blame] | 1804 | |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 1805 | rcu_read_lock(); |
Florian Westphal | 070cbf5b | 2017-10-16 15:44:35 +0200 | [diff] [blame] | 1806 | if (rtnl_fill_link_af(skb, dev, ext_filter_mask)) |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 1807 | goto nla_put_failure_rcu; |
| 1808 | rcu_read_unlock(); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 1809 | |
Jiri Pirko | 88f4fb0 | 2019-09-30 11:48:17 +0200 | [diff] [blame] | 1810 | if (rtnl_fill_prop_list(skb, dev)) |
| 1811 | goto nla_put_failure; |
| 1812 | |
Johannes Berg | 00e77ed | 2021-06-12 10:20:55 +0200 | [diff] [blame] | 1813 | if (dev->dev.parent && |
| 1814 | nla_put_string(skb, IFLA_PARENT_DEV_NAME, |
| 1815 | dev_name(dev->dev.parent))) |
| 1816 | goto nla_put_failure; |
| 1817 | |
| 1818 | if (dev->dev.parent && dev->dev.parent->bus && |
| 1819 | nla_put_string(skb, IFLA_PARENT_DEV_BUS_NAME, |
| 1820 | dev->dev.parent->bus->name)) |
| 1821 | goto nla_put_failure; |
| 1822 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 1823 | nlmsg_end(skb, nlh); |
| 1824 | return 0; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1825 | |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 1826 | nla_put_failure_rcu: |
| 1827 | rcu_read_unlock(); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1828 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 1829 | nlmsg_cancel(skb, nlh); |
| 1830 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | } |
| 1832 | |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1833 | static const struct nla_policy ifla_policy[IFLA_MAX+1] = { |
| 1834 | [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, |
| 1835 | [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, |
| 1836 | [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, |
| 1837 | [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, |
| 1838 | [IFLA_MTU] = { .type = NLA_U32 }, |
| 1839 | [IFLA_LINK] = { .type = NLA_U32 }, |
| 1840 | [IFLA_MASTER] = { .type = NLA_U32 }, |
| 1841 | [IFLA_CARRIER] = { .type = NLA_U8 }, |
| 1842 | [IFLA_TXQLEN] = { .type = NLA_U32 }, |
| 1843 | [IFLA_WEIGHT] = { .type = NLA_U32 }, |
| 1844 | [IFLA_OPERSTATE] = { .type = NLA_U8 }, |
| 1845 | [IFLA_LINKMODE] = { .type = NLA_U8 }, |
| 1846 | [IFLA_LINKINFO] = { .type = NLA_NESTED }, |
| 1847 | [IFLA_NET_NS_PID] = { .type = NLA_U32 }, |
| 1848 | [IFLA_NET_NS_FD] = { .type = NLA_U32 }, |
Nicolas Dichtel | 2459b4c | 2017-10-11 16:24:48 +0200 | [diff] [blame] | 1849 | /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to |
| 1850 | * allow 0-length string (needed to remove an alias). |
| 1851 | */ |
| 1852 | [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 }, |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1853 | [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, |
| 1854 | [IFLA_VF_PORTS] = { .type = NLA_NESTED }, |
| 1855 | [IFLA_PORT_SELF] = { .type = NLA_NESTED }, |
| 1856 | [IFLA_AF_SPEC] = { .type = NLA_NESTED }, |
| 1857 | [IFLA_EXT_MASK] = { .type = NLA_U32 }, |
| 1858 | [IFLA_PROMISCUITY] = { .type = NLA_U32 }, |
| 1859 | [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, |
| 1860 | [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, |
Stephen Hemminger | 46e6b99 | 2017-12-07 15:40:19 -0800 | [diff] [blame] | 1861 | [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 }, |
| 1862 | [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 }, |
Jiri Pirko | 02637fc | 2014-11-28 14:34:16 +0100 | [diff] [blame] | 1863 | [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, |
david decotigny | 2d3b479 | 2014-03-29 09:48:35 -0700 | [diff] [blame] | 1864 | [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1865 | [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, |
Nicolas Dichtel | 317f481 | 2015-01-15 15:11:18 +0100 | [diff] [blame] | 1866 | [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, |
Anuradha Karuppiah | 88d6378 | 2015-07-14 13:43:20 -0700 | [diff] [blame] | 1867 | [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1868 | [IFLA_XDP] = { .type = NLA_NESTED }, |
Vlad Yasevich | 3d3ea5a | 2017-05-27 10:14:34 -0400 | [diff] [blame] | 1869 | [IFLA_EVENT] = { .type = NLA_U32 }, |
Serhey Popovych | db833d4 | 2017-06-20 14:35:23 +0300 | [diff] [blame] | 1870 | [IFLA_GROUP] = { .type = NLA_U32 }, |
Christian Brauner | 7e4a8d5 | 2018-09-04 21:53:53 +0200 | [diff] [blame] | 1871 | [IFLA_TARGET_NETNSID] = { .type = NLA_S32 }, |
David Decotigny | b2d3bcf | 2018-01-18 09:59:13 -0800 | [diff] [blame] | 1872 | [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 }, |
| 1873 | [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 }, |
Stephen Hemminger | 3e7a50c | 2018-07-27 13:43:22 -0700 | [diff] [blame] | 1874 | [IFLA_MIN_MTU] = { .type = NLA_U32 }, |
| 1875 | [IFLA_MAX_MTU] = { .type = NLA_U32 }, |
Jiri Pirko | 36fbf1e | 2019-09-30 11:48:16 +0200 | [diff] [blame] | 1876 | [IFLA_PROP_LIST] = { .type = NLA_NESTED }, |
| 1877 | [IFLA_ALT_IFNAME] = { .type = NLA_STRING, |
| 1878 | .len = ALTIFNAMSIZ - 1 }, |
Michal Kubecek | f74877a | 2019-12-11 10:58:14 +0100 | [diff] [blame] | 1879 | [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT }, |
Roopa Prabhu | 829eb20 | 2020-07-31 17:34:01 -0700 | [diff] [blame] | 1880 | [IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED }, |
Andrei Vagin | 7e4a513 | 2021-04-06 23:40:03 -0700 | [diff] [blame] | 1881 | [IFLA_NEW_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1), |
Johannes Berg | 88b7105 | 2021-06-12 10:20:56 +0200 | [diff] [blame] | 1882 | [IFLA_PARENT_DEV_NAME] = { .type = NLA_NUL_STRING }, |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1883 | }; |
| 1884 | |
| 1885 | static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { |
| 1886 | [IFLA_INFO_KIND] = { .type = NLA_STRING }, |
| 1887 | [IFLA_INFO_DATA] = { .type = NLA_NESTED }, |
| 1888 | [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, |
| 1889 | [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, |
| 1890 | }; |
| 1891 | |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1892 | static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { |
Daniel Borkmann | 364d571 | 2015-02-05 18:44:04 +0100 | [diff] [blame] | 1893 | [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, |
Denis Kirjanov | 75345f88 | 2019-06-17 10:53:41 +0200 | [diff] [blame] | 1894 | [IFLA_VF_BROADCAST] = { .type = NLA_REJECT }, |
Daniel Borkmann | 364d571 | 2015-02-05 18:44:04 +0100 | [diff] [blame] | 1895 | [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 1896 | [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED }, |
Daniel Borkmann | 364d571 | 2015-02-05 18:44:04 +0100 | [diff] [blame] | 1897 | [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, |
| 1898 | [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, |
| 1899 | [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, |
| 1900 | [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, |
Vlad Zolotarov | 01a3d79 | 2015-03-30 21:35:23 +0300 | [diff] [blame] | 1901 | [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) }, |
Eran Ben Elisha | 3b766cd | 2015-06-15 17:59:07 +0300 | [diff] [blame] | 1902 | [IFLA_VF_STATS] = { .type = NLA_NESTED }, |
Hiroshi Shimamoto | dd461d6 | 2015-08-28 06:57:55 +0000 | [diff] [blame] | 1903 | [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, |
Eli Cohen | cc8e27c | 2016-03-11 22:58:34 +0200 | [diff] [blame] | 1904 | [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) }, |
| 1905 | [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) }, |
Eran Ben Elisha | 3b766cd | 2015-06-15 17:59:07 +0300 | [diff] [blame] | 1906 | }; |
| 1907 | |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1908 | static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { |
| 1909 | [IFLA_PORT_VF] = { .type = NLA_U32 }, |
| 1910 | [IFLA_PORT_PROFILE] = { .type = NLA_STRING, |
| 1911 | .len = PORT_PROFILE_MAX }, |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1912 | [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, |
| 1913 | .len = PORT_UUID_MAX }, |
| 1914 | [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, |
| 1915 | .len = PORT_UUID_MAX }, |
| 1916 | [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, |
| 1917 | [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, |
Daniel Borkmann | 025331d | 2017-02-17 01:56:11 +0100 | [diff] [blame] | 1918 | |
| 1919 | /* Unused, but we need to keep it here since user space could |
| 1920 | * fill it. It's also broken with regard to NLA_BINARY use in |
| 1921 | * combination with structs. |
| 1922 | */ |
| 1923 | [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, |
| 1924 | .len = sizeof(struct ifla_port_vsi) }, |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1925 | }; |
| 1926 | |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1927 | static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = { |
Toke Høiland-Jørgensen | 92234c8 | 2020-03-25 18:23:26 +0100 | [diff] [blame] | 1928 | [IFLA_XDP_UNSPEC] = { .strict_start_type = IFLA_XDP_EXPECTED_FD }, |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1929 | [IFLA_XDP_FD] = { .type = NLA_S32 }, |
Toke Høiland-Jørgensen | 92234c8 | 2020-03-25 18:23:26 +0100 | [diff] [blame] | 1930 | [IFLA_XDP_EXPECTED_FD] = { .type = NLA_S32 }, |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1931 | [IFLA_XDP_ATTACHED] = { .type = NLA_U8 }, |
Daniel Borkmann | 85de857 | 2016-11-28 23:16:54 +0100 | [diff] [blame] | 1932 | [IFLA_XDP_FLAGS] = { .type = NLA_U32 }, |
Martin KaFai Lau | 5803869 | 2017-06-15 17:29:09 -0700 | [diff] [blame] | 1933 | [IFLA_XDP_PROG_ID] = { .type = NLA_U32 }, |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1934 | }; |
| 1935 | |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 1936 | static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla) |
| 1937 | { |
| 1938 | const struct rtnl_link_ops *ops = NULL; |
| 1939 | struct nlattr *linfo[IFLA_INFO_MAX + 1]; |
| 1940 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 1941 | if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0) |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 1942 | return NULL; |
| 1943 | |
| 1944 | if (linfo[IFLA_INFO_KIND]) { |
| 1945 | char kind[MODULE_NAME_LEN]; |
| 1946 | |
Francis Laniel | 872f690 | 2020-11-15 18:08:06 +0100 | [diff] [blame] | 1947 | nla_strscpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind)); |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 1948 | ops = rtnl_link_ops_get(kind); |
| 1949 | } |
| 1950 | |
| 1951 | return ops; |
| 1952 | } |
| 1953 | |
| 1954 | static bool link_master_filtered(struct net_device *dev, int master_idx) |
| 1955 | { |
| 1956 | struct net_device *master; |
| 1957 | |
| 1958 | if (!master_idx) |
| 1959 | return false; |
| 1960 | |
| 1961 | master = netdev_master_upper_dev_get(dev); |
Lahav Schlesinger | d3432bf | 2021-08-10 09:06:58 +0000 | [diff] [blame] | 1962 | |
| 1963 | /* 0 is already used to denote IFLA_MASTER wasn't passed, therefore need |
| 1964 | * another invalid value for ifindex to denote "no master". |
| 1965 | */ |
| 1966 | if (master_idx == -1) |
| 1967 | return !!master; |
| 1968 | |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 1969 | if (!master || master->ifindex != master_idx) |
| 1970 | return true; |
| 1971 | |
| 1972 | return false; |
| 1973 | } |
| 1974 | |
| 1975 | static bool link_kind_filtered(const struct net_device *dev, |
| 1976 | const struct rtnl_link_ops *kind_ops) |
| 1977 | { |
| 1978 | if (kind_ops && dev->rtnl_link_ops != kind_ops) |
| 1979 | return true; |
| 1980 | |
| 1981 | return false; |
| 1982 | } |
| 1983 | |
| 1984 | static bool link_dump_filtered(struct net_device *dev, |
| 1985 | int master_idx, |
| 1986 | const struct rtnl_link_ops *kind_ops) |
| 1987 | { |
| 1988 | if (link_master_filtered(dev, master_idx) || |
| 1989 | link_kind_filtered(dev, kind_ops)) |
| 1990 | return true; |
| 1991 | |
| 1992 | return false; |
| 1993 | } |
| 1994 | |
Christian Brauner | c383edc | 2018-09-04 21:53:47 +0200 | [diff] [blame] | 1995 | /** |
| 1996 | * rtnl_get_net_ns_capable - Get netns if sufficiently privileged. |
| 1997 | * @sk: netlink socket |
| 1998 | * @netnsid: network namespace identifier |
| 1999 | * |
| 2000 | * Returns the network namespace identified by netnsid on success or an error |
| 2001 | * pointer on failure. |
| 2002 | */ |
| 2003 | struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid) |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 2004 | { |
| 2005 | struct net *net; |
| 2006 | |
Andrei Vagin | f428fe4 | 2018-01-02 23:27:33 -0800 | [diff] [blame] | 2007 | net = get_net_ns_by_id(sock_net(sk), netnsid); |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 2008 | if (!net) |
| 2009 | return ERR_PTR(-EINVAL); |
| 2010 | |
| 2011 | /* For now, the caller is required to have CAP_NET_ADMIN in |
| 2012 | * the user namespace owning the target net ns. |
| 2013 | */ |
Andrei Vagin | f428fe4 | 2018-01-02 23:27:33 -0800 | [diff] [blame] | 2014 | if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) { |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 2015 | put_net(net); |
| 2016 | return ERR_PTR(-EACCES); |
| 2017 | } |
| 2018 | return net; |
| 2019 | } |
Christian Brauner | c383edc | 2018-09-04 21:53:47 +0200 | [diff] [blame] | 2020 | EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable); |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 2021 | |
David Ahern | 905cf0a | 2018-10-07 20:16:30 -0700 | [diff] [blame] | 2022 | static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh, |
| 2023 | bool strict_check, struct nlattr **tb, |
| 2024 | struct netlink_ext_ack *extack) |
| 2025 | { |
| 2026 | int hdrlen; |
| 2027 | |
| 2028 | if (strict_check) { |
| 2029 | struct ifinfomsg *ifm; |
| 2030 | |
| 2031 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { |
| 2032 | NL_SET_ERR_MSG(extack, "Invalid header for link dump"); |
| 2033 | return -EINVAL; |
| 2034 | } |
| 2035 | |
| 2036 | ifm = nlmsg_data(nlh); |
| 2037 | if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || |
| 2038 | ifm->ifi_change) { |
| 2039 | NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request"); |
| 2040 | return -EINVAL; |
| 2041 | } |
| 2042 | if (ifm->ifi_index) { |
| 2043 | NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps"); |
| 2044 | return -EINVAL; |
| 2045 | } |
| 2046 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2047 | return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, |
| 2048 | IFLA_MAX, ifla_policy, |
| 2049 | extack); |
David Ahern | 905cf0a | 2018-10-07 20:16:30 -0700 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | /* A hack to preserve kernel<->userspace interface. |
| 2053 | * The correct header is ifinfomsg. It is consistent with rtnl_getlink. |
| 2054 | * However, before Linux v3.9 the code here assumed rtgenmsg and that's |
| 2055 | * what iproute2 < v3.9.0 used. |
| 2056 | * We can detect the old iproute2. Even including the IFLA_EXT_MASK |
| 2057 | * attribute, its netlink message is shorter than struct ifinfomsg. |
| 2058 | */ |
| 2059 | hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? |
| 2060 | sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); |
| 2061 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2062 | return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, |
| 2063 | extack); |
David Ahern | 905cf0a | 2018-10-07 20:16:30 -0700 | [diff] [blame] | 2064 | } |
| 2065 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2066 | static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | { |
David Ahern | 905cf0a | 2018-10-07 20:16:30 -0700 | [diff] [blame] | 2068 | struct netlink_ext_ack *extack = cb->extack; |
| 2069 | const struct nlmsghdr *nlh = cb->nlh; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2070 | struct net *net = sock_net(skb->sk); |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 2071 | struct net *tgt_net = net; |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 2072 | int h, s_h; |
| 2073 | int idx = 0, s_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | struct net_device *dev; |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 2075 | struct hlist_head *head; |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2076 | struct nlattr *tb[IFLA_MAX+1]; |
| 2077 | u32 ext_filter_mask = 0; |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 2078 | const struct rtnl_link_ops *kind_ops = NULL; |
| 2079 | unsigned int flags = NLM_F_MULTI; |
| 2080 | int master_idx = 0; |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 2081 | int netnsid = -1; |
David Ahern | 905cf0a | 2018-10-07 20:16:30 -0700 | [diff] [blame] | 2082 | int err, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 2084 | s_h = cb->args[0]; |
| 2085 | s_idx = cb->args[1]; |
| 2086 | |
David Ahern | 905cf0a | 2018-10-07 20:16:30 -0700 | [diff] [blame] | 2087 | err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack); |
| 2088 | if (err < 0) { |
| 2089 | if (cb->strict_check) |
| 2090 | return err; |
Michal Schmidt | e5eca6d | 2014-05-28 14:15:19 +0200 | [diff] [blame] | 2091 | |
David Ahern | 905cf0a | 2018-10-07 20:16:30 -0700 | [diff] [blame] | 2092 | goto walk_entries; |
Eric Dumazet | a4b64fb | 2012-03-04 12:32:10 +0000 | [diff] [blame] | 2093 | } |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2094 | |
David Ahern | 905cf0a | 2018-10-07 20:16:30 -0700 | [diff] [blame] | 2095 | for (i = 0; i <= IFLA_MAX; ++i) { |
| 2096 | if (!tb[i]) |
| 2097 | continue; |
| 2098 | |
| 2099 | /* new attributes should only be added with strict checking */ |
| 2100 | switch (i) { |
| 2101 | case IFLA_TARGET_NETNSID: |
| 2102 | netnsid = nla_get_s32(tb[i]); |
| 2103 | tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid); |
| 2104 | if (IS_ERR(tgt_net)) { |
| 2105 | NL_SET_ERR_MSG(extack, "Invalid target network namespace id"); |
| 2106 | return PTR_ERR(tgt_net); |
| 2107 | } |
| 2108 | break; |
| 2109 | case IFLA_EXT_MASK: |
| 2110 | ext_filter_mask = nla_get_u32(tb[i]); |
| 2111 | break; |
| 2112 | case IFLA_MASTER: |
| 2113 | master_idx = nla_get_u32(tb[i]); |
| 2114 | break; |
| 2115 | case IFLA_LINKINFO: |
| 2116 | kind_ops = linkinfo_to_kind_ops(tb[i]); |
| 2117 | break; |
| 2118 | default: |
| 2119 | if (cb->strict_check) { |
| 2120 | NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request"); |
| 2121 | return -EINVAL; |
| 2122 | } |
| 2123 | } |
| 2124 | } |
| 2125 | |
| 2126 | if (master_idx || kind_ops) |
| 2127 | flags |= NLM_F_DUMP_FILTERED; |
| 2128 | |
| 2129 | walk_entries: |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 2130 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { |
| 2131 | idx = 0; |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 2132 | head = &tgt_net->dev_index_head[h]; |
Eric Dumazet | cac5e65 | 2015-02-27 09:42:50 -0800 | [diff] [blame] | 2133 | hlist_for_each_entry(dev, head, index_hlist) { |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 2134 | if (link_dump_filtered(dev, master_idx, kind_ops)) |
Zhang Shengju | 3f0ae05 | 2016-11-19 23:28:32 +0800 | [diff] [blame] | 2135 | goto cont; |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 2136 | if (idx < s_idx) |
| 2137 | goto cont; |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 2138 | err = rtnl_fill_ifinfo(skb, dev, net, |
| 2139 | RTM_NEWLINK, |
David Gibson | 973462b | 2014-04-24 10:22:35 +1000 | [diff] [blame] | 2140 | NETLINK_CB(cb->skb).portid, |
David Ahern | 905cf0a | 2018-10-07 20:16:30 -0700 | [diff] [blame] | 2141 | nlh->nlmsg_seq, 0, flags, |
Nicolas Dichtel | 38e01b3 | 2018-01-25 15:01:39 +0100 | [diff] [blame] | 2142 | ext_filter_mask, 0, NULL, 0, |
Guillaume Nault | d4e4fdf | 2019-10-23 18:39:04 +0200 | [diff] [blame] | 2143 | netnsid, GFP_KERNEL); |
David Gibson | 973462b | 2014-04-24 10:22:35 +1000 | [diff] [blame] | 2144 | |
David Ahern | f6c5775 | 2017-05-15 23:19:17 -0700 | [diff] [blame] | 2145 | if (err < 0) { |
| 2146 | if (likely(skb->len)) |
| 2147 | goto out; |
| 2148 | |
| 2149 | goto out_err; |
| 2150 | } |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 2151 | cont: |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 2152 | idx++; |
| 2153 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | } |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 2155 | out: |
David Ahern | f6c5775 | 2017-05-15 23:19:17 -0700 | [diff] [blame] | 2156 | err = skb->len; |
| 2157 | out_err: |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 2158 | cb->args[1] = idx; |
| 2159 | cb->args[0] = h; |
zhang kai | a9ecb0c | 2021-03-02 18:16:07 +0800 | [diff] [blame] | 2160 | cb->seq = tgt_net->dev_base_seq; |
Jakub Sitnicki | d022578 | 2017-08-09 17:39:12 +0200 | [diff] [blame] | 2161 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 2162 | if (netnsid >= 0) |
| 2163 | put_net(tgt_net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | |
David Ahern | f6c5775 | 2017-05-15 23:19:17 -0700 | [diff] [blame] | 2165 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | } |
| 2167 | |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 2168 | int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len, |
| 2169 | struct netlink_ext_ack *exterr) |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 2170 | { |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2171 | return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy, |
| 2172 | exterr); |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 2173 | } |
| 2174 | EXPORT_SYMBOL(rtnl_nla_parse_ifla); |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 2175 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 2176 | struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) |
| 2177 | { |
| 2178 | struct net *net; |
| 2179 | /* Examine the link attributes and figure out which |
| 2180 | * network namespace we are talking about. |
| 2181 | */ |
| 2182 | if (tb[IFLA_NET_NS_PID]) |
| 2183 | net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); |
Eric W. Biederman | f063052 | 2011-05-04 17:51:50 -0700 | [diff] [blame] | 2184 | else if (tb[IFLA_NET_NS_FD]) |
| 2185 | net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 2186 | else |
| 2187 | net = get_net(src_net); |
| 2188 | return net; |
| 2189 | } |
| 2190 | EXPORT_SYMBOL(rtnl_link_get_net); |
| 2191 | |
Christian Brauner | 7c4f63b | 2018-01-24 15:26:32 +0100 | [diff] [blame] | 2192 | /* Figure out which network namespace we are talking about by |
| 2193 | * examining the link attributes in the following order: |
| 2194 | * |
| 2195 | * 1. IFLA_NET_NS_PID |
| 2196 | * 2. IFLA_NET_NS_FD |
Christian Brauner | 7e4a8d5 | 2018-09-04 21:53:53 +0200 | [diff] [blame] | 2197 | * 3. IFLA_TARGET_NETNSID |
Christian Brauner | 7c4f63b | 2018-01-24 15:26:32 +0100 | [diff] [blame] | 2198 | */ |
| 2199 | static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net, |
| 2200 | struct nlattr *tb[]) |
| 2201 | { |
| 2202 | struct net *net; |
| 2203 | |
| 2204 | if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) |
| 2205 | return rtnl_link_get_net(src_net, tb); |
| 2206 | |
Christian Brauner | 7e4a8d5 | 2018-09-04 21:53:53 +0200 | [diff] [blame] | 2207 | if (!tb[IFLA_TARGET_NETNSID]) |
Christian Brauner | 7c4f63b | 2018-01-24 15:26:32 +0100 | [diff] [blame] | 2208 | return get_net(src_net); |
| 2209 | |
Christian Brauner | 7e4a8d5 | 2018-09-04 21:53:53 +0200 | [diff] [blame] | 2210 | net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID])); |
Christian Brauner | 7c4f63b | 2018-01-24 15:26:32 +0100 | [diff] [blame] | 2211 | if (!net) |
| 2212 | return ERR_PTR(-EINVAL); |
| 2213 | |
| 2214 | return net; |
| 2215 | } |
| 2216 | |
| 2217 | static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb, |
| 2218 | struct net *src_net, |
| 2219 | struct nlattr *tb[], int cap) |
| 2220 | { |
| 2221 | struct net *net; |
| 2222 | |
| 2223 | net = rtnl_link_get_net_by_nlattr(src_net, tb); |
| 2224 | if (IS_ERR(net)) |
| 2225 | return net; |
| 2226 | |
| 2227 | if (!netlink_ns_capable(skb, net->user_ns, cap)) { |
| 2228 | put_net(net); |
| 2229 | return ERR_PTR(-EPERM); |
| 2230 | } |
| 2231 | |
| 2232 | return net; |
| 2233 | } |
| 2234 | |
Christian Brauner | 4ff66ca | 2018-02-07 13:53:20 +0100 | [diff] [blame] | 2235 | /* Verify that rtnetlink requests do not pass additional properties |
| 2236 | * potentially referring to different network namespaces. |
| 2237 | */ |
| 2238 | static int rtnl_ensure_unique_netns(struct nlattr *tb[], |
| 2239 | struct netlink_ext_ack *extack, |
| 2240 | bool netns_id_only) |
| 2241 | { |
| 2242 | |
| 2243 | if (netns_id_only) { |
| 2244 | if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD]) |
| 2245 | return 0; |
| 2246 | |
| 2247 | NL_SET_ERR_MSG(extack, "specified netns attribute not supported"); |
| 2248 | return -EOPNOTSUPP; |
| 2249 | } |
| 2250 | |
Christian Brauner | 7e4a8d5 | 2018-09-04 21:53:53 +0200 | [diff] [blame] | 2251 | if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])) |
Christian Brauner | 4ff66ca | 2018-02-07 13:53:20 +0100 | [diff] [blame] | 2252 | goto invalid_attr; |
| 2253 | |
Christian Brauner | 7e4a8d5 | 2018-09-04 21:53:53 +0200 | [diff] [blame] | 2254 | if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD])) |
Christian Brauner | 4ff66ca | 2018-02-07 13:53:20 +0100 | [diff] [blame] | 2255 | goto invalid_attr; |
| 2256 | |
Christian Brauner | 7e4a8d5 | 2018-09-04 21:53:53 +0200 | [diff] [blame] | 2257 | if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID])) |
Christian Brauner | 4ff66ca | 2018-02-07 13:53:20 +0100 | [diff] [blame] | 2258 | goto invalid_attr; |
| 2259 | |
| 2260 | return 0; |
| 2261 | |
| 2262 | invalid_attr: |
| 2263 | NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified"); |
| 2264 | return -EINVAL; |
| 2265 | } |
| 2266 | |
Rocco Yue | 8679c31 | 2021-08-03 20:02:50 +0800 | [diff] [blame] | 2267 | static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[], |
| 2268 | struct netlink_ext_ack *extack) |
Thomas Graf | 1840bb1 | 2008-02-23 19:54:36 -0800 | [diff] [blame] | 2269 | { |
| 2270 | if (dev) { |
| 2271 | if (tb[IFLA_ADDRESS] && |
| 2272 | nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) |
| 2273 | return -EINVAL; |
| 2274 | |
| 2275 | if (tb[IFLA_BROADCAST] && |
| 2276 | nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) |
| 2277 | return -EINVAL; |
| 2278 | } |
| 2279 | |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 2280 | if (tb[IFLA_AF_SPEC]) { |
| 2281 | struct nlattr *af; |
| 2282 | int rem, err; |
| 2283 | |
| 2284 | nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { |
| 2285 | const struct rtnl_af_ops *af_ops; |
| 2286 | |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 2287 | af_ops = rtnl_af_lookup(nla_type(af)); |
Cong Wang | a100243 | 2021-05-08 11:00:33 -0700 | [diff] [blame] | 2288 | if (!af_ops) |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 2289 | return -EAFNOSUPPORT; |
| 2290 | |
Cong Wang | a100243 | 2021-05-08 11:00:33 -0700 | [diff] [blame] | 2291 | if (!af_ops->set_link_af) |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 2292 | return -EOPNOTSUPP; |
| 2293 | |
| 2294 | if (af_ops->validate_link_af) { |
Rocco Yue | 8679c31 | 2021-08-03 20:02:50 +0800 | [diff] [blame] | 2295 | err = af_ops->validate_link_af(dev, af, extack); |
Cong Wang | a100243 | 2021-05-08 11:00:33 -0700 | [diff] [blame] | 2296 | if (err < 0) |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 2297 | return err; |
| 2298 | } |
| 2299 | } |
| 2300 | } |
| 2301 | |
Thomas Graf | 1840bb1 | 2008-02-23 19:54:36 -0800 | [diff] [blame] | 2302 | return 0; |
| 2303 | } |
| 2304 | |
Eli Cohen | cc8e27c | 2016-03-11 22:58:34 +0200 | [diff] [blame] | 2305 | static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt, |
| 2306 | int guid_type) |
| 2307 | { |
| 2308 | const struct net_device_ops *ops = dev->netdev_ops; |
| 2309 | |
| 2310 | return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type); |
| 2311 | } |
| 2312 | |
| 2313 | static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type) |
| 2314 | { |
| 2315 | if (dev->type != ARPHRD_INFINIBAND) |
| 2316 | return -EOPNOTSUPP; |
| 2317 | |
| 2318 | return handle_infiniband_guid(dev, ivt, guid_type); |
| 2319 | } |
| 2320 | |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2321 | static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 2322 | { |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 2323 | const struct net_device_ops *ops = dev->netdev_ops; |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2324 | int err = -EINVAL; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 2325 | |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2326 | if (tb[IFLA_VF_MAC]) { |
| 2327 | struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); |
Vlad Zolotarov | 01a3d79 | 2015-03-30 21:35:23 +0300 | [diff] [blame] | 2328 | |
Dan Carpenter | ff08ddb | 2019-11-20 15:34:38 +0300 | [diff] [blame] | 2329 | if (ivm->vf >= INT_MAX) |
| 2330 | return -EINVAL; |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2331 | err = -EOPNOTSUPP; |
| 2332 | if (ops->ndo_set_vf_mac) |
| 2333 | err = ops->ndo_set_vf_mac(dev, ivm->vf, |
| 2334 | ivm->mac); |
| 2335 | if (err < 0) |
| 2336 | return err; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 2337 | } |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2338 | |
| 2339 | if (tb[IFLA_VF_VLAN]) { |
| 2340 | struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); |
| 2341 | |
Dan Carpenter | ff08ddb | 2019-11-20 15:34:38 +0300 | [diff] [blame] | 2342 | if (ivv->vf >= INT_MAX) |
| 2343 | return -EINVAL; |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2344 | err = -EOPNOTSUPP; |
| 2345 | if (ops->ndo_set_vf_vlan) |
| 2346 | err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 2347 | ivv->qos, |
| 2348 | htons(ETH_P_8021Q)); |
| 2349 | if (err < 0) |
| 2350 | return err; |
| 2351 | } |
| 2352 | |
| 2353 | if (tb[IFLA_VF_VLAN_LIST]) { |
| 2354 | struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN]; |
| 2355 | struct nlattr *attr; |
| 2356 | int rem, len = 0; |
| 2357 | |
| 2358 | err = -EOPNOTSUPP; |
| 2359 | if (!ops->ndo_set_vf_vlan) |
| 2360 | return err; |
| 2361 | |
| 2362 | nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) { |
| 2363 | if (nla_type(attr) != IFLA_VF_VLAN_INFO || |
| 2364 | nla_len(attr) < NLA_HDRLEN) { |
| 2365 | return -EINVAL; |
| 2366 | } |
| 2367 | if (len >= MAX_VLAN_LIST_LEN) |
| 2368 | return -EOPNOTSUPP; |
| 2369 | ivvl[len] = nla_data(attr); |
| 2370 | |
| 2371 | len++; |
| 2372 | } |
Arnd Bergmann | fa34cd9 | 2016-09-30 18:13:49 +0200 | [diff] [blame] | 2373 | if (len == 0) |
| 2374 | return -EINVAL; |
| 2375 | |
Dan Carpenter | ff08ddb | 2019-11-20 15:34:38 +0300 | [diff] [blame] | 2376 | if (ivvl[0]->vf >= INT_MAX) |
| 2377 | return -EINVAL; |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 2378 | err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan, |
| 2379 | ivvl[0]->qos, ivvl[0]->vlan_proto); |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2380 | if (err < 0) |
| 2381 | return err; |
| 2382 | } |
| 2383 | |
| 2384 | if (tb[IFLA_VF_TX_RATE]) { |
| 2385 | struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); |
| 2386 | struct ifla_vf_info ivf; |
| 2387 | |
Dan Carpenter | ff08ddb | 2019-11-20 15:34:38 +0300 | [diff] [blame] | 2388 | if (ivt->vf >= INT_MAX) |
| 2389 | return -EINVAL; |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2390 | err = -EOPNOTSUPP; |
| 2391 | if (ops->ndo_get_vf_config) |
| 2392 | err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); |
| 2393 | if (err < 0) |
| 2394 | return err; |
| 2395 | |
| 2396 | err = -EOPNOTSUPP; |
| 2397 | if (ops->ndo_set_vf_rate) |
| 2398 | err = ops->ndo_set_vf_rate(dev, ivt->vf, |
| 2399 | ivf.min_tx_rate, |
| 2400 | ivt->rate); |
| 2401 | if (err < 0) |
| 2402 | return err; |
| 2403 | } |
| 2404 | |
| 2405 | if (tb[IFLA_VF_RATE]) { |
| 2406 | struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); |
| 2407 | |
Dan Carpenter | ff08ddb | 2019-11-20 15:34:38 +0300 | [diff] [blame] | 2408 | if (ivt->vf >= INT_MAX) |
| 2409 | return -EINVAL; |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2410 | err = -EOPNOTSUPP; |
| 2411 | if (ops->ndo_set_vf_rate) |
| 2412 | err = ops->ndo_set_vf_rate(dev, ivt->vf, |
| 2413 | ivt->min_tx_rate, |
| 2414 | ivt->max_tx_rate); |
| 2415 | if (err < 0) |
| 2416 | return err; |
| 2417 | } |
| 2418 | |
| 2419 | if (tb[IFLA_VF_SPOOFCHK]) { |
| 2420 | struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); |
| 2421 | |
Dan Carpenter | ff08ddb | 2019-11-20 15:34:38 +0300 | [diff] [blame] | 2422 | if (ivs->vf >= INT_MAX) |
| 2423 | return -EINVAL; |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2424 | err = -EOPNOTSUPP; |
| 2425 | if (ops->ndo_set_vf_spoofchk) |
| 2426 | err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, |
| 2427 | ivs->setting); |
| 2428 | if (err < 0) |
| 2429 | return err; |
| 2430 | } |
| 2431 | |
| 2432 | if (tb[IFLA_VF_LINK_STATE]) { |
| 2433 | struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); |
| 2434 | |
Dan Carpenter | ff08ddb | 2019-11-20 15:34:38 +0300 | [diff] [blame] | 2435 | if (ivl->vf >= INT_MAX) |
| 2436 | return -EINVAL; |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2437 | err = -EOPNOTSUPP; |
| 2438 | if (ops->ndo_set_vf_link_state) |
| 2439 | err = ops->ndo_set_vf_link_state(dev, ivl->vf, |
| 2440 | ivl->link_state); |
| 2441 | if (err < 0) |
| 2442 | return err; |
| 2443 | } |
| 2444 | |
| 2445 | if (tb[IFLA_VF_RSS_QUERY_EN]) { |
| 2446 | struct ifla_vf_rss_query_en *ivrssq_en; |
| 2447 | |
| 2448 | err = -EOPNOTSUPP; |
| 2449 | ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); |
Dan Carpenter | ff08ddb | 2019-11-20 15:34:38 +0300 | [diff] [blame] | 2450 | if (ivrssq_en->vf >= INT_MAX) |
| 2451 | return -EINVAL; |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2452 | if (ops->ndo_set_vf_rss_query_en) |
| 2453 | err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, |
| 2454 | ivrssq_en->setting); |
| 2455 | if (err < 0) |
| 2456 | return err; |
| 2457 | } |
| 2458 | |
Hiroshi Shimamoto | dd461d6 | 2015-08-28 06:57:55 +0000 | [diff] [blame] | 2459 | if (tb[IFLA_VF_TRUST]) { |
| 2460 | struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); |
| 2461 | |
Dan Carpenter | ff08ddb | 2019-11-20 15:34:38 +0300 | [diff] [blame] | 2462 | if (ivt->vf >= INT_MAX) |
| 2463 | return -EINVAL; |
Hiroshi Shimamoto | dd461d6 | 2015-08-28 06:57:55 +0000 | [diff] [blame] | 2464 | err = -EOPNOTSUPP; |
| 2465 | if (ops->ndo_set_vf_trust) |
| 2466 | err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); |
| 2467 | if (err < 0) |
| 2468 | return err; |
| 2469 | } |
| 2470 | |
Eli Cohen | cc8e27c | 2016-03-11 22:58:34 +0200 | [diff] [blame] | 2471 | if (tb[IFLA_VF_IB_NODE_GUID]) { |
| 2472 | struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]); |
| 2473 | |
Dan Carpenter | ff08ddb | 2019-11-20 15:34:38 +0300 | [diff] [blame] | 2474 | if (ivt->vf >= INT_MAX) |
| 2475 | return -EINVAL; |
Eli Cohen | cc8e27c | 2016-03-11 22:58:34 +0200 | [diff] [blame] | 2476 | if (!ops->ndo_set_vf_guid) |
| 2477 | return -EOPNOTSUPP; |
Eli Cohen | cc8e27c | 2016-03-11 22:58:34 +0200 | [diff] [blame] | 2478 | return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID); |
| 2479 | } |
| 2480 | |
| 2481 | if (tb[IFLA_VF_IB_PORT_GUID]) { |
| 2482 | struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]); |
| 2483 | |
Dan Carpenter | ff08ddb | 2019-11-20 15:34:38 +0300 | [diff] [blame] | 2484 | if (ivt->vf >= INT_MAX) |
| 2485 | return -EINVAL; |
Eli Cohen | cc8e27c | 2016-03-11 22:58:34 +0200 | [diff] [blame] | 2486 | if (!ops->ndo_set_vf_guid) |
| 2487 | return -EOPNOTSUPP; |
| 2488 | |
| 2489 | return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID); |
| 2490 | } |
| 2491 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 2492 | return err; |
| 2493 | } |
| 2494 | |
David Ahern | 33eaf2a | 2017-10-04 17:48:46 -0700 | [diff] [blame] | 2495 | static int do_set_master(struct net_device *dev, int ifindex, |
| 2496 | struct netlink_ext_ack *extack) |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 2497 | { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2498 | struct net_device *upper_dev = netdev_master_upper_dev_get(dev); |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 2499 | const struct net_device_ops *ops; |
| 2500 | int err; |
| 2501 | |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2502 | if (upper_dev) { |
| 2503 | if (upper_dev->ifindex == ifindex) |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 2504 | return 0; |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2505 | ops = upper_dev->netdev_ops; |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 2506 | if (ops->ndo_del_slave) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2507 | err = ops->ndo_del_slave(upper_dev, dev); |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 2508 | if (err) |
| 2509 | return err; |
| 2510 | } else { |
| 2511 | return -EOPNOTSUPP; |
| 2512 | } |
| 2513 | } |
| 2514 | |
| 2515 | if (ifindex) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2516 | upper_dev = __dev_get_by_index(dev_net(dev), ifindex); |
| 2517 | if (!upper_dev) |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 2518 | return -EINVAL; |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2519 | ops = upper_dev->netdev_ops; |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 2520 | if (ops->ndo_add_slave) { |
David Ahern | 33eaf2a | 2017-10-04 17:48:46 -0700 | [diff] [blame] | 2521 | err = ops->ndo_add_slave(upper_dev, dev, extack); |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 2522 | if (err) |
| 2523 | return err; |
| 2524 | } else { |
| 2525 | return -EOPNOTSUPP; |
| 2526 | } |
| 2527 | } |
| 2528 | return 0; |
| 2529 | } |
| 2530 | |
Roopa Prabhu | 829eb20 | 2020-07-31 17:34:01 -0700 | [diff] [blame] | 2531 | static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = { |
| 2532 | [IFLA_PROTO_DOWN_REASON_MASK] = { .type = NLA_U32 }, |
| 2533 | [IFLA_PROTO_DOWN_REASON_VALUE] = { .type = NLA_U32 }, |
| 2534 | }; |
| 2535 | |
| 2536 | static int do_set_proto_down(struct net_device *dev, |
| 2537 | struct nlattr *nl_proto_down, |
| 2538 | struct nlattr *nl_proto_down_reason, |
| 2539 | struct netlink_ext_ack *extack) |
| 2540 | { |
| 2541 | struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1]; |
| 2542 | const struct net_device_ops *ops = dev->netdev_ops; |
| 2543 | unsigned long mask = 0; |
| 2544 | u32 value; |
| 2545 | bool proto_down; |
| 2546 | int err; |
| 2547 | |
| 2548 | if (!ops->ndo_change_proto_down) { |
| 2549 | NL_SET_ERR_MSG(extack, "Protodown not supported by device"); |
| 2550 | return -EOPNOTSUPP; |
| 2551 | } |
| 2552 | |
| 2553 | if (nl_proto_down_reason) { |
| 2554 | err = nla_parse_nested_deprecated(pdreason, |
| 2555 | IFLA_PROTO_DOWN_REASON_MAX, |
| 2556 | nl_proto_down_reason, |
| 2557 | ifla_proto_down_reason_policy, |
| 2558 | NULL); |
| 2559 | if (err < 0) |
| 2560 | return err; |
| 2561 | |
| 2562 | if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) { |
| 2563 | NL_SET_ERR_MSG(extack, "Invalid protodown reason value"); |
| 2564 | return -EINVAL; |
| 2565 | } |
| 2566 | |
| 2567 | value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]); |
| 2568 | |
| 2569 | if (pdreason[IFLA_PROTO_DOWN_REASON_MASK]) |
| 2570 | mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]); |
| 2571 | |
| 2572 | dev_change_proto_down_reason(dev, mask, value); |
| 2573 | } |
| 2574 | |
| 2575 | if (nl_proto_down) { |
| 2576 | proto_down = nla_get_u8(nl_proto_down); |
| 2577 | |
Zheng Yongjun | d467d0b | 2021-06-02 14:56:23 +0800 | [diff] [blame] | 2578 | /* Don't turn off protodown if there are active reasons */ |
Roopa Prabhu | 829eb20 | 2020-07-31 17:34:01 -0700 | [diff] [blame] | 2579 | if (!proto_down && dev->proto_down_reason) { |
| 2580 | NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons"); |
| 2581 | return -EBUSY; |
| 2582 | } |
| 2583 | err = dev_change_proto_down(dev, |
| 2584 | proto_down); |
| 2585 | if (err) |
| 2586 | return err; |
| 2587 | } |
| 2588 | |
| 2589 | return 0; |
| 2590 | } |
| 2591 | |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 2592 | #define DO_SETLINK_MODIFIED 0x01 |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2593 | /* notify flag means notify + modified. */ |
| 2594 | #define DO_SETLINK_NOTIFY 0x03 |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 2595 | static int do_setlink(const struct sk_buff *skb, |
| 2596 | struct net_device *dev, struct ifinfomsg *ifm, |
Jakub Kicinski | ddf9f97 | 2017-04-30 21:46:46 -0700 | [diff] [blame] | 2597 | struct netlink_ext_ack *extack, |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 2598 | struct nlattr **tb, char *ifname, int status) |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2599 | { |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 2600 | const struct net_device_ops *ops = dev->netdev_ops; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2601 | int err; |
| 2602 | |
Rocco Yue | 8679c31 | 2021-08-03 20:02:50 +0800 | [diff] [blame] | 2603 | err = validate_linkmsg(dev, tb, extack); |
Eric Dumazet | 644c7ee | 2018-06-05 09:25:19 -0700 | [diff] [blame] | 2604 | if (err < 0) |
| 2605 | return err; |
| 2606 | |
Christian Brauner | 7e4a8d5 | 2018-09-04 21:53:53 +0200 | [diff] [blame] | 2607 | if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) { |
Andrey Ignatov | 96a6b93 | 2021-08-25 17:25:40 -0700 | [diff] [blame] | 2608 | const char *pat = ifname && ifname[0] ? ifname : NULL; |
Andrei Vagin | eeb85a1 | 2021-04-05 00:12:23 -0700 | [diff] [blame] | 2609 | struct net *net; |
| 2610 | int new_ifindex; |
| 2611 | |
| 2612 | net = rtnl_link_get_net_capable(skb, dev_net(dev), |
| 2613 | tb, CAP_NET_ADMIN); |
Eric W. Biederman | d8a5ec6 | 2007-09-12 13:57:04 +0200 | [diff] [blame] | 2614 | if (IS_ERR(net)) { |
| 2615 | err = PTR_ERR(net); |
| 2616 | goto errout; |
| 2617 | } |
Christian Brauner | 7c4f63b | 2018-01-24 15:26:32 +0100 | [diff] [blame] | 2618 | |
Andrei Vagin | eeb85a1 | 2021-04-05 00:12:23 -0700 | [diff] [blame] | 2619 | if (tb[IFLA_NEW_IFINDEX]) |
| 2620 | new_ifindex = nla_get_s32(tb[IFLA_NEW_IFINDEX]); |
| 2621 | else |
| 2622 | new_ifindex = 0; |
| 2623 | |
Andrey Ignatov | 96a6b93 | 2021-08-25 17:25:40 -0700 | [diff] [blame] | 2624 | err = __dev_change_net_namespace(dev, net, pat, new_ifindex); |
Eric W. Biederman | d8a5ec6 | 2007-09-12 13:57:04 +0200 | [diff] [blame] | 2625 | put_net(net); |
| 2626 | if (err) |
| 2627 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 2628 | status |= DO_SETLINK_MODIFIED; |
Eric W. Biederman | d8a5ec6 | 2007-09-12 13:57:04 +0200 | [diff] [blame] | 2629 | } |
| 2630 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2631 | if (tb[IFLA_MAP]) { |
| 2632 | struct rtnl_link_ifmap *u_map; |
| 2633 | struct ifmap k_map; |
| 2634 | |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 2635 | if (!ops->ndo_set_config) { |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2636 | err = -EOPNOTSUPP; |
| 2637 | goto errout; |
| 2638 | } |
| 2639 | |
| 2640 | if (!netif_device_present(dev)) { |
| 2641 | err = -ENODEV; |
| 2642 | goto errout; |
| 2643 | } |
| 2644 | |
| 2645 | u_map = nla_data(tb[IFLA_MAP]); |
| 2646 | k_map.mem_start = (unsigned long) u_map->mem_start; |
| 2647 | k_map.mem_end = (unsigned long) u_map->mem_end; |
| 2648 | k_map.base_addr = (unsigned short) u_map->base_addr; |
| 2649 | k_map.irq = (unsigned char) u_map->irq; |
| 2650 | k_map.dma = (unsigned char) u_map->dma; |
| 2651 | k_map.port = (unsigned char) u_map->port; |
| 2652 | |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 2653 | err = ops->ndo_set_config(dev, &k_map); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2654 | if (err < 0) |
| 2655 | goto errout; |
| 2656 | |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2657 | status |= DO_SETLINK_NOTIFY; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2658 | } |
| 2659 | |
| 2660 | if (tb[IFLA_ADDRESS]) { |
| 2661 | struct sockaddr *sa; |
| 2662 | int len; |
| 2663 | |
WANG Cong | 153711f | 2017-07-20 11:27:57 -0700 | [diff] [blame] | 2664 | len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len, |
| 2665 | sizeof(*sa)); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2666 | sa = kmalloc(len, GFP_KERNEL); |
| 2667 | if (!sa) { |
| 2668 | err = -ENOMEM; |
| 2669 | goto errout; |
| 2670 | } |
| 2671 | sa->sa_family = dev->type; |
| 2672 | memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), |
| 2673 | dev->addr_len); |
Cong Wang | 3b23a32 | 2021-02-11 11:34:10 -0800 | [diff] [blame] | 2674 | err = dev_set_mac_address_user(dev, sa, extack); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2675 | kfree(sa); |
| 2676 | if (err) |
| 2677 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 2678 | status |= DO_SETLINK_MODIFIED; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2679 | } |
| 2680 | |
| 2681 | if (tb[IFLA_MTU]) { |
Stephen Hemminger | 7a4c53b | 2018-07-27 13:43:23 -0700 | [diff] [blame] | 2682 | err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2683 | if (err < 0) |
| 2684 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 2685 | status |= DO_SETLINK_MODIFIED; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2686 | } |
| 2687 | |
Vlad Dogaru | cbda10f | 2011-01-13 23:38:30 +0000 | [diff] [blame] | 2688 | if (tb[IFLA_GROUP]) { |
| 2689 | dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2690 | status |= DO_SETLINK_NOTIFY; |
Vlad Dogaru | cbda10f | 2011-01-13 23:38:30 +0000 | [diff] [blame] | 2691 | } |
| 2692 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2693 | /* |
| 2694 | * Interface selected by interface index but interface |
| 2695 | * name provided implies that a name change has been |
| 2696 | * requested. |
| 2697 | */ |
| 2698 | if (ifm->ifi_index > 0 && ifname[0]) { |
| 2699 | err = dev_change_name(dev, ifname); |
| 2700 | if (err < 0) |
| 2701 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 2702 | status |= DO_SETLINK_MODIFIED; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2703 | } |
| 2704 | |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 2705 | if (tb[IFLA_IFALIAS]) { |
| 2706 | err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), |
| 2707 | nla_len(tb[IFLA_IFALIAS])); |
| 2708 | if (err < 0) |
| 2709 | goto errout; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2710 | status |= DO_SETLINK_NOTIFY; |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 2711 | } |
| 2712 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2713 | if (tb[IFLA_BROADCAST]) { |
| 2714 | nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); |
Jiri Pirko | e7c3273 | 2013-01-01 03:30:13 +0000 | [diff] [blame] | 2715 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2716 | } |
| 2717 | |
| 2718 | if (ifm->ifi_flags || ifm->ifi_change) { |
Petr Machata | 567c5e1 | 2018-12-06 17:05:42 +0000 | [diff] [blame] | 2719 | err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), |
| 2720 | extack); |
Johannes Berg | 5f9021c | 2008-11-16 23:20:31 -0800 | [diff] [blame] | 2721 | if (err < 0) |
| 2722 | goto errout; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2723 | } |
| 2724 | |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 2725 | if (tb[IFLA_MASTER]) { |
David Ahern | 33eaf2a | 2017-10-04 17:48:46 -0700 | [diff] [blame] | 2726 | err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 2727 | if (err) |
| 2728 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 2729 | status |= DO_SETLINK_MODIFIED; |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 2730 | } |
| 2731 | |
Jiri Pirko | 9a57247 | 2012-12-27 23:49:39 +0000 | [diff] [blame] | 2732 | if (tb[IFLA_CARRIER]) { |
| 2733 | err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); |
| 2734 | if (err) |
| 2735 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 2736 | status |= DO_SETLINK_MODIFIED; |
Jiri Pirko | 9a57247 | 2012-12-27 23:49:39 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
Nicolas Dichtel | 5d1180f | 2014-09-01 16:07:26 +0200 | [diff] [blame] | 2739 | if (tb[IFLA_TXQLEN]) { |
Alexey Dobriyan | 0cd2950 | 2017-05-17 13:30:44 +0300 | [diff] [blame] | 2740 | unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]); |
Nicolas Dichtel | 5d1180f | 2014-09-01 16:07:26 +0200 | [diff] [blame] | 2741 | |
Cong Wang | 6a643dd | 2018-01-25 18:26:22 -0800 | [diff] [blame] | 2742 | err = dev_change_tx_queue_len(dev, value); |
| 2743 | if (err) |
| 2744 | goto errout; |
| 2745 | status |= DO_SETLINK_MODIFIED; |
Nicolas Dichtel | 5d1180f | 2014-09-01 16:07:26 +0200 | [diff] [blame] | 2746 | } |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2747 | |
Stephen Hemminger | 46e6b99 | 2017-12-07 15:40:19 -0800 | [diff] [blame] | 2748 | if (tb[IFLA_GSO_MAX_SIZE]) { |
| 2749 | u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]); |
| 2750 | |
| 2751 | if (max_size > GSO_MAX_SIZE) { |
| 2752 | err = -EINVAL; |
| 2753 | goto errout; |
| 2754 | } |
| 2755 | |
| 2756 | if (dev->gso_max_size ^ max_size) { |
| 2757 | netif_set_gso_max_size(dev, max_size); |
| 2758 | status |= DO_SETLINK_MODIFIED; |
| 2759 | } |
| 2760 | } |
| 2761 | |
| 2762 | if (tb[IFLA_GSO_MAX_SEGS]) { |
| 2763 | u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); |
| 2764 | |
| 2765 | if (max_segs > GSO_MAX_SEGS) { |
| 2766 | err = -EINVAL; |
| 2767 | goto errout; |
| 2768 | } |
| 2769 | |
| 2770 | if (dev->gso_max_segs ^ max_segs) { |
| 2771 | dev->gso_max_segs = max_segs; |
| 2772 | status |= DO_SETLINK_MODIFIED; |
| 2773 | } |
| 2774 | } |
| 2775 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2776 | if (tb[IFLA_OPERSTATE]) |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 2777 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2778 | |
| 2779 | if (tb[IFLA_LINKMODE]) { |
Nicolas Dichtel | 1889b0e | 2014-09-01 16:07:27 +0200 | [diff] [blame] | 2780 | unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); |
| 2781 | |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 2782 | write_lock_bh(&dev_base_lock); |
Nicolas Dichtel | 1889b0e | 2014-09-01 16:07:27 +0200 | [diff] [blame] | 2783 | if (dev->link_mode ^ value) |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2784 | status |= DO_SETLINK_NOTIFY; |
Nicolas Dichtel | 1889b0e | 2014-09-01 16:07:27 +0200 | [diff] [blame] | 2785 | dev->link_mode = value; |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 2786 | write_unlock_bh(&dev_base_lock); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2787 | } |
| 2788 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 2789 | if (tb[IFLA_VFINFO_LIST]) { |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2790 | struct nlattr *vfinfo[IFLA_VF_MAX + 1]; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 2791 | struct nlattr *attr; |
| 2792 | int rem; |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2793 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 2794 | nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2795 | if (nla_type(attr) != IFLA_VF_INFO || |
| 2796 | nla_len(attr) < NLA_HDRLEN) { |
David Howells | 253683b | 2010-05-21 02:25:27 +0000 | [diff] [blame] | 2797 | err = -EINVAL; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 2798 | goto errout; |
David Howells | 253683b | 2010-05-21 02:25:27 +0000 | [diff] [blame] | 2799 | } |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2800 | err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX, |
| 2801 | attr, |
| 2802 | ifla_vf_policy, |
| 2803 | NULL); |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2804 | if (err < 0) |
| 2805 | goto errout; |
| 2806 | err = do_setvfinfo(dev, vfinfo); |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 2807 | if (err < 0) |
| 2808 | goto errout; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2809 | status |= DO_SETLINK_NOTIFY; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 2810 | } |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 2811 | } |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2812 | err = 0; |
| 2813 | |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 2814 | if (tb[IFLA_VF_PORTS]) { |
| 2815 | struct nlattr *port[IFLA_PORT_MAX+1]; |
| 2816 | struct nlattr *attr; |
| 2817 | int vf; |
| 2818 | int rem; |
| 2819 | |
| 2820 | err = -EOPNOTSUPP; |
| 2821 | if (!ops->ndo_set_vf_port) |
| 2822 | goto errout; |
| 2823 | |
| 2824 | nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { |
Daniel Borkmann | 035d210 | 2015-07-13 00:06:02 +0200 | [diff] [blame] | 2825 | if (nla_type(attr) != IFLA_VF_PORT || |
| 2826 | nla_len(attr) < NLA_HDRLEN) { |
| 2827 | err = -EINVAL; |
| 2828 | goto errout; |
| 2829 | } |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2830 | err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX, |
| 2831 | attr, |
| 2832 | ifla_port_policy, |
| 2833 | NULL); |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 2834 | if (err < 0) |
| 2835 | goto errout; |
| 2836 | if (!port[IFLA_PORT_VF]) { |
| 2837 | err = -EOPNOTSUPP; |
| 2838 | goto errout; |
| 2839 | } |
| 2840 | vf = nla_get_u32(port[IFLA_PORT_VF]); |
| 2841 | err = ops->ndo_set_vf_port(dev, vf, port); |
| 2842 | if (err < 0) |
| 2843 | goto errout; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2844 | status |= DO_SETLINK_NOTIFY; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 2845 | } |
| 2846 | } |
| 2847 | err = 0; |
| 2848 | |
| 2849 | if (tb[IFLA_PORT_SELF]) { |
| 2850 | struct nlattr *port[IFLA_PORT_MAX+1]; |
| 2851 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2852 | err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX, |
| 2853 | tb[IFLA_PORT_SELF], |
| 2854 | ifla_port_policy, NULL); |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 2855 | if (err < 0) |
| 2856 | goto errout; |
| 2857 | |
| 2858 | err = -EOPNOTSUPP; |
| 2859 | if (ops->ndo_set_vf_port) |
| 2860 | err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); |
| 2861 | if (err < 0) |
| 2862 | goto errout; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2863 | status |= DO_SETLINK_NOTIFY; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 2864 | } |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 2865 | |
| 2866 | if (tb[IFLA_AF_SPEC]) { |
| 2867 | struct nlattr *af; |
| 2868 | int rem; |
| 2869 | |
| 2870 | nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { |
| 2871 | const struct rtnl_af_ops *af_ops; |
| 2872 | |
Gustavo A. R. Silva | 058c8d5 | 2017-10-20 19:43:11 -0500 | [diff] [blame] | 2873 | BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af)))); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 2874 | |
Stephen Hemminger | 3583a4e | 2021-04-07 08:59:12 -0700 | [diff] [blame] | 2875 | err = af_ops->set_link_af(dev, af, extack); |
Cong Wang | a100243 | 2021-05-08 11:00:33 -0700 | [diff] [blame] | 2876 | if (err < 0) |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 2877 | goto errout; |
| 2878 | |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2879 | status |= DO_SETLINK_NOTIFY; |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 2880 | } |
| 2881 | } |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 2882 | err = 0; |
| 2883 | |
Roopa Prabhu | 829eb20 | 2020-07-31 17:34:01 -0700 | [diff] [blame] | 2884 | if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) { |
| 2885 | err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN], |
| 2886 | tb[IFLA_PROTO_DOWN_REASON], extack); |
Anuradha Karuppiah | 88d6378 | 2015-07-14 13:43:20 -0700 | [diff] [blame] | 2887 | if (err) |
| 2888 | goto errout; |
| 2889 | status |= DO_SETLINK_NOTIFY; |
| 2890 | } |
| 2891 | |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 2892 | if (tb[IFLA_XDP]) { |
| 2893 | struct nlattr *xdp[IFLA_XDP_MAX + 1]; |
Daniel Borkmann | 85de857 | 2016-11-28 23:16:54 +0100 | [diff] [blame] | 2894 | u32 xdp_flags = 0; |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 2895 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2896 | err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX, |
| 2897 | tb[IFLA_XDP], |
| 2898 | ifla_xdp_policy, NULL); |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 2899 | if (err < 0) |
| 2900 | goto errout; |
| 2901 | |
Martin KaFai Lau | 5803869 | 2017-06-15 17:29:09 -0700 | [diff] [blame] | 2902 | if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) { |
Brenden Blanco | 262d862 | 2016-07-20 17:22:34 -0700 | [diff] [blame] | 2903 | err = -EINVAL; |
| 2904 | goto errout; |
| 2905 | } |
Daniel Borkmann | 85de857 | 2016-11-28 23:16:54 +0100 | [diff] [blame] | 2906 | |
| 2907 | if (xdp[IFLA_XDP_FLAGS]) { |
| 2908 | xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]); |
| 2909 | if (xdp_flags & ~XDP_FLAGS_MASK) { |
| 2910 | err = -EINVAL; |
| 2911 | goto errout; |
| 2912 | } |
Jakub Kicinski | ee5d032 | 2017-06-21 18:25:04 -0700 | [diff] [blame] | 2913 | if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) { |
Daniel Borkmann | 0489df9 | 2017-05-12 01:04:45 +0200 | [diff] [blame] | 2914 | err = -EINVAL; |
| 2915 | goto errout; |
| 2916 | } |
Daniel Borkmann | 85de857 | 2016-11-28 23:16:54 +0100 | [diff] [blame] | 2917 | } |
| 2918 | |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 2919 | if (xdp[IFLA_XDP_FD]) { |
Toke Høiland-Jørgensen | 92234c8 | 2020-03-25 18:23:26 +0100 | [diff] [blame] | 2920 | int expected_fd = -1; |
| 2921 | |
| 2922 | if (xdp_flags & XDP_FLAGS_REPLACE) { |
| 2923 | if (!xdp[IFLA_XDP_EXPECTED_FD]) { |
| 2924 | err = -EINVAL; |
| 2925 | goto errout; |
| 2926 | } |
| 2927 | expected_fd = |
| 2928 | nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]); |
| 2929 | } |
| 2930 | |
Jakub Kicinski | ddf9f97 | 2017-04-30 21:46:46 -0700 | [diff] [blame] | 2931 | err = dev_change_xdp_fd(dev, extack, |
Daniel Borkmann | 85de857 | 2016-11-28 23:16:54 +0100 | [diff] [blame] | 2932 | nla_get_s32(xdp[IFLA_XDP_FD]), |
Toke Høiland-Jørgensen | 92234c8 | 2020-03-25 18:23:26 +0100 | [diff] [blame] | 2933 | expected_fd, |
Daniel Borkmann | 85de857 | 2016-11-28 23:16:54 +0100 | [diff] [blame] | 2934 | xdp_flags); |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 2935 | if (err) |
| 2936 | goto errout; |
| 2937 | status |= DO_SETLINK_NOTIFY; |
| 2938 | } |
| 2939 | } |
| 2940 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2941 | errout: |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2942 | if (status & DO_SETLINK_MODIFIED) { |
Xin Long | 64ff90c | 2017-10-15 18:13:45 +0800 | [diff] [blame] | 2943 | if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY) |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2944 | netdev_state_change(dev); |
| 2945 | |
| 2946 | if (err < 0) |
| 2947 | 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", |
| 2948 | dev->name); |
| 2949 | } |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2950 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2951 | return err; |
| 2952 | } |
| 2953 | |
Jiri Pirko | cc6090e | 2019-09-30 11:48:19 +0200 | [diff] [blame] | 2954 | static struct net_device *rtnl_dev_get(struct net *net, |
| 2955 | struct nlattr *ifname_attr, |
Jiri Pirko | 76c9ac0 | 2019-09-30 11:48:20 +0200 | [diff] [blame] | 2956 | struct nlattr *altifname_attr, |
Jiri Pirko | cc6090e | 2019-09-30 11:48:19 +0200 | [diff] [blame] | 2957 | char *ifname) |
| 2958 | { |
Jiri Pirko | 76c9ac0 | 2019-09-30 11:48:20 +0200 | [diff] [blame] | 2959 | char buffer[ALTIFNAMSIZ]; |
Jiri Pirko | cc6090e | 2019-09-30 11:48:19 +0200 | [diff] [blame] | 2960 | |
| 2961 | if (!ifname) { |
| 2962 | ifname = buffer; |
| 2963 | if (ifname_attr) |
Francis Laniel | 872f690 | 2020-11-15 18:08:06 +0100 | [diff] [blame] | 2964 | nla_strscpy(ifname, ifname_attr, IFNAMSIZ); |
Jiri Pirko | 76c9ac0 | 2019-09-30 11:48:20 +0200 | [diff] [blame] | 2965 | else if (altifname_attr) |
Francis Laniel | 872f690 | 2020-11-15 18:08:06 +0100 | [diff] [blame] | 2966 | nla_strscpy(ifname, altifname_attr, ALTIFNAMSIZ); |
Jiri Pirko | cc6090e | 2019-09-30 11:48:19 +0200 | [diff] [blame] | 2967 | else |
| 2968 | return NULL; |
| 2969 | } |
| 2970 | |
| 2971 | return __dev_get_by_name(net, ifname); |
| 2972 | } |
| 2973 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 2974 | static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 2975 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2976 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2977 | struct net *net = sock_net(skb->sk); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2978 | struct ifinfomsg *ifm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2979 | struct net_device *dev; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2980 | int err; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2981 | struct nlattr *tb[IFLA_MAX+1]; |
| 2982 | char ifname[IFNAMSIZ]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2983 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2984 | err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, |
| 2985 | ifla_policy, extack); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2986 | if (err < 0) |
| 2987 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2988 | |
Christian Brauner | 4ff66ca | 2018-02-07 13:53:20 +0100 | [diff] [blame] | 2989 | err = rtnl_ensure_unique_netns(tb, extack, false); |
| 2990 | if (err < 0) |
| 2991 | goto errout; |
| 2992 | |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 2993 | if (tb[IFLA_IFNAME]) |
Francis Laniel | 872f690 | 2020-11-15 18:08:06 +0100 | [diff] [blame] | 2994 | nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
Patrick McHardy | 78e5b891 | 2006-09-13 20:35:36 -0700 | [diff] [blame] | 2995 | else |
| 2996 | ifname[0] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2997 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2998 | err = -EINVAL; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2999 | ifm = nlmsg_data(nlh); |
Patrick McHardy | 51055be | 2007-06-05 12:40:01 -0700 | [diff] [blame] | 3000 | if (ifm->ifi_index > 0) |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 3001 | dev = __dev_get_by_index(net, ifm->ifi_index); |
Jiri Pirko | 76c9ac0 | 2019-09-30 11:48:20 +0200 | [diff] [blame] | 3002 | else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) |
| 3003 | dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 3004 | else |
| 3005 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3006 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 3007 | if (dev == NULL) { |
| 3008 | err = -ENODEV; |
| 3009 | goto errout; |
| 3010 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3011 | |
Jakub Kicinski | ddf9f97 | 2017-04-30 21:46:46 -0700 | [diff] [blame] | 3012 | err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 3013 | errout: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3014 | return err; |
| 3015 | } |
| 3016 | |
WANG Cong | 66400d5 | 2015-03-24 11:53:31 -0700 | [diff] [blame] | 3017 | static int rtnl_group_dellink(const struct net *net, int group) |
| 3018 | { |
| 3019 | struct net_device *dev, *aux; |
| 3020 | LIST_HEAD(list_kill); |
| 3021 | bool found = false; |
| 3022 | |
| 3023 | if (!group) |
| 3024 | return -EPERM; |
| 3025 | |
| 3026 | for_each_netdev(net, dev) { |
| 3027 | if (dev->group == group) { |
| 3028 | const struct rtnl_link_ops *ops; |
| 3029 | |
| 3030 | found = true; |
| 3031 | ops = dev->rtnl_link_ops; |
| 3032 | if (!ops || !ops->dellink) |
| 3033 | return -EOPNOTSUPP; |
| 3034 | } |
| 3035 | } |
| 3036 | |
| 3037 | if (!found) |
| 3038 | return -ENODEV; |
| 3039 | |
| 3040 | for_each_netdev_safe(net, dev, aux) { |
| 3041 | if (dev->group == group) { |
| 3042 | const struct rtnl_link_ops *ops; |
| 3043 | |
| 3044 | ops = dev->rtnl_link_ops; |
| 3045 | ops->dellink(dev, &list_kill); |
| 3046 | } |
| 3047 | } |
| 3048 | unregister_netdevice_many(&list_kill); |
| 3049 | |
| 3050 | return 0; |
| 3051 | } |
| 3052 | |
Thomas Graf | 614732e | 2015-07-21 10:44:06 +0200 | [diff] [blame] | 3053 | int rtnl_delete_link(struct net_device *dev) |
| 3054 | { |
| 3055 | const struct rtnl_link_ops *ops; |
| 3056 | LIST_HEAD(list_kill); |
| 3057 | |
| 3058 | ops = dev->rtnl_link_ops; |
| 3059 | if (!ops || !ops->dellink) |
| 3060 | return -EOPNOTSUPP; |
| 3061 | |
| 3062 | ops->dellink(dev, &list_kill); |
| 3063 | unregister_netdevice_many(&list_kill); |
| 3064 | |
| 3065 | return 0; |
| 3066 | } |
| 3067 | EXPORT_SYMBOL_GPL(rtnl_delete_link); |
| 3068 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 3069 | static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 3070 | struct netlink_ext_ack *extack) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3071 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 3072 | struct net *net = sock_net(skb->sk); |
Christian Brauner | b61ad68 | 2018-01-24 15:26:34 +0100 | [diff] [blame] | 3073 | struct net *tgt_net = net; |
| 3074 | struct net_device *dev = NULL; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3075 | struct ifinfomsg *ifm; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3076 | struct nlattr *tb[IFLA_MAX+1]; |
| 3077 | int err; |
Christian Brauner | b61ad68 | 2018-01-24 15:26:34 +0100 | [diff] [blame] | 3078 | int netnsid = -1; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3079 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 3080 | err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, |
| 3081 | ifla_policy, extack); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3082 | if (err < 0) |
| 3083 | return err; |
| 3084 | |
Christian Brauner | 4ff66ca | 2018-02-07 13:53:20 +0100 | [diff] [blame] | 3085 | err = rtnl_ensure_unique_netns(tb, extack, true); |
| 3086 | if (err < 0) |
| 3087 | return err; |
| 3088 | |
Christian Brauner | 7e4a8d5 | 2018-09-04 21:53:53 +0200 | [diff] [blame] | 3089 | if (tb[IFLA_TARGET_NETNSID]) { |
| 3090 | netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); |
Christian Brauner | c383edc | 2018-09-04 21:53:47 +0200 | [diff] [blame] | 3091 | tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); |
Christian Brauner | b61ad68 | 2018-01-24 15:26:34 +0100 | [diff] [blame] | 3092 | if (IS_ERR(tgt_net)) |
| 3093 | return PTR_ERR(tgt_net); |
| 3094 | } |
| 3095 | |
| 3096 | err = -EINVAL; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3097 | ifm = nlmsg_data(nlh); |
| 3098 | if (ifm->ifi_index > 0) |
Christian Brauner | b61ad68 | 2018-01-24 15:26:34 +0100 | [diff] [blame] | 3099 | dev = __dev_get_by_index(tgt_net, ifm->ifi_index); |
Jiri Pirko | 76c9ac0 | 2019-09-30 11:48:20 +0200 | [diff] [blame] | 3100 | else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) |
| 3101 | dev = rtnl_dev_get(net, tb[IFLA_IFNAME], |
| 3102 | tb[IFLA_ALT_IFNAME], NULL); |
WANG Cong | 66400d5 | 2015-03-24 11:53:31 -0700 | [diff] [blame] | 3103 | else if (tb[IFLA_GROUP]) |
Christian Brauner | b61ad68 | 2018-01-24 15:26:34 +0100 | [diff] [blame] | 3104 | err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP])); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3105 | else |
Christian Brauner | b61ad68 | 2018-01-24 15:26:34 +0100 | [diff] [blame] | 3106 | goto out; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3107 | |
Christian Brauner | b61ad68 | 2018-01-24 15:26:34 +0100 | [diff] [blame] | 3108 | if (!dev) { |
| 3109 | if (tb[IFLA_IFNAME] || ifm->ifi_index > 0) |
| 3110 | err = -ENODEV; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3111 | |
Christian Brauner | b61ad68 | 2018-01-24 15:26:34 +0100 | [diff] [blame] | 3112 | goto out; |
| 3113 | } |
| 3114 | |
| 3115 | err = rtnl_delete_link(dev); |
| 3116 | |
| 3117 | out: |
| 3118 | if (netnsid >= 0) |
| 3119 | put_net(tgt_net); |
| 3120 | |
| 3121 | return err; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3122 | } |
| 3123 | |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 3124 | int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) |
| 3125 | { |
| 3126 | unsigned int old_flags; |
| 3127 | int err; |
| 3128 | |
| 3129 | old_flags = dev->flags; |
| 3130 | if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { |
Petr Machata | 6d04032 | 2018-12-06 17:05:43 +0000 | [diff] [blame] | 3131 | err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), |
| 3132 | NULL); |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 3133 | if (err < 0) |
| 3134 | return err; |
| 3135 | } |
| 3136 | |
Roopa Prabhu | 8d356b8 | 2018-07-04 16:46:29 -0700 | [diff] [blame] | 3137 | if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) { |
Roopa Prabhu | 56a49d7 | 2018-09-12 13:21:48 -0700 | [diff] [blame] | 3138 | __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags)); |
Roopa Prabhu | 8d356b8 | 2018-07-04 16:46:29 -0700 | [diff] [blame] | 3139 | } else { |
| 3140 | dev->rtnl_link_state = RTNL_LINK_INITIALIZED; |
| 3141 | __dev_notify_flags(dev, old_flags, ~0U); |
| 3142 | } |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 3143 | return 0; |
| 3144 | } |
| 3145 | EXPORT_SYMBOL(rtnl_configure_link); |
| 3146 | |
David Ahern | d0522f1 | 2018-11-06 12:51:14 -0800 | [diff] [blame] | 3147 | struct net_device *rtnl_create_link(struct net *net, const char *ifname, |
| 3148 | unsigned char name_assign_type, |
| 3149 | const struct rtnl_link_ops *ops, |
| 3150 | struct nlattr *tb[], |
| 3151 | struct netlink_ext_ack *extack) |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 3152 | { |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 3153 | struct net_device *dev; |
Jiri Pirko | d40156a | 2012-07-20 02:28:47 +0000 | [diff] [blame] | 3154 | unsigned int num_tx_queues = 1; |
| 3155 | unsigned int num_rx_queues = 1; |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 3156 | |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 3157 | if (tb[IFLA_NUM_TX_QUEUES]) |
| 3158 | num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); |
| 3159 | else if (ops->get_num_tx_queues) |
Jiri Pirko | d40156a | 2012-07-20 02:28:47 +0000 | [diff] [blame] | 3160 | num_tx_queues = ops->get_num_tx_queues(); |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 3161 | |
| 3162 | if (tb[IFLA_NUM_RX_QUEUES]) |
| 3163 | num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); |
| 3164 | else if (ops->get_num_rx_queues) |
Jiri Pirko | d40156a | 2012-07-20 02:28:47 +0000 | [diff] [blame] | 3165 | num_rx_queues = ops->get_num_rx_queues(); |
stephen hemminger | efacb30 | 2012-04-10 18:34:43 +0000 | [diff] [blame] | 3166 | |
David Ahern | d0522f1 | 2018-11-06 12:51:14 -0800 | [diff] [blame] | 3167 | if (num_tx_queues < 1 || num_tx_queues > 4096) { |
| 3168 | NL_SET_ERR_MSG(extack, "Invalid number of transmit queues"); |
Eric Dumazet | 0e1d6ec | 2018-10-02 15:47:35 -0700 | [diff] [blame] | 3169 | return ERR_PTR(-EINVAL); |
David Ahern | d0522f1 | 2018-11-06 12:51:14 -0800 | [diff] [blame] | 3170 | } |
Eric Dumazet | 0e1d6ec | 2018-10-02 15:47:35 -0700 | [diff] [blame] | 3171 | |
David Ahern | d0522f1 | 2018-11-06 12:51:14 -0800 | [diff] [blame] | 3172 | if (num_rx_queues < 1 || num_rx_queues > 4096) { |
| 3173 | NL_SET_ERR_MSG(extack, "Invalid number of receive queues"); |
Eric Dumazet | 0e1d6ec | 2018-10-02 15:47:35 -0700 | [diff] [blame] | 3174 | return ERR_PTR(-EINVAL); |
David Ahern | d0522f1 | 2018-11-06 12:51:14 -0800 | [diff] [blame] | 3175 | } |
Eric Dumazet | 0e1d6ec | 2018-10-02 15:47:35 -0700 | [diff] [blame] | 3176 | |
Johannes Berg | 8c713dc | 2021-06-12 10:20:54 +0200 | [diff] [blame] | 3177 | if (ops->alloc) { |
| 3178 | dev = ops->alloc(tb, ifname, name_assign_type, |
| 3179 | num_tx_queues, num_rx_queues); |
| 3180 | if (IS_ERR(dev)) |
| 3181 | return dev; |
| 3182 | } else { |
| 3183 | dev = alloc_netdev_mqs(ops->priv_size, ifname, |
| 3184 | name_assign_type, ops->setup, |
| 3185 | num_tx_queues, num_rx_queues); |
| 3186 | } |
| 3187 | |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 3188 | if (!dev) |
Tobias Klauser | d1892e4 | 2017-02-20 16:32:06 +0100 | [diff] [blame] | 3189 | return ERR_PTR(-ENOMEM); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 3190 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 3191 | dev_net_set(dev, net); |
| 3192 | dev->rtnl_link_ops = ops; |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 3193 | dev->rtnl_link_state = RTNL_LINK_INITIALIZING; |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 3194 | |
Eric Dumazet | d836f5c | 2020-01-21 22:47:29 -0800 | [diff] [blame] | 3195 | if (tb[IFLA_MTU]) { |
| 3196 | u32 mtu = nla_get_u32(tb[IFLA_MTU]); |
| 3197 | int err; |
| 3198 | |
| 3199 | err = dev_validate_mtu(dev, mtu, extack); |
| 3200 | if (err) { |
| 3201 | free_netdev(dev); |
| 3202 | return ERR_PTR(err); |
| 3203 | } |
| 3204 | dev->mtu = mtu; |
| 3205 | } |
Jiri Pirko | 2afb9b5 | 2013-01-06 12:41:57 +0000 | [diff] [blame] | 3206 | if (tb[IFLA_ADDRESS]) { |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 3207 | memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), |
| 3208 | nla_len(tb[IFLA_ADDRESS])); |
Jiri Pirko | 2afb9b5 | 2013-01-06 12:41:57 +0000 | [diff] [blame] | 3209 | dev->addr_assign_type = NET_ADDR_SET; |
| 3210 | } |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 3211 | if (tb[IFLA_BROADCAST]) |
| 3212 | memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), |
| 3213 | nla_len(tb[IFLA_BROADCAST])); |
| 3214 | if (tb[IFLA_TXQLEN]) |
| 3215 | dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); |
| 3216 | if (tb[IFLA_OPERSTATE]) |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 3217 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 3218 | if (tb[IFLA_LINKMODE]) |
| 3219 | dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); |
Patrick McHardy | ffa934f | 2011-01-20 03:00:42 +0000 | [diff] [blame] | 3220 | if (tb[IFLA_GROUP]) |
| 3221 | dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); |
Stephen Hemminger | 46e6b99 | 2017-12-07 15:40:19 -0800 | [diff] [blame] | 3222 | if (tb[IFLA_GSO_MAX_SIZE]) |
| 3223 | netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE])); |
| 3224 | if (tb[IFLA_GSO_MAX_SEGS]) |
Stephen Hemminger | a0b586f | 2017-12-08 15:34:13 -0800 | [diff] [blame] | 3225 | dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 3226 | |
| 3227 | return dev; |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 3228 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 3229 | EXPORT_SYMBOL(rtnl_create_link); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 3230 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 3231 | static int rtnl_group_changelink(const struct sk_buff *skb, |
| 3232 | struct net *net, int group, |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 3233 | struct ifinfomsg *ifm, |
Jakub Kicinski | ddf9f97 | 2017-04-30 21:46:46 -0700 | [diff] [blame] | 3234 | struct netlink_ext_ack *extack, |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 3235 | struct nlattr **tb) |
| 3236 | { |
WANG Cong | d079535 | 2015-03-23 16:31:09 -0700 | [diff] [blame] | 3237 | struct net_device *dev, *aux; |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 3238 | int err; |
| 3239 | |
WANG Cong | d079535 | 2015-03-23 16:31:09 -0700 | [diff] [blame] | 3240 | for_each_netdev_safe(net, dev, aux) { |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 3241 | if (dev->group == group) { |
Jakub Kicinski | ddf9f97 | 2017-04-30 21:46:46 -0700 | [diff] [blame] | 3242 | err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0); |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 3243 | if (err < 0) |
| 3244 | return err; |
| 3245 | } |
| 3246 | } |
| 3247 | |
| 3248 | return 0; |
| 3249 | } |
| 3250 | |
Jakub Kicinski | a293974 | 2018-11-27 22:32:31 -0800 | [diff] [blame] | 3251 | static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 3252 | struct nlattr **attr, struct netlink_ext_ack *extack) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3253 | { |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3254 | struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1]; |
| 3255 | unsigned char name_assign_type = NET_NAME_USER; |
| 3256 | struct nlattr *linkinfo[IFLA_INFO_MAX + 1]; |
| 3257 | const struct rtnl_link_ops *m_ops = NULL; |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3258 | struct net_device *master_dev = NULL; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 3259 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3260 | const struct rtnl_link_ops *ops; |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3261 | struct nlattr *tb[IFLA_MAX + 1]; |
| 3262 | struct net *dest_net, *link_net; |
| 3263 | struct nlattr **slave_data; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3264 | char kind[MODULE_NAME_LEN]; |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3265 | struct net_device *dev; |
| 3266 | struct ifinfomsg *ifm; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3267 | char ifname[IFNAMSIZ]; |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3268 | struct nlattr **data; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3269 | int err; |
| 3270 | |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 3271 | #ifdef CONFIG_MODULES |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3272 | replay: |
Thomas Graf | 8072f08 | 2007-07-31 14:13:50 -0700 | [diff] [blame] | 3273 | #endif |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 3274 | err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, |
| 3275 | ifla_policy, extack); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3276 | if (err < 0) |
| 3277 | return err; |
| 3278 | |
Christian Brauner | 4ff66ca | 2018-02-07 13:53:20 +0100 | [diff] [blame] | 3279 | err = rtnl_ensure_unique_netns(tb, extack, false); |
| 3280 | if (err < 0) |
| 3281 | return err; |
| 3282 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3283 | if (tb[IFLA_IFNAME]) |
Francis Laniel | 872f690 | 2020-11-15 18:08:06 +0100 | [diff] [blame] | 3284 | nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3285 | else |
| 3286 | ifname[0] = '\0'; |
| 3287 | |
| 3288 | ifm = nlmsg_data(nlh); |
| 3289 | if (ifm->ifi_index > 0) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 3290 | dev = __dev_get_by_index(net, ifm->ifi_index); |
Jiri Pirko | 76c9ac0 | 2019-09-30 11:48:20 +0200 | [diff] [blame] | 3291 | else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) |
| 3292 | dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname); |
Jiri Pirko | 7af12cb | 2019-09-30 11:48:18 +0200 | [diff] [blame] | 3293 | else |
| 3294 | dev = NULL; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3295 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 3296 | if (dev) { |
| 3297 | master_dev = netdev_master_upper_dev_get(dev); |
| 3298 | if (master_dev) |
| 3299 | m_ops = master_dev->rtnl_link_ops; |
| 3300 | } |
| 3301 | |
Rocco Yue | 8679c31 | 2021-08-03 20:02:50 +0800 | [diff] [blame] | 3302 | err = validate_linkmsg(dev, tb, extack); |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 3303 | if (err < 0) |
Thomas Graf | 1840bb1 | 2008-02-23 19:54:36 -0800 | [diff] [blame] | 3304 | return err; |
| 3305 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3306 | if (tb[IFLA_LINKINFO]) { |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 3307 | err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX, |
| 3308 | tb[IFLA_LINKINFO], |
| 3309 | ifla_info_policy, NULL); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3310 | if (err < 0) |
| 3311 | return err; |
| 3312 | } else |
| 3313 | memset(linkinfo, 0, sizeof(linkinfo)); |
| 3314 | |
| 3315 | if (linkinfo[IFLA_INFO_KIND]) { |
Francis Laniel | 872f690 | 2020-11-15 18:08:06 +0100 | [diff] [blame] | 3316 | nla_strscpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3317 | ops = rtnl_link_ops_get(kind); |
| 3318 | } else { |
| 3319 | kind[0] = '\0'; |
| 3320 | ops = NULL; |
| 3321 | } |
| 3322 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3323 | data = NULL; |
| 3324 | if (ops) { |
| 3325 | if (ops->maxtype > RTNL_MAX_TYPE) |
| 3326 | return -EINVAL; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3327 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3328 | if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 3329 | err = nla_parse_nested_deprecated(attr, ops->maxtype, |
| 3330 | linkinfo[IFLA_INFO_DATA], |
| 3331 | ops->policy, extack); |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3332 | if (err < 0) |
| 3333 | return err; |
| 3334 | data = attr; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3335 | } |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3336 | if (ops->validate) { |
| 3337 | err = ops->validate(tb, data, extack); |
| 3338 | if (err < 0) |
| 3339 | return err; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 3340 | } |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3341 | } |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 3342 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3343 | slave_data = NULL; |
| 3344 | if (m_ops) { |
| 3345 | if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE) |
| 3346 | return -EINVAL; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3347 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3348 | if (m_ops->slave_maxtype && |
| 3349 | linkinfo[IFLA_INFO_SLAVE_DATA]) { |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 3350 | err = nla_parse_nested_deprecated(slave_attr, |
| 3351 | m_ops->slave_maxtype, |
| 3352 | linkinfo[IFLA_INFO_SLAVE_DATA], |
| 3353 | m_ops->slave_policy, |
| 3354 | extack); |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3355 | if (err < 0) |
| 3356 | return err; |
| 3357 | slave_data = slave_attr; |
| 3358 | } |
| 3359 | } |
| 3360 | |
| 3361 | if (dev) { |
| 3362 | int status = 0; |
| 3363 | |
| 3364 | if (nlh->nlmsg_flags & NLM_F_EXCL) |
| 3365 | return -EEXIST; |
| 3366 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
| 3367 | return -EOPNOTSUPP; |
| 3368 | |
| 3369 | if (linkinfo[IFLA_INFO_DATA]) { |
| 3370 | if (!ops || ops != dev->rtnl_link_ops || |
| 3371 | !ops->changelink) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3372 | return -EOPNOTSUPP; |
| 3373 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3374 | err = ops->changelink(dev, tb, data, extack); |
| 3375 | if (err < 0) |
| 3376 | return err; |
| 3377 | status |= DO_SETLINK_NOTIFY; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3378 | } |
| 3379 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3380 | if (linkinfo[IFLA_INFO_SLAVE_DATA]) { |
| 3381 | if (!m_ops || !m_ops->slave_changelink) |
| 3382 | return -EOPNOTSUPP; |
| 3383 | |
| 3384 | err = m_ops->slave_changelink(master_dev, dev, tb, |
| 3385 | slave_data, extack); |
| 3386 | if (err < 0) |
| 3387 | return err; |
| 3388 | status |= DO_SETLINK_NOTIFY; |
| 3389 | } |
| 3390 | |
| 3391 | return do_setlink(skb, dev, ifm, extack, tb, ifname, status); |
| 3392 | } |
| 3393 | |
| 3394 | if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { |
| 3395 | if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) |
| 3396 | return rtnl_group_changelink(skb, net, |
Patrick McHardy | ffa934f | 2011-01-20 03:00:42 +0000 | [diff] [blame] | 3397 | nla_get_u32(tb[IFLA_GROUP]), |
Jakub Kicinski | ddf9f97 | 2017-04-30 21:46:46 -0700 | [diff] [blame] | 3398 | ifm, extack, tb); |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3399 | return -ENODEV; |
| 3400 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3401 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3402 | if (tb[IFLA_MAP] || tb[IFLA_PROTINFO]) |
| 3403 | return -EOPNOTSUPP; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3404 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3405 | if (!ops) { |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 3406 | #ifdef CONFIG_MODULES |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3407 | if (kind[0]) { |
| 3408 | __rtnl_unlock(); |
| 3409 | request_module("rtnl-link-%s", kind); |
| 3410 | rtnl_lock(); |
| 3411 | ops = rtnl_link_ops_get(kind); |
| 3412 | if (ops) |
| 3413 | goto replay; |
| 3414 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3415 | #endif |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3416 | NL_SET_ERR_MSG(extack, "Unknown device type"); |
| 3417 | return -EOPNOTSUPP; |
| 3418 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3419 | |
Johannes Berg | 8c713dc | 2021-06-12 10:20:54 +0200 | [diff] [blame] | 3420 | if (!ops->alloc && !ops->setup) |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3421 | return -EOPNOTSUPP; |
Jiri Pirko | b0ab2fa | 2014-06-26 09:58:25 +0200 | [diff] [blame] | 3422 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3423 | if (!ifname[0]) { |
| 3424 | snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); |
| 3425 | name_assign_type = NET_NAME_ENUM; |
| 3426 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3427 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3428 | dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN); |
| 3429 | if (IS_ERR(dest_net)) |
| 3430 | return PTR_ERR(dest_net); |
Eric W. Biederman | 13ad177 | 2011-01-29 14:57:22 +0000 | [diff] [blame] | 3431 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3432 | if (tb[IFLA_LINK_NETNSID]) { |
| 3433 | int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); |
Nicolas Dichtel | 317f481 | 2015-01-15 15:11:18 +0100 | [diff] [blame] | 3434 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3435 | link_net = get_net_ns_by_id(dest_net, id); |
| 3436 | if (!link_net) { |
| 3437 | NL_SET_ERR_MSG(extack, "Unknown network namespace id"); |
| 3438 | err = -EINVAL; |
Pavel Emelyanov | 9c7dafb | 2012-08-08 21:52:46 +0000 | [diff] [blame] | 3439 | goto out; |
| 3440 | } |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3441 | err = -EPERM; |
| 3442 | if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) |
| 3443 | goto out; |
| 3444 | } else { |
| 3445 | link_net = NULL; |
| 3446 | } |
Pavel Emelyanov | 9c7dafb | 2012-08-08 21:52:46 +0000 | [diff] [blame] | 3447 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3448 | dev = rtnl_create_link(link_net ? : dest_net, ifname, |
| 3449 | name_assign_type, ops, tb, extack); |
| 3450 | if (IS_ERR(dev)) { |
| 3451 | err = PTR_ERR(dev); |
David S. Miller | 4363890 | 2015-03-10 21:58:32 -0400 | [diff] [blame] | 3452 | goto out; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3453 | } |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3454 | |
| 3455 | dev->ifindex = ifm->ifi_index; |
| 3456 | |
Jakub Kicinski | c269a24 | 2021-01-06 10:40:06 -0800 | [diff] [blame] | 3457 | if (ops->newlink) |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3458 | err = ops->newlink(link_net ? : net, dev, tb, data, extack); |
Jakub Kicinski | c269a24 | 2021-01-06 10:40:06 -0800 | [diff] [blame] | 3459 | else |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3460 | err = register_netdevice(dev); |
Jakub Kicinski | c269a24 | 2021-01-06 10:40:06 -0800 | [diff] [blame] | 3461 | if (err < 0) { |
| 3462 | free_netdev(dev); |
| 3463 | goto out; |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3464 | } |
Jakub Kicinski | c269a24 | 2021-01-06 10:40:06 -0800 | [diff] [blame] | 3465 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3466 | err = rtnl_configure_link(dev, ifm); |
| 3467 | if (err < 0) |
| 3468 | goto out_unregister; |
| 3469 | if (link_net) { |
Andrei Vagin | 0854fa8 | 2021-04-06 23:40:51 -0700 | [diff] [blame] | 3470 | err = dev_change_net_namespace(dev, dest_net, ifname); |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3471 | if (err < 0) |
| 3472 | goto out_unregister; |
| 3473 | } |
| 3474 | if (tb[IFLA_MASTER]) { |
| 3475 | err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); |
| 3476 | if (err) |
| 3477 | goto out_unregister; |
| 3478 | } |
| 3479 | out: |
| 3480 | if (link_net) |
| 3481 | put_net(link_net); |
| 3482 | put_net(dest_net); |
| 3483 | return err; |
| 3484 | out_unregister: |
| 3485 | if (ops->newlink) { |
| 3486 | LIST_HEAD(list_kill); |
| 3487 | |
| 3488 | ops->dellink(dev, &list_kill); |
| 3489 | unregister_netdevice_many(&list_kill); |
| 3490 | } else { |
| 3491 | unregister_netdevice(dev); |
| 3492 | } |
| 3493 | goto out; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3494 | } |
| 3495 | |
Jakub Kicinski | a293974 | 2018-11-27 22:32:31 -0800 | [diff] [blame] | 3496 | static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 3497 | struct netlink_ext_ack *extack) |
| 3498 | { |
| 3499 | struct nlattr **attr; |
| 3500 | int ret; |
| 3501 | |
| 3502 | attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL); |
| 3503 | if (!attr) |
| 3504 | return -ENOMEM; |
| 3505 | |
| 3506 | ret = __rtnl_newlink(skb, nlh, attr, extack); |
| 3507 | kfree(attr); |
| 3508 | return ret; |
| 3509 | } |
| 3510 | |
Jakub Kicinski | 9b3757b | 2019-01-18 10:46:16 -0800 | [diff] [blame] | 3511 | static int rtnl_valid_getlink_req(struct sk_buff *skb, |
| 3512 | const struct nlmsghdr *nlh, |
| 3513 | struct nlattr **tb, |
| 3514 | struct netlink_ext_ack *extack) |
| 3515 | { |
| 3516 | struct ifinfomsg *ifm; |
| 3517 | int i, err; |
| 3518 | |
| 3519 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { |
| 3520 | NL_SET_ERR_MSG(extack, "Invalid header for get link"); |
| 3521 | return -EINVAL; |
| 3522 | } |
| 3523 | |
| 3524 | if (!netlink_strict_get_check(skb)) |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 3525 | return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, |
| 3526 | ifla_policy, extack); |
Jakub Kicinski | 9b3757b | 2019-01-18 10:46:16 -0800 | [diff] [blame] | 3527 | |
| 3528 | ifm = nlmsg_data(nlh); |
| 3529 | if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || |
| 3530 | ifm->ifi_change) { |
| 3531 | NL_SET_ERR_MSG(extack, "Invalid values in header for get link request"); |
| 3532 | return -EINVAL; |
| 3533 | } |
| 3534 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 3535 | err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX, |
| 3536 | ifla_policy, extack); |
Jakub Kicinski | 9b3757b | 2019-01-18 10:46:16 -0800 | [diff] [blame] | 3537 | if (err) |
| 3538 | return err; |
| 3539 | |
| 3540 | for (i = 0; i <= IFLA_MAX; i++) { |
| 3541 | if (!tb[i]) |
| 3542 | continue; |
| 3543 | |
| 3544 | switch (i) { |
| 3545 | case IFLA_IFNAME: |
Jiri Pirko | 76c9ac0 | 2019-09-30 11:48:20 +0200 | [diff] [blame] | 3546 | case IFLA_ALT_IFNAME: |
Jakub Kicinski | 9b3757b | 2019-01-18 10:46:16 -0800 | [diff] [blame] | 3547 | case IFLA_EXT_MASK: |
| 3548 | case IFLA_TARGET_NETNSID: |
| 3549 | break; |
| 3550 | default: |
| 3551 | NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request"); |
| 3552 | return -EINVAL; |
| 3553 | } |
| 3554 | } |
| 3555 | |
| 3556 | return 0; |
| 3557 | } |
| 3558 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 3559 | static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 3560 | struct netlink_ext_ack *extack) |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 3561 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 3562 | struct net *net = sock_net(skb->sk); |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3563 | struct net *tgt_net = net; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 3564 | struct ifinfomsg *ifm; |
| 3565 | struct nlattr *tb[IFLA_MAX+1]; |
| 3566 | struct net_device *dev = NULL; |
| 3567 | struct sk_buff *nskb; |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3568 | int netnsid = -1; |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 3569 | int err; |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 3570 | u32 ext_filter_mask = 0; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 3571 | |
Jakub Kicinski | 9b3757b | 2019-01-18 10:46:16 -0800 | [diff] [blame] | 3572 | err = rtnl_valid_getlink_req(skb, nlh, tb, extack); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 3573 | if (err < 0) |
Eric Sesterhenn | 9918f23 | 2006-09-26 23:26:38 -0700 | [diff] [blame] | 3574 | return err; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 3575 | |
Christian Brauner | 4ff66ca | 2018-02-07 13:53:20 +0100 | [diff] [blame] | 3576 | err = rtnl_ensure_unique_netns(tb, extack, true); |
| 3577 | if (err < 0) |
| 3578 | return err; |
| 3579 | |
Christian Brauner | 7e4a8d5 | 2018-09-04 21:53:53 +0200 | [diff] [blame] | 3580 | if (tb[IFLA_TARGET_NETNSID]) { |
| 3581 | netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); |
Christian Brauner | c383edc | 2018-09-04 21:53:47 +0200 | [diff] [blame] | 3582 | tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3583 | if (IS_ERR(tgt_net)) |
| 3584 | return PTR_ERR(tgt_net); |
| 3585 | } |
| 3586 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 3587 | if (tb[IFLA_EXT_MASK]) |
| 3588 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); |
| 3589 | |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3590 | err = -EINVAL; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 3591 | ifm = nlmsg_data(nlh); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 3592 | if (ifm->ifi_index > 0) |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3593 | dev = __dev_get_by_index(tgt_net, ifm->ifi_index); |
Jiri Pirko | 76c9ac0 | 2019-09-30 11:48:20 +0200 | [diff] [blame] | 3594 | else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) |
| 3595 | dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME], |
| 3596 | tb[IFLA_ALT_IFNAME], NULL); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 3597 | else |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3598 | goto out; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 3599 | |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3600 | err = -ENODEV; |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 3601 | if (dev == NULL) |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3602 | goto out; |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 3603 | |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3604 | err = -ENOBUFS; |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 3605 | nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 3606 | if (nskb == NULL) |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3607 | goto out; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 3608 | |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3609 | err = rtnl_fill_ifinfo(nskb, dev, net, |
| 3610 | RTM_NEWLINK, NETLINK_CB(skb).portid, |
| 3611 | nlh->nlmsg_seq, 0, 0, ext_filter_mask, |
Guillaume Nault | d4e4fdf | 2019-10-23 18:39:04 +0200 | [diff] [blame] | 3612 | 0, NULL, 0, netnsid, GFP_KERNEL); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 3613 | if (err < 0) { |
| 3614 | /* -EMSGSIZE implies BUG in if_nlmsg_size */ |
| 3615 | WARN_ON(err == -EMSGSIZE); |
| 3616 | kfree_skb(nskb); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 3617 | } else |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 3618 | err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3619 | out: |
| 3620 | if (netnsid >= 0) |
| 3621 | put_net(tgt_net); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 3622 | |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 3623 | return err; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 3624 | } |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 3625 | |
Jiri Pirko | 36fbf1e | 2019-09-30 11:48:16 +0200 | [diff] [blame] | 3626 | static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr, |
| 3627 | bool *changed, struct netlink_ext_ack *extack) |
| 3628 | { |
| 3629 | char *alt_ifname; |
| 3630 | int err; |
| 3631 | |
| 3632 | err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack); |
| 3633 | if (err) |
| 3634 | return err; |
| 3635 | |
Eric Dumazet | 44bfa9c | 2020-02-12 20:58:26 -0800 | [diff] [blame] | 3636 | alt_ifname = nla_strdup(attr, GFP_KERNEL); |
| 3637 | if (!alt_ifname) |
| 3638 | return -ENOMEM; |
| 3639 | |
Jiri Pirko | 36fbf1e | 2019-09-30 11:48:16 +0200 | [diff] [blame] | 3640 | if (cmd == RTM_NEWLINKPROP) { |
Jiri Pirko | 36fbf1e | 2019-09-30 11:48:16 +0200 | [diff] [blame] | 3641 | err = netdev_name_node_alt_create(dev, alt_ifname); |
Eric Dumazet | 44bfa9c | 2020-02-12 20:58:26 -0800 | [diff] [blame] | 3642 | if (!err) |
| 3643 | alt_ifname = NULL; |
Jiri Pirko | 36fbf1e | 2019-09-30 11:48:16 +0200 | [diff] [blame] | 3644 | } else if (cmd == RTM_DELLINKPROP) { |
| 3645 | err = netdev_name_node_alt_destroy(dev, alt_ifname); |
Jiri Pirko | 36fbf1e | 2019-09-30 11:48:16 +0200 | [diff] [blame] | 3646 | } else { |
Eric Dumazet | 44bfa9c | 2020-02-12 20:58:26 -0800 | [diff] [blame] | 3647 | WARN_ON_ONCE(1); |
| 3648 | err = -EINVAL; |
Jiri Pirko | 36fbf1e | 2019-09-30 11:48:16 +0200 | [diff] [blame] | 3649 | } |
| 3650 | |
Eric Dumazet | 44bfa9c | 2020-02-12 20:58:26 -0800 | [diff] [blame] | 3651 | kfree(alt_ifname); |
| 3652 | if (!err) |
| 3653 | *changed = true; |
| 3654 | return err; |
Jiri Pirko | 36fbf1e | 2019-09-30 11:48:16 +0200 | [diff] [blame] | 3655 | } |
| 3656 | |
| 3657 | static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh, |
| 3658 | struct netlink_ext_ack *extack) |
| 3659 | { |
| 3660 | struct net *net = sock_net(skb->sk); |
| 3661 | struct nlattr *tb[IFLA_MAX + 1]; |
| 3662 | struct net_device *dev; |
| 3663 | struct ifinfomsg *ifm; |
| 3664 | bool changed = false; |
| 3665 | struct nlattr *attr; |
| 3666 | int err, rem; |
| 3667 | |
| 3668 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); |
| 3669 | if (err) |
| 3670 | return err; |
| 3671 | |
| 3672 | err = rtnl_ensure_unique_netns(tb, extack, true); |
| 3673 | if (err) |
| 3674 | return err; |
| 3675 | |
| 3676 | ifm = nlmsg_data(nlh); |
Jiri Pirko | cc6090e | 2019-09-30 11:48:19 +0200 | [diff] [blame] | 3677 | if (ifm->ifi_index > 0) |
Jiri Pirko | 36fbf1e | 2019-09-30 11:48:16 +0200 | [diff] [blame] | 3678 | dev = __dev_get_by_index(net, ifm->ifi_index); |
Jiri Pirko | 76c9ac0 | 2019-09-30 11:48:20 +0200 | [diff] [blame] | 3679 | else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) |
| 3680 | dev = rtnl_dev_get(net, tb[IFLA_IFNAME], |
| 3681 | tb[IFLA_ALT_IFNAME], NULL); |
Jiri Pirko | cc6090e | 2019-09-30 11:48:19 +0200 | [diff] [blame] | 3682 | else |
Jiri Pirko | 36fbf1e | 2019-09-30 11:48:16 +0200 | [diff] [blame] | 3683 | return -EINVAL; |
Jiri Pirko | 36fbf1e | 2019-09-30 11:48:16 +0200 | [diff] [blame] | 3684 | |
| 3685 | if (!dev) |
| 3686 | return -ENODEV; |
| 3687 | |
| 3688 | if (!tb[IFLA_PROP_LIST]) |
| 3689 | return 0; |
| 3690 | |
| 3691 | nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) { |
| 3692 | switch (nla_type(attr)) { |
| 3693 | case IFLA_ALT_IFNAME: |
| 3694 | err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack); |
| 3695 | if (err) |
| 3696 | return err; |
| 3697 | break; |
| 3698 | } |
| 3699 | } |
| 3700 | |
| 3701 | if (changed) |
| 3702 | netdev_state_change(dev); |
| 3703 | return 0; |
| 3704 | } |
| 3705 | |
| 3706 | static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 3707 | struct netlink_ext_ack *extack) |
| 3708 | { |
| 3709 | return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack); |
| 3710 | } |
| 3711 | |
| 3712 | static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 3713 | struct netlink_ext_ack *extack) |
| 3714 | { |
| 3715 | return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack); |
| 3716 | } |
| 3717 | |
Di Zhu | ebfe3c5 | 2020-10-21 10:00:53 +0800 | [diff] [blame] | 3718 | static u32 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 3719 | { |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 3720 | struct net *net = sock_net(skb->sk); |
Di Zhu | ebfe3c5 | 2020-10-21 10:00:53 +0800 | [diff] [blame] | 3721 | size_t min_ifinfo_dump_size = 0; |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 3722 | struct nlattr *tb[IFLA_MAX+1]; |
| 3723 | u32 ext_filter_mask = 0; |
Di Zhu | ebfe3c5 | 2020-10-21 10:00:53 +0800 | [diff] [blame] | 3724 | struct net_device *dev; |
Michal Schmidt | e5eca6d | 2014-05-28 14:15:19 +0200 | [diff] [blame] | 3725 | int hdrlen; |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 3726 | |
Michal Schmidt | e5eca6d | 2014-05-28 14:15:19 +0200 | [diff] [blame] | 3727 | /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ |
| 3728 | hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? |
| 3729 | sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); |
| 3730 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 3731 | if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) { |
Eric Dumazet | a4b64fb | 2012-03-04 12:32:10 +0000 | [diff] [blame] | 3732 | if (tb[IFLA_EXT_MASK]) |
| 3733 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); |
| 3734 | } |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 3735 | |
| 3736 | if (!ext_filter_mask) |
| 3737 | return NLMSG_GOODSIZE; |
| 3738 | /* |
| 3739 | * traverse the list of net devices and compute the minimum |
| 3740 | * buffer size based upon the filter mask. |
| 3741 | */ |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 3742 | rcu_read_lock(); |
| 3743 | for_each_netdev_rcu(net, dev) { |
Di Zhu | ebfe3c5 | 2020-10-21 10:00:53 +0800 | [diff] [blame] | 3744 | min_ifinfo_dump_size = max(min_ifinfo_dump_size, |
| 3745 | if_nlmsg_size(dev, ext_filter_mask)); |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 3746 | } |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 3747 | rcu_read_unlock(); |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 3748 | |
Zhang Shengju | 93af205 | 2016-11-22 14:14:28 +0800 | [diff] [blame] | 3749 | return nlmsg_total_size(min_ifinfo_dump_size); |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 3750 | } |
| 3751 | |
Adrian Bunk | 42bad1d | 2007-04-26 00:57:41 -0700 | [diff] [blame] | 3752 | static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3753 | { |
| 3754 | int idx; |
| 3755 | int s_idx = cb->family; |
Christian Brauner | 87ccbb1 | 2018-09-04 21:53:51 +0200 | [diff] [blame] | 3756 | int type = cb->nlh->nlmsg_type - RTM_BASE; |
David Ahern | c63586d | 2018-10-24 12:59:02 -0700 | [diff] [blame] | 3757 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3758 | |
| 3759 | if (s_idx == 0) |
| 3760 | s_idx = 1; |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 3761 | |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 3762 | for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { |
Jakub Kicinski | 51e1368 | 2020-12-09 18:16:08 -0800 | [diff] [blame] | 3763 | struct rtnl_link __rcu **tab; |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 3764 | struct rtnl_link *link; |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 3765 | rtnl_dumpit_func dumpit; |
| 3766 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3767 | if (idx < s_idx || idx == PF_PACKET) |
| 3768 | continue; |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 3769 | |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 3770 | if (type < 0 || type >= RTM_NR_MSGTYPES) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3771 | continue; |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 3772 | |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 3773 | tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]); |
| 3774 | if (!tab) |
| 3775 | continue; |
| 3776 | |
Jakub Kicinski | 51e1368 | 2020-12-09 18:16:08 -0800 | [diff] [blame] | 3777 | link = rcu_dereference_rtnl(tab[type]); |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 3778 | if (!link) |
| 3779 | continue; |
| 3780 | |
| 3781 | dumpit = link->dumpit; |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 3782 | if (!dumpit) |
| 3783 | continue; |
| 3784 | |
Nicolas Dichtel | 0465277 | 2013-03-22 06:28:42 +0000 | [diff] [blame] | 3785 | if (idx > s_idx) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3786 | memset(&cb->args[0], 0, sizeof(cb->args)); |
Nicolas Dichtel | 0465277 | 2013-03-22 06:28:42 +0000 | [diff] [blame] | 3787 | cb->prev_seq = 0; |
| 3788 | cb->seq = 0; |
| 3789 | } |
David Ahern | c63586d | 2018-10-24 12:59:02 -0700 | [diff] [blame] | 3790 | ret = dumpit(skb, cb); |
Alexey Kodanev | 5e1acb4 | 2018-11-02 19:11:04 +0300 | [diff] [blame] | 3791 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3792 | break; |
| 3793 | } |
| 3794 | cb->family = idx; |
| 3795 | |
David Ahern | c63586d | 2018-10-24 12:59:02 -0700 | [diff] [blame] | 3796 | return skb->len ? : ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3797 | } |
| 3798 | |
Mahesh Bandewar | 395eea6 | 2014-12-03 13:46:24 -0800 | [diff] [blame] | 3799 | struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, |
Vlad Yasevich | 3d3ea5a | 2017-05-27 10:14:34 -0400 | [diff] [blame] | 3800 | unsigned int change, |
Nicolas Dichtel | 38e01b3 | 2018-01-25 15:01:39 +0100 | [diff] [blame] | 3801 | u32 event, gfp_t flags, int *new_nsid, |
| 3802 | int new_ifindex) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3803 | { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 3804 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3805 | struct sk_buff *skb; |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 3806 | int err = -ENOBUFS; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 3807 | size_t if_info_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3808 | |
Alexei Starovoitov | 7f29405 | 2013-10-23 16:02:42 -0700 | [diff] [blame] | 3809 | skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags); |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 3810 | if (skb == NULL) |
| 3811 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3812 | |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3813 | err = rtnl_fill_ifinfo(skb, dev, dev_net(dev), |
| 3814 | type, 0, 0, change, 0, 0, event, |
Guillaume Nault | d4e4fdf | 2019-10-23 18:39:04 +0200 | [diff] [blame] | 3815 | new_nsid, new_ifindex, -1, flags); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 3816 | if (err < 0) { |
| 3817 | /* -EMSGSIZE implies BUG in if_nlmsg_size() */ |
| 3818 | WARN_ON(err == -EMSGSIZE); |
| 3819 | kfree_skb(skb); |
| 3820 | goto errout; |
| 3821 | } |
Mahesh Bandewar | 395eea6 | 2014-12-03 13:46:24 -0800 | [diff] [blame] | 3822 | return skb; |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 3823 | errout: |
| 3824 | if (err < 0) |
Eric W. Biederman | 4b3da70 | 2007-11-19 22:27:40 -0800 | [diff] [blame] | 3825 | rtnl_set_sk_err(net, RTNLGRP_LINK, err); |
Mahesh Bandewar | 395eea6 | 2014-12-03 13:46:24 -0800 | [diff] [blame] | 3826 | return NULL; |
| 3827 | } |
| 3828 | |
| 3829 | void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags) |
| 3830 | { |
| 3831 | struct net *net = dev_net(dev); |
| 3832 | |
| 3833 | rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); |
| 3834 | } |
| 3835 | |
Vlad Yasevich | 3d3ea5a | 2017-05-27 10:14:34 -0400 | [diff] [blame] | 3836 | static void rtmsg_ifinfo_event(int type, struct net_device *dev, |
| 3837 | unsigned int change, u32 event, |
Nicolas Dichtel | 38e01b3 | 2018-01-25 15:01:39 +0100 | [diff] [blame] | 3838 | gfp_t flags, int *new_nsid, int new_ifindex) |
Mahesh Bandewar | 395eea6 | 2014-12-03 13:46:24 -0800 | [diff] [blame] | 3839 | { |
| 3840 | struct sk_buff *skb; |
| 3841 | |
Nicolas Dichtel | ed2a80a | 2015-05-13 14:19:42 +0200 | [diff] [blame] | 3842 | if (dev->reg_state != NETREG_REGISTERED) |
| 3843 | return; |
| 3844 | |
Nicolas Dichtel | 38e01b3 | 2018-01-25 15:01:39 +0100 | [diff] [blame] | 3845 | skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid, |
| 3846 | new_ifindex); |
Mahesh Bandewar | 395eea6 | 2014-12-03 13:46:24 -0800 | [diff] [blame] | 3847 | if (skb) |
| 3848 | rtmsg_ifinfo_send(skb, dev, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3849 | } |
Vlad Yasevich | 3d3ea5a | 2017-05-27 10:14:34 -0400 | [diff] [blame] | 3850 | |
| 3851 | void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, |
| 3852 | gfp_t flags) |
| 3853 | { |
Nicolas Dichtel | 38e01b3 | 2018-01-25 15:01:39 +0100 | [diff] [blame] | 3854 | rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, |
| 3855 | NULL, 0); |
Vlad Yasevich | 3d3ea5a | 2017-05-27 10:14:34 -0400 | [diff] [blame] | 3856 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3857 | |
Nicolas Dichtel | 6621dd2 | 2017-10-03 13:53:23 +0200 | [diff] [blame] | 3858 | void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change, |
Nicolas Dichtel | 38e01b3 | 2018-01-25 15:01:39 +0100 | [diff] [blame] | 3859 | gfp_t flags, int *new_nsid, int new_ifindex) |
Nicolas Dichtel | 6621dd2 | 2017-10-03 13:53:23 +0200 | [diff] [blame] | 3860 | { |
| 3861 | rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, |
Nicolas Dichtel | 38e01b3 | 2018-01-25 15:01:39 +0100 | [diff] [blame] | 3862 | new_nsid, new_ifindex); |
Nicolas Dichtel | 6621dd2 | 2017-10-03 13:53:23 +0200 | [diff] [blame] | 3863 | } |
| 3864 | |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 3865 | static int nlmsg_populate_fdb_fill(struct sk_buff *skb, |
| 3866 | struct net_device *dev, |
Hubert Sokolowski | 1e53d5b | 2015-04-09 12:16:17 +0000 | [diff] [blame] | 3867 | u8 *addr, u16 vid, u32 pid, u32 seq, |
Nicolas Dichtel | 1c104a6 | 2014-03-19 17:47:49 +0100 | [diff] [blame] | 3868 | int type, unsigned int flags, |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 3869 | int nlflags, u16 ndm_state) |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 3870 | { |
| 3871 | struct nlmsghdr *nlh; |
| 3872 | struct ndmsg *ndm; |
| 3873 | |
Nicolas Dichtel | 1c104a6 | 2014-03-19 17:47:49 +0100 | [diff] [blame] | 3874 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 3875 | if (!nlh) |
| 3876 | return -EMSGSIZE; |
| 3877 | |
| 3878 | ndm = nlmsg_data(nlh); |
| 3879 | ndm->ndm_family = AF_BRIDGE; |
| 3880 | ndm->ndm_pad1 = 0; |
| 3881 | ndm->ndm_pad2 = 0; |
| 3882 | ndm->ndm_flags = flags; |
| 3883 | ndm->ndm_type = 0; |
| 3884 | ndm->ndm_ifindex = dev->ifindex; |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 3885 | ndm->ndm_state = ndm_state; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 3886 | |
| 3887 | if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) |
| 3888 | goto nla_put_failure; |
Hubert Sokolowski | 1e53d5b | 2015-04-09 12:16:17 +0000 | [diff] [blame] | 3889 | if (vid) |
| 3890 | if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) |
| 3891 | goto nla_put_failure; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 3892 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 3893 | nlmsg_end(skb, nlh); |
| 3894 | return 0; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 3895 | |
| 3896 | nla_put_failure: |
| 3897 | nlmsg_cancel(skb, nlh); |
| 3898 | return -EMSGSIZE; |
| 3899 | } |
| 3900 | |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 3901 | static inline size_t rtnl_fdb_nlmsg_size(void) |
| 3902 | { |
Sabrina Dubroca | f82ef3e | 2016-11-18 15:50:39 +0100 | [diff] [blame] | 3903 | return NLMSG_ALIGN(sizeof(struct ndmsg)) + |
| 3904 | nla_total_size(ETH_ALEN) + /* NDA_LLADDR */ |
| 3905 | nla_total_size(sizeof(u16)) + /* NDA_VLAN */ |
| 3906 | 0; |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 3907 | } |
| 3908 | |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 3909 | static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, |
| 3910 | u16 ndm_state) |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 3911 | { |
| 3912 | struct net *net = dev_net(dev); |
| 3913 | struct sk_buff *skb; |
| 3914 | int err = -ENOBUFS; |
| 3915 | |
| 3916 | skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); |
| 3917 | if (!skb) |
| 3918 | goto errout; |
| 3919 | |
Hubert Sokolowski | 1e53d5b | 2015-04-09 12:16:17 +0000 | [diff] [blame] | 3920 | err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 3921 | 0, 0, type, NTF_SELF, 0, ndm_state); |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 3922 | if (err < 0) { |
| 3923 | kfree_skb(skb); |
| 3924 | goto errout; |
| 3925 | } |
| 3926 | |
| 3927 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); |
| 3928 | return; |
| 3929 | errout: |
| 3930 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); |
| 3931 | } |
| 3932 | |
Bart Van Assche | a986967 | 2019-03-25 09:17:22 -0700 | [diff] [blame] | 3933 | /* |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 3934 | * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry |
| 3935 | */ |
| 3936 | int ndo_dflt_fdb_add(struct ndmsg *ndm, |
| 3937 | struct nlattr *tb[], |
| 3938 | struct net_device *dev, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 3939 | const unsigned char *addr, u16 vid, |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 3940 | u16 flags) |
| 3941 | { |
| 3942 | int err = -EINVAL; |
| 3943 | |
| 3944 | /* If aging addresses are supported device will need to |
| 3945 | * implement its own handler for this. |
| 3946 | */ |
| 3947 | if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { |
Vladimir Oltean | 78ecc89 | 2021-06-29 03:29:26 +0300 | [diff] [blame] | 3948 | netdev_info(dev, "default FDB implementation only supports local addresses\n"); |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 3949 | return err; |
| 3950 | } |
| 3951 | |
Or Gerlitz | 65891fe | 2014-12-14 18:19:05 +0200 | [diff] [blame] | 3952 | if (vid) { |
Vladimir Oltean | 23ac0b4 | 2021-06-29 03:29:25 +0300 | [diff] [blame] | 3953 | netdev_info(dev, "vlans aren't supported yet for dev_uc|mc_add()\n"); |
Or Gerlitz | 65891fe | 2014-12-14 18:19:05 +0200 | [diff] [blame] | 3954 | return err; |
| 3955 | } |
| 3956 | |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 3957 | if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) |
| 3958 | err = dev_uc_add_excl(dev, addr); |
| 3959 | else if (is_multicast_ether_addr(addr)) |
| 3960 | err = dev_mc_add_excl(dev, addr); |
| 3961 | |
| 3962 | /* Only return duplicate errors if NLM_F_EXCL is set */ |
| 3963 | if (err == -EEXIST && !(flags & NLM_F_EXCL)) |
| 3964 | err = 0; |
| 3965 | |
| 3966 | return err; |
| 3967 | } |
| 3968 | EXPORT_SYMBOL(ndo_dflt_fdb_add); |
| 3969 | |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 3970 | static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid, |
| 3971 | struct netlink_ext_ack *extack) |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 3972 | { |
| 3973 | u16 vid = 0; |
| 3974 | |
| 3975 | if (vlan_attr) { |
| 3976 | if (nla_len(vlan_attr) != sizeof(u16)) { |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 3977 | NL_SET_ERR_MSG(extack, "invalid vlan attribute size"); |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 3978 | return -EINVAL; |
| 3979 | } |
| 3980 | |
| 3981 | vid = nla_get_u16(vlan_attr); |
| 3982 | |
| 3983 | if (!vid || vid >= VLAN_VID_MASK) { |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 3984 | NL_SET_ERR_MSG(extack, "invalid vlan id"); |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 3985 | return -EINVAL; |
| 3986 | } |
| 3987 | } |
| 3988 | *p_vid = vid; |
| 3989 | return 0; |
| 3990 | } |
| 3991 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 3992 | static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 3993 | struct netlink_ext_ack *extack) |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3994 | { |
| 3995 | struct net *net = sock_net(skb->sk); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3996 | struct ndmsg *ndm; |
| 3997 | struct nlattr *tb[NDA_MAX+1]; |
| 3998 | struct net_device *dev; |
| 3999 | u8 *addr; |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 4000 | u16 vid; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4001 | int err; |
| 4002 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 4003 | err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, |
| 4004 | extack); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4005 | if (err < 0) |
| 4006 | return err; |
| 4007 | |
| 4008 | ndm = nlmsg_data(nlh); |
| 4009 | if (ndm->ndm_ifindex == 0) { |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 4010 | NL_SET_ERR_MSG(extack, "invalid ifindex"); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4011 | return -EINVAL; |
| 4012 | } |
| 4013 | |
| 4014 | dev = __dev_get_by_index(net, ndm->ndm_ifindex); |
| 4015 | if (dev == NULL) { |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 4016 | NL_SET_ERR_MSG(extack, "unknown ifindex"); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4017 | return -ENODEV; |
| 4018 | } |
| 4019 | |
| 4020 | if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 4021 | NL_SET_ERR_MSG(extack, "invalid address"); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4022 | return -EINVAL; |
| 4023 | } |
| 4024 | |
Ido Schimmel | da71577 | 2018-10-29 20:36:43 +0000 | [diff] [blame] | 4025 | if (dev->type != ARPHRD_ETHER) { |
| 4026 | NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices"); |
| 4027 | return -EINVAL; |
| 4028 | } |
| 4029 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4030 | addr = nla_data(tb[NDA_LLADDR]); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4031 | |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 4032 | err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 4033 | if (err) |
| 4034 | return err; |
| 4035 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4036 | err = -EOPNOTSUPP; |
| 4037 | |
| 4038 | /* Support fdb on master device the net/bridge default case */ |
| 4039 | if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && |
Julian Wiedmann | 2e92a2d | 2020-02-20 09:00:07 +0100 | [diff] [blame] | 4040 | netif_is_bridge_port(dev)) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 4041 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
| 4042 | const struct net_device_ops *ops = br_dev->netdev_ops; |
| 4043 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 4044 | err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, |
Petr Machata | 87b0984 | 2019-01-16 23:06:50 +0000 | [diff] [blame] | 4045 | nlh->nlmsg_flags, extack); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4046 | if (err) |
| 4047 | goto out; |
| 4048 | else |
| 4049 | ndm->ndm_flags &= ~NTF_MASTER; |
| 4050 | } |
| 4051 | |
| 4052 | /* Embedded bridge, macvlan, and any other device support */ |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 4053 | if ((ndm->ndm_flags & NTF_SELF)) { |
| 4054 | if (dev->netdev_ops->ndo_fdb_add) |
| 4055 | err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 4056 | vid, |
Petr Machata | 87b0984 | 2019-01-16 23:06:50 +0000 | [diff] [blame] | 4057 | nlh->nlmsg_flags, |
| 4058 | extack); |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 4059 | else |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 4060 | err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 4061 | nlh->nlmsg_flags); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4062 | |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 4063 | if (!err) { |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 4064 | rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, |
| 4065 | ndm->ndm_state); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4066 | ndm->ndm_flags &= ~NTF_SELF; |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 4067 | } |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4068 | } |
| 4069 | out: |
| 4070 | return err; |
| 4071 | } |
| 4072 | |
Bart Van Assche | a986967 | 2019-03-25 09:17:22 -0700 | [diff] [blame] | 4073 | /* |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 4074 | * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry |
| 4075 | */ |
| 4076 | int ndo_dflt_fdb_del(struct ndmsg *ndm, |
| 4077 | struct nlattr *tb[], |
| 4078 | struct net_device *dev, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 4079 | const unsigned char *addr, u16 vid) |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 4080 | { |
Alexander Duyck | c8a89c4 | 2014-07-15 15:15:20 -0700 | [diff] [blame] | 4081 | int err = -EINVAL; |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 4082 | |
| 4083 | /* If aging addresses are supported device will need to |
| 4084 | * implement its own handler for this. |
| 4085 | */ |
Sridhar Samudrala | 6453599 | 2013-08-08 15:19:48 -0700 | [diff] [blame] | 4086 | if (!(ndm->ndm_state & NUD_PERMANENT)) { |
Vladimir Oltean | 78ecc89 | 2021-06-29 03:29:26 +0300 | [diff] [blame] | 4087 | netdev_info(dev, "default FDB implementation only supports local addresses\n"); |
Alexander Duyck | c8a89c4 | 2014-07-15 15:15:20 -0700 | [diff] [blame] | 4088 | return err; |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 4089 | } |
| 4090 | |
| 4091 | if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) |
| 4092 | err = dev_uc_del(dev, addr); |
| 4093 | else if (is_multicast_ether_addr(addr)) |
| 4094 | err = dev_mc_del(dev, addr); |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 4095 | |
| 4096 | return err; |
| 4097 | } |
| 4098 | EXPORT_SYMBOL(ndo_dflt_fdb_del); |
| 4099 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 4100 | static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 4101 | struct netlink_ext_ack *extack) |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4102 | { |
| 4103 | struct net *net = sock_net(skb->sk); |
| 4104 | struct ndmsg *ndm; |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 4105 | struct nlattr *tb[NDA_MAX+1]; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4106 | struct net_device *dev; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4107 | __u8 *addr; |
Colin Ian King | 7d31180 | 2020-04-25 12:28:14 +0100 | [diff] [blame] | 4108 | int err; |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 4109 | u16 vid; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4110 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 4111 | if (!netlink_capable(skb, CAP_NET_ADMIN)) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 4112 | return -EPERM; |
| 4113 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 4114 | err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, |
| 4115 | extack); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 4116 | if (err < 0) |
| 4117 | return err; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4118 | |
| 4119 | ndm = nlmsg_data(nlh); |
| 4120 | if (ndm->ndm_ifindex == 0) { |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 4121 | NL_SET_ERR_MSG(extack, "invalid ifindex"); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4122 | return -EINVAL; |
| 4123 | } |
| 4124 | |
| 4125 | dev = __dev_get_by_index(net, ndm->ndm_ifindex); |
| 4126 | if (dev == NULL) { |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 4127 | NL_SET_ERR_MSG(extack, "unknown ifindex"); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4128 | return -ENODEV; |
| 4129 | } |
| 4130 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 4131 | if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 4132 | NL_SET_ERR_MSG(extack, "invalid address"); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4133 | return -EINVAL; |
| 4134 | } |
| 4135 | |
Ido Schimmel | da71577 | 2018-10-29 20:36:43 +0000 | [diff] [blame] | 4136 | if (dev->type != ARPHRD_ETHER) { |
| 4137 | NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices"); |
| 4138 | return -EINVAL; |
| 4139 | } |
| 4140 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 4141 | addr = nla_data(tb[NDA_LLADDR]); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 4142 | |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 4143 | err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 4144 | if (err) |
| 4145 | return err; |
| 4146 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4147 | err = -EOPNOTSUPP; |
| 4148 | |
| 4149 | /* Support fdb on master device the net/bridge default case */ |
| 4150 | if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && |
Julian Wiedmann | 2e92a2d | 2020-02-20 09:00:07 +0100 | [diff] [blame] | 4151 | netif_is_bridge_port(dev)) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 4152 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
| 4153 | const struct net_device_ops *ops = br_dev->netdev_ops; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4154 | |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 4155 | if (ops->ndo_fdb_del) |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 4156 | err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4157 | |
| 4158 | if (err) |
| 4159 | goto out; |
| 4160 | else |
| 4161 | ndm->ndm_flags &= ~NTF_MASTER; |
| 4162 | } |
| 4163 | |
| 4164 | /* Embedded bridge, macvlan, and any other device support */ |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 4165 | if (ndm->ndm_flags & NTF_SELF) { |
| 4166 | if (dev->netdev_ops->ndo_fdb_del) |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 4167 | err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, |
| 4168 | vid); |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 4169 | else |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 4170 | err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4171 | |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 4172 | if (!err) { |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 4173 | rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, |
| 4174 | ndm->ndm_state); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4175 | ndm->ndm_flags &= ~NTF_SELF; |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 4176 | } |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4177 | } |
| 4178 | out: |
| 4179 | return err; |
| 4180 | } |
| 4181 | |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4182 | static int nlmsg_populate_fdb(struct sk_buff *skb, |
| 4183 | struct netlink_callback *cb, |
| 4184 | struct net_device *dev, |
| 4185 | int *idx, |
| 4186 | struct netdev_hw_addr_list *list) |
| 4187 | { |
| 4188 | struct netdev_hw_addr *ha; |
| 4189 | int err; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4190 | u32 portid, seq; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4191 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4192 | portid = NETLINK_CB(cb->skb).portid; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4193 | seq = cb->nlh->nlmsg_seq; |
| 4194 | |
| 4195 | list_for_each_entry(ha, &list->list, list) { |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4196 | if (*idx < cb->args[2]) |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4197 | goto skip; |
| 4198 | |
Hubert Sokolowski | 1e53d5b | 2015-04-09 12:16:17 +0000 | [diff] [blame] | 4199 | err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, |
John Fastabend | a7a558f | 2012-11-01 16:23:10 +0000 | [diff] [blame] | 4200 | portid, seq, |
Nicolas Dichtel | 1c104a6 | 2014-03-19 17:47:49 +0100 | [diff] [blame] | 4201 | RTM_NEWNEIGH, NTF_SELF, |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 4202 | NLM_F_MULTI, NUD_PERMANENT); |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4203 | if (err < 0) |
| 4204 | return err; |
| 4205 | skip: |
| 4206 | *idx += 1; |
| 4207 | } |
| 4208 | return 0; |
| 4209 | } |
| 4210 | |
| 4211 | /** |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 4212 | * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. |
Bart Van Assche | a986967 | 2019-03-25 09:17:22 -0700 | [diff] [blame] | 4213 | * @skb: socket buffer to store message in |
| 4214 | * @cb: netlink callback |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4215 | * @dev: netdevice |
Bart Van Assche | a986967 | 2019-03-25 09:17:22 -0700 | [diff] [blame] | 4216 | * @filter_dev: ignored |
| 4217 | * @idx: the number of FDB table entries dumped is added to *@idx |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4218 | * |
| 4219 | * Default netdevice operation to dump the existing unicast address list. |
John Fastabend | 91f3e7b | 2013-03-29 08:18:37 +0000 | [diff] [blame] | 4220 | * Returns number of addresses from list put in skb. |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4221 | */ |
| 4222 | int ndo_dflt_fdb_dump(struct sk_buff *skb, |
| 4223 | struct netlink_callback *cb, |
| 4224 | struct net_device *dev, |
Jamal Hadi Salim | 5d5eacb | 2014-07-10 07:01:58 -0400 | [diff] [blame] | 4225 | struct net_device *filter_dev, |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4226 | int *idx) |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4227 | { |
| 4228 | int err; |
| 4229 | |
Eric Dumazet | 6888389 | 2018-12-04 09:40:35 -0800 | [diff] [blame] | 4230 | if (dev->type != ARPHRD_ETHER) |
| 4231 | return -EINVAL; |
| 4232 | |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4233 | netif_addr_lock_bh(dev); |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4234 | err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc); |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4235 | if (err) |
| 4236 | goto out; |
Zhang Shengju | 2934c9d | 2016-11-30 16:37:34 +0800 | [diff] [blame] | 4237 | err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc); |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4238 | out: |
| 4239 | netif_addr_unlock_bh(dev); |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4240 | return err; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4241 | } |
| 4242 | EXPORT_SYMBOL(ndo_dflt_fdb_dump); |
| 4243 | |
David Ahern | 8c6e137 | 2018-10-07 20:16:44 -0700 | [diff] [blame] | 4244 | static int valid_fdb_dump_strict(const struct nlmsghdr *nlh, |
| 4245 | int *br_idx, int *brport_idx, |
| 4246 | struct netlink_ext_ack *extack) |
| 4247 | { |
| 4248 | struct nlattr *tb[NDA_MAX + 1]; |
| 4249 | struct ndmsg *ndm; |
| 4250 | int err, i; |
| 4251 | |
| 4252 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { |
| 4253 | NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request"); |
| 4254 | return -EINVAL; |
| 4255 | } |
| 4256 | |
| 4257 | ndm = nlmsg_data(nlh); |
| 4258 | if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || |
| 4259 | ndm->ndm_flags || ndm->ndm_type) { |
Nikolay Aleksandrov | 8b73018 | 2019-10-29 13:59:32 +0200 | [diff] [blame] | 4260 | NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request"); |
David Ahern | 8c6e137 | 2018-10-07 20:16:44 -0700 | [diff] [blame] | 4261 | return -EINVAL; |
| 4262 | } |
| 4263 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 4264 | err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, |
| 4265 | NDA_MAX, NULL, extack); |
David Ahern | 8c6e137 | 2018-10-07 20:16:44 -0700 | [diff] [blame] | 4266 | if (err < 0) |
| 4267 | return err; |
| 4268 | |
| 4269 | *brport_idx = ndm->ndm_ifindex; |
| 4270 | for (i = 0; i <= NDA_MAX; ++i) { |
| 4271 | if (!tb[i]) |
| 4272 | continue; |
| 4273 | |
| 4274 | switch (i) { |
| 4275 | case NDA_IFINDEX: |
| 4276 | if (nla_len(tb[i]) != sizeof(u32)) { |
| 4277 | NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request"); |
| 4278 | return -EINVAL; |
| 4279 | } |
| 4280 | *brport_idx = nla_get_u32(tb[NDA_IFINDEX]); |
| 4281 | break; |
| 4282 | case NDA_MASTER: |
| 4283 | if (nla_len(tb[i]) != sizeof(u32)) { |
| 4284 | NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request"); |
| 4285 | return -EINVAL; |
| 4286 | } |
| 4287 | *br_idx = nla_get_u32(tb[NDA_MASTER]); |
| 4288 | break; |
| 4289 | default: |
| 4290 | NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request"); |
| 4291 | return -EINVAL; |
| 4292 | } |
| 4293 | } |
| 4294 | |
| 4295 | return 0; |
| 4296 | } |
| 4297 | |
David Ahern | 8dfbda1 | 2018-10-07 20:16:43 -0700 | [diff] [blame] | 4298 | static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh, |
| 4299 | int *br_idx, int *brport_idx, |
| 4300 | struct netlink_ext_ack *extack) |
| 4301 | { |
David Ahern | 8dfbda1 | 2018-10-07 20:16:43 -0700 | [diff] [blame] | 4302 | struct nlattr *tb[IFLA_MAX+1]; |
| 4303 | int err; |
| 4304 | |
| 4305 | /* A hack to preserve kernel<->userspace interface. |
| 4306 | * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0. |
| 4307 | * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails. |
| 4308 | * So, check for ndmsg with an optional u32 attribute (not used here). |
| 4309 | * Fortunately these sizes don't conflict with the size of ifinfomsg |
| 4310 | * with an optional attribute. |
| 4311 | */ |
| 4312 | if (nlmsg_len(nlh) != sizeof(struct ndmsg) && |
| 4313 | (nlmsg_len(nlh) != sizeof(struct ndmsg) + |
| 4314 | nla_attr_size(sizeof(u32)))) { |
David Ahern | 4565d7e | 2018-10-08 13:57:24 -0700 | [diff] [blame] | 4315 | struct ifinfomsg *ifm; |
| 4316 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 4317 | err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), |
| 4318 | tb, IFLA_MAX, ifla_policy, |
| 4319 | extack); |
David Ahern | 8dfbda1 | 2018-10-07 20:16:43 -0700 | [diff] [blame] | 4320 | if (err < 0) { |
| 4321 | return -EINVAL; |
| 4322 | } else if (err == 0) { |
| 4323 | if (tb[IFLA_MASTER]) |
| 4324 | *br_idx = nla_get_u32(tb[IFLA_MASTER]); |
| 4325 | } |
| 4326 | |
David Ahern | 4565d7e | 2018-10-08 13:57:24 -0700 | [diff] [blame] | 4327 | ifm = nlmsg_data(nlh); |
David Ahern | 8dfbda1 | 2018-10-07 20:16:43 -0700 | [diff] [blame] | 4328 | *brport_idx = ifm->ifi_index; |
| 4329 | } |
| 4330 | return 0; |
| 4331 | } |
| 4332 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4333 | static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 4334 | { |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4335 | struct net_device *dev; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 4336 | struct net_device *br_dev = NULL; |
| 4337 | const struct net_device_ops *ops = NULL; |
| 4338 | const struct net_device_ops *cops = NULL; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 4339 | struct net *net = sock_net(skb->sk); |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4340 | struct hlist_head *head; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 4341 | int brport_idx = 0; |
| 4342 | int br_idx = 0; |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4343 | int h, s_h; |
| 4344 | int idx = 0, s_idx; |
| 4345 | int err = 0; |
| 4346 | int fidx = 0; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4347 | |
David Ahern | 8c6e137 | 2018-10-07 20:16:44 -0700 | [diff] [blame] | 4348 | if (cb->strict_check) |
| 4349 | err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx, |
| 4350 | cb->extack); |
| 4351 | else |
| 4352 | err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx, |
| 4353 | cb->extack); |
David Ahern | 8dfbda1 | 2018-10-07 20:16:43 -0700 | [diff] [blame] | 4354 | if (err < 0) |
| 4355 | return err; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 4356 | |
| 4357 | if (br_idx) { |
| 4358 | br_dev = __dev_get_by_index(net, br_idx); |
| 4359 | if (!br_dev) |
| 4360 | return -ENODEV; |
| 4361 | |
| 4362 | ops = br_dev->netdev_ops; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 4363 | } |
| 4364 | |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4365 | s_h = cb->args[0]; |
| 4366 | s_idx = cb->args[1]; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 4367 | |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4368 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { |
| 4369 | idx = 0; |
| 4370 | head = &net->dev_index_head[h]; |
| 4371 | hlist_for_each_entry(dev, head, index_hlist) { |
| 4372 | |
| 4373 | if (brport_idx && (dev->ifindex != brport_idx)) |
| 4374 | continue; |
| 4375 | |
| 4376 | if (!br_idx) { /* user did not specify a specific bridge */ |
Julian Wiedmann | 2e92a2d | 2020-02-20 09:00:07 +0100 | [diff] [blame] | 4377 | if (netif_is_bridge_port(dev)) { |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4378 | br_dev = netdev_master_upper_dev_get(dev); |
| 4379 | cops = br_dev->netdev_ops; |
| 4380 | } |
| 4381 | } else { |
| 4382 | if (dev != br_dev && |
Julian Wiedmann | 2e92a2d | 2020-02-20 09:00:07 +0100 | [diff] [blame] | 4383 | !netif_is_bridge_port(dev)) |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4384 | continue; |
| 4385 | |
| 4386 | if (br_dev != netdev_master_upper_dev_get(dev) && |
| 4387 | !(dev->priv_flags & IFF_EBRIDGE)) |
| 4388 | continue; |
| 4389 | cops = ops; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 4390 | } |
| 4391 | |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4392 | if (idx < s_idx) |
| 4393 | goto cont; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 4394 | |
Julian Wiedmann | 2e92a2d | 2020-02-20 09:00:07 +0100 | [diff] [blame] | 4395 | if (netif_is_bridge_port(dev)) { |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4396 | if (cops && cops->ndo_fdb_dump) { |
| 4397 | err = cops->ndo_fdb_dump(skb, cb, |
| 4398 | br_dev, dev, |
| 4399 | &fidx); |
| 4400 | if (err == -EMSGSIZE) |
| 4401 | goto out; |
| 4402 | } |
| 4403 | } |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 4404 | |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4405 | if (dev->netdev_ops->ndo_fdb_dump) |
| 4406 | err = dev->netdev_ops->ndo_fdb_dump(skb, cb, |
| 4407 | dev, NULL, |
| 4408 | &fidx); |
| 4409 | else |
| 4410 | err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, |
| 4411 | &fidx); |
| 4412 | if (err == -EMSGSIZE) |
| 4413 | goto out; |
| 4414 | |
| 4415 | cops = NULL; |
| 4416 | |
| 4417 | /* reset fdb offset to 0 for rest of the interfaces */ |
| 4418 | cb->args[2] = 0; |
| 4419 | fidx = 0; |
| 4420 | cont: |
| 4421 | idx++; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4422 | } |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4423 | } |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4424 | |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4425 | out: |
| 4426 | cb->args[0] = h; |
| 4427 | cb->args[1] = idx; |
| 4428 | cb->args[2] = fidx; |
| 4429 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4430 | return skb->len; |
| 4431 | } |
| 4432 | |
Roopa Prabhu | 5b2f94b | 2018-12-15 22:35:08 -0800 | [diff] [blame] | 4433 | static int valid_fdb_get_strict(const struct nlmsghdr *nlh, |
| 4434 | struct nlattr **tb, u8 *ndm_flags, |
| 4435 | int *br_idx, int *brport_idx, u8 **addr, |
| 4436 | u16 *vid, struct netlink_ext_ack *extack) |
| 4437 | { |
| 4438 | struct ndmsg *ndm; |
| 4439 | int err, i; |
| 4440 | |
| 4441 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { |
| 4442 | NL_SET_ERR_MSG(extack, "Invalid header for fdb get request"); |
| 4443 | return -EINVAL; |
| 4444 | } |
| 4445 | |
| 4446 | ndm = nlmsg_data(nlh); |
| 4447 | if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || |
| 4448 | ndm->ndm_type) { |
| 4449 | NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request"); |
| 4450 | return -EINVAL; |
| 4451 | } |
| 4452 | |
| 4453 | if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) { |
| 4454 | NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request"); |
| 4455 | return -EINVAL; |
| 4456 | } |
| 4457 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 4458 | err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, |
| 4459 | NDA_MAX, nda_policy, extack); |
Roopa Prabhu | 5b2f94b | 2018-12-15 22:35:08 -0800 | [diff] [blame] | 4460 | if (err < 0) |
| 4461 | return err; |
| 4462 | |
| 4463 | *ndm_flags = ndm->ndm_flags; |
| 4464 | *brport_idx = ndm->ndm_ifindex; |
| 4465 | for (i = 0; i <= NDA_MAX; ++i) { |
| 4466 | if (!tb[i]) |
| 4467 | continue; |
| 4468 | |
| 4469 | switch (i) { |
| 4470 | case NDA_MASTER: |
| 4471 | *br_idx = nla_get_u32(tb[i]); |
| 4472 | break; |
| 4473 | case NDA_LLADDR: |
| 4474 | if (nla_len(tb[i]) != ETH_ALEN) { |
| 4475 | NL_SET_ERR_MSG(extack, "Invalid address in fdb get request"); |
| 4476 | return -EINVAL; |
| 4477 | } |
| 4478 | *addr = nla_data(tb[i]); |
| 4479 | break; |
| 4480 | case NDA_VLAN: |
| 4481 | err = fdb_vid_parse(tb[i], vid, extack); |
| 4482 | if (err) |
| 4483 | return err; |
| 4484 | break; |
| 4485 | case NDA_VNI: |
| 4486 | break; |
| 4487 | default: |
| 4488 | NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request"); |
| 4489 | return -EINVAL; |
| 4490 | } |
| 4491 | } |
| 4492 | |
| 4493 | return 0; |
| 4494 | } |
| 4495 | |
| 4496 | static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh, |
| 4497 | struct netlink_ext_ack *extack) |
| 4498 | { |
| 4499 | struct net_device *dev = NULL, *br_dev = NULL; |
| 4500 | const struct net_device_ops *ops = NULL; |
| 4501 | struct net *net = sock_net(in_skb->sk); |
| 4502 | struct nlattr *tb[NDA_MAX + 1]; |
| 4503 | struct sk_buff *skb; |
| 4504 | int brport_idx = 0; |
| 4505 | u8 ndm_flags = 0; |
| 4506 | int br_idx = 0; |
| 4507 | u8 *addr = NULL; |
| 4508 | u16 vid = 0; |
| 4509 | int err; |
| 4510 | |
| 4511 | err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx, |
| 4512 | &brport_idx, &addr, &vid, extack); |
| 4513 | if (err < 0) |
| 4514 | return err; |
| 4515 | |
Nikolay Aleksandrov | f989d03 | 2018-12-30 14:33:20 +0200 | [diff] [blame] | 4516 | if (!addr) { |
| 4517 | NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request"); |
| 4518 | return -EINVAL; |
| 4519 | } |
| 4520 | |
Roopa Prabhu | 5b2f94b | 2018-12-15 22:35:08 -0800 | [diff] [blame] | 4521 | if (brport_idx) { |
| 4522 | dev = __dev_get_by_index(net, brport_idx); |
| 4523 | if (!dev) { |
| 4524 | NL_SET_ERR_MSG(extack, "Unknown device ifindex"); |
| 4525 | return -ENODEV; |
| 4526 | } |
| 4527 | } |
| 4528 | |
| 4529 | if (br_idx) { |
| 4530 | if (dev) { |
| 4531 | NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive"); |
| 4532 | return -EINVAL; |
| 4533 | } |
| 4534 | |
| 4535 | br_dev = __dev_get_by_index(net, br_idx); |
| 4536 | if (!br_dev) { |
| 4537 | NL_SET_ERR_MSG(extack, "Invalid master ifindex"); |
| 4538 | return -EINVAL; |
| 4539 | } |
| 4540 | ops = br_dev->netdev_ops; |
| 4541 | } |
| 4542 | |
| 4543 | if (dev) { |
| 4544 | if (!ndm_flags || (ndm_flags & NTF_MASTER)) { |
Julian Wiedmann | 2e92a2d | 2020-02-20 09:00:07 +0100 | [diff] [blame] | 4545 | if (!netif_is_bridge_port(dev)) { |
Roopa Prabhu | 5b2f94b | 2018-12-15 22:35:08 -0800 | [diff] [blame] | 4546 | NL_SET_ERR_MSG(extack, "Device is not a bridge port"); |
| 4547 | return -EINVAL; |
| 4548 | } |
| 4549 | br_dev = netdev_master_upper_dev_get(dev); |
| 4550 | if (!br_dev) { |
| 4551 | NL_SET_ERR_MSG(extack, "Master of device not found"); |
| 4552 | return -EINVAL; |
| 4553 | } |
| 4554 | ops = br_dev->netdev_ops; |
| 4555 | } else { |
| 4556 | if (!(ndm_flags & NTF_SELF)) { |
| 4557 | NL_SET_ERR_MSG(extack, "Missing NTF_SELF"); |
| 4558 | return -EINVAL; |
| 4559 | } |
| 4560 | ops = dev->netdev_ops; |
| 4561 | } |
| 4562 | } |
| 4563 | |
| 4564 | if (!br_dev && !dev) { |
| 4565 | NL_SET_ERR_MSG(extack, "No device specified"); |
| 4566 | return -ENODEV; |
| 4567 | } |
| 4568 | |
| 4569 | if (!ops || !ops->ndo_fdb_get) { |
| 4570 | NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device"); |
| 4571 | return -EOPNOTSUPP; |
| 4572 | } |
| 4573 | |
| 4574 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
| 4575 | if (!skb) |
| 4576 | return -ENOBUFS; |
| 4577 | |
| 4578 | if (br_dev) |
| 4579 | dev = br_dev; |
| 4580 | err = ops->ndo_fdb_get(skb, tb, dev, addr, vid, |
| 4581 | NETLINK_CB(in_skb).portid, |
| 4582 | nlh->nlmsg_seq, extack); |
| 4583 | if (err) |
| 4584 | goto out; |
| 4585 | |
| 4586 | return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); |
| 4587 | out: |
| 4588 | kfree_skb(skb); |
| 4589 | return err; |
| 4590 | } |
| 4591 | |
Scott Feldman | 2c3c031 | 2014-11-28 14:34:25 +0100 | [diff] [blame] | 4592 | static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, |
| 4593 | unsigned int attrnum, unsigned int flag) |
| 4594 | { |
| 4595 | if (mask & flag) |
| 4596 | return nla_put_u8(skb, attrnum, !!(flags & flag)); |
| 4597 | return 0; |
| 4598 | } |
| 4599 | |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4600 | int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, |
Scott Feldman | 2c3c031 | 2014-11-28 14:34:25 +0100 | [diff] [blame] | 4601 | struct net_device *dev, u16 mode, |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 4602 | u32 flags, u32 mask, int nlflags, |
| 4603 | u32 filter_mask, |
| 4604 | int (*vlan_fill)(struct sk_buff *skb, |
| 4605 | struct net_device *dev, |
| 4606 | u32 filter_mask)) |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4607 | { |
| 4608 | struct nlmsghdr *nlh; |
| 4609 | struct ifinfomsg *ifm; |
| 4610 | struct nlattr *br_afspec; |
Scott Feldman | 2c3c031 | 2014-11-28 14:34:25 +0100 | [diff] [blame] | 4611 | struct nlattr *protinfo; |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4612 | u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 4613 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 4614 | int err = 0; |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4615 | |
Nicolas Dichtel | 46c264d | 2015-04-28 18:33:49 +0200 | [diff] [blame] | 4616 | nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4617 | if (nlh == NULL) |
| 4618 | return -EMSGSIZE; |
| 4619 | |
| 4620 | ifm = nlmsg_data(nlh); |
| 4621 | ifm->ifi_family = AF_BRIDGE; |
| 4622 | ifm->__ifi_pad = 0; |
| 4623 | ifm->ifi_type = dev->type; |
| 4624 | ifm->ifi_index = dev->ifindex; |
| 4625 | ifm->ifi_flags = dev_get_flags(dev); |
| 4626 | ifm->ifi_change = 0; |
| 4627 | |
| 4628 | |
| 4629 | if (nla_put_string(skb, IFLA_IFNAME, dev->name) || |
| 4630 | nla_put_u32(skb, IFLA_MTU, dev->mtu) || |
| 4631 | nla_put_u8(skb, IFLA_OPERSTATE, operstate) || |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 4632 | (br_dev && |
| 4633 | nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4634 | (dev->addr_len && |
| 4635 | nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || |
Nicolas Dichtel | a54acb3 | 2015-04-02 17:07:00 +0200 | [diff] [blame] | 4636 | (dev->ifindex != dev_get_iflink(dev) && |
| 4637 | nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4638 | goto nla_put_failure; |
| 4639 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 4640 | br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC); |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4641 | if (!br_afspec) |
| 4642 | goto nla_put_failure; |
| 4643 | |
Roopa Prabhu | 1d460b9 | 2014-12-08 14:04:20 -0800 | [diff] [blame] | 4644 | if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4645 | nla_nest_cancel(skb, br_afspec); |
| 4646 | goto nla_put_failure; |
| 4647 | } |
Roopa Prabhu | 1d460b9 | 2014-12-08 14:04:20 -0800 | [diff] [blame] | 4648 | |
| 4649 | if (mode != BRIDGE_MODE_UNDEF) { |
| 4650 | if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { |
| 4651 | nla_nest_cancel(skb, br_afspec); |
| 4652 | goto nla_put_failure; |
| 4653 | } |
| 4654 | } |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 4655 | if (vlan_fill) { |
| 4656 | err = vlan_fill(skb, dev, filter_mask); |
| 4657 | if (err) { |
| 4658 | nla_nest_cancel(skb, br_afspec); |
| 4659 | goto nla_put_failure; |
| 4660 | } |
| 4661 | } |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4662 | nla_nest_end(skb, br_afspec); |
| 4663 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 4664 | protinfo = nla_nest_start(skb, IFLA_PROTINFO); |
Scott Feldman | 2c3c031 | 2014-11-28 14:34:25 +0100 | [diff] [blame] | 4665 | if (!protinfo) |
| 4666 | goto nla_put_failure; |
| 4667 | |
| 4668 | if (brport_nla_put_flag(skb, flags, mask, |
| 4669 | IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || |
| 4670 | brport_nla_put_flag(skb, flags, mask, |
| 4671 | IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || |
| 4672 | brport_nla_put_flag(skb, flags, mask, |
| 4673 | IFLA_BRPORT_FAST_LEAVE, |
| 4674 | BR_MULTICAST_FAST_LEAVE) || |
| 4675 | brport_nla_put_flag(skb, flags, mask, |
| 4676 | IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || |
| 4677 | brport_nla_put_flag(skb, flags, mask, |
| 4678 | IFLA_BRPORT_LEARNING, BR_LEARNING) || |
| 4679 | brport_nla_put_flag(skb, flags, mask, |
| 4680 | IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || |
| 4681 | brport_nla_put_flag(skb, flags, mask, |
| 4682 | IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || |
| 4683 | brport_nla_put_flag(skb, flags, mask, |
Julian Wiedmann | 583cb0b | 2020-02-17 14:45:01 +0100 | [diff] [blame] | 4684 | IFLA_BRPORT_PROXYARP, BR_PROXYARP) || |
| 4685 | brport_nla_put_flag(skb, flags, mask, |
| 4686 | IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) || |
| 4687 | brport_nla_put_flag(skb, flags, mask, |
| 4688 | IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) { |
Scott Feldman | 2c3c031 | 2014-11-28 14:34:25 +0100 | [diff] [blame] | 4689 | nla_nest_cancel(skb, protinfo); |
| 4690 | goto nla_put_failure; |
| 4691 | } |
| 4692 | |
| 4693 | nla_nest_end(skb, protinfo); |
| 4694 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 4695 | nlmsg_end(skb, nlh); |
| 4696 | return 0; |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4697 | nla_put_failure: |
| 4698 | nlmsg_cancel(skb, nlh); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 4699 | return err ? err : -EMSGSIZE; |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4700 | } |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 4701 | EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4702 | |
David Ahern | 2d011be | 2018-10-07 20:16:31 -0700 | [diff] [blame] | 4703 | static int valid_bridge_getlink_req(const struct nlmsghdr *nlh, |
| 4704 | bool strict_check, u32 *filter_mask, |
| 4705 | struct netlink_ext_ack *extack) |
| 4706 | { |
| 4707 | struct nlattr *tb[IFLA_MAX+1]; |
| 4708 | int err, i; |
| 4709 | |
| 4710 | if (strict_check) { |
| 4711 | struct ifinfomsg *ifm; |
| 4712 | |
| 4713 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { |
| 4714 | NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump"); |
| 4715 | return -EINVAL; |
| 4716 | } |
| 4717 | |
| 4718 | ifm = nlmsg_data(nlh); |
| 4719 | if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || |
| 4720 | ifm->ifi_change || ifm->ifi_index) { |
| 4721 | NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request"); |
| 4722 | return -EINVAL; |
| 4723 | } |
| 4724 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 4725 | err = nlmsg_parse_deprecated_strict(nlh, |
| 4726 | sizeof(struct ifinfomsg), |
| 4727 | tb, IFLA_MAX, ifla_policy, |
| 4728 | extack); |
David Ahern | 2d011be | 2018-10-07 20:16:31 -0700 | [diff] [blame] | 4729 | } else { |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 4730 | err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), |
| 4731 | tb, IFLA_MAX, ifla_policy, |
| 4732 | extack); |
David Ahern | 2d011be | 2018-10-07 20:16:31 -0700 | [diff] [blame] | 4733 | } |
| 4734 | if (err < 0) |
| 4735 | return err; |
| 4736 | |
| 4737 | /* new attributes should only be added with strict checking */ |
| 4738 | for (i = 0; i <= IFLA_MAX; ++i) { |
| 4739 | if (!tb[i]) |
| 4740 | continue; |
| 4741 | |
| 4742 | switch (i) { |
| 4743 | case IFLA_EXT_MASK: |
| 4744 | *filter_mask = nla_get_u32(tb[i]); |
| 4745 | break; |
| 4746 | default: |
| 4747 | if (strict_check) { |
| 4748 | NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request"); |
| 4749 | return -EINVAL; |
| 4750 | } |
| 4751 | } |
| 4752 | } |
| 4753 | |
| 4754 | return 0; |
| 4755 | } |
| 4756 | |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4757 | static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) |
| 4758 | { |
David Ahern | 2d011be | 2018-10-07 20:16:31 -0700 | [diff] [blame] | 4759 | const struct nlmsghdr *nlh = cb->nlh; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4760 | struct net *net = sock_net(skb->sk); |
| 4761 | struct net_device *dev; |
| 4762 | int idx = 0; |
| 4763 | u32 portid = NETLINK_CB(cb->skb).portid; |
David Ahern | 2d011be | 2018-10-07 20:16:31 -0700 | [diff] [blame] | 4764 | u32 seq = nlh->nlmsg_seq; |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 4765 | u32 filter_mask = 0; |
Roopa Prabhu | d64f69b | 2015-09-15 14:44:29 -0700 | [diff] [blame] | 4766 | int err; |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 4767 | |
David Ahern | 2d011be | 2018-10-07 20:16:31 -0700 | [diff] [blame] | 4768 | err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask, |
| 4769 | cb->extack); |
| 4770 | if (err < 0 && cb->strict_check) |
| 4771 | return err; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4772 | |
| 4773 | rcu_read_lock(); |
| 4774 | for_each_netdev_rcu(net, dev) { |
| 4775 | const struct net_device_ops *ops = dev->netdev_ops; |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 4776 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4777 | |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 4778 | if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { |
Roopa Prabhu | d64f69b | 2015-09-15 14:44:29 -0700 | [diff] [blame] | 4779 | if (idx >= cb->args[0]) { |
| 4780 | err = br_dev->netdev_ops->ndo_bridge_getlink( |
| 4781 | skb, portid, seq, dev, |
| 4782 | filter_mask, NLM_F_MULTI); |
David Ahern | f6c5775 | 2017-05-15 23:19:17 -0700 | [diff] [blame] | 4783 | if (err < 0 && err != -EOPNOTSUPP) { |
| 4784 | if (likely(skb->len)) |
| 4785 | break; |
| 4786 | |
| 4787 | goto out_err; |
| 4788 | } |
Roopa Prabhu | d64f69b | 2015-09-15 14:44:29 -0700 | [diff] [blame] | 4789 | } |
Ben Hutchings | 25b1e67 | 2012-11-02 12:56:52 +0000 | [diff] [blame] | 4790 | idx++; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4791 | } |
| 4792 | |
| 4793 | if (ops->ndo_bridge_getlink) { |
Roopa Prabhu | d64f69b | 2015-09-15 14:44:29 -0700 | [diff] [blame] | 4794 | if (idx >= cb->args[0]) { |
| 4795 | err = ops->ndo_bridge_getlink(skb, portid, |
| 4796 | seq, dev, |
| 4797 | filter_mask, |
| 4798 | NLM_F_MULTI); |
David Ahern | f6c5775 | 2017-05-15 23:19:17 -0700 | [diff] [blame] | 4799 | if (err < 0 && err != -EOPNOTSUPP) { |
| 4800 | if (likely(skb->len)) |
| 4801 | break; |
| 4802 | |
| 4803 | goto out_err; |
| 4804 | } |
Roopa Prabhu | d64f69b | 2015-09-15 14:44:29 -0700 | [diff] [blame] | 4805 | } |
Ben Hutchings | 25b1e67 | 2012-11-02 12:56:52 +0000 | [diff] [blame] | 4806 | idx++; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4807 | } |
| 4808 | } |
David Ahern | f6c5775 | 2017-05-15 23:19:17 -0700 | [diff] [blame] | 4809 | err = skb->len; |
| 4810 | out_err: |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4811 | rcu_read_unlock(); |
| 4812 | cb->args[0] = idx; |
| 4813 | |
David Ahern | f6c5775 | 2017-05-15 23:19:17 -0700 | [diff] [blame] | 4814 | return err; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4815 | } |
| 4816 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4817 | static inline size_t bridge_nlmsg_size(void) |
| 4818 | { |
| 4819 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) |
| 4820 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ |
| 4821 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ |
| 4822 | + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ |
| 4823 | + nla_total_size(sizeof(u32)) /* IFLA_MTU */ |
| 4824 | + nla_total_size(sizeof(u32)) /* IFLA_LINK */ |
| 4825 | + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ |
| 4826 | + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ |
| 4827 | + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ |
| 4828 | + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ |
| 4829 | + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ |
| 4830 | } |
| 4831 | |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 4832 | static int rtnl_bridge_notify(struct net_device *dev) |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4833 | { |
| 4834 | struct net *net = dev_net(dev); |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4835 | struct sk_buff *skb; |
| 4836 | int err = -EOPNOTSUPP; |
| 4837 | |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 4838 | if (!dev->netdev_ops->ndo_bridge_getlink) |
| 4839 | return 0; |
| 4840 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4841 | skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); |
| 4842 | if (!skb) { |
| 4843 | err = -ENOMEM; |
| 4844 | goto errout; |
| 4845 | } |
| 4846 | |
Nicolas Dichtel | 46c264d | 2015-04-28 18:33:49 +0200 | [diff] [blame] | 4847 | err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 4848 | if (err < 0) |
| 4849 | goto errout; |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4850 | |
Ido Schimmel | d2e381c | 2021-06-09 14:17:53 +0300 | [diff] [blame] | 4851 | /* Notification info is only filled for bridge ports, not the bridge |
| 4852 | * device itself. Therefore, a zero notification length is valid and |
| 4853 | * should not result in an error. |
| 4854 | */ |
| 4855 | if (!skb->len) |
Roopa Prabhu | 59ccaaa | 2015-01-28 16:23:11 -0800 | [diff] [blame] | 4856 | goto errout; |
| 4857 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4858 | rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); |
| 4859 | return 0; |
| 4860 | errout: |
| 4861 | WARN_ON(err == -EMSGSIZE); |
| 4862 | kfree_skb(skb); |
Roopa Prabhu | 59ccaaa | 2015-01-28 16:23:11 -0800 | [diff] [blame] | 4863 | if (err) |
| 4864 | rtnl_set_sk_err(net, RTNLGRP_LINK, err); |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4865 | return err; |
| 4866 | } |
| 4867 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 4868 | static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 4869 | struct netlink_ext_ack *extack) |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4870 | { |
| 4871 | struct net *net = sock_net(skb->sk); |
| 4872 | struct ifinfomsg *ifm; |
| 4873 | struct net_device *dev; |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4874 | struct nlattr *br_spec, *attr = NULL; |
| 4875 | int rem, err = -EOPNOTSUPP; |
Rosen, Rami | 4de8b41 | 2015-01-19 11:45:04 +0200 | [diff] [blame] | 4876 | u16 flags = 0; |
John Fastabend | c38e01b | 2012-11-02 16:32:36 +0000 | [diff] [blame] | 4877 | bool have_flags = false; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4878 | |
| 4879 | if (nlmsg_len(nlh) < sizeof(*ifm)) |
| 4880 | return -EINVAL; |
| 4881 | |
| 4882 | ifm = nlmsg_data(nlh); |
| 4883 | if (ifm->ifi_family != AF_BRIDGE) |
| 4884 | return -EPFNOSUPPORT; |
| 4885 | |
| 4886 | dev = __dev_get_by_index(net, ifm->ifi_index); |
| 4887 | if (!dev) { |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 4888 | NL_SET_ERR_MSG(extack, "unknown ifindex"); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4889 | return -ENODEV; |
| 4890 | } |
| 4891 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4892 | br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); |
| 4893 | if (br_spec) { |
| 4894 | nla_for_each_nested(attr, br_spec, rem) { |
| 4895 | if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { |
Thomas Graf | 6e8d1c5 | 2014-11-26 13:42:16 +0100 | [diff] [blame] | 4896 | if (nla_len(attr) < sizeof(flags)) |
| 4897 | return -EINVAL; |
| 4898 | |
John Fastabend | c38e01b | 2012-11-02 16:32:36 +0000 | [diff] [blame] | 4899 | have_flags = true; |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4900 | flags = nla_get_u16(attr); |
| 4901 | break; |
| 4902 | } |
| 4903 | } |
| 4904 | } |
| 4905 | |
| 4906 | if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 4907 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
| 4908 | |
| 4909 | if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4910 | err = -EOPNOTSUPP; |
| 4911 | goto out; |
| 4912 | } |
| 4913 | |
Petr Machata | 2fd527b | 2018-12-12 17:02:48 +0000 | [diff] [blame] | 4914 | err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags, |
| 4915 | extack); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4916 | if (err) |
| 4917 | goto out; |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4918 | |
| 4919 | flags &= ~BRIDGE_FLAGS_MASTER; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4920 | } |
| 4921 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4922 | if ((flags & BRIDGE_FLAGS_SELF)) { |
| 4923 | if (!dev->netdev_ops->ndo_bridge_setlink) |
| 4924 | err = -EOPNOTSUPP; |
| 4925 | else |
Roopa Prabhu | add511b | 2015-01-29 22:40:12 -0800 | [diff] [blame] | 4926 | err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, |
Petr Machata | 2fd527b | 2018-12-12 17:02:48 +0000 | [diff] [blame] | 4927 | flags, |
| 4928 | extack); |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 4929 | if (!err) { |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4930 | flags &= ~BRIDGE_FLAGS_SELF; |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 4931 | |
| 4932 | /* Generate event to notify upper layer of bridge |
| 4933 | * change |
| 4934 | */ |
| 4935 | err = rtnl_bridge_notify(dev); |
| 4936 | } |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4937 | } |
| 4938 | |
John Fastabend | c38e01b | 2012-11-02 16:32:36 +0000 | [diff] [blame] | 4939 | if (have_flags) |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4940 | memcpy(nla_data(attr), &flags, sizeof(flags)); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4941 | out: |
| 4942 | return err; |
| 4943 | } |
| 4944 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 4945 | static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 4946 | struct netlink_ext_ack *extack) |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 4947 | { |
| 4948 | struct net *net = sock_net(skb->sk); |
| 4949 | struct ifinfomsg *ifm; |
| 4950 | struct net_device *dev; |
| 4951 | struct nlattr *br_spec, *attr = NULL; |
| 4952 | int rem, err = -EOPNOTSUPP; |
Rosen, Rami | 4de8b41 | 2015-01-19 11:45:04 +0200 | [diff] [blame] | 4953 | u16 flags = 0; |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 4954 | bool have_flags = false; |
| 4955 | |
| 4956 | if (nlmsg_len(nlh) < sizeof(*ifm)) |
| 4957 | return -EINVAL; |
| 4958 | |
| 4959 | ifm = nlmsg_data(nlh); |
| 4960 | if (ifm->ifi_family != AF_BRIDGE) |
| 4961 | return -EPFNOSUPPORT; |
| 4962 | |
| 4963 | dev = __dev_get_by_index(net, ifm->ifi_index); |
| 4964 | if (!dev) { |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 4965 | NL_SET_ERR_MSG(extack, "unknown ifindex"); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 4966 | return -ENODEV; |
| 4967 | } |
| 4968 | |
| 4969 | br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); |
| 4970 | if (br_spec) { |
| 4971 | nla_for_each_nested(attr, br_spec, rem) { |
| 4972 | if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { |
Thomas Graf | 6e8d1c5 | 2014-11-26 13:42:16 +0100 | [diff] [blame] | 4973 | if (nla_len(attr) < sizeof(flags)) |
| 4974 | return -EINVAL; |
| 4975 | |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 4976 | have_flags = true; |
| 4977 | flags = nla_get_u16(attr); |
| 4978 | break; |
| 4979 | } |
| 4980 | } |
| 4981 | } |
| 4982 | |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 4983 | if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { |
| 4984 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
| 4985 | |
| 4986 | if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { |
| 4987 | err = -EOPNOTSUPP; |
| 4988 | goto out; |
| 4989 | } |
| 4990 | |
Roopa Prabhu | add511b | 2015-01-29 22:40:12 -0800 | [diff] [blame] | 4991 | err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 4992 | if (err) |
| 4993 | goto out; |
| 4994 | |
| 4995 | flags &= ~BRIDGE_FLAGS_MASTER; |
| 4996 | } |
| 4997 | |
| 4998 | if ((flags & BRIDGE_FLAGS_SELF)) { |
| 4999 | if (!dev->netdev_ops->ndo_bridge_dellink) |
| 5000 | err = -EOPNOTSUPP; |
| 5001 | else |
Roopa Prabhu | add511b | 2015-01-29 22:40:12 -0800 | [diff] [blame] | 5002 | err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, |
| 5003 | flags); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 5004 | |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 5005 | if (!err) { |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 5006 | flags &= ~BRIDGE_FLAGS_SELF; |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 5007 | |
| 5008 | /* Generate event to notify upper layer of bridge |
| 5009 | * change |
| 5010 | */ |
| 5011 | err = rtnl_bridge_notify(dev); |
| 5012 | } |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 5013 | } |
| 5014 | |
| 5015 | if (have_flags) |
| 5016 | memcpy(nla_data(attr), &flags, sizeof(flags)); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 5017 | out: |
| 5018 | return err; |
| 5019 | } |
| 5020 | |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5021 | static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) |
| 5022 | { |
| 5023 | return (mask & IFLA_STATS_FILTER_BIT(attrid)) && |
| 5024 | (!idxattr || idxattr == attrid); |
| 5025 | } |
| 5026 | |
Nogah Frankel | 69ae6ad | 2016-09-16 15:05:37 +0200 | [diff] [blame] | 5027 | #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1) |
| 5028 | static int rtnl_get_offload_stats_attr_size(int attr_id) |
| 5029 | { |
| 5030 | switch (attr_id) { |
| 5031 | case IFLA_OFFLOAD_XSTATS_CPU_HIT: |
| 5032 | return sizeof(struct rtnl_link_stats64); |
| 5033 | } |
| 5034 | |
| 5035 | return 0; |
| 5036 | } |
| 5037 | |
| 5038 | static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev, |
| 5039 | int *prividx) |
| 5040 | { |
| 5041 | struct nlattr *attr = NULL; |
| 5042 | int attr_id, size; |
| 5043 | void *attr_data; |
| 5044 | int err; |
| 5045 | |
| 5046 | if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && |
| 5047 | dev->netdev_ops->ndo_get_offload_stats)) |
| 5048 | return -ENODATA; |
| 5049 | |
| 5050 | for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; |
| 5051 | attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { |
| 5052 | if (attr_id < *prividx) |
| 5053 | continue; |
| 5054 | |
| 5055 | size = rtnl_get_offload_stats_attr_size(attr_id); |
| 5056 | if (!size) |
| 5057 | continue; |
| 5058 | |
Or Gerlitz | 3df5b3c | 2016-11-22 23:09:54 +0200 | [diff] [blame] | 5059 | if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) |
Nogah Frankel | 69ae6ad | 2016-09-16 15:05:37 +0200 | [diff] [blame] | 5060 | continue; |
| 5061 | |
| 5062 | attr = nla_reserve_64bit(skb, attr_id, size, |
| 5063 | IFLA_OFFLOAD_XSTATS_UNSPEC); |
| 5064 | if (!attr) |
| 5065 | goto nla_put_failure; |
| 5066 | |
| 5067 | attr_data = nla_data(attr); |
| 5068 | memset(attr_data, 0, size); |
| 5069 | err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, |
| 5070 | attr_data); |
| 5071 | if (err) |
| 5072 | goto get_offload_stats_failure; |
| 5073 | } |
| 5074 | |
| 5075 | if (!attr) |
| 5076 | return -ENODATA; |
| 5077 | |
| 5078 | *prividx = 0; |
| 5079 | return 0; |
| 5080 | |
| 5081 | nla_put_failure: |
| 5082 | err = -EMSGSIZE; |
| 5083 | get_offload_stats_failure: |
| 5084 | *prividx = attr_id; |
| 5085 | return err; |
| 5086 | } |
| 5087 | |
| 5088 | static int rtnl_get_offload_stats_size(const struct net_device *dev) |
| 5089 | { |
| 5090 | int nla_size = 0; |
| 5091 | int attr_id; |
| 5092 | int size; |
| 5093 | |
| 5094 | if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && |
| 5095 | dev->netdev_ops->ndo_get_offload_stats)) |
| 5096 | return 0; |
| 5097 | |
| 5098 | for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; |
| 5099 | attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { |
Or Gerlitz | 3df5b3c | 2016-11-22 23:09:54 +0200 | [diff] [blame] | 5100 | if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) |
Nogah Frankel | 69ae6ad | 2016-09-16 15:05:37 +0200 | [diff] [blame] | 5101 | continue; |
| 5102 | size = rtnl_get_offload_stats_attr_size(attr_id); |
| 5103 | nla_size += nla_total_size_64bit(size); |
| 5104 | } |
| 5105 | |
| 5106 | if (nla_size != 0) |
| 5107 | nla_size += nla_total_size(0); |
| 5108 | |
| 5109 | return nla_size; |
| 5110 | } |
| 5111 | |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5112 | static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, |
| 5113 | int type, u32 pid, u32 seq, u32 change, |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5114 | unsigned int flags, unsigned int filter_mask, |
| 5115 | int *idxattr, int *prividx) |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5116 | { |
| 5117 | struct if_stats_msg *ifsm; |
| 5118 | struct nlmsghdr *nlh; |
| 5119 | struct nlattr *attr; |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5120 | int s_prividx = *prividx; |
Nogah Frankel | 69ae6ad | 2016-09-16 15:05:37 +0200 | [diff] [blame] | 5121 | int err; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5122 | |
| 5123 | ASSERT_RTNL(); |
| 5124 | |
| 5125 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); |
| 5126 | if (!nlh) |
| 5127 | return -EMSGSIZE; |
| 5128 | |
| 5129 | ifsm = nlmsg_data(nlh); |
Nikolay Aleksandrov | ce024f4 | 2017-10-03 13:20:48 +0300 | [diff] [blame] | 5130 | ifsm->family = PF_UNSPEC; |
| 5131 | ifsm->pad1 = 0; |
| 5132 | ifsm->pad2 = 0; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5133 | ifsm->ifindex = dev->ifindex; |
| 5134 | ifsm->filter_mask = filter_mask; |
| 5135 | |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5136 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5137 | struct rtnl_link_stats64 *sp; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5138 | |
Nicolas Dichtel | 58414d3 | 2016-04-21 18:58:25 +0200 | [diff] [blame] | 5139 | attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, |
| 5140 | sizeof(struct rtnl_link_stats64), |
| 5141 | IFLA_STATS_UNSPEC); |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5142 | if (!attr) |
| 5143 | goto nla_put_failure; |
| 5144 | |
| 5145 | sp = nla_data(attr); |
| 5146 | dev_get_stats(dev, sp); |
| 5147 | } |
| 5148 | |
Nikolay Aleksandrov | 97a47fa | 2016-04-30 10:25:27 +0200 | [diff] [blame] | 5149 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { |
| 5150 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; |
| 5151 | |
| 5152 | if (ops && ops->fill_linkxstats) { |
Nikolay Aleksandrov | 97a47fa | 2016-04-30 10:25:27 +0200 | [diff] [blame] | 5153 | *idxattr = IFLA_STATS_LINK_XSTATS; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 5154 | attr = nla_nest_start_noflag(skb, |
| 5155 | IFLA_STATS_LINK_XSTATS); |
Nikolay Aleksandrov | 97a47fa | 2016-04-30 10:25:27 +0200 | [diff] [blame] | 5156 | if (!attr) |
| 5157 | goto nla_put_failure; |
| 5158 | |
Nikolay Aleksandrov | 80e73cc | 2016-06-28 16:57:05 +0200 | [diff] [blame] | 5159 | err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); |
| 5160 | nla_nest_end(skb, attr); |
| 5161 | if (err) |
| 5162 | goto nla_put_failure; |
| 5163 | *idxattr = 0; |
| 5164 | } |
| 5165 | } |
| 5166 | |
| 5167 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, |
| 5168 | *idxattr)) { |
| 5169 | const struct rtnl_link_ops *ops = NULL; |
| 5170 | const struct net_device *master; |
| 5171 | |
| 5172 | master = netdev_master_upper_dev_get(dev); |
| 5173 | if (master) |
| 5174 | ops = master->rtnl_link_ops; |
| 5175 | if (ops && ops->fill_linkxstats) { |
Nikolay Aleksandrov | 80e73cc | 2016-06-28 16:57:05 +0200 | [diff] [blame] | 5176 | *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 5177 | attr = nla_nest_start_noflag(skb, |
| 5178 | IFLA_STATS_LINK_XSTATS_SLAVE); |
Nikolay Aleksandrov | 80e73cc | 2016-06-28 16:57:05 +0200 | [diff] [blame] | 5179 | if (!attr) |
| 5180 | goto nla_put_failure; |
| 5181 | |
| 5182 | err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); |
Nikolay Aleksandrov | 97a47fa | 2016-04-30 10:25:27 +0200 | [diff] [blame] | 5183 | nla_nest_end(skb, attr); |
| 5184 | if (err) |
| 5185 | goto nla_put_failure; |
| 5186 | *idxattr = 0; |
| 5187 | } |
| 5188 | } |
| 5189 | |
Nogah Frankel | 69ae6ad | 2016-09-16 15:05:37 +0200 | [diff] [blame] | 5190 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, |
| 5191 | *idxattr)) { |
| 5192 | *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 5193 | attr = nla_nest_start_noflag(skb, |
| 5194 | IFLA_STATS_LINK_OFFLOAD_XSTATS); |
Nogah Frankel | 69ae6ad | 2016-09-16 15:05:37 +0200 | [diff] [blame] | 5195 | if (!attr) |
| 5196 | goto nla_put_failure; |
| 5197 | |
| 5198 | err = rtnl_get_offload_stats(skb, dev, prividx); |
| 5199 | if (err == -ENODATA) |
| 5200 | nla_nest_cancel(skb, attr); |
| 5201 | else |
| 5202 | nla_nest_end(skb, attr); |
| 5203 | |
| 5204 | if (err && err != -ENODATA) |
| 5205 | goto nla_put_failure; |
| 5206 | *idxattr = 0; |
| 5207 | } |
| 5208 | |
Robert Shearman | aefb4d4 | 2017-01-16 14:16:36 +0000 | [diff] [blame] | 5209 | if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) { |
| 5210 | struct rtnl_af_ops *af_ops; |
| 5211 | |
| 5212 | *idxattr = IFLA_STATS_AF_SPEC; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 5213 | attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC); |
Robert Shearman | aefb4d4 | 2017-01-16 14:16:36 +0000 | [diff] [blame] | 5214 | if (!attr) |
| 5215 | goto nla_put_failure; |
| 5216 | |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 5217 | rcu_read_lock(); |
| 5218 | list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { |
Robert Shearman | aefb4d4 | 2017-01-16 14:16:36 +0000 | [diff] [blame] | 5219 | if (af_ops->fill_stats_af) { |
| 5220 | struct nlattr *af; |
| 5221 | int err; |
| 5222 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 5223 | af = nla_nest_start_noflag(skb, |
| 5224 | af_ops->family); |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 5225 | if (!af) { |
| 5226 | rcu_read_unlock(); |
Robert Shearman | aefb4d4 | 2017-01-16 14:16:36 +0000 | [diff] [blame] | 5227 | goto nla_put_failure; |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 5228 | } |
Robert Shearman | aefb4d4 | 2017-01-16 14:16:36 +0000 | [diff] [blame] | 5229 | err = af_ops->fill_stats_af(skb, dev); |
| 5230 | |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 5231 | if (err == -ENODATA) { |
Robert Shearman | aefb4d4 | 2017-01-16 14:16:36 +0000 | [diff] [blame] | 5232 | nla_nest_cancel(skb, af); |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 5233 | } else if (err < 0) { |
| 5234 | rcu_read_unlock(); |
Robert Shearman | aefb4d4 | 2017-01-16 14:16:36 +0000 | [diff] [blame] | 5235 | goto nla_put_failure; |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 5236 | } |
Robert Shearman | aefb4d4 | 2017-01-16 14:16:36 +0000 | [diff] [blame] | 5237 | |
| 5238 | nla_nest_end(skb, af); |
| 5239 | } |
| 5240 | } |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 5241 | rcu_read_unlock(); |
Robert Shearman | aefb4d4 | 2017-01-16 14:16:36 +0000 | [diff] [blame] | 5242 | |
| 5243 | nla_nest_end(skb, attr); |
| 5244 | |
| 5245 | *idxattr = 0; |
| 5246 | } |
| 5247 | |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5248 | nlmsg_end(skb, nlh); |
| 5249 | |
| 5250 | return 0; |
| 5251 | |
| 5252 | nla_put_failure: |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5253 | /* not a multi message or no progress mean a real error */ |
| 5254 | if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) |
| 5255 | nlmsg_cancel(skb, nlh); |
| 5256 | else |
| 5257 | nlmsg_end(skb, nlh); |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5258 | |
| 5259 | return -EMSGSIZE; |
| 5260 | } |
| 5261 | |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5262 | static size_t if_nlmsg_stats_size(const struct net_device *dev, |
| 5263 | u32 filter_mask) |
| 5264 | { |
Eric Dumazet | d343679 | 2021-10-05 14:04:17 -0700 | [diff] [blame^] | 5265 | size_t size = NLMSG_ALIGN(sizeof(struct if_stats_msg)); |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5266 | |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5267 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5268 | size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); |
| 5269 | |
Nikolay Aleksandrov | 97a47fa | 2016-04-30 10:25:27 +0200 | [diff] [blame] | 5270 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { |
| 5271 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; |
Nikolay Aleksandrov | 80e73cc | 2016-06-28 16:57:05 +0200 | [diff] [blame] | 5272 | int attr = IFLA_STATS_LINK_XSTATS; |
Nikolay Aleksandrov | 97a47fa | 2016-04-30 10:25:27 +0200 | [diff] [blame] | 5273 | |
| 5274 | if (ops && ops->get_linkxstats_size) { |
Nikolay Aleksandrov | 80e73cc | 2016-06-28 16:57:05 +0200 | [diff] [blame] | 5275 | size += nla_total_size(ops->get_linkxstats_size(dev, |
| 5276 | attr)); |
Nikolay Aleksandrov | 97a47fa | 2016-04-30 10:25:27 +0200 | [diff] [blame] | 5277 | /* for IFLA_STATS_LINK_XSTATS */ |
| 5278 | size += nla_total_size(0); |
| 5279 | } |
| 5280 | } |
| 5281 | |
Nikolay Aleksandrov | 80e73cc | 2016-06-28 16:57:05 +0200 | [diff] [blame] | 5282 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) { |
| 5283 | struct net_device *_dev = (struct net_device *)dev; |
| 5284 | const struct rtnl_link_ops *ops = NULL; |
| 5285 | const struct net_device *master; |
| 5286 | |
| 5287 | /* netdev_master_upper_dev_get can't take const */ |
| 5288 | master = netdev_master_upper_dev_get(_dev); |
| 5289 | if (master) |
| 5290 | ops = master->rtnl_link_ops; |
| 5291 | if (ops && ops->get_linkxstats_size) { |
| 5292 | int attr = IFLA_STATS_LINK_XSTATS_SLAVE; |
| 5293 | |
| 5294 | size += nla_total_size(ops->get_linkxstats_size(dev, |
| 5295 | attr)); |
| 5296 | /* for IFLA_STATS_LINK_XSTATS_SLAVE */ |
| 5297 | size += nla_total_size(0); |
| 5298 | } |
| 5299 | } |
| 5300 | |
Nogah Frankel | 69ae6ad | 2016-09-16 15:05:37 +0200 | [diff] [blame] | 5301 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) |
| 5302 | size += rtnl_get_offload_stats_size(dev); |
| 5303 | |
Robert Shearman | aefb4d4 | 2017-01-16 14:16:36 +0000 | [diff] [blame] | 5304 | if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) { |
| 5305 | struct rtnl_af_ops *af_ops; |
| 5306 | |
| 5307 | /* for IFLA_STATS_AF_SPEC */ |
| 5308 | size += nla_total_size(0); |
| 5309 | |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 5310 | rcu_read_lock(); |
| 5311 | list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { |
Robert Shearman | aefb4d4 | 2017-01-16 14:16:36 +0000 | [diff] [blame] | 5312 | if (af_ops->get_stats_af_size) { |
| 5313 | size += nla_total_size( |
| 5314 | af_ops->get_stats_af_size(dev)); |
| 5315 | |
| 5316 | /* for AF_* */ |
| 5317 | size += nla_total_size(0); |
| 5318 | } |
| 5319 | } |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 5320 | rcu_read_unlock(); |
Robert Shearman | aefb4d4 | 2017-01-16 14:16:36 +0000 | [diff] [blame] | 5321 | } |
| 5322 | |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5323 | return size; |
| 5324 | } |
| 5325 | |
Jakub Kicinski | 51bc860 | 2019-01-18 10:46:14 -0800 | [diff] [blame] | 5326 | static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check, |
| 5327 | bool is_dump, struct netlink_ext_ack *extack) |
| 5328 | { |
| 5329 | struct if_stats_msg *ifsm; |
| 5330 | |
Eric Dumazet | 69f23a0 | 2019-04-14 11:02:05 -0700 | [diff] [blame] | 5331 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) { |
Jakub Kicinski | 51bc860 | 2019-01-18 10:46:14 -0800 | [diff] [blame] | 5332 | NL_SET_ERR_MSG(extack, "Invalid header for stats dump"); |
| 5333 | return -EINVAL; |
| 5334 | } |
| 5335 | |
| 5336 | if (!strict_check) |
| 5337 | return 0; |
| 5338 | |
| 5339 | ifsm = nlmsg_data(nlh); |
| 5340 | |
| 5341 | /* only requests using strict checks can pass data to influence |
| 5342 | * the dump. The legacy exception is filter_mask. |
| 5343 | */ |
| 5344 | if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) { |
| 5345 | NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request"); |
| 5346 | return -EINVAL; |
| 5347 | } |
| 5348 | if (nlmsg_attrlen(nlh, sizeof(*ifsm))) { |
| 5349 | NL_SET_ERR_MSG(extack, "Invalid attributes after stats header"); |
| 5350 | return -EINVAL; |
| 5351 | } |
Jakub Kicinski | 6300acb | 2019-01-18 10:46:15 -0800 | [diff] [blame] | 5352 | if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) { |
| 5353 | NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask"); |
| 5354 | return -EINVAL; |
| 5355 | } |
Jakub Kicinski | 51bc860 | 2019-01-18 10:46:14 -0800 | [diff] [blame] | 5356 | |
| 5357 | return 0; |
| 5358 | } |
| 5359 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 5360 | static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 5361 | struct netlink_ext_ack *extack) |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5362 | { |
| 5363 | struct net *net = sock_net(skb->sk); |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5364 | struct net_device *dev = NULL; |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5365 | int idxattr = 0, prividx = 0; |
| 5366 | struct if_stats_msg *ifsm; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5367 | struct sk_buff *nskb; |
| 5368 | u32 filter_mask; |
| 5369 | int err; |
| 5370 | |
Jakub Kicinski | 51bc860 | 2019-01-18 10:46:14 -0800 | [diff] [blame] | 5371 | err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb), |
| 5372 | false, extack); |
| 5373 | if (err) |
| 5374 | return err; |
Mathias Krause | 4775cc1 | 2016-12-28 17:52:15 +0100 | [diff] [blame] | 5375 | |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5376 | ifsm = nlmsg_data(nlh); |
| 5377 | if (ifsm->ifindex > 0) |
| 5378 | dev = __dev_get_by_index(net, ifsm->ifindex); |
| 5379 | else |
| 5380 | return -EINVAL; |
| 5381 | |
| 5382 | if (!dev) |
| 5383 | return -ENODEV; |
| 5384 | |
| 5385 | filter_mask = ifsm->filter_mask; |
| 5386 | if (!filter_mask) |
| 5387 | return -EINVAL; |
| 5388 | |
| 5389 | nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL); |
| 5390 | if (!nskb) |
| 5391 | return -ENOBUFS; |
| 5392 | |
| 5393 | err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, |
| 5394 | NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5395 | 0, filter_mask, &idxattr, &prividx); |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5396 | if (err < 0) { |
| 5397 | /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ |
| 5398 | WARN_ON(err == -EMSGSIZE); |
| 5399 | kfree_skb(nskb); |
| 5400 | } else { |
| 5401 | err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); |
| 5402 | } |
| 5403 | |
| 5404 | return err; |
| 5405 | } |
| 5406 | |
| 5407 | static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 5408 | { |
David Ahern | 841891e | 2018-10-07 20:16:32 -0700 | [diff] [blame] | 5409 | struct netlink_ext_ack *extack = cb->extack; |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5410 | int h, s_h, err, s_idx, s_idxattr, s_prividx; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5411 | struct net *net = sock_net(skb->sk); |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5412 | unsigned int flags = NLM_F_MULTI; |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5413 | struct if_stats_msg *ifsm; |
| 5414 | struct hlist_head *head; |
| 5415 | struct net_device *dev; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5416 | u32 filter_mask = 0; |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5417 | int idx = 0; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5418 | |
| 5419 | s_h = cb->args[0]; |
| 5420 | s_idx = cb->args[1]; |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5421 | s_idxattr = cb->args[2]; |
| 5422 | s_prividx = cb->args[3]; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5423 | |
| 5424 | cb->seq = net->dev_base_seq; |
| 5425 | |
Jakub Kicinski | 51bc860 | 2019-01-18 10:46:14 -0800 | [diff] [blame] | 5426 | err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack); |
| 5427 | if (err) |
| 5428 | return err; |
Mathias Krause | 4775cc1 | 2016-12-28 17:52:15 +0100 | [diff] [blame] | 5429 | |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5430 | ifsm = nlmsg_data(cb->nlh); |
| 5431 | filter_mask = ifsm->filter_mask; |
David Ahern | 841891e | 2018-10-07 20:16:32 -0700 | [diff] [blame] | 5432 | if (!filter_mask) { |
| 5433 | NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump"); |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5434 | return -EINVAL; |
David Ahern | 841891e | 2018-10-07 20:16:32 -0700 | [diff] [blame] | 5435 | } |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5436 | |
| 5437 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { |
| 5438 | idx = 0; |
| 5439 | head = &net->dev_index_head[h]; |
| 5440 | hlist_for_each_entry(dev, head, index_hlist) { |
| 5441 | if (idx < s_idx) |
| 5442 | goto cont; |
| 5443 | err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, |
| 5444 | NETLINK_CB(cb->skb).portid, |
| 5445 | cb->nlh->nlmsg_seq, 0, |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5446 | flags, filter_mask, |
| 5447 | &s_idxattr, &s_prividx); |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5448 | /* If we ran out of room on the first message, |
| 5449 | * we're in trouble |
| 5450 | */ |
| 5451 | WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); |
| 5452 | |
| 5453 | if (err < 0) |
| 5454 | goto out; |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5455 | s_prividx = 0; |
| 5456 | s_idxattr = 0; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5457 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
| 5458 | cont: |
| 5459 | idx++; |
| 5460 | } |
| 5461 | } |
| 5462 | out: |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5463 | cb->args[3] = s_prividx; |
| 5464 | cb->args[2] = s_idxattr; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5465 | cb->args[1] = idx; |
| 5466 | cb->args[0] = h; |
| 5467 | |
| 5468 | return skb->len; |
| 5469 | } |
| 5470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5471 | /* Process one rtnetlink message. */ |
| 5472 | |
Johannes Berg | 2d4bc93 | 2017-04-12 14:34:04 +0200 | [diff] [blame] | 5473 | static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 5474 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5475 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 5476 | struct net *net = sock_net(skb->sk); |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 5477 | struct rtnl_link *link; |
Florian Westphal | e420251 | 2017-12-02 21:44:06 +0100 | [diff] [blame] | 5478 | struct module *owner; |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 5479 | int err = -EOPNOTSUPP; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 5480 | rtnl_doit_func doit; |
Florian Westphal | 62256f9 | 2017-08-09 20:41:52 +0200 | [diff] [blame] | 5481 | unsigned int flags; |
Alexander Kuleshov | 617cfc7 | 2016-01-10 21:26:57 +0600 | [diff] [blame] | 5482 | int kind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5483 | int family; |
| 5484 | int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5486 | type = nlh->nlmsg_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5487 | if (type > RTM_MAX) |
Thomas Graf | 038890f | 2007-04-05 14:35:52 -0700 | [diff] [blame] | 5488 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5489 | |
| 5490 | type -= RTM_BASE; |
| 5491 | |
| 5492 | /* All the messages must have at least 1 byte length */ |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 5493 | if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5494 | return 0; |
| 5495 | |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 5496 | family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5497 | kind = type&3; |
| 5498 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 5499 | if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 5500 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5501 | |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 5502 | rcu_read_lock(); |
David S. Miller | b8f3ab4 | 2011-01-18 12:40:38 -0800 | [diff] [blame] | 5503 | if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 5504 | struct sock *rtnl; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 5505 | rtnl_dumpit_func dumpit; |
Di Zhu | ebfe3c5 | 2020-10-21 10:00:53 +0800 | [diff] [blame] | 5506 | u32 min_dump_alloc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5507 | |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 5508 | link = rtnl_get_link(family, type); |
| 5509 | if (!link || !link->dumpit) { |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 5510 | family = PF_UNSPEC; |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 5511 | link = rtnl_get_link(family, type); |
| 5512 | if (!link || !link->dumpit) |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 5513 | goto err_unlock; |
| 5514 | } |
Florian Westphal | e420251 | 2017-12-02 21:44:06 +0100 | [diff] [blame] | 5515 | owner = link->owner; |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 5516 | dumpit = link->dumpit; |
Florian Westphal | e1fa6d2 | 2017-08-09 20:41:47 +0200 | [diff] [blame] | 5517 | |
Florian Westphal | 5c2bb9b | 2017-08-10 16:52:58 +0200 | [diff] [blame] | 5518 | if (type == RTM_GETLINK - RTM_BASE) |
Florian Westphal | e1fa6d2 | 2017-08-09 20:41:47 +0200 | [diff] [blame] | 5519 | min_dump_alloc = rtnl_calcit(skb, nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5520 | |
Florian Westphal | e420251 | 2017-12-02 21:44:06 +0100 | [diff] [blame] | 5521 | err = 0; |
| 5522 | /* need to do this before rcu_read_unlock() */ |
| 5523 | if (!try_module_get(owner)) |
| 5524 | err = -EPROTONOSUPPORT; |
| 5525 | |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 5526 | rcu_read_unlock(); |
| 5527 | |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 5528 | rtnl = net->rtnl; |
Florian Westphal | e420251 | 2017-12-02 21:44:06 +0100 | [diff] [blame] | 5529 | if (err == 0) { |
Pablo Neira Ayuso | 80d326f | 2012-02-24 14:30:15 +0000 | [diff] [blame] | 5530 | struct netlink_dump_control c = { |
| 5531 | .dump = dumpit, |
| 5532 | .min_dump_alloc = min_dump_alloc, |
Florian Westphal | e420251 | 2017-12-02 21:44:06 +0100 | [diff] [blame] | 5533 | .module = owner, |
Pablo Neira Ayuso | 80d326f | 2012-02-24 14:30:15 +0000 | [diff] [blame] | 5534 | }; |
| 5535 | err = netlink_dump_start(rtnl, skb, nlh, &c); |
Florian Westphal | e420251 | 2017-12-02 21:44:06 +0100 | [diff] [blame] | 5536 | /* netlink_dump_start() will keep a reference on |
| 5537 | * module if dump is still in progress. |
| 5538 | */ |
| 5539 | module_put(owner); |
Pablo Neira Ayuso | 80d326f | 2012-02-24 14:30:15 +0000 | [diff] [blame] | 5540 | } |
Eric Dumazet | 2907c35 | 2011-05-25 07:34:04 +0000 | [diff] [blame] | 5541 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5542 | } |
| 5543 | |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 5544 | link = rtnl_get_link(family, type); |
| 5545 | if (!link || !link->doit) { |
Florian Westphal | 8caa38b | 2017-08-10 16:53:01 +0200 | [diff] [blame] | 5546 | family = PF_UNSPEC; |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 5547 | link = rtnl_get_link(PF_UNSPEC, type); |
| 5548 | if (!link || !link->doit) |
| 5549 | goto out_unlock; |
Florian Westphal | 8caa38b | 2017-08-10 16:53:01 +0200 | [diff] [blame] | 5550 | } |
| 5551 | |
Florian Westphal | e420251 | 2017-12-02 21:44:06 +0100 | [diff] [blame] | 5552 | owner = link->owner; |
| 5553 | if (!try_module_get(owner)) { |
| 5554 | err = -EPROTONOSUPPORT; |
| 5555 | goto out_unlock; |
| 5556 | } |
| 5557 | |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 5558 | flags = link->flags; |
Florian Westphal | 62256f9 | 2017-08-09 20:41:52 +0200 | [diff] [blame] | 5559 | if (flags & RTNL_FLAG_DOIT_UNLOCKED) { |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 5560 | doit = link->doit; |
Florian Westphal | 62256f9 | 2017-08-09 20:41:52 +0200 | [diff] [blame] | 5561 | rcu_read_unlock(); |
| 5562 | if (doit) |
| 5563 | err = doit(skb, nlh, extack); |
Florian Westphal | e420251 | 2017-12-02 21:44:06 +0100 | [diff] [blame] | 5564 | module_put(owner); |
Florian Westphal | 62256f9 | 2017-08-09 20:41:52 +0200 | [diff] [blame] | 5565 | return err; |
| 5566 | } |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 5567 | rcu_read_unlock(); |
| 5568 | |
Florian Westphal | 0cc0902 | 2017-08-09 20:41:50 +0200 | [diff] [blame] | 5569 | rtnl_lock(); |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 5570 | link = rtnl_get_link(family, type); |
| 5571 | if (link && link->doit) |
| 5572 | err = link->doit(skb, nlh, extack); |
Florian Westphal | 0cc0902 | 2017-08-09 20:41:50 +0200 | [diff] [blame] | 5573 | rtnl_unlock(); |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 5574 | |
Florian Westphal | e420251 | 2017-12-02 21:44:06 +0100 | [diff] [blame] | 5575 | module_put(owner); |
| 5576 | |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 5577 | return err; |
| 5578 | |
| 5579 | out_unlock: |
| 5580 | rcu_read_unlock(); |
Florian Westphal | 0cc0902 | 2017-08-09 20:41:50 +0200 | [diff] [blame] | 5581 | return err; |
| 5582 | |
| 5583 | err_unlock: |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 5584 | rcu_read_unlock(); |
Florian Westphal | 0cc0902 | 2017-08-09 20:41:50 +0200 | [diff] [blame] | 5585 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5586 | } |
| 5587 | |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 5588 | static void rtnetlink_rcv(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5589 | { |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 5590 | netlink_rcv_skb(skb, &rtnetlink_rcv_msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5591 | } |
| 5592 | |
Julien Gomes | 5f729ea | 2017-06-20 13:54:16 -0700 | [diff] [blame] | 5593 | static int rtnetlink_bind(struct net *net, int group) |
| 5594 | { |
| 5595 | switch (group) { |
| 5596 | case RTNLGRP_IPV4_MROUTE_R: |
| 5597 | case RTNLGRP_IPV6_MROUTE_R: |
| 5598 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
| 5599 | return -EPERM; |
| 5600 | break; |
| 5601 | } |
| 5602 | return 0; |
| 5603 | } |
| 5604 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5605 | static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 5606 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 5607 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 5608 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5609 | switch (event) { |
Vlad Yasevich | 5138e86 | 2017-04-04 09:23:41 -0400 | [diff] [blame] | 5610 | case NETDEV_REBOOT: |
Xin Long | 8a21258 | 2017-10-15 18:13:41 +0800 | [diff] [blame] | 5611 | case NETDEV_CHANGEMTU: |
David Ahern | 3753654 | 2017-07-19 10:22:40 -0700 | [diff] [blame] | 5612 | case NETDEV_CHANGEADDR: |
Vlad Yasevich | 5138e86 | 2017-04-04 09:23:41 -0400 | [diff] [blame] | 5613 | case NETDEV_CHANGENAME: |
| 5614 | case NETDEV_FEAT_CHANGE: |
| 5615 | case NETDEV_BONDING_FAILOVER: |
Xin Long | e6e6659 | 2017-10-15 18:13:43 +0800 | [diff] [blame] | 5616 | case NETDEV_POST_TYPE_CHANGE: |
Vlad Yasevich | 5138e86 | 2017-04-04 09:23:41 -0400 | [diff] [blame] | 5617 | case NETDEV_NOTIFY_PEERS: |
Xin Long | dc709f3 | 2017-10-15 18:13:44 +0800 | [diff] [blame] | 5618 | case NETDEV_CHANGEUPPER: |
Vlad Yasevich | 5138e86 | 2017-04-04 09:23:41 -0400 | [diff] [blame] | 5619 | case NETDEV_RESEND_IGMP: |
Vlad Yasevich | 5138e86 | 2017-04-04 09:23:41 -0400 | [diff] [blame] | 5620 | case NETDEV_CHANGEINFODATA: |
Xin Long | eeda3fb | 2017-10-24 13:54:19 +0800 | [diff] [blame] | 5621 | case NETDEV_CHANGELOWERSTATE: |
Xin Long | ebdcf04 | 2017-10-15 18:13:42 +0800 | [diff] [blame] | 5622 | case NETDEV_CHANGE_TX_QUEUE_LEN: |
Vlad Yasevich | 3d3ea5a | 2017-05-27 10:14:34 -0400 | [diff] [blame] | 5623 | rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event), |
Nicolas Dichtel | 38e01b3 | 2018-01-25 15:01:39 +0100 | [diff] [blame] | 5624 | GFP_KERNEL, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5625 | break; |
| 5626 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5627 | break; |
| 5628 | } |
| 5629 | return NOTIFY_DONE; |
| 5630 | } |
| 5631 | |
| 5632 | static struct notifier_block rtnetlink_dev_notifier = { |
| 5633 | .notifier_call = rtnetlink_event, |
| 5634 | }; |
| 5635 | |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 5636 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 5637 | static int __net_init rtnetlink_net_init(struct net *net) |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 5638 | { |
| 5639 | struct sock *sk; |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 5640 | struct netlink_kernel_cfg cfg = { |
| 5641 | .groups = RTNLGRP_MAX, |
| 5642 | .input = rtnetlink_rcv, |
| 5643 | .cb_mutex = &rtnl_mutex, |
Pablo Neira Ayuso | 9785e10 | 2012-09-08 02:53:53 +0000 | [diff] [blame] | 5644 | .flags = NL_CFG_F_NONROOT_RECV, |
Julien Gomes | 5f729ea | 2017-06-20 13:54:16 -0700 | [diff] [blame] | 5645 | .bind = rtnetlink_bind, |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 5646 | }; |
| 5647 | |
Pablo Neira Ayuso | 9f00d97 | 2012-09-08 02:53:54 +0000 | [diff] [blame] | 5648 | sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 5649 | if (!sk) |
| 5650 | return -ENOMEM; |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 5651 | net->rtnl = sk; |
| 5652 | return 0; |
| 5653 | } |
| 5654 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 5655 | static void __net_exit rtnetlink_net_exit(struct net *net) |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 5656 | { |
Denis V. Lunev | 775516b | 2008-01-18 23:55:19 -0800 | [diff] [blame] | 5657 | netlink_kernel_release(net->rtnl); |
| 5658 | net->rtnl = NULL; |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 5659 | } |
| 5660 | |
| 5661 | static struct pernet_operations rtnetlink_net_ops = { |
| 5662 | .init = rtnetlink_net_init, |
| 5663 | .exit = rtnetlink_net_exit, |
| 5664 | }; |
| 5665 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5666 | void __init rtnetlink_init(void) |
| 5667 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 5668 | if (register_pernet_subsys(&rtnetlink_net_ops)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5669 | panic("rtnetlink_init: cannot initialize rtnetlink\n"); |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 5670 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5671 | register_netdevice_notifier(&rtnetlink_dev_notifier); |
Thomas Graf | 340d17f | 2007-03-22 11:49:22 -0700 | [diff] [blame] | 5672 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 5673 | rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 5674 | rtnl_dump_ifinfo, 0); |
| 5675 | rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0); |
| 5676 | rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0); |
| 5677 | rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0); |
Thomas Graf | 687ad8c | 2007-03-22 11:59:42 -0700 | [diff] [blame] | 5678 | |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 5679 | rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0); |
| 5680 | rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0); |
| 5681 | rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 5682 | |
Jiri Pirko | 36fbf1e | 2019-09-30 11:48:16 +0200 | [diff] [blame] | 5683 | rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0); |
| 5684 | rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0); |
| 5685 | |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 5686 | rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0); |
| 5687 | rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0); |
Roopa Prabhu | 5b2f94b | 2018-12-15 22:35:08 -0800 | [diff] [blame] | 5688 | rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 5689 | |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 5690 | rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0); |
| 5691 | rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0); |
| 5692 | rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0); |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5693 | |
| 5694 | rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump, |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 5695 | 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5696 | } |