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]); |
Yajun Deng | 4fc2998 | 2021-09-18 14:36:07 +0800 | [diff] [blame] | 304 | RCU_INIT_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 | |
Yajun Deng | 4fc2998 | 2021-09-18 14:36:07 +0800 | [diff] [blame] | 340 | RCU_INIT_POINTER(tab[msgindex], NULL); |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 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) { |
Sebastian Andrzej Siewior | fd888e8 | 2021-11-26 17:15:29 +0100 | [diff] [blame] | 845 | write_lock(&dev_base_lock); |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 846 | dev->operstate = operstate; |
Sebastian Andrzej Siewior | fd888e8 | 2021-11-26 17:15:29 +0100 | [diff] [blame] | 847 | write_unlock(&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 */ |
Coco Li | eac1b93 | 2022-01-05 02:48:38 -0800 | [diff] [blame] | 1029 | + nla_total_size(4) /* IFLA_GRO_MAX_SIZE */ |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 1030 | + nla_total_size(1) /* IFLA_OPERSTATE */ |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1031 | + nla_total_size(1) /* IFLA_LINKMODE */ |
david decotigny | 2d3b479 | 2014-03-29 09:48:35 -0700 | [diff] [blame] | 1032 | + nla_total_size(4) /* IFLA_CARRIER_CHANGES */ |
Nicolas Dichtel | d37512a | 2015-01-15 15:11:16 +0100 | [diff] [blame] | 1033 | + nla_total_size(4) /* IFLA_LINK_NETNSID */ |
Serhey Popovych | db833d4 | 2017-06-20 14:35:23 +0300 | [diff] [blame] | 1034 | + nla_total_size(4) /* IFLA_GROUP */ |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1035 | + nla_total_size(ext_filter_mask |
| 1036 | & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ |
| 1037 | + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 1038 | + 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] | 1039 | + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ |
Arad, Ronen | b1974ed | 2015-10-19 09:23:28 -0700 | [diff] [blame] | 1040 | + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */ |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1041 | + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */ |
Anuradha Karuppiah | 88d6378 | 2015-07-14 13:43:20 -0700 | [diff] [blame] | 1042 | + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */ |
Nicolas Dichtel | c57c7a9 | 2016-03-31 18:10:31 +0200 | [diff] [blame] | 1043 | + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */ |
David S. Miller | b5cdae3 | 2017-04-18 15:36:58 -0400 | [diff] [blame] | 1044 | + rtnl_xdp_size() /* IFLA_XDP */ |
Vlad Yasevich | 3d3ea5a | 2017-05-27 10:14:34 -0400 | [diff] [blame] | 1045 | + nla_total_size(4) /* IFLA_EVENT */ |
Nicolas Dichtel | 6621dd2 | 2017-10-03 13:53:23 +0200 | [diff] [blame] | 1046 | + nla_total_size(4) /* IFLA_NEW_NETNSID */ |
Nicolas Dichtel | 38e01b3 | 2018-01-25 15:01:39 +0100 | [diff] [blame] | 1047 | + nla_total_size(4) /* IFLA_NEW_IFINDEX */ |
Roopa Prabhu | 829eb20 | 2020-07-31 17:34:01 -0700 | [diff] [blame] | 1048 | + rtnl_proto_down_size(dev) /* proto down */ |
Christian Brauner | 7e4a8d5 | 2018-09-04 21:53:53 +0200 | [diff] [blame] | 1049 | + nla_total_size(4) /* IFLA_TARGET_NETNSID */ |
David Decotigny | b2d3bcf | 2018-01-18 09:59:13 -0800 | [diff] [blame] | 1050 | + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */ |
| 1051 | + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */ |
Stephen Hemminger | 3e7a50c | 2018-07-27 13:43:22 -0700 | [diff] [blame] | 1052 | + nla_total_size(4) /* IFLA_MIN_MTU */ |
| 1053 | + nla_total_size(4) /* IFLA_MAX_MTU */ |
Jiri Pirko | 88f4fb0 | 2019-09-30 11:48:17 +0200 | [diff] [blame] | 1054 | + rtnl_prop_list_size(dev) |
Michal Kubecek | f74877a | 2019-12-11 10:58:14 +0100 | [diff] [blame] | 1055 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */ |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 1056 | + 0; |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 1057 | } |
| 1058 | |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1059 | static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) |
| 1060 | { |
| 1061 | struct nlattr *vf_ports; |
| 1062 | struct nlattr *vf_port; |
| 1063 | int vf; |
| 1064 | int err; |
| 1065 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1066 | vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS); |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1067 | if (!vf_ports) |
| 1068 | return -EMSGSIZE; |
| 1069 | |
| 1070 | for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1071 | vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT); |
Scott Feldman | 8ca9418 | 2010-05-28 03:42:18 -0700 | [diff] [blame] | 1072 | if (!vf_port) |
| 1073 | goto nla_put_failure; |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1074 | if (nla_put_u32(skb, IFLA_PORT_VF, vf)) |
| 1075 | goto nla_put_failure; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1076 | err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); |
Scott Feldman | 8ca9418 | 2010-05-28 03:42:18 -0700 | [diff] [blame] | 1077 | if (err == -EMSGSIZE) |
| 1078 | goto nla_put_failure; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1079 | if (err) { |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1080 | nla_nest_cancel(skb, vf_port); |
| 1081 | continue; |
| 1082 | } |
| 1083 | nla_nest_end(skb, vf_port); |
| 1084 | } |
| 1085 | |
| 1086 | nla_nest_end(skb, vf_ports); |
| 1087 | |
| 1088 | return 0; |
Scott Feldman | 8ca9418 | 2010-05-28 03:42:18 -0700 | [diff] [blame] | 1089 | |
| 1090 | nla_put_failure: |
| 1091 | nla_nest_cancel(skb, vf_ports); |
| 1092 | return -EMSGSIZE; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) |
| 1096 | { |
| 1097 | struct nlattr *port_self; |
| 1098 | int err; |
| 1099 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1100 | port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF); |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1101 | if (!port_self) |
| 1102 | return -EMSGSIZE; |
| 1103 | |
| 1104 | err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); |
| 1105 | if (err) { |
| 1106 | nla_nest_cancel(skb, port_self); |
Scott Feldman | 8ca9418 | 2010-05-28 03:42:18 -0700 | [diff] [blame] | 1107 | return (err == -EMSGSIZE) ? err : 0; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | nla_nest_end(skb, port_self); |
| 1111 | |
| 1112 | return 0; |
| 1113 | } |
| 1114 | |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 1115 | static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, |
| 1116 | u32 ext_filter_mask) |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1117 | { |
| 1118 | int err; |
| 1119 | |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 1120 | if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || |
| 1121 | !(ext_filter_mask & RTEXT_FILTER_VF)) |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1122 | return 0; |
| 1123 | |
| 1124 | err = rtnl_port_self_fill(skb, dev); |
| 1125 | if (err) |
| 1126 | return err; |
| 1127 | |
| 1128 | if (dev_num_vf(dev->dev.parent)) { |
| 1129 | err = rtnl_vf_ports_fill(skb, dev); |
| 1130 | if (err) |
| 1131 | return err; |
| 1132 | } |
| 1133 | |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
Jiri Pirko | 66cae9e | 2013-07-29 18:16:50 +0200 | [diff] [blame] | 1137 | static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev) |
| 1138 | { |
| 1139 | int err; |
Jiri Pirko | 02637fc | 2014-11-28 14:34:16 +0100 | [diff] [blame] | 1140 | struct netdev_phys_item_id ppid; |
Jiri Pirko | 66cae9e | 2013-07-29 18:16:50 +0200 | [diff] [blame] | 1141 | |
| 1142 | err = dev_get_phys_port_id(dev, &ppid); |
| 1143 | if (err) { |
| 1144 | if (err == -EOPNOTSUPP) |
| 1145 | return 0; |
| 1146 | return err; |
| 1147 | } |
| 1148 | |
| 1149 | if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id)) |
| 1150 | return -EMSGSIZE; |
| 1151 | |
| 1152 | return 0; |
| 1153 | } |
| 1154 | |
David Ahern | db24a90 | 2015-03-17 20:23:15 -0600 | [diff] [blame] | 1155 | static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev) |
| 1156 | { |
| 1157 | char name[IFNAMSIZ]; |
| 1158 | int err; |
| 1159 | |
| 1160 | err = dev_get_phys_port_name(dev, name, sizeof(name)); |
| 1161 | if (err) { |
| 1162 | if (err == -EOPNOTSUPP) |
| 1163 | return 0; |
| 1164 | return err; |
| 1165 | } |
| 1166 | |
Michal Schmidt | 77ef033 | 2017-05-04 16:48:58 +0200 | [diff] [blame] | 1167 | if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name)) |
David Ahern | db24a90 | 2015-03-17 20:23:15 -0600 | [diff] [blame] | 1168 | return -EMSGSIZE; |
| 1169 | |
| 1170 | return 0; |
| 1171 | } |
| 1172 | |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1173 | static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) |
| 1174 | { |
Florian Fainelli | bccb302 | 2019-02-06 09:45:46 -0800 | [diff] [blame] | 1175 | struct netdev_phys_item_id ppid = { }; |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1176 | int err; |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1177 | |
Florian Fainelli | bccb302 | 2019-02-06 09:45:46 -0800 | [diff] [blame] | 1178 | err = dev_get_port_parent_id(dev, &ppid, false); |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1179 | if (err) { |
| 1180 | if (err == -EOPNOTSUPP) |
| 1181 | return 0; |
| 1182 | return err; |
| 1183 | } |
| 1184 | |
Florian Fainelli | bccb302 | 2019-02-06 09:45:46 -0800 | [diff] [blame] | 1185 | 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] | 1186 | return -EMSGSIZE; |
| 1187 | |
| 1188 | return 0; |
| 1189 | } |
| 1190 | |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1191 | static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb, |
| 1192 | struct net_device *dev) |
| 1193 | { |
Roopa Prabhu | 550bce5 | 2016-04-15 20:36:25 -0700 | [diff] [blame] | 1194 | struct rtnl_link_stats64 *sp; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1195 | struct nlattr *attr; |
| 1196 | |
Nicolas Dichtel | 58414d3 | 2016-04-21 18:58:25 +0200 | [diff] [blame] | 1197 | attr = nla_reserve_64bit(skb, IFLA_STATS64, |
| 1198 | sizeof(struct rtnl_link_stats64), IFLA_PAD); |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1199 | if (!attr) |
| 1200 | return -EMSGSIZE; |
| 1201 | |
Roopa Prabhu | 550bce5 | 2016-04-15 20:36:25 -0700 | [diff] [blame] | 1202 | sp = nla_data(attr); |
| 1203 | dev_get_stats(dev, sp); |
| 1204 | |
| 1205 | attr = nla_reserve(skb, IFLA_STATS, |
| 1206 | sizeof(struct rtnl_link_stats)); |
| 1207 | if (!attr) |
| 1208 | return -EMSGSIZE; |
| 1209 | |
| 1210 | copy_rtnl_link_stats(nla_data(attr), sp); |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1211 | |
| 1212 | return 0; |
| 1213 | } |
| 1214 | |
| 1215 | static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, |
| 1216 | struct net_device *dev, |
| 1217 | int vfs_num, |
| 1218 | struct nlattr *vfinfo) |
| 1219 | { |
| 1220 | struct ifla_vf_rss_query_en vf_rss_query_en; |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 1221 | struct nlattr *vf, *vfstats, *vfvlanlist; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1222 | struct ifla_vf_link_state vf_linkstate; |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 1223 | struct ifla_vf_vlan_info vf_vlan_info; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1224 | struct ifla_vf_spoofchk vf_spoofchk; |
| 1225 | struct ifla_vf_tx_rate vf_tx_rate; |
| 1226 | struct ifla_vf_stats vf_stats; |
| 1227 | struct ifla_vf_trust vf_trust; |
| 1228 | struct ifla_vf_vlan vf_vlan; |
| 1229 | struct ifla_vf_rate vf_rate; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1230 | struct ifla_vf_mac vf_mac; |
Denis Kirjanov | 75345f88 | 2019-06-17 10:53:41 +0200 | [diff] [blame] | 1231 | struct ifla_vf_broadcast vf_broadcast; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1232 | struct ifla_vf_info ivi; |
Danit Goldberg | 30aad41 | 2019-11-06 15:30:07 +0200 | [diff] [blame] | 1233 | struct ifla_vf_guid node_guid; |
| 1234 | struct ifla_vf_guid port_guid; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1235 | |
Mintz, Yuval | 0eed9cf | 2017-06-07 21:00:33 +0300 | [diff] [blame] | 1236 | memset(&ivi, 0, sizeof(ivi)); |
| 1237 | |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1238 | /* Not all SR-IOV capable drivers support the |
| 1239 | * spoofcheck and "RSS query enable" query. Preset to |
| 1240 | * -1 so the user space tool can detect that the driver |
| 1241 | * didn't report anything. |
| 1242 | */ |
| 1243 | ivi.spoofchk = -1; |
| 1244 | ivi.rss_query_en = -1; |
| 1245 | ivi.trusted = -1; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1246 | /* The default value for VF link state is "auto" |
| 1247 | * IFLA_VF_LINK_STATE_AUTO which equals zero |
| 1248 | */ |
| 1249 | ivi.linkstate = 0; |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 1250 | /* VLAN Protocol by default is 802.1Q */ |
| 1251 | ivi.vlan_proto = htons(ETH_P_8021Q); |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1252 | if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi)) |
| 1253 | return 0; |
| 1254 | |
Dan Carpenter | 775f4f0 | 2016-10-13 11:45:28 +0300 | [diff] [blame] | 1255 | memset(&vf_vlan_info, 0, sizeof(vf_vlan_info)); |
Leon Romanovsky | 9fbf082 | 2020-01-30 14:59:49 +0200 | [diff] [blame] | 1256 | memset(&node_guid, 0, sizeof(node_guid)); |
| 1257 | memset(&port_guid, 0, sizeof(port_guid)); |
Dan Carpenter | 775f4f0 | 2016-10-13 11:45:28 +0300 | [diff] [blame] | 1258 | |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1259 | vf_mac.vf = |
| 1260 | vf_vlan.vf = |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 1261 | vf_vlan_info.vf = |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1262 | vf_rate.vf = |
| 1263 | vf_tx_rate.vf = |
| 1264 | vf_spoofchk.vf = |
| 1265 | vf_linkstate.vf = |
| 1266 | vf_rss_query_en.vf = |
Danit Goldberg | 9aed6ae | 2019-12-03 17:43:36 +0200 | [diff] [blame] | 1267 | vf_trust.vf = |
| 1268 | node_guid.vf = |
| 1269 | port_guid.vf = ivi.vf; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1270 | |
| 1271 | memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); |
Denis Kirjanov | 75345f88 | 2019-06-17 10:53:41 +0200 | [diff] [blame] | 1272 | memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len); |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1273 | vf_vlan.vlan = ivi.vlan; |
| 1274 | vf_vlan.qos = ivi.qos; |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 1275 | vf_vlan_info.vlan = ivi.vlan; |
| 1276 | vf_vlan_info.qos = ivi.qos; |
| 1277 | vf_vlan_info.vlan_proto = ivi.vlan_proto; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1278 | vf_tx_rate.rate = ivi.max_tx_rate; |
| 1279 | vf_rate.min_tx_rate = ivi.min_tx_rate; |
| 1280 | vf_rate.max_tx_rate = ivi.max_tx_rate; |
| 1281 | vf_spoofchk.setting = ivi.spoofchk; |
| 1282 | vf_linkstate.link_state = ivi.linkstate; |
| 1283 | vf_rss_query_en.setting = ivi.rss_query_en; |
| 1284 | vf_trust.setting = ivi.trusted; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1285 | vf = nla_nest_start_noflag(skb, IFLA_VF_INFO); |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 1286 | if (!vf) |
| 1287 | goto nla_put_vfinfo_failure; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1288 | if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || |
Denis Kirjanov | 75345f88 | 2019-06-17 10:53:41 +0200 | [diff] [blame] | 1289 | nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) || |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1290 | nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || |
| 1291 | nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), |
| 1292 | &vf_rate) || |
| 1293 | nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), |
| 1294 | &vf_tx_rate) || |
| 1295 | nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), |
| 1296 | &vf_spoofchk) || |
| 1297 | nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), |
| 1298 | &vf_linkstate) || |
| 1299 | nla_put(skb, IFLA_VF_RSS_QUERY_EN, |
| 1300 | sizeof(vf_rss_query_en), |
| 1301 | &vf_rss_query_en) || |
| 1302 | nla_put(skb, IFLA_VF_TRUST, |
| 1303 | sizeof(vf_trust), &vf_trust)) |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 1304 | goto nla_put_vf_failure; |
Danit Goldberg | 30aad41 | 2019-11-06 15:30:07 +0200 | [diff] [blame] | 1305 | |
Danit Goldberg | 30aad41 | 2019-11-06 15:30:07 +0200 | [diff] [blame] | 1306 | if (dev->netdev_ops->ndo_get_vf_guid && |
| 1307 | !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid, |
| 1308 | &port_guid)) { |
| 1309 | if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid), |
| 1310 | &node_guid) || |
| 1311 | nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid), |
| 1312 | &port_guid)) |
| 1313 | goto nla_put_vf_failure; |
| 1314 | } |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1315 | vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST); |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 1316 | if (!vfvlanlist) |
| 1317 | goto nla_put_vf_failure; |
| 1318 | if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info), |
| 1319 | &vf_vlan_info)) { |
| 1320 | nla_nest_cancel(skb, vfvlanlist); |
| 1321 | goto nla_put_vf_failure; |
| 1322 | } |
| 1323 | nla_nest_end(skb, vfvlanlist); |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1324 | memset(&vf_stats, 0, sizeof(vf_stats)); |
| 1325 | if (dev->netdev_ops->ndo_get_vf_stats) |
| 1326 | dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, |
| 1327 | &vf_stats); |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1328 | vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS); |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 1329 | if (!vfstats) |
| 1330 | goto nla_put_vf_failure; |
Nicolas Dichtel | 343a6d8 | 2016-04-25 10:25:14 +0200 | [diff] [blame] | 1331 | if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS, |
| 1332 | vf_stats.rx_packets, IFLA_VF_STATS_PAD) || |
| 1333 | nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS, |
| 1334 | vf_stats.tx_packets, IFLA_VF_STATS_PAD) || |
| 1335 | nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES, |
| 1336 | vf_stats.rx_bytes, IFLA_VF_STATS_PAD) || |
| 1337 | nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES, |
| 1338 | vf_stats.tx_bytes, IFLA_VF_STATS_PAD) || |
| 1339 | nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST, |
| 1340 | vf_stats.broadcast, IFLA_VF_STATS_PAD) || |
| 1341 | nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST, |
Eugenia Emantayev | c5a9f6f | 2017-07-17 13:47:07 +0300 | [diff] [blame] | 1342 | vf_stats.multicast, IFLA_VF_STATS_PAD) || |
| 1343 | nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED, |
| 1344 | vf_stats.rx_dropped, IFLA_VF_STATS_PAD) || |
| 1345 | nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED, |
| 1346 | vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) { |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 1347 | nla_nest_cancel(skb, vfstats); |
| 1348 | goto nla_put_vf_failure; |
| 1349 | } |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1350 | nla_nest_end(skb, vfstats); |
| 1351 | nla_nest_end(skb, vf); |
| 1352 | return 0; |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 1353 | |
| 1354 | nla_put_vf_failure: |
| 1355 | nla_nest_cancel(skb, vf); |
| 1356 | nla_put_vfinfo_failure: |
| 1357 | nla_nest_cancel(skb, vfinfo); |
| 1358 | return -EMSGSIZE; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1359 | } |
| 1360 | |
Florian Westphal | 250fc3d | 2017-09-26 13:58:41 +0200 | [diff] [blame] | 1361 | static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb, |
| 1362 | struct net_device *dev, |
| 1363 | u32 ext_filter_mask) |
| 1364 | { |
| 1365 | struct nlattr *vfinfo; |
| 1366 | int i, num_vfs; |
| 1367 | |
| 1368 | if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0)) |
| 1369 | return 0; |
| 1370 | |
| 1371 | num_vfs = dev_num_vf(dev->dev.parent); |
| 1372 | if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs)) |
| 1373 | return -EMSGSIZE; |
| 1374 | |
| 1375 | if (!dev->netdev_ops->ndo_get_vf_config) |
| 1376 | return 0; |
| 1377 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1378 | vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST); |
Florian Westphal | 250fc3d | 2017-09-26 13:58:41 +0200 | [diff] [blame] | 1379 | if (!vfinfo) |
| 1380 | return -EMSGSIZE; |
| 1381 | |
| 1382 | for (i = 0; i < num_vfs; i++) { |
| 1383 | if (rtnl_fill_vfinfo(skb, dev, i, vfinfo)) |
| 1384 | return -EMSGSIZE; |
| 1385 | } |
| 1386 | |
| 1387 | nla_nest_end(skb, vfinfo); |
| 1388 | return 0; |
| 1389 | } |
| 1390 | |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1391 | static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev) |
| 1392 | { |
Kangjie Lu | 5f8e447 | 2016-05-03 16:46:24 -0400 | [diff] [blame] | 1393 | struct rtnl_link_ifmap map; |
| 1394 | |
| 1395 | memset(&map, 0, sizeof(map)); |
| 1396 | map.mem_start = dev->mem_start; |
| 1397 | map.mem_end = dev->mem_end; |
| 1398 | map.base_addr = dev->base_addr; |
| 1399 | map.irq = dev->irq; |
| 1400 | map.dma = dev->dma; |
| 1401 | map.port = dev->if_port; |
| 1402 | |
Nicolas Dichtel | 270cb4d | 2016-04-26 10:06:16 +0200 | [diff] [blame] | 1403 | 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] | 1404 | return -EMSGSIZE; |
| 1405 | |
| 1406 | return 0; |
| 1407 | } |
| 1408 | |
Jakub Kicinski | a25717d | 2018-07-11 20:36:41 -0700 | [diff] [blame] | 1409 | static u32 rtnl_xdp_prog_skb(struct net_device *dev) |
Daniel Borkmann | d67b9cd | 2017-05-12 01:04:46 +0200 | [diff] [blame] | 1410 | { |
Martin KaFai Lau | 5803869 | 2017-06-15 17:29:09 -0700 | [diff] [blame] | 1411 | const struct bpf_prog *generic_xdp_prog; |
Daniel Borkmann | d67b9cd | 2017-05-12 01:04:46 +0200 | [diff] [blame] | 1412 | |
| 1413 | ASSERT_RTNL(); |
| 1414 | |
Martin KaFai Lau | 5803869 | 2017-06-15 17:29:09 -0700 | [diff] [blame] | 1415 | generic_xdp_prog = rtnl_dereference(dev->xdp_prog); |
Jakub Kicinski | a25717d | 2018-07-11 20:36:41 -0700 | [diff] [blame] | 1416 | if (!generic_xdp_prog) |
| 1417 | return 0; |
| 1418 | return generic_xdp_prog->aux->id; |
| 1419 | } |
Daniel Borkmann | d67b9cd | 2017-05-12 01:04:46 +0200 | [diff] [blame] | 1420 | |
Jakub Kicinski | a25717d | 2018-07-11 20:36:41 -0700 | [diff] [blame] | 1421 | static u32 rtnl_xdp_prog_drv(struct net_device *dev) |
| 1422 | { |
Andrii Nakryiko | 7f0a838 | 2020-07-21 23:45:55 -0700 | [diff] [blame] | 1423 | return dev_xdp_prog_id(dev, XDP_MODE_DRV); |
Jakub Kicinski | a25717d | 2018-07-11 20:36:41 -0700 | [diff] [blame] | 1424 | } |
Jakub Kicinski | 118b4aa | 2017-12-01 15:08:55 -0800 | [diff] [blame] | 1425 | |
Jakub Kicinski | a25717d | 2018-07-11 20:36:41 -0700 | [diff] [blame] | 1426 | static u32 rtnl_xdp_prog_hw(struct net_device *dev) |
| 1427 | { |
Andrii Nakryiko | 7f0a838 | 2020-07-21 23:45:55 -0700 | [diff] [blame] | 1428 | return dev_xdp_prog_id(dev, XDP_MODE_HW); |
Jakub Kicinski | a25717d | 2018-07-11 20:36:41 -0700 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev, |
| 1432 | u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr, |
| 1433 | u32 (*get_prog_id)(struct net_device *dev)) |
| 1434 | { |
| 1435 | u32 curr_id; |
| 1436 | int err; |
| 1437 | |
| 1438 | curr_id = get_prog_id(dev); |
| 1439 | if (!curr_id) |
| 1440 | return 0; |
| 1441 | |
| 1442 | *prog_id = curr_id; |
| 1443 | err = nla_put_u32(skb, attr, curr_id); |
| 1444 | if (err) |
| 1445 | return err; |
| 1446 | |
| 1447 | if (*mode != XDP_ATTACHED_NONE) |
| 1448 | *mode = XDP_ATTACHED_MULTI; |
| 1449 | else |
| 1450 | *mode = tgt_mode; |
| 1451 | |
| 1452 | return 0; |
Daniel Borkmann | d67b9cd | 2017-05-12 01:04:46 +0200 | [diff] [blame] | 1453 | } |
| 1454 | |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1455 | static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev) |
| 1456 | { |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1457 | struct nlattr *xdp; |
Martin KaFai Lau | 5803869 | 2017-06-15 17:29:09 -0700 | [diff] [blame] | 1458 | u32 prog_id; |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1459 | int err; |
Jakub Kicinski | 4f91da2 | 2018-07-11 20:36:38 -0700 | [diff] [blame] | 1460 | u8 mode; |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1461 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1462 | xdp = nla_nest_start_noflag(skb, IFLA_XDP); |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1463 | if (!xdp) |
| 1464 | return -EMSGSIZE; |
David S. Miller | b5cdae3 | 2017-04-18 15:36:58 -0400 | [diff] [blame] | 1465 | |
Jakub Kicinski | a25717d | 2018-07-11 20:36:41 -0700 | [diff] [blame] | 1466 | prog_id = 0; |
| 1467 | mode = XDP_ATTACHED_NONE; |
Jakub Kicinski | 202aabe | 2018-07-16 19:08:50 -0700 | [diff] [blame] | 1468 | err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB, |
| 1469 | IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb); |
| 1470 | if (err) |
Jakub Kicinski | a25717d | 2018-07-11 20:36:41 -0700 | [diff] [blame] | 1471 | goto err_cancel; |
Jakub Kicinski | 202aabe | 2018-07-16 19:08:50 -0700 | [diff] [blame] | 1472 | err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV, |
| 1473 | IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv); |
| 1474 | if (err) |
Jakub Kicinski | a25717d | 2018-07-11 20:36:41 -0700 | [diff] [blame] | 1475 | goto err_cancel; |
Jakub Kicinski | 202aabe | 2018-07-16 19:08:50 -0700 | [diff] [blame] | 1476 | err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW, |
| 1477 | IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw); |
| 1478 | if (err) |
Jakub Kicinski | a25717d | 2018-07-11 20:36:41 -0700 | [diff] [blame] | 1479 | goto err_cancel; |
| 1480 | |
Jakub Kicinski | 4f91da2 | 2018-07-11 20:36:38 -0700 | [diff] [blame] | 1481 | err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode); |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1482 | if (err) |
| 1483 | goto err_cancel; |
| 1484 | |
Jakub Kicinski | a25717d | 2018-07-11 20:36:41 -0700 | [diff] [blame] | 1485 | if (prog_id && mode != XDP_ATTACHED_MULTI) { |
Martin KaFai Lau | 5803869 | 2017-06-15 17:29:09 -0700 | [diff] [blame] | 1486 | err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id); |
| 1487 | if (err) |
| 1488 | goto err_cancel; |
| 1489 | } |
| 1490 | |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1491 | nla_nest_end(skb, xdp); |
| 1492 | return 0; |
| 1493 | |
| 1494 | err_cancel: |
| 1495 | nla_nest_cancel(skb, xdp); |
| 1496 | return err; |
| 1497 | } |
| 1498 | |
Vlad Yasevich | 3d3ea5a | 2017-05-27 10:14:34 -0400 | [diff] [blame] | 1499 | static u32 rtnl_get_event(unsigned long event) |
| 1500 | { |
| 1501 | u32 rtnl_event_type = IFLA_EVENT_NONE; |
| 1502 | |
| 1503 | switch (event) { |
| 1504 | case NETDEV_REBOOT: |
| 1505 | rtnl_event_type = IFLA_EVENT_REBOOT; |
| 1506 | break; |
| 1507 | case NETDEV_FEAT_CHANGE: |
| 1508 | rtnl_event_type = IFLA_EVENT_FEATURES; |
| 1509 | break; |
| 1510 | case NETDEV_BONDING_FAILOVER: |
| 1511 | rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER; |
| 1512 | break; |
| 1513 | case NETDEV_NOTIFY_PEERS: |
| 1514 | rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS; |
| 1515 | break; |
| 1516 | case NETDEV_RESEND_IGMP: |
| 1517 | rtnl_event_type = IFLA_EVENT_IGMP_RESEND; |
| 1518 | break; |
| 1519 | case NETDEV_CHANGEINFODATA: |
| 1520 | rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS; |
| 1521 | break; |
| 1522 | default: |
| 1523 | break; |
| 1524 | } |
| 1525 | |
| 1526 | return rtnl_event_type; |
| 1527 | } |
| 1528 | |
Florian Westphal | 79110a0 | 2017-09-26 13:58:40 +0200 | [diff] [blame] | 1529 | static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev) |
| 1530 | { |
| 1531 | const struct net_device *upper_dev; |
| 1532 | int ret = 0; |
| 1533 | |
| 1534 | rcu_read_lock(); |
| 1535 | |
| 1536 | upper_dev = netdev_master_upper_dev_get_rcu(dev); |
| 1537 | if (upper_dev) |
| 1538 | ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex); |
| 1539 | |
| 1540 | rcu_read_unlock(); |
| 1541 | return ret; |
| 1542 | } |
| 1543 | |
Sabrina Dubroca | feadc4b | 2019-05-14 15:12:19 +0200 | [diff] [blame] | 1544 | static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev, |
| 1545 | bool force) |
Florian Westphal | 79110a0 | 2017-09-26 13:58:40 +0200 | [diff] [blame] | 1546 | { |
| 1547 | int ifindex = dev_get_iflink(dev); |
| 1548 | |
Sabrina Dubroca | feadc4b | 2019-05-14 15:12:19 +0200 | [diff] [blame] | 1549 | if (force || dev->ifindex != ifindex) |
| 1550 | return nla_put_u32(skb, IFLA_LINK, ifindex); |
Florian Westphal | 79110a0 | 2017-09-26 13:58:40 +0200 | [diff] [blame] | 1551 | |
Sabrina Dubroca | feadc4b | 2019-05-14 15:12:19 +0200 | [diff] [blame] | 1552 | return 0; |
Florian Westphal | 79110a0 | 2017-09-26 13:58:40 +0200 | [diff] [blame] | 1553 | } |
| 1554 | |
Florian Westphal | 6c55700 | 2017-10-02 23:50:05 +0200 | [diff] [blame] | 1555 | static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb, |
| 1556 | struct net_device *dev) |
| 1557 | { |
| 1558 | char buf[IFALIASZ]; |
| 1559 | int ret; |
| 1560 | |
| 1561 | ret = dev_get_alias(dev, buf, sizeof(buf)); |
| 1562 | return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0; |
| 1563 | } |
| 1564 | |
Florian Westphal | b1e66b9 | 2017-09-26 13:58:42 +0200 | [diff] [blame] | 1565 | static int rtnl_fill_link_netnsid(struct sk_buff *skb, |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 1566 | const struct net_device *dev, |
Guillaume Nault | d4e4fdf | 2019-10-23 18:39:04 +0200 | [diff] [blame] | 1567 | struct net *src_net, gfp_t gfp) |
Florian Westphal | b1e66b9 | 2017-09-26 13:58:42 +0200 | [diff] [blame] | 1568 | { |
Sabrina Dubroca | feadc4b | 2019-05-14 15:12:19 +0200 | [diff] [blame] | 1569 | bool put_iflink = false; |
| 1570 | |
Florian Westphal | b1e66b9 | 2017-09-26 13:58:42 +0200 | [diff] [blame] | 1571 | if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) { |
| 1572 | struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); |
| 1573 | |
| 1574 | if (!net_eq(dev_net(dev), link_net)) { |
Guillaume Nault | d4e4fdf | 2019-10-23 18:39:04 +0200 | [diff] [blame] | 1575 | int id = peernet2id_alloc(src_net, link_net, gfp); |
Florian Westphal | b1e66b9 | 2017-09-26 13:58:42 +0200 | [diff] [blame] | 1576 | |
| 1577 | if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) |
| 1578 | return -EMSGSIZE; |
Sabrina Dubroca | feadc4b | 2019-05-14 15:12:19 +0200 | [diff] [blame] | 1579 | |
| 1580 | put_iflink = true; |
Florian Westphal | b1e66b9 | 2017-09-26 13:58:42 +0200 | [diff] [blame] | 1581 | } |
| 1582 | } |
| 1583 | |
Sabrina Dubroca | feadc4b | 2019-05-14 15:12:19 +0200 | [diff] [blame] | 1584 | return nla_put_iflink(skb, dev, put_iflink); |
Florian Westphal | b1e66b9 | 2017-09-26 13:58:42 +0200 | [diff] [blame] | 1585 | } |
| 1586 | |
Florian Westphal | 070cbf5b | 2017-10-16 15:44:35 +0200 | [diff] [blame] | 1587 | static int rtnl_fill_link_af(struct sk_buff *skb, |
| 1588 | const struct net_device *dev, |
| 1589 | u32 ext_filter_mask) |
| 1590 | { |
| 1591 | const struct rtnl_af_ops *af_ops; |
| 1592 | struct nlattr *af_spec; |
| 1593 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1594 | af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC); |
Florian Westphal | 070cbf5b | 2017-10-16 15:44:35 +0200 | [diff] [blame] | 1595 | if (!af_spec) |
| 1596 | return -EMSGSIZE; |
| 1597 | |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 1598 | list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { |
Florian Westphal | 070cbf5b | 2017-10-16 15:44:35 +0200 | [diff] [blame] | 1599 | struct nlattr *af; |
| 1600 | int err; |
| 1601 | |
| 1602 | if (!af_ops->fill_link_af) |
| 1603 | continue; |
| 1604 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1605 | af = nla_nest_start_noflag(skb, af_ops->family); |
Florian Westphal | 070cbf5b | 2017-10-16 15:44:35 +0200 | [diff] [blame] | 1606 | if (!af) |
| 1607 | return -EMSGSIZE; |
| 1608 | |
| 1609 | err = af_ops->fill_link_af(skb, dev, ext_filter_mask); |
| 1610 | /* |
| 1611 | * Caller may return ENODATA to indicate that there |
| 1612 | * was no data to be dumped. This is not an error, it |
| 1613 | * means we should trim the attribute header and |
| 1614 | * continue. |
| 1615 | */ |
| 1616 | if (err == -ENODATA) |
| 1617 | nla_nest_cancel(skb, af); |
| 1618 | else if (err < 0) |
| 1619 | return -EMSGSIZE; |
| 1620 | |
| 1621 | nla_nest_end(skb, af); |
| 1622 | } |
| 1623 | |
| 1624 | nla_nest_end(skb, af_spec); |
| 1625 | return 0; |
| 1626 | } |
| 1627 | |
Jiri Pirko | 88f4fb0 | 2019-09-30 11:48:17 +0200 | [diff] [blame] | 1628 | static int rtnl_fill_alt_ifnames(struct sk_buff *skb, |
| 1629 | const struct net_device *dev) |
| 1630 | { |
| 1631 | struct netdev_name_node *name_node; |
| 1632 | int count = 0; |
| 1633 | |
| 1634 | list_for_each_entry(name_node, &dev->name_node->list, list) { |
| 1635 | if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name)) |
| 1636 | return -EMSGSIZE; |
| 1637 | count++; |
| 1638 | } |
| 1639 | return count; |
| 1640 | } |
| 1641 | |
| 1642 | static int rtnl_fill_prop_list(struct sk_buff *skb, |
| 1643 | const struct net_device *dev) |
| 1644 | { |
| 1645 | struct nlattr *prop_list; |
| 1646 | int ret; |
| 1647 | |
| 1648 | prop_list = nla_nest_start(skb, IFLA_PROP_LIST); |
| 1649 | if (!prop_list) |
| 1650 | return -EMSGSIZE; |
| 1651 | |
| 1652 | ret = rtnl_fill_alt_ifnames(skb, dev); |
| 1653 | if (ret <= 0) |
| 1654 | goto nest_cancel; |
| 1655 | |
| 1656 | nla_nest_end(skb, prop_list); |
| 1657 | return 0; |
| 1658 | |
| 1659 | nest_cancel: |
| 1660 | nla_nest_cancel(skb, prop_list); |
| 1661 | return ret; |
| 1662 | } |
| 1663 | |
Roopa Prabhu | 829eb20 | 2020-07-31 17:34:01 -0700 | [diff] [blame] | 1664 | static int rtnl_fill_proto_down(struct sk_buff *skb, |
| 1665 | const struct net_device *dev) |
| 1666 | { |
| 1667 | struct nlattr *pr; |
| 1668 | u32 preason; |
| 1669 | |
| 1670 | if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down)) |
| 1671 | goto nla_put_failure; |
| 1672 | |
| 1673 | preason = dev->proto_down_reason; |
| 1674 | if (!preason) |
| 1675 | return 0; |
| 1676 | |
| 1677 | pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON); |
| 1678 | if (!pr) |
| 1679 | return -EMSGSIZE; |
| 1680 | |
| 1681 | if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) { |
| 1682 | nla_nest_cancel(skb, pr); |
| 1683 | goto nla_put_failure; |
| 1684 | } |
| 1685 | |
| 1686 | nla_nest_end(skb, pr); |
| 1687 | return 0; |
| 1688 | |
| 1689 | nla_put_failure: |
| 1690 | return -EMSGSIZE; |
| 1691 | } |
| 1692 | |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 1693 | static int rtnl_fill_ifinfo(struct sk_buff *skb, |
| 1694 | struct net_device *dev, struct net *src_net, |
Patrick McHardy | 575c3e2 | 2007-05-22 17:00:49 -0700 | [diff] [blame] | 1695 | int type, u32 pid, u32 seq, u32 change, |
Vlad Yasevich | 3d3ea5a | 2017-05-27 10:14:34 -0400 | [diff] [blame] | 1696 | unsigned int flags, u32 ext_filter_mask, |
Nicolas Dichtel | 38e01b3 | 2018-01-25 15:01:39 +0100 | [diff] [blame] | 1697 | u32 event, int *new_nsid, int new_ifindex, |
Guillaume Nault | d4e4fdf | 2019-10-23 18:39:04 +0200 | [diff] [blame] | 1698 | int tgt_netnsid, gfp_t gfp) |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1699 | { |
| 1700 | struct ifinfomsg *ifm; |
| 1701 | struct nlmsghdr *nlh; |
| 1702 | |
Eric Dumazet | 2907c35 | 2011-05-25 07:34:04 +0000 | [diff] [blame] | 1703 | ASSERT_RTNL(); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1704 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); |
| 1705 | if (nlh == NULL) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 1706 | return -EMSGSIZE; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1707 | |
| 1708 | ifm = nlmsg_data(nlh); |
| 1709 | ifm->ifi_family = AF_UNSPEC; |
| 1710 | ifm->__ifi_pad = 0; |
| 1711 | ifm->ifi_type = dev->type; |
| 1712 | ifm->ifi_index = dev->ifindex; |
| 1713 | ifm->ifi_flags = dev_get_flags(dev); |
| 1714 | ifm->ifi_change = change; |
| 1715 | |
Christian Brauner | 7e4a8d5 | 2018-09-04 21:53:53 +0200 | [diff] [blame] | 1716 | 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] | 1717 | goto nla_put_failure; |
| 1718 | |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1719 | if (nla_put_string(skb, IFLA_IFNAME, dev->name) || |
| 1720 | nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || |
| 1721 | nla_put_u8(skb, IFLA_OPERSTATE, |
| 1722 | netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || |
| 1723 | nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || |
| 1724 | nla_put_u32(skb, IFLA_MTU, dev->mtu) || |
Stephen Hemminger | 3e7a50c | 2018-07-27 13:43:22 -0700 | [diff] [blame] | 1725 | nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) || |
| 1726 | nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) || |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1727 | nla_put_u32(skb, IFLA_GROUP, dev->group) || |
Ben Greear | edbc0bb | 2012-03-29 12:51:30 +0000 | [diff] [blame] | 1728 | nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 1729 | nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || |
Eric Dumazet | c70ce02 | 2016-03-21 09:55:10 -0700 | [diff] [blame] | 1730 | nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) || |
| 1731 | nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) || |
Coco Li | eac1b93 | 2022-01-05 02:48:38 -0800 | [diff] [blame] | 1732 | nla_put_u32(skb, IFLA_GRO_MAX_SIZE, dev->gro_max_size) || |
Mark A. Greer | 1d69c2b | 2012-07-20 13:35:13 +0000 | [diff] [blame] | 1733 | #ifdef CONFIG_RPS |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 1734 | 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] | 1735 | #endif |
Florian Westphal | 79110a0 | 2017-09-26 13:58:40 +0200 | [diff] [blame] | 1736 | put_master_ifindex(skb, dev) || |
Jiri Pirko | 9a57247 | 2012-12-27 23:49:39 +0000 | [diff] [blame] | 1737 | nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1738 | (dev->qdisc && |
| 1739 | nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) || |
Florian Westphal | 6c55700 | 2017-10-02 23:50:05 +0200 | [diff] [blame] | 1740 | nla_put_ifalias(skb, dev) || |
david decotigny | 2d3b479 | 2014-03-29 09:48:35 -0700 | [diff] [blame] | 1741 | nla_put_u32(skb, IFLA_CARRIER_CHANGES, |
David Decotigny | b2d3bcf | 2018-01-18 09:59:13 -0800 | [diff] [blame] | 1742 | atomic_read(&dev->carrier_up_count) + |
| 1743 | atomic_read(&dev->carrier_down_count)) || |
David Decotigny | b2d3bcf | 2018-01-18 09:59:13 -0800 | [diff] [blame] | 1744 | nla_put_u32(skb, IFLA_CARRIER_UP_COUNT, |
| 1745 | atomic_read(&dev->carrier_up_count)) || |
| 1746 | nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT, |
| 1747 | atomic_read(&dev->carrier_down_count))) |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1748 | goto nla_put_failure; |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 1749 | |
Roopa Prabhu | 829eb20 | 2020-07-31 17:34:01 -0700 | [diff] [blame] | 1750 | if (rtnl_fill_proto_down(skb, dev)) |
| 1751 | goto nla_put_failure; |
| 1752 | |
Vlad Yasevich | 3d3ea5a | 2017-05-27 10:14:34 -0400 | [diff] [blame] | 1753 | if (event != IFLA_EVENT_NONE) { |
| 1754 | if (nla_put_u32(skb, IFLA_EVENT, event)) |
| 1755 | goto nla_put_failure; |
| 1756 | } |
| 1757 | |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1758 | if (rtnl_fill_link_ifmap(skb, dev)) |
| 1759 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | |
| 1761 | if (dev->addr_len) { |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1762 | if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || |
| 1763 | nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) |
| 1764 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | } |
| 1766 | |
Jiri Pirko | 66cae9e | 2013-07-29 18:16:50 +0200 | [diff] [blame] | 1767 | if (rtnl_phys_port_id_fill(skb, dev)) |
| 1768 | goto nla_put_failure; |
| 1769 | |
David Ahern | db24a90 | 2015-03-17 20:23:15 -0600 | [diff] [blame] | 1770 | if (rtnl_phys_port_name_fill(skb, dev)) |
| 1771 | goto nla_put_failure; |
| 1772 | |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1773 | if (rtnl_phys_switch_id_fill(skb, dev)) |
| 1774 | goto nla_put_failure; |
| 1775 | |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1776 | if (rtnl_fill_stats(skb, dev)) |
Pavel Emelyanov | 96e7408 | 2008-05-21 14:12:46 -0700 | [diff] [blame] | 1777 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | |
Florian Westphal | 250fc3d | 2017-09-26 13:58:41 +0200 | [diff] [blame] | 1779 | if (rtnl_fill_vf(skb, dev, ext_filter_mask)) |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1780 | goto nla_put_failure; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1781 | |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 1782 | if (rtnl_port_fill(skb, dev, ext_filter_mask)) |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1783 | goto nla_put_failure; |
| 1784 | |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1785 | if (rtnl_xdp_fill(skb, dev)) |
| 1786 | goto nla_put_failure; |
| 1787 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 1788 | if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1789 | if (rtnl_link_fill(skb, dev) < 0) |
| 1790 | goto nla_put_failure; |
| 1791 | } |
| 1792 | |
Guillaume Nault | d4e4fdf | 2019-10-23 18:39:04 +0200 | [diff] [blame] | 1793 | if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp)) |
Florian Westphal | b1e66b9 | 2017-09-26 13:58:42 +0200 | [diff] [blame] | 1794 | goto nla_put_failure; |
Nicolas Dichtel | d37512a | 2015-01-15 15:11:16 +0100 | [diff] [blame] | 1795 | |
Nicolas Dichtel | 6621dd2 | 2017-10-03 13:53:23 +0200 | [diff] [blame] | 1796 | if (new_nsid && |
| 1797 | nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0) |
| 1798 | goto nla_put_failure; |
Nicolas Dichtel | 38e01b3 | 2018-01-25 15:01:39 +0100 | [diff] [blame] | 1799 | if (new_ifindex && |
| 1800 | nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0) |
| 1801 | goto nla_put_failure; |
| 1802 | |
Michal Kubecek | f74877a | 2019-12-11 10:58:14 +0100 | [diff] [blame] | 1803 | if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) && |
| 1804 | nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr)) |
| 1805 | goto nla_put_failure; |
Nicolas Dichtel | 6621dd2 | 2017-10-03 13:53:23 +0200 | [diff] [blame] | 1806 | |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 1807 | rcu_read_lock(); |
Florian Westphal | 070cbf5b | 2017-10-16 15:44:35 +0200 | [diff] [blame] | 1808 | if (rtnl_fill_link_af(skb, dev, ext_filter_mask)) |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 1809 | goto nla_put_failure_rcu; |
| 1810 | rcu_read_unlock(); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 1811 | |
Jiri Pirko | 88f4fb0 | 2019-09-30 11:48:17 +0200 | [diff] [blame] | 1812 | if (rtnl_fill_prop_list(skb, dev)) |
| 1813 | goto nla_put_failure; |
| 1814 | |
Johannes Berg | 00e77ed | 2021-06-12 10:20:55 +0200 | [diff] [blame] | 1815 | if (dev->dev.parent && |
| 1816 | nla_put_string(skb, IFLA_PARENT_DEV_NAME, |
| 1817 | dev_name(dev->dev.parent))) |
| 1818 | goto nla_put_failure; |
| 1819 | |
| 1820 | if (dev->dev.parent && dev->dev.parent->bus && |
| 1821 | nla_put_string(skb, IFLA_PARENT_DEV_BUS_NAME, |
| 1822 | dev->dev.parent->bus->name)) |
| 1823 | goto nla_put_failure; |
| 1824 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 1825 | nlmsg_end(skb, nlh); |
| 1826 | return 0; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1827 | |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 1828 | nla_put_failure_rcu: |
| 1829 | rcu_read_unlock(); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1830 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 1831 | nlmsg_cancel(skb, nlh); |
| 1832 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | } |
| 1834 | |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1835 | static const struct nla_policy ifla_policy[IFLA_MAX+1] = { |
| 1836 | [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, |
| 1837 | [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, |
| 1838 | [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, |
| 1839 | [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, |
| 1840 | [IFLA_MTU] = { .type = NLA_U32 }, |
| 1841 | [IFLA_LINK] = { .type = NLA_U32 }, |
| 1842 | [IFLA_MASTER] = { .type = NLA_U32 }, |
| 1843 | [IFLA_CARRIER] = { .type = NLA_U8 }, |
| 1844 | [IFLA_TXQLEN] = { .type = NLA_U32 }, |
| 1845 | [IFLA_WEIGHT] = { .type = NLA_U32 }, |
| 1846 | [IFLA_OPERSTATE] = { .type = NLA_U8 }, |
| 1847 | [IFLA_LINKMODE] = { .type = NLA_U8 }, |
| 1848 | [IFLA_LINKINFO] = { .type = NLA_NESTED }, |
| 1849 | [IFLA_NET_NS_PID] = { .type = NLA_U32 }, |
| 1850 | [IFLA_NET_NS_FD] = { .type = NLA_U32 }, |
Nicolas Dichtel | 2459b4c | 2017-10-11 16:24:48 +0200 | [diff] [blame] | 1851 | /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to |
| 1852 | * allow 0-length string (needed to remove an alias). |
| 1853 | */ |
| 1854 | [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 }, |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1855 | [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, |
| 1856 | [IFLA_VF_PORTS] = { .type = NLA_NESTED }, |
| 1857 | [IFLA_PORT_SELF] = { .type = NLA_NESTED }, |
| 1858 | [IFLA_AF_SPEC] = { .type = NLA_NESTED }, |
| 1859 | [IFLA_EXT_MASK] = { .type = NLA_U32 }, |
| 1860 | [IFLA_PROMISCUITY] = { .type = NLA_U32 }, |
| 1861 | [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, |
| 1862 | [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, |
Stephen Hemminger | 46e6b99 | 2017-12-07 15:40:19 -0800 | [diff] [blame] | 1863 | [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 }, |
| 1864 | [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 }, |
Jiri Pirko | 02637fc | 2014-11-28 14:34:16 +0100 | [diff] [blame] | 1865 | [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] | 1866 | [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1867 | [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] | 1868 | [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, |
Anuradha Karuppiah | 88d6378 | 2015-07-14 13:43:20 -0700 | [diff] [blame] | 1869 | [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1870 | [IFLA_XDP] = { .type = NLA_NESTED }, |
Vlad Yasevich | 3d3ea5a | 2017-05-27 10:14:34 -0400 | [diff] [blame] | 1871 | [IFLA_EVENT] = { .type = NLA_U32 }, |
Serhey Popovych | db833d4 | 2017-06-20 14:35:23 +0300 | [diff] [blame] | 1872 | [IFLA_GROUP] = { .type = NLA_U32 }, |
Christian Brauner | 7e4a8d5 | 2018-09-04 21:53:53 +0200 | [diff] [blame] | 1873 | [IFLA_TARGET_NETNSID] = { .type = NLA_S32 }, |
David Decotigny | b2d3bcf | 2018-01-18 09:59:13 -0800 | [diff] [blame] | 1874 | [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 }, |
| 1875 | [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 }, |
Stephen Hemminger | 3e7a50c | 2018-07-27 13:43:22 -0700 | [diff] [blame] | 1876 | [IFLA_MIN_MTU] = { .type = NLA_U32 }, |
| 1877 | [IFLA_MAX_MTU] = { .type = NLA_U32 }, |
Jiri Pirko | 36fbf1e | 2019-09-30 11:48:16 +0200 | [diff] [blame] | 1878 | [IFLA_PROP_LIST] = { .type = NLA_NESTED }, |
| 1879 | [IFLA_ALT_IFNAME] = { .type = NLA_STRING, |
| 1880 | .len = ALTIFNAMSIZ - 1 }, |
Michal Kubecek | f74877a | 2019-12-11 10:58:14 +0100 | [diff] [blame] | 1881 | [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT }, |
Roopa Prabhu | 829eb20 | 2020-07-31 17:34:01 -0700 | [diff] [blame] | 1882 | [IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED }, |
Andrei Vagin | 7e4a513 | 2021-04-06 23:40:03 -0700 | [diff] [blame] | 1883 | [IFLA_NEW_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1), |
Johannes Berg | 88b7105 | 2021-06-12 10:20:56 +0200 | [diff] [blame] | 1884 | [IFLA_PARENT_DEV_NAME] = { .type = NLA_NUL_STRING }, |
Coco Li | eac1b93 | 2022-01-05 02:48:38 -0800 | [diff] [blame] | 1885 | [IFLA_GRO_MAX_SIZE] = { .type = NLA_U32 }, |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1886 | }; |
| 1887 | |
| 1888 | static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { |
| 1889 | [IFLA_INFO_KIND] = { .type = NLA_STRING }, |
| 1890 | [IFLA_INFO_DATA] = { .type = NLA_NESTED }, |
| 1891 | [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, |
| 1892 | [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, |
| 1893 | }; |
| 1894 | |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1895 | static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { |
Daniel Borkmann | 364d571 | 2015-02-05 18:44:04 +0100 | [diff] [blame] | 1896 | [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, |
Denis Kirjanov | 75345f88 | 2019-06-17 10:53:41 +0200 | [diff] [blame] | 1897 | [IFLA_VF_BROADCAST] = { .type = NLA_REJECT }, |
Daniel Borkmann | 364d571 | 2015-02-05 18:44:04 +0100 | [diff] [blame] | 1898 | [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 1899 | [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED }, |
Daniel Borkmann | 364d571 | 2015-02-05 18:44:04 +0100 | [diff] [blame] | 1900 | [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, |
| 1901 | [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, |
| 1902 | [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, |
| 1903 | [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, |
Vlad Zolotarov | 01a3d79 | 2015-03-30 21:35:23 +0300 | [diff] [blame] | 1904 | [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] | 1905 | [IFLA_VF_STATS] = { .type = NLA_NESTED }, |
Hiroshi Shimamoto | dd461d6 | 2015-08-28 06:57:55 +0000 | [diff] [blame] | 1906 | [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, |
Eli Cohen | cc8e27c | 2016-03-11 22:58:34 +0200 | [diff] [blame] | 1907 | [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) }, |
| 1908 | [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) }, |
Eran Ben Elisha | 3b766cd | 2015-06-15 17:59:07 +0300 | [diff] [blame] | 1909 | }; |
| 1910 | |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1911 | static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { |
| 1912 | [IFLA_PORT_VF] = { .type = NLA_U32 }, |
| 1913 | [IFLA_PORT_PROFILE] = { .type = NLA_STRING, |
| 1914 | .len = PORT_PROFILE_MAX }, |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1915 | [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, |
| 1916 | .len = PORT_UUID_MAX }, |
| 1917 | [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, |
| 1918 | .len = PORT_UUID_MAX }, |
| 1919 | [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, |
| 1920 | [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, |
Daniel Borkmann | 025331d | 2017-02-17 01:56:11 +0100 | [diff] [blame] | 1921 | |
| 1922 | /* Unused, but we need to keep it here since user space could |
| 1923 | * fill it. It's also broken with regard to NLA_BINARY use in |
| 1924 | * combination with structs. |
| 1925 | */ |
| 1926 | [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, |
| 1927 | .len = sizeof(struct ifla_port_vsi) }, |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1928 | }; |
| 1929 | |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1930 | 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] | 1931 | [IFLA_XDP_UNSPEC] = { .strict_start_type = IFLA_XDP_EXPECTED_FD }, |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1932 | [IFLA_XDP_FD] = { .type = NLA_S32 }, |
Toke Høiland-Jørgensen | 92234c8 | 2020-03-25 18:23:26 +0100 | [diff] [blame] | 1933 | [IFLA_XDP_EXPECTED_FD] = { .type = NLA_S32 }, |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1934 | [IFLA_XDP_ATTACHED] = { .type = NLA_U8 }, |
Daniel Borkmann | 85de857 | 2016-11-28 23:16:54 +0100 | [diff] [blame] | 1935 | [IFLA_XDP_FLAGS] = { .type = NLA_U32 }, |
Martin KaFai Lau | 5803869 | 2017-06-15 17:29:09 -0700 | [diff] [blame] | 1936 | [IFLA_XDP_PROG_ID] = { .type = NLA_U32 }, |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 1937 | }; |
| 1938 | |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 1939 | static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla) |
| 1940 | { |
| 1941 | const struct rtnl_link_ops *ops = NULL; |
| 1942 | struct nlattr *linfo[IFLA_INFO_MAX + 1]; |
| 1943 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 1944 | 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] | 1945 | return NULL; |
| 1946 | |
| 1947 | if (linfo[IFLA_INFO_KIND]) { |
| 1948 | char kind[MODULE_NAME_LEN]; |
| 1949 | |
Francis Laniel | 872f690 | 2020-11-15 18:08:06 +0100 | [diff] [blame] | 1950 | nla_strscpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind)); |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 1951 | ops = rtnl_link_ops_get(kind); |
| 1952 | } |
| 1953 | |
| 1954 | return ops; |
| 1955 | } |
| 1956 | |
| 1957 | static bool link_master_filtered(struct net_device *dev, int master_idx) |
| 1958 | { |
| 1959 | struct net_device *master; |
| 1960 | |
| 1961 | if (!master_idx) |
| 1962 | return false; |
| 1963 | |
| 1964 | master = netdev_master_upper_dev_get(dev); |
Lahav Schlesinger | d3432bf | 2021-08-10 09:06:58 +0000 | [diff] [blame] | 1965 | |
| 1966 | /* 0 is already used to denote IFLA_MASTER wasn't passed, therefore need |
| 1967 | * another invalid value for ifindex to denote "no master". |
| 1968 | */ |
| 1969 | if (master_idx == -1) |
| 1970 | return !!master; |
| 1971 | |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 1972 | if (!master || master->ifindex != master_idx) |
| 1973 | return true; |
| 1974 | |
| 1975 | return false; |
| 1976 | } |
| 1977 | |
| 1978 | static bool link_kind_filtered(const struct net_device *dev, |
| 1979 | const struct rtnl_link_ops *kind_ops) |
| 1980 | { |
| 1981 | if (kind_ops && dev->rtnl_link_ops != kind_ops) |
| 1982 | return true; |
| 1983 | |
| 1984 | return false; |
| 1985 | } |
| 1986 | |
| 1987 | static bool link_dump_filtered(struct net_device *dev, |
| 1988 | int master_idx, |
| 1989 | const struct rtnl_link_ops *kind_ops) |
| 1990 | { |
| 1991 | if (link_master_filtered(dev, master_idx) || |
| 1992 | link_kind_filtered(dev, kind_ops)) |
| 1993 | return true; |
| 1994 | |
| 1995 | return false; |
| 1996 | } |
| 1997 | |
Christian Brauner | c383edc | 2018-09-04 21:53:47 +0200 | [diff] [blame] | 1998 | /** |
| 1999 | * rtnl_get_net_ns_capable - Get netns if sufficiently privileged. |
| 2000 | * @sk: netlink socket |
| 2001 | * @netnsid: network namespace identifier |
| 2002 | * |
| 2003 | * Returns the network namespace identified by netnsid on success or an error |
| 2004 | * pointer on failure. |
| 2005 | */ |
| 2006 | struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid) |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 2007 | { |
| 2008 | struct net *net; |
| 2009 | |
Andrei Vagin | f428fe4 | 2018-01-02 23:27:33 -0800 | [diff] [blame] | 2010 | net = get_net_ns_by_id(sock_net(sk), netnsid); |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 2011 | if (!net) |
| 2012 | return ERR_PTR(-EINVAL); |
| 2013 | |
| 2014 | /* For now, the caller is required to have CAP_NET_ADMIN in |
| 2015 | * the user namespace owning the target net ns. |
| 2016 | */ |
Andrei Vagin | f428fe4 | 2018-01-02 23:27:33 -0800 | [diff] [blame] | 2017 | if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) { |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 2018 | put_net(net); |
| 2019 | return ERR_PTR(-EACCES); |
| 2020 | } |
| 2021 | return net; |
| 2022 | } |
Christian Brauner | c383edc | 2018-09-04 21:53:47 +0200 | [diff] [blame] | 2023 | EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable); |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 2024 | |
David Ahern | 905cf0a | 2018-10-07 20:16:30 -0700 | [diff] [blame] | 2025 | static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh, |
| 2026 | bool strict_check, struct nlattr **tb, |
| 2027 | struct netlink_ext_ack *extack) |
| 2028 | { |
| 2029 | int hdrlen; |
| 2030 | |
| 2031 | if (strict_check) { |
| 2032 | struct ifinfomsg *ifm; |
| 2033 | |
| 2034 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { |
| 2035 | NL_SET_ERR_MSG(extack, "Invalid header for link dump"); |
| 2036 | return -EINVAL; |
| 2037 | } |
| 2038 | |
| 2039 | ifm = nlmsg_data(nlh); |
| 2040 | if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || |
| 2041 | ifm->ifi_change) { |
| 2042 | NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request"); |
| 2043 | return -EINVAL; |
| 2044 | } |
| 2045 | if (ifm->ifi_index) { |
| 2046 | NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps"); |
| 2047 | return -EINVAL; |
| 2048 | } |
| 2049 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2050 | return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, |
| 2051 | IFLA_MAX, ifla_policy, |
| 2052 | extack); |
David Ahern | 905cf0a | 2018-10-07 20:16:30 -0700 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | /* A hack to preserve kernel<->userspace interface. |
| 2056 | * The correct header is ifinfomsg. It is consistent with rtnl_getlink. |
| 2057 | * However, before Linux v3.9 the code here assumed rtgenmsg and that's |
| 2058 | * what iproute2 < v3.9.0 used. |
| 2059 | * We can detect the old iproute2. Even including the IFLA_EXT_MASK |
| 2060 | * attribute, its netlink message is shorter than struct ifinfomsg. |
| 2061 | */ |
| 2062 | hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? |
| 2063 | sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); |
| 2064 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2065 | return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, |
| 2066 | extack); |
David Ahern | 905cf0a | 2018-10-07 20:16:30 -0700 | [diff] [blame] | 2067 | } |
| 2068 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2069 | 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] | 2070 | { |
David Ahern | 905cf0a | 2018-10-07 20:16:30 -0700 | [diff] [blame] | 2071 | struct netlink_ext_ack *extack = cb->extack; |
| 2072 | const struct nlmsghdr *nlh = cb->nlh; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2073 | struct net *net = sock_net(skb->sk); |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 2074 | struct net *tgt_net = net; |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 2075 | int h, s_h; |
| 2076 | int idx = 0, s_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | struct net_device *dev; |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 2078 | struct hlist_head *head; |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2079 | struct nlattr *tb[IFLA_MAX+1]; |
| 2080 | u32 ext_filter_mask = 0; |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 2081 | const struct rtnl_link_ops *kind_ops = NULL; |
| 2082 | unsigned int flags = NLM_F_MULTI; |
| 2083 | int master_idx = 0; |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 2084 | int netnsid = -1; |
David Ahern | 905cf0a | 2018-10-07 20:16:30 -0700 | [diff] [blame] | 2085 | int err, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 2087 | s_h = cb->args[0]; |
| 2088 | s_idx = cb->args[1]; |
| 2089 | |
David Ahern | 905cf0a | 2018-10-07 20:16:30 -0700 | [diff] [blame] | 2090 | err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack); |
| 2091 | if (err < 0) { |
| 2092 | if (cb->strict_check) |
| 2093 | return err; |
Michal Schmidt | e5eca6d | 2014-05-28 14:15:19 +0200 | [diff] [blame] | 2094 | |
David Ahern | 905cf0a | 2018-10-07 20:16:30 -0700 | [diff] [blame] | 2095 | goto walk_entries; |
Eric Dumazet | a4b64fb | 2012-03-04 12:32:10 +0000 | [diff] [blame] | 2096 | } |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2097 | |
David Ahern | 905cf0a | 2018-10-07 20:16:30 -0700 | [diff] [blame] | 2098 | for (i = 0; i <= IFLA_MAX; ++i) { |
| 2099 | if (!tb[i]) |
| 2100 | continue; |
| 2101 | |
| 2102 | /* new attributes should only be added with strict checking */ |
| 2103 | switch (i) { |
| 2104 | case IFLA_TARGET_NETNSID: |
| 2105 | netnsid = nla_get_s32(tb[i]); |
| 2106 | tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid); |
| 2107 | if (IS_ERR(tgt_net)) { |
| 2108 | NL_SET_ERR_MSG(extack, "Invalid target network namespace id"); |
| 2109 | return PTR_ERR(tgt_net); |
| 2110 | } |
| 2111 | break; |
| 2112 | case IFLA_EXT_MASK: |
| 2113 | ext_filter_mask = nla_get_u32(tb[i]); |
| 2114 | break; |
| 2115 | case IFLA_MASTER: |
| 2116 | master_idx = nla_get_u32(tb[i]); |
| 2117 | break; |
| 2118 | case IFLA_LINKINFO: |
| 2119 | kind_ops = linkinfo_to_kind_ops(tb[i]); |
| 2120 | break; |
| 2121 | default: |
| 2122 | if (cb->strict_check) { |
| 2123 | NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request"); |
| 2124 | return -EINVAL; |
| 2125 | } |
| 2126 | } |
| 2127 | } |
| 2128 | |
| 2129 | if (master_idx || kind_ops) |
| 2130 | flags |= NLM_F_DUMP_FILTERED; |
| 2131 | |
| 2132 | walk_entries: |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 2133 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { |
| 2134 | idx = 0; |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 2135 | head = &tgt_net->dev_index_head[h]; |
Eric Dumazet | cac5e65 | 2015-02-27 09:42:50 -0800 | [diff] [blame] | 2136 | hlist_for_each_entry(dev, head, index_hlist) { |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 2137 | if (link_dump_filtered(dev, master_idx, kind_ops)) |
Zhang Shengju | 3f0ae05 | 2016-11-19 23:28:32 +0800 | [diff] [blame] | 2138 | goto cont; |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 2139 | if (idx < s_idx) |
| 2140 | goto cont; |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 2141 | err = rtnl_fill_ifinfo(skb, dev, net, |
| 2142 | RTM_NEWLINK, |
David Gibson | 973462b | 2014-04-24 10:22:35 +1000 | [diff] [blame] | 2143 | NETLINK_CB(cb->skb).portid, |
David Ahern | 905cf0a | 2018-10-07 20:16:30 -0700 | [diff] [blame] | 2144 | nlh->nlmsg_seq, 0, flags, |
Nicolas Dichtel | 38e01b3 | 2018-01-25 15:01:39 +0100 | [diff] [blame] | 2145 | ext_filter_mask, 0, NULL, 0, |
Guillaume Nault | d4e4fdf | 2019-10-23 18:39:04 +0200 | [diff] [blame] | 2146 | netnsid, GFP_KERNEL); |
David Gibson | 973462b | 2014-04-24 10:22:35 +1000 | [diff] [blame] | 2147 | |
David Ahern | f6c5775 | 2017-05-15 23:19:17 -0700 | [diff] [blame] | 2148 | if (err < 0) { |
| 2149 | if (likely(skb->len)) |
| 2150 | goto out; |
| 2151 | |
| 2152 | goto out_err; |
| 2153 | } |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 2154 | cont: |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 2155 | idx++; |
| 2156 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | } |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 2158 | out: |
David Ahern | f6c5775 | 2017-05-15 23:19:17 -0700 | [diff] [blame] | 2159 | err = skb->len; |
| 2160 | out_err: |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 2161 | cb->args[1] = idx; |
| 2162 | cb->args[0] = h; |
zhang kai | a9ecb0c | 2021-03-02 18:16:07 +0800 | [diff] [blame] | 2163 | cb->seq = tgt_net->dev_base_seq; |
Jakub Sitnicki | d022578 | 2017-08-09 17:39:12 +0200 | [diff] [blame] | 2164 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 2165 | if (netnsid >= 0) |
| 2166 | put_net(tgt_net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | |
David Ahern | f6c5775 | 2017-05-15 23:19:17 -0700 | [diff] [blame] | 2168 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | } |
| 2170 | |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 2171 | int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len, |
| 2172 | struct netlink_ext_ack *exterr) |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 2173 | { |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2174 | return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy, |
| 2175 | exterr); |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 2176 | } |
| 2177 | EXPORT_SYMBOL(rtnl_nla_parse_ifla); |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 2178 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 2179 | struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) |
| 2180 | { |
| 2181 | struct net *net; |
| 2182 | /* Examine the link attributes and figure out which |
| 2183 | * network namespace we are talking about. |
| 2184 | */ |
| 2185 | if (tb[IFLA_NET_NS_PID]) |
| 2186 | 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] | 2187 | else if (tb[IFLA_NET_NS_FD]) |
| 2188 | 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] | 2189 | else |
| 2190 | net = get_net(src_net); |
| 2191 | return net; |
| 2192 | } |
| 2193 | EXPORT_SYMBOL(rtnl_link_get_net); |
| 2194 | |
Christian Brauner | 7c4f63b | 2018-01-24 15:26:32 +0100 | [diff] [blame] | 2195 | /* Figure out which network namespace we are talking about by |
| 2196 | * examining the link attributes in the following order: |
| 2197 | * |
| 2198 | * 1. IFLA_NET_NS_PID |
| 2199 | * 2. IFLA_NET_NS_FD |
Christian Brauner | 7e4a8d5 | 2018-09-04 21:53:53 +0200 | [diff] [blame] | 2200 | * 3. IFLA_TARGET_NETNSID |
Christian Brauner | 7c4f63b | 2018-01-24 15:26:32 +0100 | [diff] [blame] | 2201 | */ |
| 2202 | static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net, |
| 2203 | struct nlattr *tb[]) |
| 2204 | { |
| 2205 | struct net *net; |
| 2206 | |
| 2207 | if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) |
| 2208 | return rtnl_link_get_net(src_net, tb); |
| 2209 | |
Christian Brauner | 7e4a8d5 | 2018-09-04 21:53:53 +0200 | [diff] [blame] | 2210 | if (!tb[IFLA_TARGET_NETNSID]) |
Christian Brauner | 7c4f63b | 2018-01-24 15:26:32 +0100 | [diff] [blame] | 2211 | return get_net(src_net); |
| 2212 | |
Christian Brauner | 7e4a8d5 | 2018-09-04 21:53:53 +0200 | [diff] [blame] | 2213 | 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] | 2214 | if (!net) |
| 2215 | return ERR_PTR(-EINVAL); |
| 2216 | |
| 2217 | return net; |
| 2218 | } |
| 2219 | |
| 2220 | static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb, |
| 2221 | struct net *src_net, |
| 2222 | struct nlattr *tb[], int cap) |
| 2223 | { |
| 2224 | struct net *net; |
| 2225 | |
| 2226 | net = rtnl_link_get_net_by_nlattr(src_net, tb); |
| 2227 | if (IS_ERR(net)) |
| 2228 | return net; |
| 2229 | |
| 2230 | if (!netlink_ns_capable(skb, net->user_ns, cap)) { |
| 2231 | put_net(net); |
| 2232 | return ERR_PTR(-EPERM); |
| 2233 | } |
| 2234 | |
| 2235 | return net; |
| 2236 | } |
| 2237 | |
Christian Brauner | 4ff66ca | 2018-02-07 13:53:20 +0100 | [diff] [blame] | 2238 | /* Verify that rtnetlink requests do not pass additional properties |
| 2239 | * potentially referring to different network namespaces. |
| 2240 | */ |
| 2241 | static int rtnl_ensure_unique_netns(struct nlattr *tb[], |
| 2242 | struct netlink_ext_ack *extack, |
| 2243 | bool netns_id_only) |
| 2244 | { |
| 2245 | |
| 2246 | if (netns_id_only) { |
| 2247 | if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD]) |
| 2248 | return 0; |
| 2249 | |
| 2250 | NL_SET_ERR_MSG(extack, "specified netns attribute not supported"); |
| 2251 | return -EOPNOTSUPP; |
| 2252 | } |
| 2253 | |
Christian Brauner | 7e4a8d5 | 2018-09-04 21:53:53 +0200 | [diff] [blame] | 2254 | 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] | 2255 | goto invalid_attr; |
| 2256 | |
Christian Brauner | 7e4a8d5 | 2018-09-04 21:53:53 +0200 | [diff] [blame] | 2257 | 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] | 2258 | goto invalid_attr; |
| 2259 | |
Christian Brauner | 7e4a8d5 | 2018-09-04 21:53:53 +0200 | [diff] [blame] | 2260 | 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] | 2261 | goto invalid_attr; |
| 2262 | |
| 2263 | return 0; |
| 2264 | |
| 2265 | invalid_attr: |
| 2266 | NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified"); |
| 2267 | return -EINVAL; |
| 2268 | } |
| 2269 | |
Rocco Yue | 8679c31 | 2021-08-03 20:02:50 +0800 | [diff] [blame] | 2270 | static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[], |
| 2271 | struct netlink_ext_ack *extack) |
Thomas Graf | 1840bb1 | 2008-02-23 19:54:36 -0800 | [diff] [blame] | 2272 | { |
| 2273 | if (dev) { |
| 2274 | if (tb[IFLA_ADDRESS] && |
| 2275 | nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) |
| 2276 | return -EINVAL; |
| 2277 | |
| 2278 | if (tb[IFLA_BROADCAST] && |
| 2279 | nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) |
| 2280 | return -EINVAL; |
| 2281 | } |
| 2282 | |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 2283 | if (tb[IFLA_AF_SPEC]) { |
| 2284 | struct nlattr *af; |
| 2285 | int rem, err; |
| 2286 | |
| 2287 | nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { |
| 2288 | const struct rtnl_af_ops *af_ops; |
| 2289 | |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 2290 | af_ops = rtnl_af_lookup(nla_type(af)); |
Cong Wang | a100243 | 2021-05-08 11:00:33 -0700 | [diff] [blame] | 2291 | if (!af_ops) |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 2292 | return -EAFNOSUPPORT; |
| 2293 | |
Cong Wang | a100243 | 2021-05-08 11:00:33 -0700 | [diff] [blame] | 2294 | if (!af_ops->set_link_af) |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 2295 | return -EOPNOTSUPP; |
| 2296 | |
| 2297 | if (af_ops->validate_link_af) { |
Rocco Yue | 8679c31 | 2021-08-03 20:02:50 +0800 | [diff] [blame] | 2298 | err = af_ops->validate_link_af(dev, af, extack); |
Cong Wang | a100243 | 2021-05-08 11:00:33 -0700 | [diff] [blame] | 2299 | if (err < 0) |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 2300 | return err; |
| 2301 | } |
| 2302 | } |
| 2303 | } |
| 2304 | |
Coco Li | eac1b93 | 2022-01-05 02:48:38 -0800 | [diff] [blame] | 2305 | if (tb[IFLA_GRO_MAX_SIZE]) { |
| 2306 | u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_MAX_SIZE]); |
| 2307 | |
| 2308 | if (gro_max_size > GRO_MAX_SIZE) { |
| 2309 | NL_SET_ERR_MSG(extack, "too big gro_max_size"); |
| 2310 | return -EINVAL; |
| 2311 | } |
| 2312 | } |
Thomas Graf | 1840bb1 | 2008-02-23 19:54:36 -0800 | [diff] [blame] | 2313 | return 0; |
| 2314 | } |
| 2315 | |
Eli Cohen | cc8e27c | 2016-03-11 22:58:34 +0200 | [diff] [blame] | 2316 | static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt, |
| 2317 | int guid_type) |
| 2318 | { |
| 2319 | const struct net_device_ops *ops = dev->netdev_ops; |
| 2320 | |
| 2321 | return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type); |
| 2322 | } |
| 2323 | |
| 2324 | static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type) |
| 2325 | { |
| 2326 | if (dev->type != ARPHRD_INFINIBAND) |
| 2327 | return -EOPNOTSUPP; |
| 2328 | |
| 2329 | return handle_infiniband_guid(dev, ivt, guid_type); |
| 2330 | } |
| 2331 | |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2332 | static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 2333 | { |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 2334 | const struct net_device_ops *ops = dev->netdev_ops; |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2335 | int err = -EINVAL; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 2336 | |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2337 | if (tb[IFLA_VF_MAC]) { |
| 2338 | struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); |
Vlad Zolotarov | 01a3d79 | 2015-03-30 21:35:23 +0300 | [diff] [blame] | 2339 | |
Dan Carpenter | ff08ddb | 2019-11-20 15:34:38 +0300 | [diff] [blame] | 2340 | if (ivm->vf >= INT_MAX) |
| 2341 | return -EINVAL; |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2342 | err = -EOPNOTSUPP; |
| 2343 | if (ops->ndo_set_vf_mac) |
| 2344 | err = ops->ndo_set_vf_mac(dev, ivm->vf, |
| 2345 | ivm->mac); |
| 2346 | if (err < 0) |
| 2347 | return err; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 2348 | } |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2349 | |
| 2350 | if (tb[IFLA_VF_VLAN]) { |
| 2351 | struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); |
| 2352 | |
Dan Carpenter | ff08ddb | 2019-11-20 15:34:38 +0300 | [diff] [blame] | 2353 | if (ivv->vf >= INT_MAX) |
| 2354 | return -EINVAL; |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2355 | err = -EOPNOTSUPP; |
| 2356 | if (ops->ndo_set_vf_vlan) |
| 2357 | err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 2358 | ivv->qos, |
| 2359 | htons(ETH_P_8021Q)); |
| 2360 | if (err < 0) |
| 2361 | return err; |
| 2362 | } |
| 2363 | |
| 2364 | if (tb[IFLA_VF_VLAN_LIST]) { |
| 2365 | struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN]; |
| 2366 | struct nlattr *attr; |
| 2367 | int rem, len = 0; |
| 2368 | |
| 2369 | err = -EOPNOTSUPP; |
| 2370 | if (!ops->ndo_set_vf_vlan) |
| 2371 | return err; |
| 2372 | |
| 2373 | nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) { |
| 2374 | if (nla_type(attr) != IFLA_VF_VLAN_INFO || |
| 2375 | nla_len(attr) < NLA_HDRLEN) { |
| 2376 | return -EINVAL; |
| 2377 | } |
| 2378 | if (len >= MAX_VLAN_LIST_LEN) |
| 2379 | return -EOPNOTSUPP; |
| 2380 | ivvl[len] = nla_data(attr); |
| 2381 | |
| 2382 | len++; |
| 2383 | } |
Arnd Bergmann | fa34cd9 | 2016-09-30 18:13:49 +0200 | [diff] [blame] | 2384 | if (len == 0) |
| 2385 | return -EINVAL; |
| 2386 | |
Dan Carpenter | ff08ddb | 2019-11-20 15:34:38 +0300 | [diff] [blame] | 2387 | if (ivvl[0]->vf >= INT_MAX) |
| 2388 | return -EINVAL; |
Moshe Shemesh | 79aab09 | 2016-09-22 12:11:15 +0300 | [diff] [blame] | 2389 | err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan, |
| 2390 | ivvl[0]->qos, ivvl[0]->vlan_proto); |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2391 | if (err < 0) |
| 2392 | return err; |
| 2393 | } |
| 2394 | |
| 2395 | if (tb[IFLA_VF_TX_RATE]) { |
| 2396 | struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); |
| 2397 | struct ifla_vf_info ivf; |
| 2398 | |
Dan Carpenter | ff08ddb | 2019-11-20 15:34:38 +0300 | [diff] [blame] | 2399 | if (ivt->vf >= INT_MAX) |
| 2400 | return -EINVAL; |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2401 | err = -EOPNOTSUPP; |
| 2402 | if (ops->ndo_get_vf_config) |
| 2403 | err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); |
| 2404 | if (err < 0) |
| 2405 | return err; |
| 2406 | |
| 2407 | err = -EOPNOTSUPP; |
| 2408 | if (ops->ndo_set_vf_rate) |
| 2409 | err = ops->ndo_set_vf_rate(dev, ivt->vf, |
| 2410 | ivf.min_tx_rate, |
| 2411 | ivt->rate); |
| 2412 | if (err < 0) |
| 2413 | return err; |
| 2414 | } |
| 2415 | |
| 2416 | if (tb[IFLA_VF_RATE]) { |
| 2417 | struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); |
| 2418 | |
Dan Carpenter | ff08ddb | 2019-11-20 15:34:38 +0300 | [diff] [blame] | 2419 | if (ivt->vf >= INT_MAX) |
| 2420 | return -EINVAL; |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2421 | err = -EOPNOTSUPP; |
| 2422 | if (ops->ndo_set_vf_rate) |
| 2423 | err = ops->ndo_set_vf_rate(dev, ivt->vf, |
| 2424 | ivt->min_tx_rate, |
| 2425 | ivt->max_tx_rate); |
| 2426 | if (err < 0) |
| 2427 | return err; |
| 2428 | } |
| 2429 | |
| 2430 | if (tb[IFLA_VF_SPOOFCHK]) { |
| 2431 | struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); |
| 2432 | |
Dan Carpenter | ff08ddb | 2019-11-20 15:34:38 +0300 | [diff] [blame] | 2433 | if (ivs->vf >= INT_MAX) |
| 2434 | return -EINVAL; |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2435 | err = -EOPNOTSUPP; |
| 2436 | if (ops->ndo_set_vf_spoofchk) |
| 2437 | err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, |
| 2438 | ivs->setting); |
| 2439 | if (err < 0) |
| 2440 | return err; |
| 2441 | } |
| 2442 | |
| 2443 | if (tb[IFLA_VF_LINK_STATE]) { |
| 2444 | struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); |
| 2445 | |
Dan Carpenter | ff08ddb | 2019-11-20 15:34:38 +0300 | [diff] [blame] | 2446 | if (ivl->vf >= INT_MAX) |
| 2447 | return -EINVAL; |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2448 | err = -EOPNOTSUPP; |
| 2449 | if (ops->ndo_set_vf_link_state) |
| 2450 | err = ops->ndo_set_vf_link_state(dev, ivl->vf, |
| 2451 | ivl->link_state); |
| 2452 | if (err < 0) |
| 2453 | return err; |
| 2454 | } |
| 2455 | |
| 2456 | if (tb[IFLA_VF_RSS_QUERY_EN]) { |
| 2457 | struct ifla_vf_rss_query_en *ivrssq_en; |
| 2458 | |
| 2459 | err = -EOPNOTSUPP; |
| 2460 | ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); |
Dan Carpenter | ff08ddb | 2019-11-20 15:34:38 +0300 | [diff] [blame] | 2461 | if (ivrssq_en->vf >= INT_MAX) |
| 2462 | return -EINVAL; |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2463 | if (ops->ndo_set_vf_rss_query_en) |
| 2464 | err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, |
| 2465 | ivrssq_en->setting); |
| 2466 | if (err < 0) |
| 2467 | return err; |
| 2468 | } |
| 2469 | |
Hiroshi Shimamoto | dd461d6 | 2015-08-28 06:57:55 +0000 | [diff] [blame] | 2470 | if (tb[IFLA_VF_TRUST]) { |
| 2471 | struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); |
| 2472 | |
Dan Carpenter | ff08ddb | 2019-11-20 15:34:38 +0300 | [diff] [blame] | 2473 | if (ivt->vf >= INT_MAX) |
| 2474 | return -EINVAL; |
Hiroshi Shimamoto | dd461d6 | 2015-08-28 06:57:55 +0000 | [diff] [blame] | 2475 | err = -EOPNOTSUPP; |
| 2476 | if (ops->ndo_set_vf_trust) |
| 2477 | err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); |
| 2478 | if (err < 0) |
| 2479 | return err; |
| 2480 | } |
| 2481 | |
Eli Cohen | cc8e27c | 2016-03-11 22:58:34 +0200 | [diff] [blame] | 2482 | if (tb[IFLA_VF_IB_NODE_GUID]) { |
| 2483 | struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]); |
| 2484 | |
Dan Carpenter | ff08ddb | 2019-11-20 15:34:38 +0300 | [diff] [blame] | 2485 | if (ivt->vf >= INT_MAX) |
| 2486 | return -EINVAL; |
Eli Cohen | cc8e27c | 2016-03-11 22:58:34 +0200 | [diff] [blame] | 2487 | if (!ops->ndo_set_vf_guid) |
| 2488 | return -EOPNOTSUPP; |
Eli Cohen | cc8e27c | 2016-03-11 22:58:34 +0200 | [diff] [blame] | 2489 | return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID); |
| 2490 | } |
| 2491 | |
| 2492 | if (tb[IFLA_VF_IB_PORT_GUID]) { |
| 2493 | struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]); |
| 2494 | |
Dan Carpenter | ff08ddb | 2019-11-20 15:34:38 +0300 | [diff] [blame] | 2495 | if (ivt->vf >= INT_MAX) |
| 2496 | return -EINVAL; |
Eli Cohen | cc8e27c | 2016-03-11 22:58:34 +0200 | [diff] [blame] | 2497 | if (!ops->ndo_set_vf_guid) |
| 2498 | return -EOPNOTSUPP; |
| 2499 | |
| 2500 | return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID); |
| 2501 | } |
| 2502 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 2503 | return err; |
| 2504 | } |
| 2505 | |
David Ahern | 33eaf2a | 2017-10-04 17:48:46 -0700 | [diff] [blame] | 2506 | static int do_set_master(struct net_device *dev, int ifindex, |
| 2507 | struct netlink_ext_ack *extack) |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 2508 | { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2509 | struct net_device *upper_dev = netdev_master_upper_dev_get(dev); |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 2510 | const struct net_device_ops *ops; |
| 2511 | int err; |
| 2512 | |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2513 | if (upper_dev) { |
| 2514 | if (upper_dev->ifindex == ifindex) |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 2515 | return 0; |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2516 | ops = upper_dev->netdev_ops; |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 2517 | if (ops->ndo_del_slave) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2518 | err = ops->ndo_del_slave(upper_dev, dev); |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 2519 | if (err) |
| 2520 | return err; |
| 2521 | } else { |
| 2522 | return -EOPNOTSUPP; |
| 2523 | } |
| 2524 | } |
| 2525 | |
| 2526 | if (ifindex) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2527 | upper_dev = __dev_get_by_index(dev_net(dev), ifindex); |
| 2528 | if (!upper_dev) |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 2529 | return -EINVAL; |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2530 | ops = upper_dev->netdev_ops; |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 2531 | if (ops->ndo_add_slave) { |
David Ahern | 33eaf2a | 2017-10-04 17:48:46 -0700 | [diff] [blame] | 2532 | err = ops->ndo_add_slave(upper_dev, dev, extack); |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 2533 | if (err) |
| 2534 | return err; |
| 2535 | } else { |
| 2536 | return -EOPNOTSUPP; |
| 2537 | } |
| 2538 | } |
| 2539 | return 0; |
| 2540 | } |
| 2541 | |
Roopa Prabhu | 829eb20 | 2020-07-31 17:34:01 -0700 | [diff] [blame] | 2542 | static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = { |
| 2543 | [IFLA_PROTO_DOWN_REASON_MASK] = { .type = NLA_U32 }, |
| 2544 | [IFLA_PROTO_DOWN_REASON_VALUE] = { .type = NLA_U32 }, |
| 2545 | }; |
| 2546 | |
| 2547 | static int do_set_proto_down(struct net_device *dev, |
| 2548 | struct nlattr *nl_proto_down, |
| 2549 | struct nlattr *nl_proto_down_reason, |
| 2550 | struct netlink_ext_ack *extack) |
| 2551 | { |
| 2552 | struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1]; |
Roopa Prabhu | 829eb20 | 2020-07-31 17:34:01 -0700 | [diff] [blame] | 2553 | unsigned long mask = 0; |
| 2554 | u32 value; |
| 2555 | bool proto_down; |
| 2556 | int err; |
| 2557 | |
Jakub Kicinski | 2106efd | 2021-11-22 17:24:47 -0800 | [diff] [blame] | 2558 | if (!(dev->priv_flags & IFF_CHANGE_PROTO_DOWN)) { |
Roopa Prabhu | 829eb20 | 2020-07-31 17:34:01 -0700 | [diff] [blame] | 2559 | NL_SET_ERR_MSG(extack, "Protodown not supported by device"); |
| 2560 | return -EOPNOTSUPP; |
| 2561 | } |
| 2562 | |
| 2563 | if (nl_proto_down_reason) { |
| 2564 | err = nla_parse_nested_deprecated(pdreason, |
| 2565 | IFLA_PROTO_DOWN_REASON_MAX, |
| 2566 | nl_proto_down_reason, |
| 2567 | ifla_proto_down_reason_policy, |
| 2568 | NULL); |
| 2569 | if (err < 0) |
| 2570 | return err; |
| 2571 | |
| 2572 | if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) { |
| 2573 | NL_SET_ERR_MSG(extack, "Invalid protodown reason value"); |
| 2574 | return -EINVAL; |
| 2575 | } |
| 2576 | |
| 2577 | value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]); |
| 2578 | |
| 2579 | if (pdreason[IFLA_PROTO_DOWN_REASON_MASK]) |
| 2580 | mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]); |
| 2581 | |
| 2582 | dev_change_proto_down_reason(dev, mask, value); |
| 2583 | } |
| 2584 | |
| 2585 | if (nl_proto_down) { |
| 2586 | proto_down = nla_get_u8(nl_proto_down); |
| 2587 | |
Zheng Yongjun | d467d0b | 2021-06-02 14:56:23 +0800 | [diff] [blame] | 2588 | /* Don't turn off protodown if there are active reasons */ |
Roopa Prabhu | 829eb20 | 2020-07-31 17:34:01 -0700 | [diff] [blame] | 2589 | if (!proto_down && dev->proto_down_reason) { |
| 2590 | NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons"); |
| 2591 | return -EBUSY; |
| 2592 | } |
| 2593 | err = dev_change_proto_down(dev, |
| 2594 | proto_down); |
| 2595 | if (err) |
| 2596 | return err; |
| 2597 | } |
| 2598 | |
| 2599 | return 0; |
| 2600 | } |
| 2601 | |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 2602 | #define DO_SETLINK_MODIFIED 0x01 |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2603 | /* notify flag means notify + modified. */ |
| 2604 | #define DO_SETLINK_NOTIFY 0x03 |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 2605 | static int do_setlink(const struct sk_buff *skb, |
| 2606 | struct net_device *dev, struct ifinfomsg *ifm, |
Jakub Kicinski | ddf9f97 | 2017-04-30 21:46:46 -0700 | [diff] [blame] | 2607 | struct netlink_ext_ack *extack, |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 2608 | struct nlattr **tb, char *ifname, int status) |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2609 | { |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 2610 | const struct net_device_ops *ops = dev->netdev_ops; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2611 | int err; |
| 2612 | |
Rocco Yue | 8679c31 | 2021-08-03 20:02:50 +0800 | [diff] [blame] | 2613 | err = validate_linkmsg(dev, tb, extack); |
Eric Dumazet | 644c7ee | 2018-06-05 09:25:19 -0700 | [diff] [blame] | 2614 | if (err < 0) |
| 2615 | return err; |
| 2616 | |
Christian Brauner | 7e4a8d5 | 2018-09-04 21:53:53 +0200 | [diff] [blame] | 2617 | 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] | 2618 | const char *pat = ifname && ifname[0] ? ifname : NULL; |
Andrei Vagin | eeb85a1 | 2021-04-05 00:12:23 -0700 | [diff] [blame] | 2619 | struct net *net; |
| 2620 | int new_ifindex; |
| 2621 | |
| 2622 | net = rtnl_link_get_net_capable(skb, dev_net(dev), |
| 2623 | tb, CAP_NET_ADMIN); |
Eric W. Biederman | d8a5ec6 | 2007-09-12 13:57:04 +0200 | [diff] [blame] | 2624 | if (IS_ERR(net)) { |
| 2625 | err = PTR_ERR(net); |
| 2626 | goto errout; |
| 2627 | } |
Christian Brauner | 7c4f63b | 2018-01-24 15:26:32 +0100 | [diff] [blame] | 2628 | |
Andrei Vagin | eeb85a1 | 2021-04-05 00:12:23 -0700 | [diff] [blame] | 2629 | if (tb[IFLA_NEW_IFINDEX]) |
| 2630 | new_ifindex = nla_get_s32(tb[IFLA_NEW_IFINDEX]); |
| 2631 | else |
| 2632 | new_ifindex = 0; |
| 2633 | |
Andrey Ignatov | 96a6b93 | 2021-08-25 17:25:40 -0700 | [diff] [blame] | 2634 | err = __dev_change_net_namespace(dev, net, pat, new_ifindex); |
Eric W. Biederman | d8a5ec6 | 2007-09-12 13:57:04 +0200 | [diff] [blame] | 2635 | put_net(net); |
| 2636 | if (err) |
| 2637 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 2638 | status |= DO_SETLINK_MODIFIED; |
Eric W. Biederman | d8a5ec6 | 2007-09-12 13:57:04 +0200 | [diff] [blame] | 2639 | } |
| 2640 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2641 | if (tb[IFLA_MAP]) { |
| 2642 | struct rtnl_link_ifmap *u_map; |
| 2643 | struct ifmap k_map; |
| 2644 | |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 2645 | if (!ops->ndo_set_config) { |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2646 | err = -EOPNOTSUPP; |
| 2647 | goto errout; |
| 2648 | } |
| 2649 | |
| 2650 | if (!netif_device_present(dev)) { |
| 2651 | err = -ENODEV; |
| 2652 | goto errout; |
| 2653 | } |
| 2654 | |
| 2655 | u_map = nla_data(tb[IFLA_MAP]); |
| 2656 | k_map.mem_start = (unsigned long) u_map->mem_start; |
| 2657 | k_map.mem_end = (unsigned long) u_map->mem_end; |
| 2658 | k_map.base_addr = (unsigned short) u_map->base_addr; |
| 2659 | k_map.irq = (unsigned char) u_map->irq; |
| 2660 | k_map.dma = (unsigned char) u_map->dma; |
| 2661 | k_map.port = (unsigned char) u_map->port; |
| 2662 | |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 2663 | err = ops->ndo_set_config(dev, &k_map); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2664 | if (err < 0) |
| 2665 | goto errout; |
| 2666 | |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2667 | status |= DO_SETLINK_NOTIFY; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2668 | } |
| 2669 | |
| 2670 | if (tb[IFLA_ADDRESS]) { |
| 2671 | struct sockaddr *sa; |
| 2672 | int len; |
| 2673 | |
WANG Cong | 153711f | 2017-07-20 11:27:57 -0700 | [diff] [blame] | 2674 | len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len, |
| 2675 | sizeof(*sa)); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2676 | sa = kmalloc(len, GFP_KERNEL); |
| 2677 | if (!sa) { |
| 2678 | err = -ENOMEM; |
| 2679 | goto errout; |
| 2680 | } |
| 2681 | sa->sa_family = dev->type; |
| 2682 | memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), |
| 2683 | dev->addr_len); |
Cong Wang | 3b23a32 | 2021-02-11 11:34:10 -0800 | [diff] [blame] | 2684 | err = dev_set_mac_address_user(dev, sa, extack); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2685 | kfree(sa); |
| 2686 | if (err) |
| 2687 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 2688 | status |= DO_SETLINK_MODIFIED; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2689 | } |
| 2690 | |
| 2691 | if (tb[IFLA_MTU]) { |
Stephen Hemminger | 7a4c53b | 2018-07-27 13:43:23 -0700 | [diff] [blame] | 2692 | 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] | 2693 | if (err < 0) |
| 2694 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 2695 | status |= DO_SETLINK_MODIFIED; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2696 | } |
| 2697 | |
Vlad Dogaru | cbda10f | 2011-01-13 23:38:30 +0000 | [diff] [blame] | 2698 | if (tb[IFLA_GROUP]) { |
| 2699 | dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2700 | status |= DO_SETLINK_NOTIFY; |
Vlad Dogaru | cbda10f | 2011-01-13 23:38:30 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2703 | /* |
| 2704 | * Interface selected by interface index but interface |
| 2705 | * name provided implies that a name change has been |
| 2706 | * requested. |
| 2707 | */ |
| 2708 | if (ifm->ifi_index > 0 && ifname[0]) { |
| 2709 | err = dev_change_name(dev, ifname); |
| 2710 | if (err < 0) |
| 2711 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 2712 | status |= DO_SETLINK_MODIFIED; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2713 | } |
| 2714 | |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 2715 | if (tb[IFLA_IFALIAS]) { |
| 2716 | err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), |
| 2717 | nla_len(tb[IFLA_IFALIAS])); |
| 2718 | if (err < 0) |
| 2719 | goto errout; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2720 | status |= DO_SETLINK_NOTIFY; |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 2721 | } |
| 2722 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2723 | if (tb[IFLA_BROADCAST]) { |
| 2724 | nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); |
Jiri Pirko | e7c3273 | 2013-01-01 03:30:13 +0000 | [diff] [blame] | 2725 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2726 | } |
| 2727 | |
| 2728 | if (ifm->ifi_flags || ifm->ifi_change) { |
Petr Machata | 567c5e1 | 2018-12-06 17:05:42 +0000 | [diff] [blame] | 2729 | err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), |
| 2730 | extack); |
Johannes Berg | 5f9021c | 2008-11-16 23:20:31 -0800 | [diff] [blame] | 2731 | if (err < 0) |
| 2732 | goto errout; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2733 | } |
| 2734 | |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 2735 | if (tb[IFLA_MASTER]) { |
David Ahern | 33eaf2a | 2017-10-04 17:48:46 -0700 | [diff] [blame] | 2736 | err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 2737 | if (err) |
| 2738 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 2739 | status |= DO_SETLINK_MODIFIED; |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 2740 | } |
| 2741 | |
Jiri Pirko | 9a57247 | 2012-12-27 23:49:39 +0000 | [diff] [blame] | 2742 | if (tb[IFLA_CARRIER]) { |
| 2743 | err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); |
| 2744 | if (err) |
| 2745 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 2746 | status |= DO_SETLINK_MODIFIED; |
Jiri Pirko | 9a57247 | 2012-12-27 23:49:39 +0000 | [diff] [blame] | 2747 | } |
| 2748 | |
Nicolas Dichtel | 5d1180f | 2014-09-01 16:07:26 +0200 | [diff] [blame] | 2749 | if (tb[IFLA_TXQLEN]) { |
Alexey Dobriyan | 0cd2950 | 2017-05-17 13:30:44 +0300 | [diff] [blame] | 2750 | unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]); |
Nicolas Dichtel | 5d1180f | 2014-09-01 16:07:26 +0200 | [diff] [blame] | 2751 | |
Cong Wang | 6a643dd | 2018-01-25 18:26:22 -0800 | [diff] [blame] | 2752 | err = dev_change_tx_queue_len(dev, value); |
| 2753 | if (err) |
| 2754 | goto errout; |
| 2755 | status |= DO_SETLINK_MODIFIED; |
Nicolas Dichtel | 5d1180f | 2014-09-01 16:07:26 +0200 | [diff] [blame] | 2756 | } |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2757 | |
Stephen Hemminger | 46e6b99 | 2017-12-07 15:40:19 -0800 | [diff] [blame] | 2758 | if (tb[IFLA_GSO_MAX_SIZE]) { |
| 2759 | u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]); |
| 2760 | |
| 2761 | if (max_size > GSO_MAX_SIZE) { |
| 2762 | err = -EINVAL; |
| 2763 | goto errout; |
| 2764 | } |
| 2765 | |
| 2766 | if (dev->gso_max_size ^ max_size) { |
| 2767 | netif_set_gso_max_size(dev, max_size); |
| 2768 | status |= DO_SETLINK_MODIFIED; |
| 2769 | } |
| 2770 | } |
| 2771 | |
| 2772 | if (tb[IFLA_GSO_MAX_SEGS]) { |
| 2773 | u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); |
| 2774 | |
| 2775 | if (max_segs > GSO_MAX_SEGS) { |
| 2776 | err = -EINVAL; |
| 2777 | goto errout; |
| 2778 | } |
| 2779 | |
| 2780 | if (dev->gso_max_segs ^ max_segs) { |
Eric Dumazet | 6d872df | 2021-11-19 07:43:32 -0800 | [diff] [blame] | 2781 | netif_set_gso_max_segs(dev, max_segs); |
Stephen Hemminger | 46e6b99 | 2017-12-07 15:40:19 -0800 | [diff] [blame] | 2782 | status |= DO_SETLINK_MODIFIED; |
| 2783 | } |
| 2784 | } |
| 2785 | |
Coco Li | eac1b93 | 2022-01-05 02:48:38 -0800 | [diff] [blame] | 2786 | if (tb[IFLA_GRO_MAX_SIZE]) { |
| 2787 | u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_MAX_SIZE]); |
| 2788 | |
| 2789 | if (dev->gro_max_size ^ gro_max_size) { |
| 2790 | netif_set_gro_max_size(dev, gro_max_size); |
| 2791 | status |= DO_SETLINK_MODIFIED; |
| 2792 | } |
| 2793 | } |
| 2794 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2795 | if (tb[IFLA_OPERSTATE]) |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 2796 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2797 | |
| 2798 | if (tb[IFLA_LINKMODE]) { |
Nicolas Dichtel | 1889b0e | 2014-09-01 16:07:27 +0200 | [diff] [blame] | 2799 | unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); |
| 2800 | |
Sebastian Andrzej Siewior | fd888e8 | 2021-11-26 17:15:29 +0100 | [diff] [blame] | 2801 | write_lock(&dev_base_lock); |
Nicolas Dichtel | 1889b0e | 2014-09-01 16:07:27 +0200 | [diff] [blame] | 2802 | if (dev->link_mode ^ value) |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2803 | status |= DO_SETLINK_NOTIFY; |
Nicolas Dichtel | 1889b0e | 2014-09-01 16:07:27 +0200 | [diff] [blame] | 2804 | dev->link_mode = value; |
Sebastian Andrzej Siewior | fd888e8 | 2021-11-26 17:15:29 +0100 | [diff] [blame] | 2805 | write_unlock(&dev_base_lock); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2806 | } |
| 2807 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 2808 | if (tb[IFLA_VFINFO_LIST]) { |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2809 | struct nlattr *vfinfo[IFLA_VF_MAX + 1]; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 2810 | struct nlattr *attr; |
| 2811 | int rem; |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2812 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 2813 | nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2814 | if (nla_type(attr) != IFLA_VF_INFO || |
| 2815 | nla_len(attr) < NLA_HDRLEN) { |
David Howells | 253683b | 2010-05-21 02:25:27 +0000 | [diff] [blame] | 2816 | err = -EINVAL; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 2817 | goto errout; |
David Howells | 253683b | 2010-05-21 02:25:27 +0000 | [diff] [blame] | 2818 | } |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2819 | err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX, |
| 2820 | attr, |
| 2821 | ifla_vf_policy, |
| 2822 | NULL); |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 2823 | if (err < 0) |
| 2824 | goto errout; |
| 2825 | err = do_setvfinfo(dev, vfinfo); |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 2826 | if (err < 0) |
| 2827 | goto errout; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2828 | status |= DO_SETLINK_NOTIFY; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 2829 | } |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 2830 | } |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2831 | err = 0; |
| 2832 | |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 2833 | if (tb[IFLA_VF_PORTS]) { |
| 2834 | struct nlattr *port[IFLA_PORT_MAX+1]; |
| 2835 | struct nlattr *attr; |
| 2836 | int vf; |
| 2837 | int rem; |
| 2838 | |
| 2839 | err = -EOPNOTSUPP; |
| 2840 | if (!ops->ndo_set_vf_port) |
| 2841 | goto errout; |
| 2842 | |
| 2843 | nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { |
Daniel Borkmann | 035d210 | 2015-07-13 00:06:02 +0200 | [diff] [blame] | 2844 | if (nla_type(attr) != IFLA_VF_PORT || |
| 2845 | nla_len(attr) < NLA_HDRLEN) { |
| 2846 | err = -EINVAL; |
| 2847 | goto errout; |
| 2848 | } |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2849 | err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX, |
| 2850 | attr, |
| 2851 | ifla_port_policy, |
| 2852 | NULL); |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 2853 | if (err < 0) |
| 2854 | goto errout; |
| 2855 | if (!port[IFLA_PORT_VF]) { |
| 2856 | err = -EOPNOTSUPP; |
| 2857 | goto errout; |
| 2858 | } |
| 2859 | vf = nla_get_u32(port[IFLA_PORT_VF]); |
| 2860 | err = ops->ndo_set_vf_port(dev, 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 | } |
| 2865 | } |
| 2866 | err = 0; |
| 2867 | |
| 2868 | if (tb[IFLA_PORT_SELF]) { |
| 2869 | struct nlattr *port[IFLA_PORT_MAX+1]; |
| 2870 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2871 | err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX, |
| 2872 | tb[IFLA_PORT_SELF], |
| 2873 | ifla_port_policy, NULL); |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 2874 | if (err < 0) |
| 2875 | goto errout; |
| 2876 | |
| 2877 | err = -EOPNOTSUPP; |
| 2878 | if (ops->ndo_set_vf_port) |
| 2879 | err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); |
| 2880 | if (err < 0) |
| 2881 | goto errout; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2882 | status |= DO_SETLINK_NOTIFY; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 2883 | } |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 2884 | |
| 2885 | if (tb[IFLA_AF_SPEC]) { |
| 2886 | struct nlattr *af; |
| 2887 | int rem; |
| 2888 | |
| 2889 | nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { |
| 2890 | const struct rtnl_af_ops *af_ops; |
| 2891 | |
Gustavo A. R. Silva | 058c8d5 | 2017-10-20 19:43:11 -0500 | [diff] [blame] | 2892 | BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af)))); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 2893 | |
Stephen Hemminger | 3583a4e | 2021-04-07 08:59:12 -0700 | [diff] [blame] | 2894 | err = af_ops->set_link_af(dev, af, extack); |
Cong Wang | a100243 | 2021-05-08 11:00:33 -0700 | [diff] [blame] | 2895 | if (err < 0) |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 2896 | goto errout; |
| 2897 | |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2898 | status |= DO_SETLINK_NOTIFY; |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 2899 | } |
| 2900 | } |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 2901 | err = 0; |
| 2902 | |
Roopa Prabhu | 829eb20 | 2020-07-31 17:34:01 -0700 | [diff] [blame] | 2903 | if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) { |
| 2904 | err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN], |
| 2905 | tb[IFLA_PROTO_DOWN_REASON], extack); |
Anuradha Karuppiah | 88d6378 | 2015-07-14 13:43:20 -0700 | [diff] [blame] | 2906 | if (err) |
| 2907 | goto errout; |
| 2908 | status |= DO_SETLINK_NOTIFY; |
| 2909 | } |
| 2910 | |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 2911 | if (tb[IFLA_XDP]) { |
| 2912 | struct nlattr *xdp[IFLA_XDP_MAX + 1]; |
Daniel Borkmann | 85de857 | 2016-11-28 23:16:54 +0100 | [diff] [blame] | 2913 | u32 xdp_flags = 0; |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 2914 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2915 | err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX, |
| 2916 | tb[IFLA_XDP], |
| 2917 | ifla_xdp_policy, NULL); |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 2918 | if (err < 0) |
| 2919 | goto errout; |
| 2920 | |
Martin KaFai Lau | 5803869 | 2017-06-15 17:29:09 -0700 | [diff] [blame] | 2921 | if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) { |
Brenden Blanco | 262d862 | 2016-07-20 17:22:34 -0700 | [diff] [blame] | 2922 | err = -EINVAL; |
| 2923 | goto errout; |
| 2924 | } |
Daniel Borkmann | 85de857 | 2016-11-28 23:16:54 +0100 | [diff] [blame] | 2925 | |
| 2926 | if (xdp[IFLA_XDP_FLAGS]) { |
| 2927 | xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]); |
| 2928 | if (xdp_flags & ~XDP_FLAGS_MASK) { |
| 2929 | err = -EINVAL; |
| 2930 | goto errout; |
| 2931 | } |
Jakub Kicinski | ee5d032 | 2017-06-21 18:25:04 -0700 | [diff] [blame] | 2932 | if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) { |
Daniel Borkmann | 0489df9 | 2017-05-12 01:04:45 +0200 | [diff] [blame] | 2933 | err = -EINVAL; |
| 2934 | goto errout; |
| 2935 | } |
Daniel Borkmann | 85de857 | 2016-11-28 23:16:54 +0100 | [diff] [blame] | 2936 | } |
| 2937 | |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 2938 | if (xdp[IFLA_XDP_FD]) { |
Toke Høiland-Jørgensen | 92234c8 | 2020-03-25 18:23:26 +0100 | [diff] [blame] | 2939 | int expected_fd = -1; |
| 2940 | |
| 2941 | if (xdp_flags & XDP_FLAGS_REPLACE) { |
| 2942 | if (!xdp[IFLA_XDP_EXPECTED_FD]) { |
| 2943 | err = -EINVAL; |
| 2944 | goto errout; |
| 2945 | } |
| 2946 | expected_fd = |
| 2947 | nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]); |
| 2948 | } |
| 2949 | |
Jakub Kicinski | ddf9f97 | 2017-04-30 21:46:46 -0700 | [diff] [blame] | 2950 | err = dev_change_xdp_fd(dev, extack, |
Daniel Borkmann | 85de857 | 2016-11-28 23:16:54 +0100 | [diff] [blame] | 2951 | nla_get_s32(xdp[IFLA_XDP_FD]), |
Toke Høiland-Jørgensen | 92234c8 | 2020-03-25 18:23:26 +0100 | [diff] [blame] | 2952 | expected_fd, |
Daniel Borkmann | 85de857 | 2016-11-28 23:16:54 +0100 | [diff] [blame] | 2953 | xdp_flags); |
Brenden Blanco | d1fdd91 | 2016-07-19 12:16:49 -0700 | [diff] [blame] | 2954 | if (err) |
| 2955 | goto errout; |
| 2956 | status |= DO_SETLINK_NOTIFY; |
| 2957 | } |
| 2958 | } |
| 2959 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2960 | errout: |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2961 | if (status & DO_SETLINK_MODIFIED) { |
Xin Long | 64ff90c | 2017-10-15 18:13:45 +0800 | [diff] [blame] | 2962 | if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY) |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2963 | netdev_state_change(dev); |
| 2964 | |
| 2965 | if (err < 0) |
| 2966 | 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", |
| 2967 | dev->name); |
| 2968 | } |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2969 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2970 | return err; |
| 2971 | } |
| 2972 | |
Jiri Pirko | cc6090e | 2019-09-30 11:48:19 +0200 | [diff] [blame] | 2973 | static struct net_device *rtnl_dev_get(struct net *net, |
| 2974 | struct nlattr *ifname_attr, |
Jiri Pirko | 76c9ac0 | 2019-09-30 11:48:20 +0200 | [diff] [blame] | 2975 | struct nlattr *altifname_attr, |
Jiri Pirko | cc6090e | 2019-09-30 11:48:19 +0200 | [diff] [blame] | 2976 | char *ifname) |
| 2977 | { |
Jiri Pirko | 76c9ac0 | 2019-09-30 11:48:20 +0200 | [diff] [blame] | 2978 | char buffer[ALTIFNAMSIZ]; |
Jiri Pirko | cc6090e | 2019-09-30 11:48:19 +0200 | [diff] [blame] | 2979 | |
| 2980 | if (!ifname) { |
| 2981 | ifname = buffer; |
| 2982 | if (ifname_attr) |
Francis Laniel | 872f690 | 2020-11-15 18:08:06 +0100 | [diff] [blame] | 2983 | nla_strscpy(ifname, ifname_attr, IFNAMSIZ); |
Jiri Pirko | 76c9ac0 | 2019-09-30 11:48:20 +0200 | [diff] [blame] | 2984 | else if (altifname_attr) |
Francis Laniel | 872f690 | 2020-11-15 18:08:06 +0100 | [diff] [blame] | 2985 | nla_strscpy(ifname, altifname_attr, ALTIFNAMSIZ); |
Jiri Pirko | cc6090e | 2019-09-30 11:48:19 +0200 | [diff] [blame] | 2986 | else |
| 2987 | return NULL; |
| 2988 | } |
| 2989 | |
| 2990 | return __dev_get_by_name(net, ifname); |
| 2991 | } |
| 2992 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 2993 | static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 2994 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2995 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2996 | struct net *net = sock_net(skb->sk); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2997 | struct ifinfomsg *ifm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2998 | struct net_device *dev; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2999 | int err; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 3000 | struct nlattr *tb[IFLA_MAX+1]; |
| 3001 | char ifname[IFNAMSIZ]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3002 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 3003 | err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, |
| 3004 | ifla_policy, extack); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 3005 | if (err < 0) |
| 3006 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3007 | |
Christian Brauner | 4ff66ca | 2018-02-07 13:53:20 +0100 | [diff] [blame] | 3008 | err = rtnl_ensure_unique_netns(tb, extack, false); |
| 3009 | if (err < 0) |
| 3010 | goto errout; |
| 3011 | |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 3012 | if (tb[IFLA_IFNAME]) |
Francis Laniel | 872f690 | 2020-11-15 18:08:06 +0100 | [diff] [blame] | 3013 | nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
Patrick McHardy | 78e5b891 | 2006-09-13 20:35:36 -0700 | [diff] [blame] | 3014 | else |
| 3015 | ifname[0] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3016 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3017 | err = -EINVAL; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 3018 | ifm = nlmsg_data(nlh); |
Patrick McHardy | 51055be | 2007-06-05 12:40:01 -0700 | [diff] [blame] | 3019 | if (ifm->ifi_index > 0) |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 3020 | dev = __dev_get_by_index(net, ifm->ifi_index); |
Jiri Pirko | 76c9ac0 | 2019-09-30 11:48:20 +0200 | [diff] [blame] | 3021 | else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) |
| 3022 | dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 3023 | else |
| 3024 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3025 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 3026 | if (dev == NULL) { |
| 3027 | err = -ENODEV; |
| 3028 | goto errout; |
| 3029 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3030 | |
Jakub Kicinski | ddf9f97 | 2017-04-30 21:46:46 -0700 | [diff] [blame] | 3031 | err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 3032 | errout: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3033 | return err; |
| 3034 | } |
| 3035 | |
WANG Cong | 66400d5 | 2015-03-24 11:53:31 -0700 | [diff] [blame] | 3036 | static int rtnl_group_dellink(const struct net *net, int group) |
| 3037 | { |
| 3038 | struct net_device *dev, *aux; |
| 3039 | LIST_HEAD(list_kill); |
| 3040 | bool found = false; |
| 3041 | |
| 3042 | if (!group) |
| 3043 | return -EPERM; |
| 3044 | |
| 3045 | for_each_netdev(net, dev) { |
| 3046 | if (dev->group == group) { |
| 3047 | const struct rtnl_link_ops *ops; |
| 3048 | |
| 3049 | found = true; |
| 3050 | ops = dev->rtnl_link_ops; |
| 3051 | if (!ops || !ops->dellink) |
| 3052 | return -EOPNOTSUPP; |
| 3053 | } |
| 3054 | } |
| 3055 | |
| 3056 | if (!found) |
| 3057 | return -ENODEV; |
| 3058 | |
| 3059 | for_each_netdev_safe(net, dev, aux) { |
| 3060 | if (dev->group == group) { |
| 3061 | const struct rtnl_link_ops *ops; |
| 3062 | |
| 3063 | ops = dev->rtnl_link_ops; |
| 3064 | ops->dellink(dev, &list_kill); |
| 3065 | } |
| 3066 | } |
| 3067 | unregister_netdevice_many(&list_kill); |
| 3068 | |
| 3069 | return 0; |
| 3070 | } |
| 3071 | |
Thomas Graf | 614732e | 2015-07-21 10:44:06 +0200 | [diff] [blame] | 3072 | int rtnl_delete_link(struct net_device *dev) |
| 3073 | { |
| 3074 | const struct rtnl_link_ops *ops; |
| 3075 | LIST_HEAD(list_kill); |
| 3076 | |
| 3077 | ops = dev->rtnl_link_ops; |
| 3078 | if (!ops || !ops->dellink) |
| 3079 | return -EOPNOTSUPP; |
| 3080 | |
| 3081 | ops->dellink(dev, &list_kill); |
| 3082 | unregister_netdevice_many(&list_kill); |
| 3083 | |
| 3084 | return 0; |
| 3085 | } |
| 3086 | EXPORT_SYMBOL_GPL(rtnl_delete_link); |
| 3087 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 3088 | static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 3089 | struct netlink_ext_ack *extack) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3090 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 3091 | struct net *net = sock_net(skb->sk); |
Christian Brauner | b61ad68 | 2018-01-24 15:26:34 +0100 | [diff] [blame] | 3092 | struct net *tgt_net = net; |
| 3093 | struct net_device *dev = NULL; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3094 | struct ifinfomsg *ifm; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3095 | struct nlattr *tb[IFLA_MAX+1]; |
| 3096 | int err; |
Christian Brauner | b61ad68 | 2018-01-24 15:26:34 +0100 | [diff] [blame] | 3097 | int netnsid = -1; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3098 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 3099 | err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, |
| 3100 | ifla_policy, extack); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3101 | if (err < 0) |
| 3102 | return err; |
| 3103 | |
Christian Brauner | 4ff66ca | 2018-02-07 13:53:20 +0100 | [diff] [blame] | 3104 | err = rtnl_ensure_unique_netns(tb, extack, true); |
| 3105 | if (err < 0) |
| 3106 | return err; |
| 3107 | |
Christian Brauner | 7e4a8d5 | 2018-09-04 21:53:53 +0200 | [diff] [blame] | 3108 | if (tb[IFLA_TARGET_NETNSID]) { |
| 3109 | netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); |
Christian Brauner | c383edc | 2018-09-04 21:53:47 +0200 | [diff] [blame] | 3110 | tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); |
Christian Brauner | b61ad68 | 2018-01-24 15:26:34 +0100 | [diff] [blame] | 3111 | if (IS_ERR(tgt_net)) |
| 3112 | return PTR_ERR(tgt_net); |
| 3113 | } |
| 3114 | |
| 3115 | err = -EINVAL; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3116 | ifm = nlmsg_data(nlh); |
| 3117 | if (ifm->ifi_index > 0) |
Christian Brauner | b61ad68 | 2018-01-24 15:26:34 +0100 | [diff] [blame] | 3118 | dev = __dev_get_by_index(tgt_net, ifm->ifi_index); |
Jiri Pirko | 76c9ac0 | 2019-09-30 11:48:20 +0200 | [diff] [blame] | 3119 | else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) |
| 3120 | dev = rtnl_dev_get(net, tb[IFLA_IFNAME], |
| 3121 | tb[IFLA_ALT_IFNAME], NULL); |
WANG Cong | 66400d5 | 2015-03-24 11:53:31 -0700 | [diff] [blame] | 3122 | else if (tb[IFLA_GROUP]) |
Christian Brauner | b61ad68 | 2018-01-24 15:26:34 +0100 | [diff] [blame] | 3123 | err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP])); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3124 | else |
Christian Brauner | b61ad68 | 2018-01-24 15:26:34 +0100 | [diff] [blame] | 3125 | goto out; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3126 | |
Christian Brauner | b61ad68 | 2018-01-24 15:26:34 +0100 | [diff] [blame] | 3127 | if (!dev) { |
| 3128 | if (tb[IFLA_IFNAME] || ifm->ifi_index > 0) |
| 3129 | err = -ENODEV; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3130 | |
Christian Brauner | b61ad68 | 2018-01-24 15:26:34 +0100 | [diff] [blame] | 3131 | goto out; |
| 3132 | } |
| 3133 | |
| 3134 | err = rtnl_delete_link(dev); |
| 3135 | |
| 3136 | out: |
| 3137 | if (netnsid >= 0) |
| 3138 | put_net(tgt_net); |
| 3139 | |
| 3140 | return err; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3141 | } |
| 3142 | |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 3143 | int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) |
| 3144 | { |
| 3145 | unsigned int old_flags; |
| 3146 | int err; |
| 3147 | |
| 3148 | old_flags = dev->flags; |
| 3149 | if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { |
Petr Machata | 6d04032 | 2018-12-06 17:05:43 +0000 | [diff] [blame] | 3150 | err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), |
| 3151 | NULL); |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 3152 | if (err < 0) |
| 3153 | return err; |
| 3154 | } |
| 3155 | |
Roopa Prabhu | 8d356b8 | 2018-07-04 16:46:29 -0700 | [diff] [blame] | 3156 | if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) { |
Roopa Prabhu | 56a49d7 | 2018-09-12 13:21:48 -0700 | [diff] [blame] | 3157 | __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags)); |
Roopa Prabhu | 8d356b8 | 2018-07-04 16:46:29 -0700 | [diff] [blame] | 3158 | } else { |
| 3159 | dev->rtnl_link_state = RTNL_LINK_INITIALIZED; |
| 3160 | __dev_notify_flags(dev, old_flags, ~0U); |
| 3161 | } |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 3162 | return 0; |
| 3163 | } |
| 3164 | EXPORT_SYMBOL(rtnl_configure_link); |
| 3165 | |
David Ahern | d0522f1 | 2018-11-06 12:51:14 -0800 | [diff] [blame] | 3166 | struct net_device *rtnl_create_link(struct net *net, const char *ifname, |
| 3167 | unsigned char name_assign_type, |
| 3168 | const struct rtnl_link_ops *ops, |
| 3169 | struct nlattr *tb[], |
| 3170 | struct netlink_ext_ack *extack) |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 3171 | { |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 3172 | struct net_device *dev; |
Jiri Pirko | d40156a | 2012-07-20 02:28:47 +0000 | [diff] [blame] | 3173 | unsigned int num_tx_queues = 1; |
| 3174 | unsigned int num_rx_queues = 1; |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 3175 | |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 3176 | if (tb[IFLA_NUM_TX_QUEUES]) |
| 3177 | num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); |
| 3178 | else if (ops->get_num_tx_queues) |
Jiri Pirko | d40156a | 2012-07-20 02:28:47 +0000 | [diff] [blame] | 3179 | num_tx_queues = ops->get_num_tx_queues(); |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 3180 | |
| 3181 | if (tb[IFLA_NUM_RX_QUEUES]) |
| 3182 | num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); |
| 3183 | else if (ops->get_num_rx_queues) |
Jiri Pirko | d40156a | 2012-07-20 02:28:47 +0000 | [diff] [blame] | 3184 | num_rx_queues = ops->get_num_rx_queues(); |
stephen hemminger | efacb30 | 2012-04-10 18:34:43 +0000 | [diff] [blame] | 3185 | |
David Ahern | d0522f1 | 2018-11-06 12:51:14 -0800 | [diff] [blame] | 3186 | if (num_tx_queues < 1 || num_tx_queues > 4096) { |
| 3187 | NL_SET_ERR_MSG(extack, "Invalid number of transmit queues"); |
Eric Dumazet | 0e1d6ec | 2018-10-02 15:47:35 -0700 | [diff] [blame] | 3188 | return ERR_PTR(-EINVAL); |
David Ahern | d0522f1 | 2018-11-06 12:51:14 -0800 | [diff] [blame] | 3189 | } |
Eric Dumazet | 0e1d6ec | 2018-10-02 15:47:35 -0700 | [diff] [blame] | 3190 | |
David Ahern | d0522f1 | 2018-11-06 12:51:14 -0800 | [diff] [blame] | 3191 | if (num_rx_queues < 1 || num_rx_queues > 4096) { |
| 3192 | NL_SET_ERR_MSG(extack, "Invalid number of receive queues"); |
Eric Dumazet | 0e1d6ec | 2018-10-02 15:47:35 -0700 | [diff] [blame] | 3193 | return ERR_PTR(-EINVAL); |
David Ahern | d0522f1 | 2018-11-06 12:51:14 -0800 | [diff] [blame] | 3194 | } |
Eric Dumazet | 0e1d6ec | 2018-10-02 15:47:35 -0700 | [diff] [blame] | 3195 | |
Johannes Berg | 8c713dc | 2021-06-12 10:20:54 +0200 | [diff] [blame] | 3196 | if (ops->alloc) { |
| 3197 | dev = ops->alloc(tb, ifname, name_assign_type, |
| 3198 | num_tx_queues, num_rx_queues); |
| 3199 | if (IS_ERR(dev)) |
| 3200 | return dev; |
| 3201 | } else { |
| 3202 | dev = alloc_netdev_mqs(ops->priv_size, ifname, |
| 3203 | name_assign_type, ops->setup, |
| 3204 | num_tx_queues, num_rx_queues); |
| 3205 | } |
| 3206 | |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 3207 | if (!dev) |
Tobias Klauser | d1892e4 | 2017-02-20 16:32:06 +0100 | [diff] [blame] | 3208 | return ERR_PTR(-ENOMEM); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 3209 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 3210 | dev_net_set(dev, net); |
| 3211 | dev->rtnl_link_ops = ops; |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 3212 | dev->rtnl_link_state = RTNL_LINK_INITIALIZING; |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 3213 | |
Eric Dumazet | d836f5c | 2020-01-21 22:47:29 -0800 | [diff] [blame] | 3214 | if (tb[IFLA_MTU]) { |
| 3215 | u32 mtu = nla_get_u32(tb[IFLA_MTU]); |
| 3216 | int err; |
| 3217 | |
| 3218 | err = dev_validate_mtu(dev, mtu, extack); |
| 3219 | if (err) { |
| 3220 | free_netdev(dev); |
| 3221 | return ERR_PTR(err); |
| 3222 | } |
| 3223 | dev->mtu = mtu; |
| 3224 | } |
Jiri Pirko | 2afb9b5 | 2013-01-06 12:41:57 +0000 | [diff] [blame] | 3225 | if (tb[IFLA_ADDRESS]) { |
Jakub Kicinski | efd38f7 | 2021-10-22 16:20:57 -0700 | [diff] [blame] | 3226 | __dev_addr_set(dev, nla_data(tb[IFLA_ADDRESS]), |
| 3227 | nla_len(tb[IFLA_ADDRESS])); |
Jiri Pirko | 2afb9b5 | 2013-01-06 12:41:57 +0000 | [diff] [blame] | 3228 | dev->addr_assign_type = NET_ADDR_SET; |
| 3229 | } |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 3230 | if (tb[IFLA_BROADCAST]) |
| 3231 | memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), |
| 3232 | nla_len(tb[IFLA_BROADCAST])); |
| 3233 | if (tb[IFLA_TXQLEN]) |
| 3234 | dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); |
| 3235 | if (tb[IFLA_OPERSTATE]) |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 3236 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 3237 | if (tb[IFLA_LINKMODE]) |
| 3238 | dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); |
Patrick McHardy | ffa934f | 2011-01-20 03:00:42 +0000 | [diff] [blame] | 3239 | if (tb[IFLA_GROUP]) |
| 3240 | dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); |
Stephen Hemminger | 46e6b99 | 2017-12-07 15:40:19 -0800 | [diff] [blame] | 3241 | if (tb[IFLA_GSO_MAX_SIZE]) |
| 3242 | netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE])); |
| 3243 | if (tb[IFLA_GSO_MAX_SEGS]) |
Eric Dumazet | 6d872df | 2021-11-19 07:43:32 -0800 | [diff] [blame] | 3244 | netif_set_gso_max_segs(dev, nla_get_u32(tb[IFLA_GSO_MAX_SEGS])); |
Coco Li | eac1b93 | 2022-01-05 02:48:38 -0800 | [diff] [blame] | 3245 | if (tb[IFLA_GRO_MAX_SIZE]) |
| 3246 | netif_set_gro_max_size(dev, nla_get_u32(tb[IFLA_GRO_MAX_SIZE])); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 3247 | |
| 3248 | return dev; |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 3249 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 3250 | EXPORT_SYMBOL(rtnl_create_link); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 3251 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 3252 | static int rtnl_group_changelink(const struct sk_buff *skb, |
| 3253 | struct net *net, int group, |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 3254 | struct ifinfomsg *ifm, |
Jakub Kicinski | ddf9f97 | 2017-04-30 21:46:46 -0700 | [diff] [blame] | 3255 | struct netlink_ext_ack *extack, |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 3256 | struct nlattr **tb) |
| 3257 | { |
WANG Cong | d079535 | 2015-03-23 16:31:09 -0700 | [diff] [blame] | 3258 | struct net_device *dev, *aux; |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 3259 | int err; |
| 3260 | |
WANG Cong | d079535 | 2015-03-23 16:31:09 -0700 | [diff] [blame] | 3261 | for_each_netdev_safe(net, dev, aux) { |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 3262 | if (dev->group == group) { |
Jakub Kicinski | ddf9f97 | 2017-04-30 21:46:46 -0700 | [diff] [blame] | 3263 | err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0); |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 3264 | if (err < 0) |
| 3265 | return err; |
| 3266 | } |
| 3267 | } |
| 3268 | |
| 3269 | return 0; |
| 3270 | } |
| 3271 | |
Jakub Kicinski | a293974 | 2018-11-27 22:32:31 -0800 | [diff] [blame] | 3272 | static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 3273 | struct nlattr **attr, struct netlink_ext_ack *extack) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3274 | { |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3275 | struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1]; |
| 3276 | unsigned char name_assign_type = NET_NAME_USER; |
| 3277 | struct nlattr *linkinfo[IFLA_INFO_MAX + 1]; |
Eric Dumazet | c6f6f24 | 2022-01-31 17:21:06 -0800 | [diff] [blame] | 3278 | const struct rtnl_link_ops *m_ops; |
| 3279 | struct net_device *master_dev; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 3280 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3281 | const struct rtnl_link_ops *ops; |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3282 | struct nlattr *tb[IFLA_MAX + 1]; |
| 3283 | struct net *dest_net, *link_net; |
| 3284 | struct nlattr **slave_data; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3285 | char kind[MODULE_NAME_LEN]; |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3286 | struct net_device *dev; |
| 3287 | struct ifinfomsg *ifm; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3288 | char ifname[IFNAMSIZ]; |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3289 | struct nlattr **data; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3290 | int err; |
| 3291 | |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 3292 | #ifdef CONFIG_MODULES |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3293 | replay: |
Thomas Graf | 8072f08 | 2007-07-31 14:13:50 -0700 | [diff] [blame] | 3294 | #endif |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 3295 | err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, |
| 3296 | ifla_policy, extack); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3297 | if (err < 0) |
| 3298 | return err; |
| 3299 | |
Christian Brauner | 4ff66ca | 2018-02-07 13:53:20 +0100 | [diff] [blame] | 3300 | err = rtnl_ensure_unique_netns(tb, extack, false); |
| 3301 | if (err < 0) |
| 3302 | return err; |
| 3303 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3304 | if (tb[IFLA_IFNAME]) |
Francis Laniel | 872f690 | 2020-11-15 18:08:06 +0100 | [diff] [blame] | 3305 | nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3306 | else |
| 3307 | ifname[0] = '\0'; |
| 3308 | |
| 3309 | ifm = nlmsg_data(nlh); |
| 3310 | if (ifm->ifi_index > 0) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 3311 | dev = __dev_get_by_index(net, ifm->ifi_index); |
Jiri Pirko | 76c9ac0 | 2019-09-30 11:48:20 +0200 | [diff] [blame] | 3312 | else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) |
| 3313 | dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname); |
Jiri Pirko | 7af12cb | 2019-09-30 11:48:18 +0200 | [diff] [blame] | 3314 | else |
| 3315 | dev = NULL; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3316 | |
Eric Dumazet | c6f6f24 | 2022-01-31 17:21:06 -0800 | [diff] [blame] | 3317 | master_dev = NULL; |
| 3318 | m_ops = NULL; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 3319 | if (dev) { |
| 3320 | master_dev = netdev_master_upper_dev_get(dev); |
| 3321 | if (master_dev) |
| 3322 | m_ops = master_dev->rtnl_link_ops; |
| 3323 | } |
| 3324 | |
Rocco Yue | 8679c31 | 2021-08-03 20:02:50 +0800 | [diff] [blame] | 3325 | err = validate_linkmsg(dev, tb, extack); |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 3326 | if (err < 0) |
Thomas Graf | 1840bb1 | 2008-02-23 19:54:36 -0800 | [diff] [blame] | 3327 | return err; |
| 3328 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3329 | if (tb[IFLA_LINKINFO]) { |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 3330 | err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX, |
| 3331 | tb[IFLA_LINKINFO], |
| 3332 | ifla_info_policy, NULL); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3333 | if (err < 0) |
| 3334 | return err; |
| 3335 | } else |
| 3336 | memset(linkinfo, 0, sizeof(linkinfo)); |
| 3337 | |
| 3338 | if (linkinfo[IFLA_INFO_KIND]) { |
Francis Laniel | 872f690 | 2020-11-15 18:08:06 +0100 | [diff] [blame] | 3339 | nla_strscpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3340 | ops = rtnl_link_ops_get(kind); |
| 3341 | } else { |
| 3342 | kind[0] = '\0'; |
| 3343 | ops = NULL; |
| 3344 | } |
| 3345 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3346 | data = NULL; |
| 3347 | if (ops) { |
| 3348 | if (ops->maxtype > RTNL_MAX_TYPE) |
| 3349 | return -EINVAL; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3350 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3351 | if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 3352 | err = nla_parse_nested_deprecated(attr, ops->maxtype, |
| 3353 | linkinfo[IFLA_INFO_DATA], |
| 3354 | ops->policy, extack); |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3355 | if (err < 0) |
| 3356 | return err; |
| 3357 | data = attr; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3358 | } |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3359 | if (ops->validate) { |
| 3360 | err = ops->validate(tb, data, extack); |
| 3361 | if (err < 0) |
| 3362 | return err; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 3363 | } |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3364 | } |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 3365 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3366 | slave_data = NULL; |
| 3367 | if (m_ops) { |
| 3368 | if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE) |
| 3369 | return -EINVAL; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3370 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3371 | if (m_ops->slave_maxtype && |
| 3372 | linkinfo[IFLA_INFO_SLAVE_DATA]) { |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 3373 | err = nla_parse_nested_deprecated(slave_attr, |
| 3374 | m_ops->slave_maxtype, |
| 3375 | linkinfo[IFLA_INFO_SLAVE_DATA], |
| 3376 | m_ops->slave_policy, |
| 3377 | extack); |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3378 | if (err < 0) |
| 3379 | return err; |
| 3380 | slave_data = slave_attr; |
| 3381 | } |
| 3382 | } |
| 3383 | |
| 3384 | if (dev) { |
| 3385 | int status = 0; |
| 3386 | |
| 3387 | if (nlh->nlmsg_flags & NLM_F_EXCL) |
| 3388 | return -EEXIST; |
| 3389 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
| 3390 | return -EOPNOTSUPP; |
| 3391 | |
| 3392 | if (linkinfo[IFLA_INFO_DATA]) { |
| 3393 | if (!ops || ops != dev->rtnl_link_ops || |
| 3394 | !ops->changelink) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3395 | return -EOPNOTSUPP; |
| 3396 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3397 | err = ops->changelink(dev, tb, data, extack); |
| 3398 | if (err < 0) |
| 3399 | return err; |
| 3400 | status |= DO_SETLINK_NOTIFY; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3401 | } |
| 3402 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3403 | if (linkinfo[IFLA_INFO_SLAVE_DATA]) { |
| 3404 | if (!m_ops || !m_ops->slave_changelink) |
| 3405 | return -EOPNOTSUPP; |
| 3406 | |
| 3407 | err = m_ops->slave_changelink(master_dev, dev, tb, |
| 3408 | slave_data, extack); |
| 3409 | if (err < 0) |
| 3410 | return err; |
| 3411 | status |= DO_SETLINK_NOTIFY; |
| 3412 | } |
| 3413 | |
| 3414 | return do_setlink(skb, dev, ifm, extack, tb, ifname, status); |
| 3415 | } |
| 3416 | |
| 3417 | if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { |
| 3418 | if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) |
| 3419 | return rtnl_group_changelink(skb, net, |
Patrick McHardy | ffa934f | 2011-01-20 03:00:42 +0000 | [diff] [blame] | 3420 | nla_get_u32(tb[IFLA_GROUP]), |
Jakub Kicinski | ddf9f97 | 2017-04-30 21:46:46 -0700 | [diff] [blame] | 3421 | ifm, extack, tb); |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3422 | return -ENODEV; |
| 3423 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3424 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3425 | if (tb[IFLA_MAP] || tb[IFLA_PROTINFO]) |
| 3426 | return -EOPNOTSUPP; |
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 | if (!ops) { |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 3429 | #ifdef CONFIG_MODULES |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3430 | if (kind[0]) { |
| 3431 | __rtnl_unlock(); |
| 3432 | request_module("rtnl-link-%s", kind); |
| 3433 | rtnl_lock(); |
| 3434 | ops = rtnl_link_ops_get(kind); |
| 3435 | if (ops) |
| 3436 | goto replay; |
| 3437 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3438 | #endif |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3439 | NL_SET_ERR_MSG(extack, "Unknown device type"); |
| 3440 | return -EOPNOTSUPP; |
| 3441 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3442 | |
Johannes Berg | 8c713dc | 2021-06-12 10:20:54 +0200 | [diff] [blame] | 3443 | if (!ops->alloc && !ops->setup) |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3444 | return -EOPNOTSUPP; |
Jiri Pirko | b0ab2fa | 2014-06-26 09:58:25 +0200 | [diff] [blame] | 3445 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3446 | if (!ifname[0]) { |
| 3447 | snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); |
| 3448 | name_assign_type = NET_NAME_ENUM; |
| 3449 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3450 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3451 | dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN); |
| 3452 | if (IS_ERR(dest_net)) |
| 3453 | return PTR_ERR(dest_net); |
Eric W. Biederman | 13ad177 | 2011-01-29 14:57:22 +0000 | [diff] [blame] | 3454 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3455 | if (tb[IFLA_LINK_NETNSID]) { |
| 3456 | int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); |
Nicolas Dichtel | 317f481 | 2015-01-15 15:11:18 +0100 | [diff] [blame] | 3457 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3458 | link_net = get_net_ns_by_id(dest_net, id); |
| 3459 | if (!link_net) { |
| 3460 | NL_SET_ERR_MSG(extack, "Unknown network namespace id"); |
| 3461 | err = -EINVAL; |
Pavel Emelyanov | 9c7dafb | 2012-08-08 21:52:46 +0000 | [diff] [blame] | 3462 | goto out; |
| 3463 | } |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3464 | err = -EPERM; |
| 3465 | if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) |
| 3466 | goto out; |
| 3467 | } else { |
| 3468 | link_net = NULL; |
| 3469 | } |
Pavel Emelyanov | 9c7dafb | 2012-08-08 21:52:46 +0000 | [diff] [blame] | 3470 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3471 | dev = rtnl_create_link(link_net ? : dest_net, ifname, |
| 3472 | name_assign_type, ops, tb, extack); |
| 3473 | if (IS_ERR(dev)) { |
| 3474 | err = PTR_ERR(dev); |
David S. Miller | 4363890 | 2015-03-10 21:58:32 -0400 | [diff] [blame] | 3475 | goto out; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3476 | } |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3477 | |
| 3478 | dev->ifindex = ifm->ifi_index; |
| 3479 | |
Jakub Kicinski | c269a24 | 2021-01-06 10:40:06 -0800 | [diff] [blame] | 3480 | if (ops->newlink) |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3481 | err = ops->newlink(link_net ? : net, dev, tb, data, extack); |
Jakub Kicinski | c269a24 | 2021-01-06 10:40:06 -0800 | [diff] [blame] | 3482 | else |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3483 | err = register_netdevice(dev); |
Jakub Kicinski | c269a24 | 2021-01-06 10:40:06 -0800 | [diff] [blame] | 3484 | if (err < 0) { |
| 3485 | free_netdev(dev); |
| 3486 | goto out; |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3487 | } |
Jakub Kicinski | c269a24 | 2021-01-06 10:40:06 -0800 | [diff] [blame] | 3488 | |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3489 | err = rtnl_configure_link(dev, ifm); |
| 3490 | if (err < 0) |
| 3491 | goto out_unregister; |
| 3492 | if (link_net) { |
Andrei Vagin | 0854fa8 | 2021-04-06 23:40:51 -0700 | [diff] [blame] | 3493 | err = dev_change_net_namespace(dev, dest_net, ifname); |
Jakub Kicinski | 420d031 | 2018-11-27 22:32:30 -0800 | [diff] [blame] | 3494 | if (err < 0) |
| 3495 | goto out_unregister; |
| 3496 | } |
| 3497 | if (tb[IFLA_MASTER]) { |
| 3498 | err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); |
| 3499 | if (err) |
| 3500 | goto out_unregister; |
| 3501 | } |
| 3502 | out: |
| 3503 | if (link_net) |
| 3504 | put_net(link_net); |
| 3505 | put_net(dest_net); |
| 3506 | return err; |
| 3507 | out_unregister: |
| 3508 | if (ops->newlink) { |
| 3509 | LIST_HEAD(list_kill); |
| 3510 | |
| 3511 | ops->dellink(dev, &list_kill); |
| 3512 | unregister_netdevice_many(&list_kill); |
| 3513 | } else { |
| 3514 | unregister_netdevice(dev); |
| 3515 | } |
| 3516 | goto out; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 3517 | } |
| 3518 | |
Jakub Kicinski | a293974 | 2018-11-27 22:32:31 -0800 | [diff] [blame] | 3519 | static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 3520 | struct netlink_ext_ack *extack) |
| 3521 | { |
| 3522 | struct nlattr **attr; |
| 3523 | int ret; |
| 3524 | |
| 3525 | attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL); |
| 3526 | if (!attr) |
| 3527 | return -ENOMEM; |
| 3528 | |
| 3529 | ret = __rtnl_newlink(skb, nlh, attr, extack); |
| 3530 | kfree(attr); |
| 3531 | return ret; |
| 3532 | } |
| 3533 | |
Jakub Kicinski | 9b3757b | 2019-01-18 10:46:16 -0800 | [diff] [blame] | 3534 | static int rtnl_valid_getlink_req(struct sk_buff *skb, |
| 3535 | const struct nlmsghdr *nlh, |
| 3536 | struct nlattr **tb, |
| 3537 | struct netlink_ext_ack *extack) |
| 3538 | { |
| 3539 | struct ifinfomsg *ifm; |
| 3540 | int i, err; |
| 3541 | |
| 3542 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { |
| 3543 | NL_SET_ERR_MSG(extack, "Invalid header for get link"); |
| 3544 | return -EINVAL; |
| 3545 | } |
| 3546 | |
| 3547 | if (!netlink_strict_get_check(skb)) |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 3548 | return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, |
| 3549 | ifla_policy, extack); |
Jakub Kicinski | 9b3757b | 2019-01-18 10:46:16 -0800 | [diff] [blame] | 3550 | |
| 3551 | ifm = nlmsg_data(nlh); |
| 3552 | if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || |
| 3553 | ifm->ifi_change) { |
| 3554 | NL_SET_ERR_MSG(extack, "Invalid values in header for get link request"); |
| 3555 | return -EINVAL; |
| 3556 | } |
| 3557 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 3558 | err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX, |
| 3559 | ifla_policy, extack); |
Jakub Kicinski | 9b3757b | 2019-01-18 10:46:16 -0800 | [diff] [blame] | 3560 | if (err) |
| 3561 | return err; |
| 3562 | |
| 3563 | for (i = 0; i <= IFLA_MAX; i++) { |
| 3564 | if (!tb[i]) |
| 3565 | continue; |
| 3566 | |
| 3567 | switch (i) { |
| 3568 | case IFLA_IFNAME: |
Jiri Pirko | 76c9ac0 | 2019-09-30 11:48:20 +0200 | [diff] [blame] | 3569 | case IFLA_ALT_IFNAME: |
Jakub Kicinski | 9b3757b | 2019-01-18 10:46:16 -0800 | [diff] [blame] | 3570 | case IFLA_EXT_MASK: |
| 3571 | case IFLA_TARGET_NETNSID: |
| 3572 | break; |
| 3573 | default: |
| 3574 | NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request"); |
| 3575 | return -EINVAL; |
| 3576 | } |
| 3577 | } |
| 3578 | |
| 3579 | return 0; |
| 3580 | } |
| 3581 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 3582 | static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 3583 | struct netlink_ext_ack *extack) |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 3584 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 3585 | struct net *net = sock_net(skb->sk); |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3586 | struct net *tgt_net = net; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 3587 | struct ifinfomsg *ifm; |
| 3588 | struct nlattr *tb[IFLA_MAX+1]; |
| 3589 | struct net_device *dev = NULL; |
| 3590 | struct sk_buff *nskb; |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3591 | int netnsid = -1; |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 3592 | int err; |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 3593 | u32 ext_filter_mask = 0; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 3594 | |
Jakub Kicinski | 9b3757b | 2019-01-18 10:46:16 -0800 | [diff] [blame] | 3595 | err = rtnl_valid_getlink_req(skb, nlh, tb, extack); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 3596 | if (err < 0) |
Eric Sesterhenn | 9918f23 | 2006-09-26 23:26:38 -0700 | [diff] [blame] | 3597 | return err; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 3598 | |
Christian Brauner | 4ff66ca | 2018-02-07 13:53:20 +0100 | [diff] [blame] | 3599 | err = rtnl_ensure_unique_netns(tb, extack, true); |
| 3600 | if (err < 0) |
| 3601 | return err; |
| 3602 | |
Christian Brauner | 7e4a8d5 | 2018-09-04 21:53:53 +0200 | [diff] [blame] | 3603 | if (tb[IFLA_TARGET_NETNSID]) { |
| 3604 | netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); |
Christian Brauner | c383edc | 2018-09-04 21:53:47 +0200 | [diff] [blame] | 3605 | tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3606 | if (IS_ERR(tgt_net)) |
| 3607 | return PTR_ERR(tgt_net); |
| 3608 | } |
| 3609 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 3610 | if (tb[IFLA_EXT_MASK]) |
| 3611 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); |
| 3612 | |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3613 | err = -EINVAL; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 3614 | ifm = nlmsg_data(nlh); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 3615 | if (ifm->ifi_index > 0) |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3616 | dev = __dev_get_by_index(tgt_net, ifm->ifi_index); |
Jiri Pirko | 76c9ac0 | 2019-09-30 11:48:20 +0200 | [diff] [blame] | 3617 | else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) |
| 3618 | dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME], |
| 3619 | tb[IFLA_ALT_IFNAME], NULL); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 3620 | else |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3621 | goto out; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 3622 | |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3623 | err = -ENODEV; |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 3624 | if (dev == NULL) |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3625 | goto out; |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 3626 | |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3627 | err = -ENOBUFS; |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 3628 | nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 3629 | if (nskb == NULL) |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3630 | goto out; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 3631 | |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3632 | err = rtnl_fill_ifinfo(nskb, dev, net, |
| 3633 | RTM_NEWLINK, NETLINK_CB(skb).portid, |
| 3634 | nlh->nlmsg_seq, 0, 0, ext_filter_mask, |
Guillaume Nault | d4e4fdf | 2019-10-23 18:39:04 +0200 | [diff] [blame] | 3635 | 0, NULL, 0, netnsid, GFP_KERNEL); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 3636 | if (err < 0) { |
| 3637 | /* -EMSGSIZE implies BUG in if_nlmsg_size */ |
| 3638 | WARN_ON(err == -EMSGSIZE); |
| 3639 | kfree_skb(nskb); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 3640 | } else |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 3641 | err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3642 | out: |
| 3643 | if (netnsid >= 0) |
| 3644 | put_net(tgt_net); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 3645 | |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 3646 | return err; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 3647 | } |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 3648 | |
Jiri Pirko | 36fbf1e | 2019-09-30 11:48:16 +0200 | [diff] [blame] | 3649 | static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr, |
| 3650 | bool *changed, struct netlink_ext_ack *extack) |
| 3651 | { |
| 3652 | char *alt_ifname; |
| 3653 | int err; |
| 3654 | |
| 3655 | err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack); |
| 3656 | if (err) |
| 3657 | return err; |
| 3658 | |
Eric Dumazet | 44bfa9c | 2020-02-12 20:58:26 -0800 | [diff] [blame] | 3659 | alt_ifname = nla_strdup(attr, GFP_KERNEL); |
| 3660 | if (!alt_ifname) |
| 3661 | return -ENOMEM; |
| 3662 | |
Jiri Pirko | 36fbf1e | 2019-09-30 11:48:16 +0200 | [diff] [blame] | 3663 | if (cmd == RTM_NEWLINKPROP) { |
Jiri Pirko | 36fbf1e | 2019-09-30 11:48:16 +0200 | [diff] [blame] | 3664 | err = netdev_name_node_alt_create(dev, alt_ifname); |
Eric Dumazet | 44bfa9c | 2020-02-12 20:58:26 -0800 | [diff] [blame] | 3665 | if (!err) |
| 3666 | alt_ifname = NULL; |
Jiri Pirko | 36fbf1e | 2019-09-30 11:48:16 +0200 | [diff] [blame] | 3667 | } else if (cmd == RTM_DELLINKPROP) { |
| 3668 | err = netdev_name_node_alt_destroy(dev, alt_ifname); |
Jiri Pirko | 36fbf1e | 2019-09-30 11:48:16 +0200 | [diff] [blame] | 3669 | } else { |
Eric Dumazet | 44bfa9c | 2020-02-12 20:58:26 -0800 | [diff] [blame] | 3670 | WARN_ON_ONCE(1); |
| 3671 | err = -EINVAL; |
Jiri Pirko | 36fbf1e | 2019-09-30 11:48:16 +0200 | [diff] [blame] | 3672 | } |
| 3673 | |
Eric Dumazet | 44bfa9c | 2020-02-12 20:58:26 -0800 | [diff] [blame] | 3674 | kfree(alt_ifname); |
| 3675 | if (!err) |
| 3676 | *changed = true; |
| 3677 | return err; |
Jiri Pirko | 36fbf1e | 2019-09-30 11:48:16 +0200 | [diff] [blame] | 3678 | } |
| 3679 | |
| 3680 | static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh, |
| 3681 | struct netlink_ext_ack *extack) |
| 3682 | { |
| 3683 | struct net *net = sock_net(skb->sk); |
| 3684 | struct nlattr *tb[IFLA_MAX + 1]; |
| 3685 | struct net_device *dev; |
| 3686 | struct ifinfomsg *ifm; |
| 3687 | bool changed = false; |
| 3688 | struct nlattr *attr; |
| 3689 | int err, rem; |
| 3690 | |
| 3691 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); |
| 3692 | if (err) |
| 3693 | return err; |
| 3694 | |
| 3695 | err = rtnl_ensure_unique_netns(tb, extack, true); |
| 3696 | if (err) |
| 3697 | return err; |
| 3698 | |
| 3699 | ifm = nlmsg_data(nlh); |
Jiri Pirko | cc6090e | 2019-09-30 11:48:19 +0200 | [diff] [blame] | 3700 | if (ifm->ifi_index > 0) |
Jiri Pirko | 36fbf1e | 2019-09-30 11:48:16 +0200 | [diff] [blame] | 3701 | dev = __dev_get_by_index(net, ifm->ifi_index); |
Jiri Pirko | 76c9ac0 | 2019-09-30 11:48:20 +0200 | [diff] [blame] | 3702 | else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) |
| 3703 | dev = rtnl_dev_get(net, tb[IFLA_IFNAME], |
| 3704 | tb[IFLA_ALT_IFNAME], NULL); |
Jiri Pirko | cc6090e | 2019-09-30 11:48:19 +0200 | [diff] [blame] | 3705 | else |
Jiri Pirko | 36fbf1e | 2019-09-30 11:48:16 +0200 | [diff] [blame] | 3706 | return -EINVAL; |
Jiri Pirko | 36fbf1e | 2019-09-30 11:48:16 +0200 | [diff] [blame] | 3707 | |
| 3708 | if (!dev) |
| 3709 | return -ENODEV; |
| 3710 | |
| 3711 | if (!tb[IFLA_PROP_LIST]) |
| 3712 | return 0; |
| 3713 | |
| 3714 | nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) { |
| 3715 | switch (nla_type(attr)) { |
| 3716 | case IFLA_ALT_IFNAME: |
| 3717 | err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack); |
| 3718 | if (err) |
| 3719 | return err; |
| 3720 | break; |
| 3721 | } |
| 3722 | } |
| 3723 | |
| 3724 | if (changed) |
| 3725 | netdev_state_change(dev); |
| 3726 | return 0; |
| 3727 | } |
| 3728 | |
| 3729 | static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 3730 | struct netlink_ext_ack *extack) |
| 3731 | { |
| 3732 | return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack); |
| 3733 | } |
| 3734 | |
| 3735 | static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 3736 | struct netlink_ext_ack *extack) |
| 3737 | { |
| 3738 | return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack); |
| 3739 | } |
| 3740 | |
Di Zhu | ebfe3c5 | 2020-10-21 10:00:53 +0800 | [diff] [blame] | 3741 | static u32 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 3742 | { |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 3743 | struct net *net = sock_net(skb->sk); |
Di Zhu | ebfe3c5 | 2020-10-21 10:00:53 +0800 | [diff] [blame] | 3744 | size_t min_ifinfo_dump_size = 0; |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 3745 | struct nlattr *tb[IFLA_MAX+1]; |
| 3746 | u32 ext_filter_mask = 0; |
Di Zhu | ebfe3c5 | 2020-10-21 10:00:53 +0800 | [diff] [blame] | 3747 | struct net_device *dev; |
Michal Schmidt | e5eca6d | 2014-05-28 14:15:19 +0200 | [diff] [blame] | 3748 | int hdrlen; |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 3749 | |
Michal Schmidt | e5eca6d | 2014-05-28 14:15:19 +0200 | [diff] [blame] | 3750 | /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ |
| 3751 | hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? |
| 3752 | sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); |
| 3753 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 3754 | 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] | 3755 | if (tb[IFLA_EXT_MASK]) |
| 3756 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); |
| 3757 | } |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 3758 | |
| 3759 | if (!ext_filter_mask) |
| 3760 | return NLMSG_GOODSIZE; |
| 3761 | /* |
| 3762 | * traverse the list of net devices and compute the minimum |
| 3763 | * buffer size based upon the filter mask. |
| 3764 | */ |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 3765 | rcu_read_lock(); |
| 3766 | for_each_netdev_rcu(net, dev) { |
Di Zhu | ebfe3c5 | 2020-10-21 10:00:53 +0800 | [diff] [blame] | 3767 | min_ifinfo_dump_size = max(min_ifinfo_dump_size, |
| 3768 | if_nlmsg_size(dev, ext_filter_mask)); |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 3769 | } |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 3770 | rcu_read_unlock(); |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 3771 | |
Zhang Shengju | 93af205 | 2016-11-22 14:14:28 +0800 | [diff] [blame] | 3772 | return nlmsg_total_size(min_ifinfo_dump_size); |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 3773 | } |
| 3774 | |
Adrian Bunk | 42bad1d | 2007-04-26 00:57:41 -0700 | [diff] [blame] | 3775 | 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] | 3776 | { |
| 3777 | int idx; |
| 3778 | int s_idx = cb->family; |
Christian Brauner | 87ccbb1 | 2018-09-04 21:53:51 +0200 | [diff] [blame] | 3779 | int type = cb->nlh->nlmsg_type - RTM_BASE; |
David Ahern | c63586d | 2018-10-24 12:59:02 -0700 | [diff] [blame] | 3780 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3781 | |
| 3782 | if (s_idx == 0) |
| 3783 | s_idx = 1; |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 3784 | |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 3785 | for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { |
Jakub Kicinski | 51e1368 | 2020-12-09 18:16:08 -0800 | [diff] [blame] | 3786 | struct rtnl_link __rcu **tab; |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 3787 | struct rtnl_link *link; |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 3788 | rtnl_dumpit_func dumpit; |
| 3789 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3790 | if (idx < s_idx || idx == PF_PACKET) |
| 3791 | continue; |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 3792 | |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 3793 | if (type < 0 || type >= RTM_NR_MSGTYPES) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3794 | continue; |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 3795 | |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 3796 | tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]); |
| 3797 | if (!tab) |
| 3798 | continue; |
| 3799 | |
Jakub Kicinski | 51e1368 | 2020-12-09 18:16:08 -0800 | [diff] [blame] | 3800 | link = rcu_dereference_rtnl(tab[type]); |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 3801 | if (!link) |
| 3802 | continue; |
| 3803 | |
| 3804 | dumpit = link->dumpit; |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 3805 | if (!dumpit) |
| 3806 | continue; |
| 3807 | |
Nicolas Dichtel | 0465277 | 2013-03-22 06:28:42 +0000 | [diff] [blame] | 3808 | if (idx > s_idx) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3809 | memset(&cb->args[0], 0, sizeof(cb->args)); |
Nicolas Dichtel | 0465277 | 2013-03-22 06:28:42 +0000 | [diff] [blame] | 3810 | cb->prev_seq = 0; |
| 3811 | cb->seq = 0; |
| 3812 | } |
David Ahern | c63586d | 2018-10-24 12:59:02 -0700 | [diff] [blame] | 3813 | ret = dumpit(skb, cb); |
Alexey Kodanev | 5e1acb4 | 2018-11-02 19:11:04 +0300 | [diff] [blame] | 3814 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3815 | break; |
| 3816 | } |
| 3817 | cb->family = idx; |
| 3818 | |
David Ahern | c63586d | 2018-10-24 12:59:02 -0700 | [diff] [blame] | 3819 | return skb->len ? : ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3820 | } |
| 3821 | |
Mahesh Bandewar | 395eea6 | 2014-12-03 13:46:24 -0800 | [diff] [blame] | 3822 | 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] | 3823 | unsigned int change, |
Nicolas Dichtel | 38e01b3 | 2018-01-25 15:01:39 +0100 | [diff] [blame] | 3824 | u32 event, gfp_t flags, int *new_nsid, |
| 3825 | int new_ifindex) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3826 | { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 3827 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3828 | struct sk_buff *skb; |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 3829 | int err = -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3830 | |
luo penghao | 50af596 | 2021-10-21 06:40:20 +0000 | [diff] [blame] | 3831 | skb = nlmsg_new(if_nlmsg_size(dev, 0), flags); |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 3832 | if (skb == NULL) |
| 3833 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3834 | |
Jiri Benc | 79e1ad1 | 2017-11-02 17:04:38 -0200 | [diff] [blame] | 3835 | err = rtnl_fill_ifinfo(skb, dev, dev_net(dev), |
| 3836 | type, 0, 0, change, 0, 0, event, |
Guillaume Nault | d4e4fdf | 2019-10-23 18:39:04 +0200 | [diff] [blame] | 3837 | new_nsid, new_ifindex, -1, flags); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 3838 | if (err < 0) { |
| 3839 | /* -EMSGSIZE implies BUG in if_nlmsg_size() */ |
| 3840 | WARN_ON(err == -EMSGSIZE); |
| 3841 | kfree_skb(skb); |
| 3842 | goto errout; |
| 3843 | } |
Mahesh Bandewar | 395eea6 | 2014-12-03 13:46:24 -0800 | [diff] [blame] | 3844 | return skb; |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 3845 | errout: |
| 3846 | if (err < 0) |
Eric W. Biederman | 4b3da70 | 2007-11-19 22:27:40 -0800 | [diff] [blame] | 3847 | rtnl_set_sk_err(net, RTNLGRP_LINK, err); |
Mahesh Bandewar | 395eea6 | 2014-12-03 13:46:24 -0800 | [diff] [blame] | 3848 | return NULL; |
| 3849 | } |
| 3850 | |
| 3851 | void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags) |
| 3852 | { |
| 3853 | struct net *net = dev_net(dev); |
| 3854 | |
| 3855 | rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); |
| 3856 | } |
| 3857 | |
Vlad Yasevich | 3d3ea5a | 2017-05-27 10:14:34 -0400 | [diff] [blame] | 3858 | static void rtmsg_ifinfo_event(int type, struct net_device *dev, |
| 3859 | unsigned int change, u32 event, |
Nicolas Dichtel | 38e01b3 | 2018-01-25 15:01:39 +0100 | [diff] [blame] | 3860 | gfp_t flags, int *new_nsid, int new_ifindex) |
Mahesh Bandewar | 395eea6 | 2014-12-03 13:46:24 -0800 | [diff] [blame] | 3861 | { |
| 3862 | struct sk_buff *skb; |
| 3863 | |
Nicolas Dichtel | ed2a80a | 2015-05-13 14:19:42 +0200 | [diff] [blame] | 3864 | if (dev->reg_state != NETREG_REGISTERED) |
| 3865 | return; |
| 3866 | |
Nicolas Dichtel | 38e01b3 | 2018-01-25 15:01:39 +0100 | [diff] [blame] | 3867 | skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid, |
| 3868 | new_ifindex); |
Mahesh Bandewar | 395eea6 | 2014-12-03 13:46:24 -0800 | [diff] [blame] | 3869 | if (skb) |
| 3870 | rtmsg_ifinfo_send(skb, dev, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3871 | } |
Vlad Yasevich | 3d3ea5a | 2017-05-27 10:14:34 -0400 | [diff] [blame] | 3872 | |
| 3873 | void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, |
| 3874 | gfp_t flags) |
| 3875 | { |
Nicolas Dichtel | 38e01b3 | 2018-01-25 15:01:39 +0100 | [diff] [blame] | 3876 | rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, |
| 3877 | NULL, 0); |
Vlad Yasevich | 3d3ea5a | 2017-05-27 10:14:34 -0400 | [diff] [blame] | 3878 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3879 | |
Nicolas Dichtel | 6621dd2 | 2017-10-03 13:53:23 +0200 | [diff] [blame] | 3880 | 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] | 3881 | gfp_t flags, int *new_nsid, int new_ifindex) |
Nicolas Dichtel | 6621dd2 | 2017-10-03 13:53:23 +0200 | [diff] [blame] | 3882 | { |
| 3883 | rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, |
Nicolas Dichtel | 38e01b3 | 2018-01-25 15:01:39 +0100 | [diff] [blame] | 3884 | new_nsid, new_ifindex); |
Nicolas Dichtel | 6621dd2 | 2017-10-03 13:53:23 +0200 | [diff] [blame] | 3885 | } |
| 3886 | |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 3887 | static int nlmsg_populate_fdb_fill(struct sk_buff *skb, |
| 3888 | struct net_device *dev, |
Hubert Sokolowski | 1e53d5b | 2015-04-09 12:16:17 +0000 | [diff] [blame] | 3889 | u8 *addr, u16 vid, u32 pid, u32 seq, |
Nicolas Dichtel | 1c104a6 | 2014-03-19 17:47:49 +0100 | [diff] [blame] | 3890 | int type, unsigned int flags, |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 3891 | int nlflags, u16 ndm_state) |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 3892 | { |
| 3893 | struct nlmsghdr *nlh; |
| 3894 | struct ndmsg *ndm; |
| 3895 | |
Nicolas Dichtel | 1c104a6 | 2014-03-19 17:47:49 +0100 | [diff] [blame] | 3896 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 3897 | if (!nlh) |
| 3898 | return -EMSGSIZE; |
| 3899 | |
| 3900 | ndm = nlmsg_data(nlh); |
| 3901 | ndm->ndm_family = AF_BRIDGE; |
| 3902 | ndm->ndm_pad1 = 0; |
| 3903 | ndm->ndm_pad2 = 0; |
| 3904 | ndm->ndm_flags = flags; |
| 3905 | ndm->ndm_type = 0; |
| 3906 | ndm->ndm_ifindex = dev->ifindex; |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 3907 | ndm->ndm_state = ndm_state; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 3908 | |
| 3909 | if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) |
| 3910 | goto nla_put_failure; |
Hubert Sokolowski | 1e53d5b | 2015-04-09 12:16:17 +0000 | [diff] [blame] | 3911 | if (vid) |
| 3912 | if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) |
| 3913 | goto nla_put_failure; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 3914 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 3915 | nlmsg_end(skb, nlh); |
| 3916 | return 0; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 3917 | |
| 3918 | nla_put_failure: |
| 3919 | nlmsg_cancel(skb, nlh); |
| 3920 | return -EMSGSIZE; |
| 3921 | } |
| 3922 | |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 3923 | static inline size_t rtnl_fdb_nlmsg_size(void) |
| 3924 | { |
Sabrina Dubroca | f82ef3e | 2016-11-18 15:50:39 +0100 | [diff] [blame] | 3925 | return NLMSG_ALIGN(sizeof(struct ndmsg)) + |
| 3926 | nla_total_size(ETH_ALEN) + /* NDA_LLADDR */ |
| 3927 | nla_total_size(sizeof(u16)) + /* NDA_VLAN */ |
| 3928 | 0; |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 3929 | } |
| 3930 | |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 3931 | static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, |
| 3932 | u16 ndm_state) |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 3933 | { |
| 3934 | struct net *net = dev_net(dev); |
| 3935 | struct sk_buff *skb; |
| 3936 | int err = -ENOBUFS; |
| 3937 | |
| 3938 | skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); |
| 3939 | if (!skb) |
| 3940 | goto errout; |
| 3941 | |
Hubert Sokolowski | 1e53d5b | 2015-04-09 12:16:17 +0000 | [diff] [blame] | 3942 | err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 3943 | 0, 0, type, NTF_SELF, 0, ndm_state); |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 3944 | if (err < 0) { |
| 3945 | kfree_skb(skb); |
| 3946 | goto errout; |
| 3947 | } |
| 3948 | |
| 3949 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); |
| 3950 | return; |
| 3951 | errout: |
| 3952 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); |
| 3953 | } |
| 3954 | |
Bart Van Assche | a986967 | 2019-03-25 09:17:22 -0700 | [diff] [blame] | 3955 | /* |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 3956 | * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry |
| 3957 | */ |
| 3958 | int ndo_dflt_fdb_add(struct ndmsg *ndm, |
| 3959 | struct nlattr *tb[], |
| 3960 | struct net_device *dev, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 3961 | const unsigned char *addr, u16 vid, |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 3962 | u16 flags) |
| 3963 | { |
| 3964 | int err = -EINVAL; |
| 3965 | |
| 3966 | /* If aging addresses are supported device will need to |
| 3967 | * implement its own handler for this. |
| 3968 | */ |
| 3969 | if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { |
Vladimir Oltean | 78ecc89 | 2021-06-29 03:29:26 +0300 | [diff] [blame] | 3970 | netdev_info(dev, "default FDB implementation only supports local addresses\n"); |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 3971 | return err; |
| 3972 | } |
| 3973 | |
Or Gerlitz | 65891fe | 2014-12-14 18:19:05 +0200 | [diff] [blame] | 3974 | if (vid) { |
Vladimir Oltean | 23ac0b4 | 2021-06-29 03:29:25 +0300 | [diff] [blame] | 3975 | 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] | 3976 | return err; |
| 3977 | } |
| 3978 | |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 3979 | if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) |
| 3980 | err = dev_uc_add_excl(dev, addr); |
| 3981 | else if (is_multicast_ether_addr(addr)) |
| 3982 | err = dev_mc_add_excl(dev, addr); |
| 3983 | |
| 3984 | /* Only return duplicate errors if NLM_F_EXCL is set */ |
| 3985 | if (err == -EEXIST && !(flags & NLM_F_EXCL)) |
| 3986 | err = 0; |
| 3987 | |
| 3988 | return err; |
| 3989 | } |
| 3990 | EXPORT_SYMBOL(ndo_dflt_fdb_add); |
| 3991 | |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 3992 | static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid, |
| 3993 | struct netlink_ext_ack *extack) |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 3994 | { |
| 3995 | u16 vid = 0; |
| 3996 | |
| 3997 | if (vlan_attr) { |
| 3998 | if (nla_len(vlan_attr) != sizeof(u16)) { |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 3999 | NL_SET_ERR_MSG(extack, "invalid vlan attribute size"); |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 4000 | return -EINVAL; |
| 4001 | } |
| 4002 | |
| 4003 | vid = nla_get_u16(vlan_attr); |
| 4004 | |
| 4005 | if (!vid || vid >= VLAN_VID_MASK) { |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 4006 | NL_SET_ERR_MSG(extack, "invalid vlan id"); |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 4007 | return -EINVAL; |
| 4008 | } |
| 4009 | } |
| 4010 | *p_vid = vid; |
| 4011 | return 0; |
| 4012 | } |
| 4013 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 4014 | static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 4015 | struct netlink_ext_ack *extack) |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4016 | { |
| 4017 | struct net *net = sock_net(skb->sk); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4018 | struct ndmsg *ndm; |
| 4019 | struct nlattr *tb[NDA_MAX+1]; |
| 4020 | struct net_device *dev; |
| 4021 | u8 *addr; |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 4022 | u16 vid; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4023 | int err; |
| 4024 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 4025 | err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, |
| 4026 | extack); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4027 | if (err < 0) |
| 4028 | return err; |
| 4029 | |
| 4030 | ndm = nlmsg_data(nlh); |
| 4031 | if (ndm->ndm_ifindex == 0) { |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 4032 | NL_SET_ERR_MSG(extack, "invalid ifindex"); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4033 | return -EINVAL; |
| 4034 | } |
| 4035 | |
| 4036 | dev = __dev_get_by_index(net, ndm->ndm_ifindex); |
| 4037 | if (dev == NULL) { |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 4038 | NL_SET_ERR_MSG(extack, "unknown ifindex"); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4039 | return -ENODEV; |
| 4040 | } |
| 4041 | |
| 4042 | if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 4043 | NL_SET_ERR_MSG(extack, "invalid address"); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4044 | return -EINVAL; |
| 4045 | } |
| 4046 | |
Ido Schimmel | da71577 | 2018-10-29 20:36:43 +0000 | [diff] [blame] | 4047 | if (dev->type != ARPHRD_ETHER) { |
| 4048 | NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices"); |
| 4049 | return -EINVAL; |
| 4050 | } |
| 4051 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4052 | addr = nla_data(tb[NDA_LLADDR]); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4053 | |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 4054 | err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 4055 | if (err) |
| 4056 | return err; |
| 4057 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4058 | err = -EOPNOTSUPP; |
| 4059 | |
| 4060 | /* Support fdb on master device the net/bridge default case */ |
| 4061 | if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && |
Julian Wiedmann | 2e92a2d | 2020-02-20 09:00:07 +0100 | [diff] [blame] | 4062 | netif_is_bridge_port(dev)) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 4063 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
| 4064 | const struct net_device_ops *ops = br_dev->netdev_ops; |
| 4065 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 4066 | err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, |
Petr Machata | 87b0984 | 2019-01-16 23:06:50 +0000 | [diff] [blame] | 4067 | nlh->nlmsg_flags, extack); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4068 | if (err) |
| 4069 | goto out; |
| 4070 | else |
| 4071 | ndm->ndm_flags &= ~NTF_MASTER; |
| 4072 | } |
| 4073 | |
| 4074 | /* Embedded bridge, macvlan, and any other device support */ |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 4075 | if ((ndm->ndm_flags & NTF_SELF)) { |
| 4076 | if (dev->netdev_ops->ndo_fdb_add) |
| 4077 | err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 4078 | vid, |
Petr Machata | 87b0984 | 2019-01-16 23:06:50 +0000 | [diff] [blame] | 4079 | nlh->nlmsg_flags, |
| 4080 | extack); |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 4081 | else |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 4082 | err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 4083 | nlh->nlmsg_flags); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4084 | |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 4085 | if (!err) { |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 4086 | rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, |
| 4087 | ndm->ndm_state); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4088 | ndm->ndm_flags &= ~NTF_SELF; |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 4089 | } |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4090 | } |
| 4091 | out: |
| 4092 | return err; |
| 4093 | } |
| 4094 | |
Bart Van Assche | a986967 | 2019-03-25 09:17:22 -0700 | [diff] [blame] | 4095 | /* |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 4096 | * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry |
| 4097 | */ |
| 4098 | int ndo_dflt_fdb_del(struct ndmsg *ndm, |
| 4099 | struct nlattr *tb[], |
| 4100 | struct net_device *dev, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 4101 | const unsigned char *addr, u16 vid) |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 4102 | { |
Alexander Duyck | c8a89c4 | 2014-07-15 15:15:20 -0700 | [diff] [blame] | 4103 | int err = -EINVAL; |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 4104 | |
| 4105 | /* If aging addresses are supported device will need to |
| 4106 | * implement its own handler for this. |
| 4107 | */ |
Sridhar Samudrala | 6453599 | 2013-08-08 15:19:48 -0700 | [diff] [blame] | 4108 | if (!(ndm->ndm_state & NUD_PERMANENT)) { |
Vladimir Oltean | 78ecc89 | 2021-06-29 03:29:26 +0300 | [diff] [blame] | 4109 | netdev_info(dev, "default FDB implementation only supports local addresses\n"); |
Alexander Duyck | c8a89c4 | 2014-07-15 15:15:20 -0700 | [diff] [blame] | 4110 | return err; |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 4111 | } |
| 4112 | |
| 4113 | if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) |
| 4114 | err = dev_uc_del(dev, addr); |
| 4115 | else if (is_multicast_ether_addr(addr)) |
| 4116 | err = dev_mc_del(dev, addr); |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 4117 | |
| 4118 | return err; |
| 4119 | } |
| 4120 | EXPORT_SYMBOL(ndo_dflt_fdb_del); |
| 4121 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 4122 | static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 4123 | struct netlink_ext_ack *extack) |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4124 | { |
| 4125 | struct net *net = sock_net(skb->sk); |
| 4126 | struct ndmsg *ndm; |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 4127 | struct nlattr *tb[NDA_MAX+1]; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4128 | struct net_device *dev; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4129 | __u8 *addr; |
Colin Ian King | 7d31180 | 2020-04-25 12:28:14 +0100 | [diff] [blame] | 4130 | int err; |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 4131 | u16 vid; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4132 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 4133 | if (!netlink_capable(skb, CAP_NET_ADMIN)) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 4134 | return -EPERM; |
| 4135 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 4136 | err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, |
| 4137 | extack); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 4138 | if (err < 0) |
| 4139 | return err; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4140 | |
| 4141 | ndm = nlmsg_data(nlh); |
| 4142 | if (ndm->ndm_ifindex == 0) { |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 4143 | NL_SET_ERR_MSG(extack, "invalid ifindex"); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4144 | return -EINVAL; |
| 4145 | } |
| 4146 | |
| 4147 | dev = __dev_get_by_index(net, ndm->ndm_ifindex); |
| 4148 | if (dev == NULL) { |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 4149 | NL_SET_ERR_MSG(extack, "unknown ifindex"); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4150 | return -ENODEV; |
| 4151 | } |
| 4152 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 4153 | if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 4154 | NL_SET_ERR_MSG(extack, "invalid address"); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4155 | return -EINVAL; |
| 4156 | } |
| 4157 | |
Ido Schimmel | da71577 | 2018-10-29 20:36:43 +0000 | [diff] [blame] | 4158 | if (dev->type != ARPHRD_ETHER) { |
| 4159 | NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices"); |
| 4160 | return -EINVAL; |
| 4161 | } |
| 4162 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 4163 | addr = nla_data(tb[NDA_LLADDR]); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 4164 | |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 4165 | err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 4166 | if (err) |
| 4167 | return err; |
| 4168 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4169 | err = -EOPNOTSUPP; |
| 4170 | |
| 4171 | /* Support fdb on master device the net/bridge default case */ |
| 4172 | if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && |
Julian Wiedmann | 2e92a2d | 2020-02-20 09:00:07 +0100 | [diff] [blame] | 4173 | netif_is_bridge_port(dev)) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 4174 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
| 4175 | const struct net_device_ops *ops = br_dev->netdev_ops; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4176 | |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 4177 | if (ops->ndo_fdb_del) |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 4178 | err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4179 | |
| 4180 | if (err) |
| 4181 | goto out; |
| 4182 | else |
| 4183 | ndm->ndm_flags &= ~NTF_MASTER; |
| 4184 | } |
| 4185 | |
| 4186 | /* Embedded bridge, macvlan, and any other device support */ |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 4187 | if (ndm->ndm_flags & NTF_SELF) { |
| 4188 | if (dev->netdev_ops->ndo_fdb_del) |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 4189 | err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, |
| 4190 | vid); |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 4191 | else |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 4192 | err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4193 | |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 4194 | if (!err) { |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 4195 | rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, |
| 4196 | ndm->ndm_state); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4197 | ndm->ndm_flags &= ~NTF_SELF; |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 4198 | } |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4199 | } |
| 4200 | out: |
| 4201 | return err; |
| 4202 | } |
| 4203 | |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4204 | static int nlmsg_populate_fdb(struct sk_buff *skb, |
| 4205 | struct netlink_callback *cb, |
| 4206 | struct net_device *dev, |
| 4207 | int *idx, |
| 4208 | struct netdev_hw_addr_list *list) |
| 4209 | { |
| 4210 | struct netdev_hw_addr *ha; |
| 4211 | int err; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4212 | u32 portid, seq; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4213 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4214 | portid = NETLINK_CB(cb->skb).portid; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4215 | seq = cb->nlh->nlmsg_seq; |
| 4216 | |
| 4217 | list_for_each_entry(ha, &list->list, list) { |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4218 | if (*idx < cb->args[2]) |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4219 | goto skip; |
| 4220 | |
Hubert Sokolowski | 1e53d5b | 2015-04-09 12:16:17 +0000 | [diff] [blame] | 4221 | err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, |
John Fastabend | a7a558f | 2012-11-01 16:23:10 +0000 | [diff] [blame] | 4222 | portid, seq, |
Nicolas Dichtel | 1c104a6 | 2014-03-19 17:47:49 +0100 | [diff] [blame] | 4223 | RTM_NEWNEIGH, NTF_SELF, |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 4224 | NLM_F_MULTI, NUD_PERMANENT); |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4225 | if (err < 0) |
| 4226 | return err; |
| 4227 | skip: |
| 4228 | *idx += 1; |
| 4229 | } |
| 4230 | return 0; |
| 4231 | } |
| 4232 | |
| 4233 | /** |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 4234 | * 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] | 4235 | * @skb: socket buffer to store message in |
| 4236 | * @cb: netlink callback |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4237 | * @dev: netdevice |
Bart Van Assche | a986967 | 2019-03-25 09:17:22 -0700 | [diff] [blame] | 4238 | * @filter_dev: ignored |
| 4239 | * @idx: the number of FDB table entries dumped is added to *@idx |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4240 | * |
| 4241 | * Default netdevice operation to dump the existing unicast address list. |
John Fastabend | 91f3e7b | 2013-03-29 08:18:37 +0000 | [diff] [blame] | 4242 | * Returns number of addresses from list put in skb. |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4243 | */ |
| 4244 | int ndo_dflt_fdb_dump(struct sk_buff *skb, |
| 4245 | struct netlink_callback *cb, |
| 4246 | struct net_device *dev, |
Jamal Hadi Salim | 5d5eacb | 2014-07-10 07:01:58 -0400 | [diff] [blame] | 4247 | struct net_device *filter_dev, |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4248 | int *idx) |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4249 | { |
| 4250 | int err; |
| 4251 | |
Eric Dumazet | 6888389 | 2018-12-04 09:40:35 -0800 | [diff] [blame] | 4252 | if (dev->type != ARPHRD_ETHER) |
| 4253 | return -EINVAL; |
| 4254 | |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4255 | netif_addr_lock_bh(dev); |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4256 | err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc); |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4257 | if (err) |
| 4258 | goto out; |
Zhang Shengju | 2934c9d | 2016-11-30 16:37:34 +0800 | [diff] [blame] | 4259 | err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc); |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4260 | out: |
| 4261 | netif_addr_unlock_bh(dev); |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4262 | return err; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 4263 | } |
| 4264 | EXPORT_SYMBOL(ndo_dflt_fdb_dump); |
| 4265 | |
David Ahern | 8c6e137 | 2018-10-07 20:16:44 -0700 | [diff] [blame] | 4266 | static int valid_fdb_dump_strict(const struct nlmsghdr *nlh, |
| 4267 | int *br_idx, int *brport_idx, |
| 4268 | struct netlink_ext_ack *extack) |
| 4269 | { |
| 4270 | struct nlattr *tb[NDA_MAX + 1]; |
| 4271 | struct ndmsg *ndm; |
| 4272 | int err, i; |
| 4273 | |
| 4274 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { |
| 4275 | NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request"); |
| 4276 | return -EINVAL; |
| 4277 | } |
| 4278 | |
| 4279 | ndm = nlmsg_data(nlh); |
| 4280 | if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || |
| 4281 | ndm->ndm_flags || ndm->ndm_type) { |
Nikolay Aleksandrov | 8b73018 | 2019-10-29 13:59:32 +0200 | [diff] [blame] | 4282 | 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] | 4283 | return -EINVAL; |
| 4284 | } |
| 4285 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 4286 | err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, |
| 4287 | NDA_MAX, NULL, extack); |
David Ahern | 8c6e137 | 2018-10-07 20:16:44 -0700 | [diff] [blame] | 4288 | if (err < 0) |
| 4289 | return err; |
| 4290 | |
| 4291 | *brport_idx = ndm->ndm_ifindex; |
| 4292 | for (i = 0; i <= NDA_MAX; ++i) { |
| 4293 | if (!tb[i]) |
| 4294 | continue; |
| 4295 | |
| 4296 | switch (i) { |
| 4297 | case NDA_IFINDEX: |
| 4298 | if (nla_len(tb[i]) != sizeof(u32)) { |
| 4299 | NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request"); |
| 4300 | return -EINVAL; |
| 4301 | } |
| 4302 | *brport_idx = nla_get_u32(tb[NDA_IFINDEX]); |
| 4303 | break; |
| 4304 | case NDA_MASTER: |
| 4305 | if (nla_len(tb[i]) != sizeof(u32)) { |
| 4306 | NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request"); |
| 4307 | return -EINVAL; |
| 4308 | } |
| 4309 | *br_idx = nla_get_u32(tb[NDA_MASTER]); |
| 4310 | break; |
| 4311 | default: |
| 4312 | NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request"); |
| 4313 | return -EINVAL; |
| 4314 | } |
| 4315 | } |
| 4316 | |
| 4317 | return 0; |
| 4318 | } |
| 4319 | |
David Ahern | 8dfbda1 | 2018-10-07 20:16:43 -0700 | [diff] [blame] | 4320 | static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh, |
| 4321 | int *br_idx, int *brport_idx, |
| 4322 | struct netlink_ext_ack *extack) |
| 4323 | { |
David Ahern | 8dfbda1 | 2018-10-07 20:16:43 -0700 | [diff] [blame] | 4324 | struct nlattr *tb[IFLA_MAX+1]; |
| 4325 | int err; |
| 4326 | |
| 4327 | /* A hack to preserve kernel<->userspace interface. |
| 4328 | * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0. |
| 4329 | * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails. |
| 4330 | * So, check for ndmsg with an optional u32 attribute (not used here). |
| 4331 | * Fortunately these sizes don't conflict with the size of ifinfomsg |
| 4332 | * with an optional attribute. |
| 4333 | */ |
| 4334 | if (nlmsg_len(nlh) != sizeof(struct ndmsg) && |
| 4335 | (nlmsg_len(nlh) != sizeof(struct ndmsg) + |
| 4336 | nla_attr_size(sizeof(u32)))) { |
David Ahern | 4565d7e | 2018-10-08 13:57:24 -0700 | [diff] [blame] | 4337 | struct ifinfomsg *ifm; |
| 4338 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 4339 | err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), |
| 4340 | tb, IFLA_MAX, ifla_policy, |
| 4341 | extack); |
David Ahern | 8dfbda1 | 2018-10-07 20:16:43 -0700 | [diff] [blame] | 4342 | if (err < 0) { |
| 4343 | return -EINVAL; |
| 4344 | } else if (err == 0) { |
| 4345 | if (tb[IFLA_MASTER]) |
| 4346 | *br_idx = nla_get_u32(tb[IFLA_MASTER]); |
| 4347 | } |
| 4348 | |
David Ahern | 4565d7e | 2018-10-08 13:57:24 -0700 | [diff] [blame] | 4349 | ifm = nlmsg_data(nlh); |
David Ahern | 8dfbda1 | 2018-10-07 20:16:43 -0700 | [diff] [blame] | 4350 | *brport_idx = ifm->ifi_index; |
| 4351 | } |
| 4352 | return 0; |
| 4353 | } |
| 4354 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4355 | static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 4356 | { |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4357 | struct net_device *dev; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 4358 | struct net_device *br_dev = NULL; |
| 4359 | const struct net_device_ops *ops = NULL; |
| 4360 | const struct net_device_ops *cops = NULL; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 4361 | struct net *net = sock_net(skb->sk); |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4362 | struct hlist_head *head; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 4363 | int brport_idx = 0; |
| 4364 | int br_idx = 0; |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4365 | int h, s_h; |
| 4366 | int idx = 0, s_idx; |
| 4367 | int err = 0; |
| 4368 | int fidx = 0; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4369 | |
David Ahern | 8c6e137 | 2018-10-07 20:16:44 -0700 | [diff] [blame] | 4370 | if (cb->strict_check) |
| 4371 | err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx, |
| 4372 | cb->extack); |
| 4373 | else |
| 4374 | err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx, |
| 4375 | cb->extack); |
David Ahern | 8dfbda1 | 2018-10-07 20:16:43 -0700 | [diff] [blame] | 4376 | if (err < 0) |
| 4377 | return err; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 4378 | |
| 4379 | if (br_idx) { |
| 4380 | br_dev = __dev_get_by_index(net, br_idx); |
| 4381 | if (!br_dev) |
| 4382 | return -ENODEV; |
| 4383 | |
| 4384 | ops = br_dev->netdev_ops; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 4385 | } |
| 4386 | |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4387 | s_h = cb->args[0]; |
| 4388 | s_idx = cb->args[1]; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 4389 | |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4390 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { |
| 4391 | idx = 0; |
| 4392 | head = &net->dev_index_head[h]; |
| 4393 | hlist_for_each_entry(dev, head, index_hlist) { |
| 4394 | |
| 4395 | if (brport_idx && (dev->ifindex != brport_idx)) |
| 4396 | continue; |
| 4397 | |
| 4398 | if (!br_idx) { /* user did not specify a specific bridge */ |
Julian Wiedmann | 2e92a2d | 2020-02-20 09:00:07 +0100 | [diff] [blame] | 4399 | if (netif_is_bridge_port(dev)) { |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4400 | br_dev = netdev_master_upper_dev_get(dev); |
| 4401 | cops = br_dev->netdev_ops; |
| 4402 | } |
| 4403 | } else { |
| 4404 | if (dev != br_dev && |
Julian Wiedmann | 2e92a2d | 2020-02-20 09:00:07 +0100 | [diff] [blame] | 4405 | !netif_is_bridge_port(dev)) |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4406 | continue; |
| 4407 | |
| 4408 | if (br_dev != netdev_master_upper_dev_get(dev) && |
Kyungrok Chung | 254ec03 | 2021-10-16 20:21:36 +0900 | [diff] [blame] | 4409 | !netif_is_bridge_master(dev)) |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4410 | continue; |
| 4411 | cops = ops; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 4412 | } |
| 4413 | |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4414 | if (idx < s_idx) |
| 4415 | goto cont; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 4416 | |
Julian Wiedmann | 2e92a2d | 2020-02-20 09:00:07 +0100 | [diff] [blame] | 4417 | if (netif_is_bridge_port(dev)) { |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4418 | if (cops && cops->ndo_fdb_dump) { |
| 4419 | err = cops->ndo_fdb_dump(skb, cb, |
| 4420 | br_dev, dev, |
| 4421 | &fidx); |
| 4422 | if (err == -EMSGSIZE) |
| 4423 | goto out; |
| 4424 | } |
| 4425 | } |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 4426 | |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4427 | if (dev->netdev_ops->ndo_fdb_dump) |
| 4428 | err = dev->netdev_ops->ndo_fdb_dump(skb, cb, |
| 4429 | dev, NULL, |
| 4430 | &fidx); |
| 4431 | else |
| 4432 | err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, |
| 4433 | &fidx); |
| 4434 | if (err == -EMSGSIZE) |
| 4435 | goto out; |
| 4436 | |
| 4437 | cops = NULL; |
| 4438 | |
| 4439 | /* reset fdb offset to 0 for rest of the interfaces */ |
| 4440 | cb->args[2] = 0; |
| 4441 | fidx = 0; |
| 4442 | cont: |
| 4443 | idx++; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4444 | } |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4445 | } |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4446 | |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 4447 | out: |
| 4448 | cb->args[0] = h; |
| 4449 | cb->args[1] = idx; |
| 4450 | cb->args[2] = fidx; |
| 4451 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 4452 | return skb->len; |
| 4453 | } |
| 4454 | |
Roopa Prabhu | 5b2f94b | 2018-12-15 22:35:08 -0800 | [diff] [blame] | 4455 | static int valid_fdb_get_strict(const struct nlmsghdr *nlh, |
| 4456 | struct nlattr **tb, u8 *ndm_flags, |
| 4457 | int *br_idx, int *brport_idx, u8 **addr, |
| 4458 | u16 *vid, struct netlink_ext_ack *extack) |
| 4459 | { |
| 4460 | struct ndmsg *ndm; |
| 4461 | int err, i; |
| 4462 | |
| 4463 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { |
| 4464 | NL_SET_ERR_MSG(extack, "Invalid header for fdb get request"); |
| 4465 | return -EINVAL; |
| 4466 | } |
| 4467 | |
| 4468 | ndm = nlmsg_data(nlh); |
| 4469 | if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || |
| 4470 | ndm->ndm_type) { |
| 4471 | NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request"); |
| 4472 | return -EINVAL; |
| 4473 | } |
| 4474 | |
| 4475 | if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) { |
| 4476 | NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request"); |
| 4477 | return -EINVAL; |
| 4478 | } |
| 4479 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 4480 | err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, |
| 4481 | NDA_MAX, nda_policy, extack); |
Roopa Prabhu | 5b2f94b | 2018-12-15 22:35:08 -0800 | [diff] [blame] | 4482 | if (err < 0) |
| 4483 | return err; |
| 4484 | |
| 4485 | *ndm_flags = ndm->ndm_flags; |
| 4486 | *brport_idx = ndm->ndm_ifindex; |
| 4487 | for (i = 0; i <= NDA_MAX; ++i) { |
| 4488 | if (!tb[i]) |
| 4489 | continue; |
| 4490 | |
| 4491 | switch (i) { |
| 4492 | case NDA_MASTER: |
| 4493 | *br_idx = nla_get_u32(tb[i]); |
| 4494 | break; |
| 4495 | case NDA_LLADDR: |
| 4496 | if (nla_len(tb[i]) != ETH_ALEN) { |
| 4497 | NL_SET_ERR_MSG(extack, "Invalid address in fdb get request"); |
| 4498 | return -EINVAL; |
| 4499 | } |
| 4500 | *addr = nla_data(tb[i]); |
| 4501 | break; |
| 4502 | case NDA_VLAN: |
| 4503 | err = fdb_vid_parse(tb[i], vid, extack); |
| 4504 | if (err) |
| 4505 | return err; |
| 4506 | break; |
| 4507 | case NDA_VNI: |
| 4508 | break; |
| 4509 | default: |
| 4510 | NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request"); |
| 4511 | return -EINVAL; |
| 4512 | } |
| 4513 | } |
| 4514 | |
| 4515 | return 0; |
| 4516 | } |
| 4517 | |
| 4518 | static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh, |
| 4519 | struct netlink_ext_ack *extack) |
| 4520 | { |
| 4521 | struct net_device *dev = NULL, *br_dev = NULL; |
| 4522 | const struct net_device_ops *ops = NULL; |
| 4523 | struct net *net = sock_net(in_skb->sk); |
| 4524 | struct nlattr *tb[NDA_MAX + 1]; |
| 4525 | struct sk_buff *skb; |
| 4526 | int brport_idx = 0; |
| 4527 | u8 ndm_flags = 0; |
| 4528 | int br_idx = 0; |
| 4529 | u8 *addr = NULL; |
| 4530 | u16 vid = 0; |
| 4531 | int err; |
| 4532 | |
| 4533 | err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx, |
| 4534 | &brport_idx, &addr, &vid, extack); |
| 4535 | if (err < 0) |
| 4536 | return err; |
| 4537 | |
Nikolay Aleksandrov | f989d03 | 2018-12-30 14:33:20 +0200 | [diff] [blame] | 4538 | if (!addr) { |
| 4539 | NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request"); |
| 4540 | return -EINVAL; |
| 4541 | } |
| 4542 | |
Roopa Prabhu | 5b2f94b | 2018-12-15 22:35:08 -0800 | [diff] [blame] | 4543 | if (brport_idx) { |
| 4544 | dev = __dev_get_by_index(net, brport_idx); |
| 4545 | if (!dev) { |
| 4546 | NL_SET_ERR_MSG(extack, "Unknown device ifindex"); |
| 4547 | return -ENODEV; |
| 4548 | } |
| 4549 | } |
| 4550 | |
| 4551 | if (br_idx) { |
| 4552 | if (dev) { |
| 4553 | NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive"); |
| 4554 | return -EINVAL; |
| 4555 | } |
| 4556 | |
| 4557 | br_dev = __dev_get_by_index(net, br_idx); |
| 4558 | if (!br_dev) { |
| 4559 | NL_SET_ERR_MSG(extack, "Invalid master ifindex"); |
| 4560 | return -EINVAL; |
| 4561 | } |
| 4562 | ops = br_dev->netdev_ops; |
| 4563 | } |
| 4564 | |
| 4565 | if (dev) { |
| 4566 | if (!ndm_flags || (ndm_flags & NTF_MASTER)) { |
Julian Wiedmann | 2e92a2d | 2020-02-20 09:00:07 +0100 | [diff] [blame] | 4567 | if (!netif_is_bridge_port(dev)) { |
Roopa Prabhu | 5b2f94b | 2018-12-15 22:35:08 -0800 | [diff] [blame] | 4568 | NL_SET_ERR_MSG(extack, "Device is not a bridge port"); |
| 4569 | return -EINVAL; |
| 4570 | } |
| 4571 | br_dev = netdev_master_upper_dev_get(dev); |
| 4572 | if (!br_dev) { |
| 4573 | NL_SET_ERR_MSG(extack, "Master of device not found"); |
| 4574 | return -EINVAL; |
| 4575 | } |
| 4576 | ops = br_dev->netdev_ops; |
| 4577 | } else { |
| 4578 | if (!(ndm_flags & NTF_SELF)) { |
| 4579 | NL_SET_ERR_MSG(extack, "Missing NTF_SELF"); |
| 4580 | return -EINVAL; |
| 4581 | } |
| 4582 | ops = dev->netdev_ops; |
| 4583 | } |
| 4584 | } |
| 4585 | |
| 4586 | if (!br_dev && !dev) { |
| 4587 | NL_SET_ERR_MSG(extack, "No device specified"); |
| 4588 | return -ENODEV; |
| 4589 | } |
| 4590 | |
| 4591 | if (!ops || !ops->ndo_fdb_get) { |
| 4592 | NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device"); |
| 4593 | return -EOPNOTSUPP; |
| 4594 | } |
| 4595 | |
| 4596 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
| 4597 | if (!skb) |
| 4598 | return -ENOBUFS; |
| 4599 | |
| 4600 | if (br_dev) |
| 4601 | dev = br_dev; |
| 4602 | err = ops->ndo_fdb_get(skb, tb, dev, addr, vid, |
| 4603 | NETLINK_CB(in_skb).portid, |
| 4604 | nlh->nlmsg_seq, extack); |
| 4605 | if (err) |
| 4606 | goto out; |
| 4607 | |
| 4608 | return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); |
| 4609 | out: |
| 4610 | kfree_skb(skb); |
| 4611 | return err; |
| 4612 | } |
| 4613 | |
Scott Feldman | 2c3c031 | 2014-11-28 14:34:25 +0100 | [diff] [blame] | 4614 | static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, |
| 4615 | unsigned int attrnum, unsigned int flag) |
| 4616 | { |
| 4617 | if (mask & flag) |
| 4618 | return nla_put_u8(skb, attrnum, !!(flags & flag)); |
| 4619 | return 0; |
| 4620 | } |
| 4621 | |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4622 | 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] | 4623 | struct net_device *dev, u16 mode, |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 4624 | u32 flags, u32 mask, int nlflags, |
| 4625 | u32 filter_mask, |
| 4626 | int (*vlan_fill)(struct sk_buff *skb, |
| 4627 | struct net_device *dev, |
| 4628 | u32 filter_mask)) |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4629 | { |
| 4630 | struct nlmsghdr *nlh; |
| 4631 | struct ifinfomsg *ifm; |
| 4632 | struct nlattr *br_afspec; |
Scott Feldman | 2c3c031 | 2014-11-28 14:34:25 +0100 | [diff] [blame] | 4633 | struct nlattr *protinfo; |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4634 | u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 4635 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 4636 | int err = 0; |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4637 | |
Nicolas Dichtel | 46c264d | 2015-04-28 18:33:49 +0200 | [diff] [blame] | 4638 | nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4639 | if (nlh == NULL) |
| 4640 | return -EMSGSIZE; |
| 4641 | |
| 4642 | ifm = nlmsg_data(nlh); |
| 4643 | ifm->ifi_family = AF_BRIDGE; |
| 4644 | ifm->__ifi_pad = 0; |
| 4645 | ifm->ifi_type = dev->type; |
| 4646 | ifm->ifi_index = dev->ifindex; |
| 4647 | ifm->ifi_flags = dev_get_flags(dev); |
| 4648 | ifm->ifi_change = 0; |
| 4649 | |
| 4650 | |
| 4651 | if (nla_put_string(skb, IFLA_IFNAME, dev->name) || |
| 4652 | nla_put_u32(skb, IFLA_MTU, dev->mtu) || |
| 4653 | nla_put_u8(skb, IFLA_OPERSTATE, operstate) || |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 4654 | (br_dev && |
| 4655 | nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4656 | (dev->addr_len && |
| 4657 | nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || |
Nicolas Dichtel | a54acb3 | 2015-04-02 17:07:00 +0200 | [diff] [blame] | 4658 | (dev->ifindex != dev_get_iflink(dev) && |
| 4659 | nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4660 | goto nla_put_failure; |
| 4661 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 4662 | br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC); |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4663 | if (!br_afspec) |
| 4664 | goto nla_put_failure; |
| 4665 | |
Roopa Prabhu | 1d460b9 | 2014-12-08 14:04:20 -0800 | [diff] [blame] | 4666 | if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4667 | nla_nest_cancel(skb, br_afspec); |
| 4668 | goto nla_put_failure; |
| 4669 | } |
Roopa Prabhu | 1d460b9 | 2014-12-08 14:04:20 -0800 | [diff] [blame] | 4670 | |
| 4671 | if (mode != BRIDGE_MODE_UNDEF) { |
| 4672 | if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { |
| 4673 | nla_nest_cancel(skb, br_afspec); |
| 4674 | goto nla_put_failure; |
| 4675 | } |
| 4676 | } |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 4677 | if (vlan_fill) { |
| 4678 | err = vlan_fill(skb, dev, filter_mask); |
| 4679 | if (err) { |
| 4680 | nla_nest_cancel(skb, br_afspec); |
| 4681 | goto nla_put_failure; |
| 4682 | } |
| 4683 | } |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4684 | nla_nest_end(skb, br_afspec); |
| 4685 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 4686 | protinfo = nla_nest_start(skb, IFLA_PROTINFO); |
Scott Feldman | 2c3c031 | 2014-11-28 14:34:25 +0100 | [diff] [blame] | 4687 | if (!protinfo) |
| 4688 | goto nla_put_failure; |
| 4689 | |
| 4690 | if (brport_nla_put_flag(skb, flags, mask, |
| 4691 | IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || |
| 4692 | brport_nla_put_flag(skb, flags, mask, |
| 4693 | IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || |
| 4694 | brport_nla_put_flag(skb, flags, mask, |
| 4695 | IFLA_BRPORT_FAST_LEAVE, |
| 4696 | BR_MULTICAST_FAST_LEAVE) || |
| 4697 | brport_nla_put_flag(skb, flags, mask, |
| 4698 | IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || |
| 4699 | brport_nla_put_flag(skb, flags, mask, |
| 4700 | IFLA_BRPORT_LEARNING, BR_LEARNING) || |
| 4701 | brport_nla_put_flag(skb, flags, mask, |
| 4702 | IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || |
| 4703 | brport_nla_put_flag(skb, flags, mask, |
| 4704 | IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || |
| 4705 | brport_nla_put_flag(skb, flags, mask, |
Julian Wiedmann | 583cb0b | 2020-02-17 14:45:01 +0100 | [diff] [blame] | 4706 | IFLA_BRPORT_PROXYARP, BR_PROXYARP) || |
| 4707 | brport_nla_put_flag(skb, flags, mask, |
| 4708 | IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) || |
| 4709 | brport_nla_put_flag(skb, flags, mask, |
| 4710 | IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) { |
Scott Feldman | 2c3c031 | 2014-11-28 14:34:25 +0100 | [diff] [blame] | 4711 | nla_nest_cancel(skb, protinfo); |
| 4712 | goto nla_put_failure; |
| 4713 | } |
| 4714 | |
| 4715 | nla_nest_end(skb, protinfo); |
| 4716 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 4717 | nlmsg_end(skb, nlh); |
| 4718 | return 0; |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4719 | nla_put_failure: |
| 4720 | nlmsg_cancel(skb, nlh); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 4721 | return err ? err : -EMSGSIZE; |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4722 | } |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 4723 | EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 4724 | |
David Ahern | 2d011be | 2018-10-07 20:16:31 -0700 | [diff] [blame] | 4725 | static int valid_bridge_getlink_req(const struct nlmsghdr *nlh, |
| 4726 | bool strict_check, u32 *filter_mask, |
| 4727 | struct netlink_ext_ack *extack) |
| 4728 | { |
| 4729 | struct nlattr *tb[IFLA_MAX+1]; |
| 4730 | int err, i; |
| 4731 | |
| 4732 | if (strict_check) { |
| 4733 | struct ifinfomsg *ifm; |
| 4734 | |
| 4735 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { |
| 4736 | NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump"); |
| 4737 | return -EINVAL; |
| 4738 | } |
| 4739 | |
| 4740 | ifm = nlmsg_data(nlh); |
| 4741 | if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || |
| 4742 | ifm->ifi_change || ifm->ifi_index) { |
| 4743 | NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request"); |
| 4744 | return -EINVAL; |
| 4745 | } |
| 4746 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 4747 | err = nlmsg_parse_deprecated_strict(nlh, |
| 4748 | sizeof(struct ifinfomsg), |
| 4749 | tb, IFLA_MAX, ifla_policy, |
| 4750 | extack); |
David Ahern | 2d011be | 2018-10-07 20:16:31 -0700 | [diff] [blame] | 4751 | } else { |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 4752 | err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), |
| 4753 | tb, IFLA_MAX, ifla_policy, |
| 4754 | extack); |
David Ahern | 2d011be | 2018-10-07 20:16:31 -0700 | [diff] [blame] | 4755 | } |
| 4756 | if (err < 0) |
| 4757 | return err; |
| 4758 | |
| 4759 | /* new attributes should only be added with strict checking */ |
| 4760 | for (i = 0; i <= IFLA_MAX; ++i) { |
| 4761 | if (!tb[i]) |
| 4762 | continue; |
| 4763 | |
| 4764 | switch (i) { |
| 4765 | case IFLA_EXT_MASK: |
| 4766 | *filter_mask = nla_get_u32(tb[i]); |
| 4767 | break; |
| 4768 | default: |
| 4769 | if (strict_check) { |
| 4770 | NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request"); |
| 4771 | return -EINVAL; |
| 4772 | } |
| 4773 | } |
| 4774 | } |
| 4775 | |
| 4776 | return 0; |
| 4777 | } |
| 4778 | |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4779 | static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) |
| 4780 | { |
David Ahern | 2d011be | 2018-10-07 20:16:31 -0700 | [diff] [blame] | 4781 | const struct nlmsghdr *nlh = cb->nlh; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4782 | struct net *net = sock_net(skb->sk); |
| 4783 | struct net_device *dev; |
| 4784 | int idx = 0; |
| 4785 | u32 portid = NETLINK_CB(cb->skb).portid; |
David Ahern | 2d011be | 2018-10-07 20:16:31 -0700 | [diff] [blame] | 4786 | u32 seq = nlh->nlmsg_seq; |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 4787 | u32 filter_mask = 0; |
Roopa Prabhu | d64f69b | 2015-09-15 14:44:29 -0700 | [diff] [blame] | 4788 | int err; |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 4789 | |
David Ahern | 2d011be | 2018-10-07 20:16:31 -0700 | [diff] [blame] | 4790 | err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask, |
| 4791 | cb->extack); |
| 4792 | if (err < 0 && cb->strict_check) |
| 4793 | return err; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4794 | |
| 4795 | rcu_read_lock(); |
| 4796 | for_each_netdev_rcu(net, dev) { |
| 4797 | const struct net_device_ops *ops = dev->netdev_ops; |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 4798 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4799 | |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 4800 | if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { |
Roopa Prabhu | d64f69b | 2015-09-15 14:44:29 -0700 | [diff] [blame] | 4801 | if (idx >= cb->args[0]) { |
| 4802 | err = br_dev->netdev_ops->ndo_bridge_getlink( |
| 4803 | skb, portid, seq, dev, |
| 4804 | filter_mask, NLM_F_MULTI); |
David Ahern | f6c5775 | 2017-05-15 23:19:17 -0700 | [diff] [blame] | 4805 | if (err < 0 && err != -EOPNOTSUPP) { |
| 4806 | if (likely(skb->len)) |
| 4807 | break; |
| 4808 | |
| 4809 | goto out_err; |
| 4810 | } |
Roopa Prabhu | d64f69b | 2015-09-15 14:44:29 -0700 | [diff] [blame] | 4811 | } |
Ben Hutchings | 25b1e67 | 2012-11-02 12:56:52 +0000 | [diff] [blame] | 4812 | idx++; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4813 | } |
| 4814 | |
| 4815 | if (ops->ndo_bridge_getlink) { |
Roopa Prabhu | d64f69b | 2015-09-15 14:44:29 -0700 | [diff] [blame] | 4816 | if (idx >= cb->args[0]) { |
| 4817 | err = ops->ndo_bridge_getlink(skb, portid, |
| 4818 | seq, dev, |
| 4819 | filter_mask, |
| 4820 | NLM_F_MULTI); |
David Ahern | f6c5775 | 2017-05-15 23:19:17 -0700 | [diff] [blame] | 4821 | if (err < 0 && err != -EOPNOTSUPP) { |
| 4822 | if (likely(skb->len)) |
| 4823 | break; |
| 4824 | |
| 4825 | goto out_err; |
| 4826 | } |
Roopa Prabhu | d64f69b | 2015-09-15 14:44:29 -0700 | [diff] [blame] | 4827 | } |
Ben Hutchings | 25b1e67 | 2012-11-02 12:56:52 +0000 | [diff] [blame] | 4828 | idx++; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4829 | } |
| 4830 | } |
David Ahern | f6c5775 | 2017-05-15 23:19:17 -0700 | [diff] [blame] | 4831 | err = skb->len; |
| 4832 | out_err: |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4833 | rcu_read_unlock(); |
| 4834 | cb->args[0] = idx; |
| 4835 | |
David Ahern | f6c5775 | 2017-05-15 23:19:17 -0700 | [diff] [blame] | 4836 | return err; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4837 | } |
| 4838 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4839 | static inline size_t bridge_nlmsg_size(void) |
| 4840 | { |
| 4841 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) |
| 4842 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ |
| 4843 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ |
| 4844 | + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ |
| 4845 | + nla_total_size(sizeof(u32)) /* IFLA_MTU */ |
| 4846 | + nla_total_size(sizeof(u32)) /* IFLA_LINK */ |
| 4847 | + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ |
| 4848 | + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ |
| 4849 | + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ |
| 4850 | + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ |
| 4851 | + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ |
| 4852 | } |
| 4853 | |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 4854 | static int rtnl_bridge_notify(struct net_device *dev) |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4855 | { |
| 4856 | struct net *net = dev_net(dev); |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4857 | struct sk_buff *skb; |
| 4858 | int err = -EOPNOTSUPP; |
| 4859 | |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 4860 | if (!dev->netdev_ops->ndo_bridge_getlink) |
| 4861 | return 0; |
| 4862 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4863 | skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); |
| 4864 | if (!skb) { |
| 4865 | err = -ENOMEM; |
| 4866 | goto errout; |
| 4867 | } |
| 4868 | |
Nicolas Dichtel | 46c264d | 2015-04-28 18:33:49 +0200 | [diff] [blame] | 4869 | 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] | 4870 | if (err < 0) |
| 4871 | goto errout; |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4872 | |
Ido Schimmel | d2e381c | 2021-06-09 14:17:53 +0300 | [diff] [blame] | 4873 | /* Notification info is only filled for bridge ports, not the bridge |
| 4874 | * device itself. Therefore, a zero notification length is valid and |
| 4875 | * should not result in an error. |
| 4876 | */ |
| 4877 | if (!skb->len) |
Roopa Prabhu | 59ccaaa | 2015-01-28 16:23:11 -0800 | [diff] [blame] | 4878 | goto errout; |
| 4879 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4880 | rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); |
| 4881 | return 0; |
| 4882 | errout: |
| 4883 | WARN_ON(err == -EMSGSIZE); |
| 4884 | kfree_skb(skb); |
Roopa Prabhu | 59ccaaa | 2015-01-28 16:23:11 -0800 | [diff] [blame] | 4885 | if (err) |
| 4886 | rtnl_set_sk_err(net, RTNLGRP_LINK, err); |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4887 | return err; |
| 4888 | } |
| 4889 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 4890 | static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 4891 | struct netlink_ext_ack *extack) |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4892 | { |
| 4893 | struct net *net = sock_net(skb->sk); |
| 4894 | struct ifinfomsg *ifm; |
| 4895 | struct net_device *dev; |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4896 | struct nlattr *br_spec, *attr = NULL; |
| 4897 | int rem, err = -EOPNOTSUPP; |
Rosen, Rami | 4de8b41 | 2015-01-19 11:45:04 +0200 | [diff] [blame] | 4898 | u16 flags = 0; |
John Fastabend | c38e01b | 2012-11-02 16:32:36 +0000 | [diff] [blame] | 4899 | bool have_flags = false; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4900 | |
| 4901 | if (nlmsg_len(nlh) < sizeof(*ifm)) |
| 4902 | return -EINVAL; |
| 4903 | |
| 4904 | ifm = nlmsg_data(nlh); |
| 4905 | if (ifm->ifi_family != AF_BRIDGE) |
| 4906 | return -EPFNOSUPPORT; |
| 4907 | |
| 4908 | dev = __dev_get_by_index(net, ifm->ifi_index); |
| 4909 | if (!dev) { |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 4910 | NL_SET_ERR_MSG(extack, "unknown ifindex"); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4911 | return -ENODEV; |
| 4912 | } |
| 4913 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4914 | br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); |
| 4915 | if (br_spec) { |
| 4916 | nla_for_each_nested(attr, br_spec, rem) { |
| 4917 | if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { |
Thomas Graf | 6e8d1c5 | 2014-11-26 13:42:16 +0100 | [diff] [blame] | 4918 | if (nla_len(attr) < sizeof(flags)) |
| 4919 | return -EINVAL; |
| 4920 | |
John Fastabend | c38e01b | 2012-11-02 16:32:36 +0000 | [diff] [blame] | 4921 | have_flags = true; |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4922 | flags = nla_get_u16(attr); |
| 4923 | break; |
| 4924 | } |
| 4925 | } |
| 4926 | } |
| 4927 | |
| 4928 | if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 4929 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
| 4930 | |
| 4931 | if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4932 | err = -EOPNOTSUPP; |
| 4933 | goto out; |
| 4934 | } |
| 4935 | |
Petr Machata | 2fd527b | 2018-12-12 17:02:48 +0000 | [diff] [blame] | 4936 | err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags, |
| 4937 | extack); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4938 | if (err) |
| 4939 | goto out; |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4940 | |
| 4941 | flags &= ~BRIDGE_FLAGS_MASTER; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4942 | } |
| 4943 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4944 | if ((flags & BRIDGE_FLAGS_SELF)) { |
| 4945 | if (!dev->netdev_ops->ndo_bridge_setlink) |
| 4946 | err = -EOPNOTSUPP; |
| 4947 | else |
Roopa Prabhu | add511b | 2015-01-29 22:40:12 -0800 | [diff] [blame] | 4948 | err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, |
Petr Machata | 2fd527b | 2018-12-12 17:02:48 +0000 | [diff] [blame] | 4949 | flags, |
| 4950 | extack); |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 4951 | if (!err) { |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4952 | flags &= ~BRIDGE_FLAGS_SELF; |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 4953 | |
| 4954 | /* Generate event to notify upper layer of bridge |
| 4955 | * change |
| 4956 | */ |
| 4957 | err = rtnl_bridge_notify(dev); |
| 4958 | } |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4959 | } |
| 4960 | |
John Fastabend | c38e01b | 2012-11-02 16:32:36 +0000 | [diff] [blame] | 4961 | if (have_flags) |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 4962 | memcpy(nla_data(attr), &flags, sizeof(flags)); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 4963 | out: |
| 4964 | return err; |
| 4965 | } |
| 4966 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 4967 | static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 4968 | struct netlink_ext_ack *extack) |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 4969 | { |
| 4970 | struct net *net = sock_net(skb->sk); |
| 4971 | struct ifinfomsg *ifm; |
| 4972 | struct net_device *dev; |
| 4973 | struct nlattr *br_spec, *attr = NULL; |
| 4974 | int rem, err = -EOPNOTSUPP; |
Rosen, Rami | 4de8b41 | 2015-01-19 11:45:04 +0200 | [diff] [blame] | 4975 | u16 flags = 0; |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 4976 | bool have_flags = false; |
| 4977 | |
| 4978 | if (nlmsg_len(nlh) < sizeof(*ifm)) |
| 4979 | return -EINVAL; |
| 4980 | |
| 4981 | ifm = nlmsg_data(nlh); |
| 4982 | if (ifm->ifi_family != AF_BRIDGE) |
| 4983 | return -EPFNOSUPPORT; |
| 4984 | |
| 4985 | dev = __dev_get_by_index(net, ifm->ifi_index); |
| 4986 | if (!dev) { |
Florian Westphal | b88d12e | 2017-10-10 17:10:04 +0200 | [diff] [blame] | 4987 | NL_SET_ERR_MSG(extack, "unknown ifindex"); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 4988 | return -ENODEV; |
| 4989 | } |
| 4990 | |
| 4991 | br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); |
| 4992 | if (br_spec) { |
| 4993 | nla_for_each_nested(attr, br_spec, rem) { |
| 4994 | if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { |
Thomas Graf | 6e8d1c5 | 2014-11-26 13:42:16 +0100 | [diff] [blame] | 4995 | if (nla_len(attr) < sizeof(flags)) |
| 4996 | return -EINVAL; |
| 4997 | |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 4998 | have_flags = true; |
| 4999 | flags = nla_get_u16(attr); |
| 5000 | break; |
| 5001 | } |
| 5002 | } |
| 5003 | } |
| 5004 | |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 5005 | if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { |
| 5006 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
| 5007 | |
| 5008 | if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { |
| 5009 | err = -EOPNOTSUPP; |
| 5010 | goto out; |
| 5011 | } |
| 5012 | |
Roopa Prabhu | add511b | 2015-01-29 22:40:12 -0800 | [diff] [blame] | 5013 | err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 5014 | if (err) |
| 5015 | goto out; |
| 5016 | |
| 5017 | flags &= ~BRIDGE_FLAGS_MASTER; |
| 5018 | } |
| 5019 | |
| 5020 | if ((flags & BRIDGE_FLAGS_SELF)) { |
| 5021 | if (!dev->netdev_ops->ndo_bridge_dellink) |
| 5022 | err = -EOPNOTSUPP; |
| 5023 | else |
Roopa Prabhu | add511b | 2015-01-29 22:40:12 -0800 | [diff] [blame] | 5024 | err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, |
| 5025 | flags); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 5026 | |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 5027 | if (!err) { |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 5028 | flags &= ~BRIDGE_FLAGS_SELF; |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 5029 | |
| 5030 | /* Generate event to notify upper layer of bridge |
| 5031 | * change |
| 5032 | */ |
| 5033 | err = rtnl_bridge_notify(dev); |
| 5034 | } |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 5035 | } |
| 5036 | |
| 5037 | if (have_flags) |
| 5038 | memcpy(nla_data(attr), &flags, sizeof(flags)); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 5039 | out: |
| 5040 | return err; |
| 5041 | } |
| 5042 | |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5043 | static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) |
| 5044 | { |
| 5045 | return (mask & IFLA_STATS_FILTER_BIT(attrid)) && |
| 5046 | (!idxattr || idxattr == attrid); |
| 5047 | } |
| 5048 | |
Nogah Frankel | 69ae6ad | 2016-09-16 15:05:37 +0200 | [diff] [blame] | 5049 | #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1) |
| 5050 | static int rtnl_get_offload_stats_attr_size(int attr_id) |
| 5051 | { |
| 5052 | switch (attr_id) { |
| 5053 | case IFLA_OFFLOAD_XSTATS_CPU_HIT: |
| 5054 | return sizeof(struct rtnl_link_stats64); |
| 5055 | } |
| 5056 | |
| 5057 | return 0; |
| 5058 | } |
| 5059 | |
| 5060 | static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev, |
| 5061 | int *prividx) |
| 5062 | { |
| 5063 | struct nlattr *attr = NULL; |
| 5064 | int attr_id, size; |
| 5065 | void *attr_data; |
| 5066 | int err; |
| 5067 | |
| 5068 | if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && |
| 5069 | dev->netdev_ops->ndo_get_offload_stats)) |
| 5070 | return -ENODATA; |
| 5071 | |
| 5072 | for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; |
| 5073 | attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { |
| 5074 | if (attr_id < *prividx) |
| 5075 | continue; |
| 5076 | |
| 5077 | size = rtnl_get_offload_stats_attr_size(attr_id); |
| 5078 | if (!size) |
| 5079 | continue; |
| 5080 | |
Or Gerlitz | 3df5b3c | 2016-11-22 23:09:54 +0200 | [diff] [blame] | 5081 | if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) |
Nogah Frankel | 69ae6ad | 2016-09-16 15:05:37 +0200 | [diff] [blame] | 5082 | continue; |
| 5083 | |
| 5084 | attr = nla_reserve_64bit(skb, attr_id, size, |
| 5085 | IFLA_OFFLOAD_XSTATS_UNSPEC); |
| 5086 | if (!attr) |
| 5087 | goto nla_put_failure; |
| 5088 | |
| 5089 | attr_data = nla_data(attr); |
| 5090 | memset(attr_data, 0, size); |
| 5091 | err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, |
| 5092 | attr_data); |
| 5093 | if (err) |
| 5094 | goto get_offload_stats_failure; |
| 5095 | } |
| 5096 | |
| 5097 | if (!attr) |
| 5098 | return -ENODATA; |
| 5099 | |
| 5100 | *prividx = 0; |
| 5101 | return 0; |
| 5102 | |
| 5103 | nla_put_failure: |
| 5104 | err = -EMSGSIZE; |
| 5105 | get_offload_stats_failure: |
| 5106 | *prividx = attr_id; |
| 5107 | return err; |
| 5108 | } |
| 5109 | |
| 5110 | static int rtnl_get_offload_stats_size(const struct net_device *dev) |
| 5111 | { |
| 5112 | int nla_size = 0; |
| 5113 | int attr_id; |
| 5114 | int size; |
| 5115 | |
| 5116 | if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && |
| 5117 | dev->netdev_ops->ndo_get_offload_stats)) |
| 5118 | return 0; |
| 5119 | |
| 5120 | for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; |
| 5121 | attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { |
Or Gerlitz | 3df5b3c | 2016-11-22 23:09:54 +0200 | [diff] [blame] | 5122 | if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) |
Nogah Frankel | 69ae6ad | 2016-09-16 15:05:37 +0200 | [diff] [blame] | 5123 | continue; |
| 5124 | size = rtnl_get_offload_stats_attr_size(attr_id); |
| 5125 | nla_size += nla_total_size_64bit(size); |
| 5126 | } |
| 5127 | |
| 5128 | if (nla_size != 0) |
| 5129 | nla_size += nla_total_size(0); |
| 5130 | |
| 5131 | return nla_size; |
| 5132 | } |
| 5133 | |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5134 | static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, |
| 5135 | int type, u32 pid, u32 seq, u32 change, |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5136 | unsigned int flags, unsigned int filter_mask, |
| 5137 | int *idxattr, int *prividx) |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5138 | { |
| 5139 | struct if_stats_msg *ifsm; |
| 5140 | struct nlmsghdr *nlh; |
| 5141 | struct nlattr *attr; |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5142 | int s_prividx = *prividx; |
Nogah Frankel | 69ae6ad | 2016-09-16 15:05:37 +0200 | [diff] [blame] | 5143 | int err; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5144 | |
| 5145 | ASSERT_RTNL(); |
| 5146 | |
| 5147 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); |
| 5148 | if (!nlh) |
| 5149 | return -EMSGSIZE; |
| 5150 | |
| 5151 | ifsm = nlmsg_data(nlh); |
Nikolay Aleksandrov | ce024f4 | 2017-10-03 13:20:48 +0300 | [diff] [blame] | 5152 | ifsm->family = PF_UNSPEC; |
| 5153 | ifsm->pad1 = 0; |
| 5154 | ifsm->pad2 = 0; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5155 | ifsm->ifindex = dev->ifindex; |
| 5156 | ifsm->filter_mask = filter_mask; |
| 5157 | |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5158 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5159 | struct rtnl_link_stats64 *sp; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5160 | |
Nicolas Dichtel | 58414d3 | 2016-04-21 18:58:25 +0200 | [diff] [blame] | 5161 | attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, |
| 5162 | sizeof(struct rtnl_link_stats64), |
| 5163 | IFLA_STATS_UNSPEC); |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5164 | if (!attr) |
| 5165 | goto nla_put_failure; |
| 5166 | |
| 5167 | sp = nla_data(attr); |
| 5168 | dev_get_stats(dev, sp); |
| 5169 | } |
| 5170 | |
Nikolay Aleksandrov | 97a47fa | 2016-04-30 10:25:27 +0200 | [diff] [blame] | 5171 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { |
| 5172 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; |
| 5173 | |
| 5174 | if (ops && ops->fill_linkxstats) { |
Nikolay Aleksandrov | 97a47fa | 2016-04-30 10:25:27 +0200 | [diff] [blame] | 5175 | *idxattr = IFLA_STATS_LINK_XSTATS; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 5176 | attr = nla_nest_start_noflag(skb, |
| 5177 | IFLA_STATS_LINK_XSTATS); |
Nikolay Aleksandrov | 97a47fa | 2016-04-30 10:25:27 +0200 | [diff] [blame] | 5178 | if (!attr) |
| 5179 | goto nla_put_failure; |
| 5180 | |
Nikolay Aleksandrov | 80e73cc | 2016-06-28 16:57:05 +0200 | [diff] [blame] | 5181 | err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); |
| 5182 | nla_nest_end(skb, attr); |
| 5183 | if (err) |
| 5184 | goto nla_put_failure; |
| 5185 | *idxattr = 0; |
| 5186 | } |
| 5187 | } |
| 5188 | |
| 5189 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, |
| 5190 | *idxattr)) { |
| 5191 | const struct rtnl_link_ops *ops = NULL; |
| 5192 | const struct net_device *master; |
| 5193 | |
| 5194 | master = netdev_master_upper_dev_get(dev); |
| 5195 | if (master) |
| 5196 | ops = master->rtnl_link_ops; |
| 5197 | if (ops && ops->fill_linkxstats) { |
Nikolay Aleksandrov | 80e73cc | 2016-06-28 16:57:05 +0200 | [diff] [blame] | 5198 | *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 5199 | attr = nla_nest_start_noflag(skb, |
| 5200 | IFLA_STATS_LINK_XSTATS_SLAVE); |
Nikolay Aleksandrov | 80e73cc | 2016-06-28 16:57:05 +0200 | [diff] [blame] | 5201 | if (!attr) |
| 5202 | goto nla_put_failure; |
| 5203 | |
| 5204 | err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); |
Nikolay Aleksandrov | 97a47fa | 2016-04-30 10:25:27 +0200 | [diff] [blame] | 5205 | nla_nest_end(skb, attr); |
| 5206 | if (err) |
| 5207 | goto nla_put_failure; |
| 5208 | *idxattr = 0; |
| 5209 | } |
| 5210 | } |
| 5211 | |
Nogah Frankel | 69ae6ad | 2016-09-16 15:05:37 +0200 | [diff] [blame] | 5212 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, |
| 5213 | *idxattr)) { |
| 5214 | *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 5215 | attr = nla_nest_start_noflag(skb, |
| 5216 | IFLA_STATS_LINK_OFFLOAD_XSTATS); |
Nogah Frankel | 69ae6ad | 2016-09-16 15:05:37 +0200 | [diff] [blame] | 5217 | if (!attr) |
| 5218 | goto nla_put_failure; |
| 5219 | |
| 5220 | err = rtnl_get_offload_stats(skb, dev, prividx); |
| 5221 | if (err == -ENODATA) |
| 5222 | nla_nest_cancel(skb, attr); |
| 5223 | else |
| 5224 | nla_nest_end(skb, attr); |
| 5225 | |
| 5226 | if (err && err != -ENODATA) |
| 5227 | goto nla_put_failure; |
| 5228 | *idxattr = 0; |
| 5229 | } |
| 5230 | |
Robert Shearman | aefb4d4 | 2017-01-16 14:16:36 +0000 | [diff] [blame] | 5231 | if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) { |
| 5232 | struct rtnl_af_ops *af_ops; |
| 5233 | |
| 5234 | *idxattr = IFLA_STATS_AF_SPEC; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 5235 | attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC); |
Robert Shearman | aefb4d4 | 2017-01-16 14:16:36 +0000 | [diff] [blame] | 5236 | if (!attr) |
| 5237 | goto nla_put_failure; |
| 5238 | |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 5239 | rcu_read_lock(); |
| 5240 | list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { |
Robert Shearman | aefb4d4 | 2017-01-16 14:16:36 +0000 | [diff] [blame] | 5241 | if (af_ops->fill_stats_af) { |
| 5242 | struct nlattr *af; |
| 5243 | int err; |
| 5244 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 5245 | af = nla_nest_start_noflag(skb, |
| 5246 | af_ops->family); |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 5247 | if (!af) { |
| 5248 | rcu_read_unlock(); |
Robert Shearman | aefb4d4 | 2017-01-16 14:16:36 +0000 | [diff] [blame] | 5249 | goto nla_put_failure; |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 5250 | } |
Robert Shearman | aefb4d4 | 2017-01-16 14:16:36 +0000 | [diff] [blame] | 5251 | err = af_ops->fill_stats_af(skb, dev); |
| 5252 | |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 5253 | if (err == -ENODATA) { |
Robert Shearman | aefb4d4 | 2017-01-16 14:16:36 +0000 | [diff] [blame] | 5254 | nla_nest_cancel(skb, af); |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 5255 | } else if (err < 0) { |
| 5256 | rcu_read_unlock(); |
Robert Shearman | aefb4d4 | 2017-01-16 14:16:36 +0000 | [diff] [blame] | 5257 | goto nla_put_failure; |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 5258 | } |
Robert Shearman | aefb4d4 | 2017-01-16 14:16:36 +0000 | [diff] [blame] | 5259 | |
| 5260 | nla_nest_end(skb, af); |
| 5261 | } |
| 5262 | } |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 5263 | rcu_read_unlock(); |
Robert Shearman | aefb4d4 | 2017-01-16 14:16:36 +0000 | [diff] [blame] | 5264 | |
| 5265 | nla_nest_end(skb, attr); |
| 5266 | |
| 5267 | *idxattr = 0; |
| 5268 | } |
| 5269 | |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5270 | nlmsg_end(skb, nlh); |
| 5271 | |
| 5272 | return 0; |
| 5273 | |
| 5274 | nla_put_failure: |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5275 | /* not a multi message or no progress mean a real error */ |
| 5276 | if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) |
| 5277 | nlmsg_cancel(skb, nlh); |
| 5278 | else |
| 5279 | nlmsg_end(skb, nlh); |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5280 | |
| 5281 | return -EMSGSIZE; |
| 5282 | } |
| 5283 | |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5284 | static size_t if_nlmsg_stats_size(const struct net_device *dev, |
| 5285 | u32 filter_mask) |
| 5286 | { |
Eric Dumazet | d343679 | 2021-10-05 14:04:17 -0700 | [diff] [blame] | 5287 | size_t size = NLMSG_ALIGN(sizeof(struct if_stats_msg)); |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5288 | |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5289 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5290 | size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); |
| 5291 | |
Nikolay Aleksandrov | 97a47fa | 2016-04-30 10:25:27 +0200 | [diff] [blame] | 5292 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { |
| 5293 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; |
Nikolay Aleksandrov | 80e73cc | 2016-06-28 16:57:05 +0200 | [diff] [blame] | 5294 | int attr = IFLA_STATS_LINK_XSTATS; |
Nikolay Aleksandrov | 97a47fa | 2016-04-30 10:25:27 +0200 | [diff] [blame] | 5295 | |
| 5296 | if (ops && ops->get_linkxstats_size) { |
Nikolay Aleksandrov | 80e73cc | 2016-06-28 16:57:05 +0200 | [diff] [blame] | 5297 | size += nla_total_size(ops->get_linkxstats_size(dev, |
| 5298 | attr)); |
Nikolay Aleksandrov | 97a47fa | 2016-04-30 10:25:27 +0200 | [diff] [blame] | 5299 | /* for IFLA_STATS_LINK_XSTATS */ |
| 5300 | size += nla_total_size(0); |
| 5301 | } |
| 5302 | } |
| 5303 | |
Nikolay Aleksandrov | 80e73cc | 2016-06-28 16:57:05 +0200 | [diff] [blame] | 5304 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) { |
| 5305 | struct net_device *_dev = (struct net_device *)dev; |
| 5306 | const struct rtnl_link_ops *ops = NULL; |
| 5307 | const struct net_device *master; |
| 5308 | |
| 5309 | /* netdev_master_upper_dev_get can't take const */ |
| 5310 | master = netdev_master_upper_dev_get(_dev); |
| 5311 | if (master) |
| 5312 | ops = master->rtnl_link_ops; |
| 5313 | if (ops && ops->get_linkxstats_size) { |
| 5314 | int attr = IFLA_STATS_LINK_XSTATS_SLAVE; |
| 5315 | |
| 5316 | size += nla_total_size(ops->get_linkxstats_size(dev, |
| 5317 | attr)); |
| 5318 | /* for IFLA_STATS_LINK_XSTATS_SLAVE */ |
| 5319 | size += nla_total_size(0); |
| 5320 | } |
| 5321 | } |
| 5322 | |
Nogah Frankel | 69ae6ad | 2016-09-16 15:05:37 +0200 | [diff] [blame] | 5323 | if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) |
| 5324 | size += rtnl_get_offload_stats_size(dev); |
| 5325 | |
Robert Shearman | aefb4d4 | 2017-01-16 14:16:36 +0000 | [diff] [blame] | 5326 | if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) { |
| 5327 | struct rtnl_af_ops *af_ops; |
| 5328 | |
| 5329 | /* for IFLA_STATS_AF_SPEC */ |
| 5330 | size += nla_total_size(0); |
| 5331 | |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 5332 | rcu_read_lock(); |
| 5333 | list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { |
Robert Shearman | aefb4d4 | 2017-01-16 14:16:36 +0000 | [diff] [blame] | 5334 | if (af_ops->get_stats_af_size) { |
| 5335 | size += nla_total_size( |
| 5336 | af_ops->get_stats_af_size(dev)); |
| 5337 | |
| 5338 | /* for AF_* */ |
| 5339 | size += nla_total_size(0); |
| 5340 | } |
| 5341 | } |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 5342 | rcu_read_unlock(); |
Robert Shearman | aefb4d4 | 2017-01-16 14:16:36 +0000 | [diff] [blame] | 5343 | } |
| 5344 | |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5345 | return size; |
| 5346 | } |
| 5347 | |
Jakub Kicinski | 51bc860 | 2019-01-18 10:46:14 -0800 | [diff] [blame] | 5348 | static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check, |
| 5349 | bool is_dump, struct netlink_ext_ack *extack) |
| 5350 | { |
| 5351 | struct if_stats_msg *ifsm; |
| 5352 | |
Eric Dumazet | 69f23a0 | 2019-04-14 11:02:05 -0700 | [diff] [blame] | 5353 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) { |
Jakub Kicinski | 51bc860 | 2019-01-18 10:46:14 -0800 | [diff] [blame] | 5354 | NL_SET_ERR_MSG(extack, "Invalid header for stats dump"); |
| 5355 | return -EINVAL; |
| 5356 | } |
| 5357 | |
| 5358 | if (!strict_check) |
| 5359 | return 0; |
| 5360 | |
| 5361 | ifsm = nlmsg_data(nlh); |
| 5362 | |
| 5363 | /* only requests using strict checks can pass data to influence |
| 5364 | * the dump. The legacy exception is filter_mask. |
| 5365 | */ |
| 5366 | if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) { |
| 5367 | NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request"); |
| 5368 | return -EINVAL; |
| 5369 | } |
| 5370 | if (nlmsg_attrlen(nlh, sizeof(*ifsm))) { |
| 5371 | NL_SET_ERR_MSG(extack, "Invalid attributes after stats header"); |
| 5372 | return -EINVAL; |
| 5373 | } |
Jakub Kicinski | 6300acb | 2019-01-18 10:46:15 -0800 | [diff] [blame] | 5374 | if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) { |
| 5375 | NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask"); |
| 5376 | return -EINVAL; |
| 5377 | } |
Jakub Kicinski | 51bc860 | 2019-01-18 10:46:14 -0800 | [diff] [blame] | 5378 | |
| 5379 | return 0; |
| 5380 | } |
| 5381 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 5382 | static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 5383 | struct netlink_ext_ack *extack) |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5384 | { |
| 5385 | struct net *net = sock_net(skb->sk); |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5386 | struct net_device *dev = NULL; |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5387 | int idxattr = 0, prividx = 0; |
| 5388 | struct if_stats_msg *ifsm; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5389 | struct sk_buff *nskb; |
| 5390 | u32 filter_mask; |
| 5391 | int err; |
| 5392 | |
Jakub Kicinski | 51bc860 | 2019-01-18 10:46:14 -0800 | [diff] [blame] | 5393 | err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb), |
| 5394 | false, extack); |
| 5395 | if (err) |
| 5396 | return err; |
Mathias Krause | 4775cc1 | 2016-12-28 17:52:15 +0100 | [diff] [blame] | 5397 | |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5398 | ifsm = nlmsg_data(nlh); |
| 5399 | if (ifsm->ifindex > 0) |
| 5400 | dev = __dev_get_by_index(net, ifsm->ifindex); |
| 5401 | else |
| 5402 | return -EINVAL; |
| 5403 | |
| 5404 | if (!dev) |
| 5405 | return -ENODEV; |
| 5406 | |
| 5407 | filter_mask = ifsm->filter_mask; |
| 5408 | if (!filter_mask) |
| 5409 | return -EINVAL; |
| 5410 | |
| 5411 | nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL); |
| 5412 | if (!nskb) |
| 5413 | return -ENOBUFS; |
| 5414 | |
| 5415 | err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, |
| 5416 | NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5417 | 0, filter_mask, &idxattr, &prividx); |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5418 | if (err < 0) { |
| 5419 | /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ |
| 5420 | WARN_ON(err == -EMSGSIZE); |
| 5421 | kfree_skb(nskb); |
| 5422 | } else { |
| 5423 | err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); |
| 5424 | } |
| 5425 | |
| 5426 | return err; |
| 5427 | } |
| 5428 | |
| 5429 | static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 5430 | { |
David Ahern | 841891e | 2018-10-07 20:16:32 -0700 | [diff] [blame] | 5431 | struct netlink_ext_ack *extack = cb->extack; |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5432 | int h, s_h, err, s_idx, s_idxattr, s_prividx; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5433 | struct net *net = sock_net(skb->sk); |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5434 | unsigned int flags = NLM_F_MULTI; |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5435 | struct if_stats_msg *ifsm; |
| 5436 | struct hlist_head *head; |
| 5437 | struct net_device *dev; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5438 | u32 filter_mask = 0; |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5439 | int idx = 0; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5440 | |
| 5441 | s_h = cb->args[0]; |
| 5442 | s_idx = cb->args[1]; |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5443 | s_idxattr = cb->args[2]; |
| 5444 | s_prividx = cb->args[3]; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5445 | |
| 5446 | cb->seq = net->dev_base_seq; |
| 5447 | |
Jakub Kicinski | 51bc860 | 2019-01-18 10:46:14 -0800 | [diff] [blame] | 5448 | err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack); |
| 5449 | if (err) |
| 5450 | return err; |
Mathias Krause | 4775cc1 | 2016-12-28 17:52:15 +0100 | [diff] [blame] | 5451 | |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5452 | ifsm = nlmsg_data(cb->nlh); |
| 5453 | filter_mask = ifsm->filter_mask; |
David Ahern | 841891e | 2018-10-07 20:16:32 -0700 | [diff] [blame] | 5454 | if (!filter_mask) { |
| 5455 | 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] | 5456 | return -EINVAL; |
David Ahern | 841891e | 2018-10-07 20:16:32 -0700 | [diff] [blame] | 5457 | } |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5458 | |
| 5459 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { |
| 5460 | idx = 0; |
| 5461 | head = &net->dev_index_head[h]; |
| 5462 | hlist_for_each_entry(dev, head, index_hlist) { |
| 5463 | if (idx < s_idx) |
| 5464 | goto cont; |
| 5465 | err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, |
| 5466 | NETLINK_CB(cb->skb).portid, |
| 5467 | cb->nlh->nlmsg_seq, 0, |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5468 | flags, filter_mask, |
| 5469 | &s_idxattr, &s_prividx); |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5470 | /* If we ran out of room on the first message, |
| 5471 | * we're in trouble |
| 5472 | */ |
| 5473 | WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); |
| 5474 | |
| 5475 | if (err < 0) |
| 5476 | goto out; |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5477 | s_prividx = 0; |
| 5478 | s_idxattr = 0; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5479 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
| 5480 | cont: |
| 5481 | idx++; |
| 5482 | } |
| 5483 | } |
| 5484 | out: |
Nikolay Aleksandrov | e8872a2 | 2016-04-30 10:25:26 +0200 | [diff] [blame] | 5485 | cb->args[3] = s_prividx; |
| 5486 | cb->args[2] = s_idxattr; |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5487 | cb->args[1] = idx; |
| 5488 | cb->args[0] = h; |
| 5489 | |
| 5490 | return skb->len; |
| 5491 | } |
| 5492 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5493 | /* Process one rtnetlink message. */ |
| 5494 | |
Johannes Berg | 2d4bc93 | 2017-04-12 14:34:04 +0200 | [diff] [blame] | 5495 | static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 5496 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5497 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 5498 | struct net *net = sock_net(skb->sk); |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 5499 | struct rtnl_link *link; |
Florian Westphal | e420251 | 2017-12-02 21:44:06 +0100 | [diff] [blame] | 5500 | struct module *owner; |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 5501 | int err = -EOPNOTSUPP; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 5502 | rtnl_doit_func doit; |
Florian Westphal | 62256f9 | 2017-08-09 20:41:52 +0200 | [diff] [blame] | 5503 | unsigned int flags; |
Alexander Kuleshov | 617cfc7 | 2016-01-10 21:26:57 +0600 | [diff] [blame] | 5504 | int kind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5505 | int family; |
| 5506 | int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5507 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5508 | type = nlh->nlmsg_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5509 | if (type > RTM_MAX) |
Thomas Graf | 038890f | 2007-04-05 14:35:52 -0700 | [diff] [blame] | 5510 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5511 | |
| 5512 | type -= RTM_BASE; |
| 5513 | |
| 5514 | /* All the messages must have at least 1 byte length */ |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 5515 | if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5516 | return 0; |
| 5517 | |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 5518 | family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5519 | kind = type&3; |
| 5520 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 5521 | if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 5522 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5523 | |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 5524 | rcu_read_lock(); |
David S. Miller | b8f3ab4 | 2011-01-18 12:40:38 -0800 | [diff] [blame] | 5525 | if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 5526 | struct sock *rtnl; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 5527 | rtnl_dumpit_func dumpit; |
Di Zhu | ebfe3c5 | 2020-10-21 10:00:53 +0800 | [diff] [blame] | 5528 | u32 min_dump_alloc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5529 | |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 5530 | link = rtnl_get_link(family, type); |
| 5531 | if (!link || !link->dumpit) { |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 5532 | family = PF_UNSPEC; |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 5533 | link = rtnl_get_link(family, type); |
| 5534 | if (!link || !link->dumpit) |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 5535 | goto err_unlock; |
| 5536 | } |
Florian Westphal | e420251 | 2017-12-02 21:44:06 +0100 | [diff] [blame] | 5537 | owner = link->owner; |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 5538 | dumpit = link->dumpit; |
Florian Westphal | e1fa6d2 | 2017-08-09 20:41:47 +0200 | [diff] [blame] | 5539 | |
Florian Westphal | 5c2bb9b | 2017-08-10 16:52:58 +0200 | [diff] [blame] | 5540 | if (type == RTM_GETLINK - RTM_BASE) |
Florian Westphal | e1fa6d2 | 2017-08-09 20:41:47 +0200 | [diff] [blame] | 5541 | min_dump_alloc = rtnl_calcit(skb, nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5542 | |
Florian Westphal | e420251 | 2017-12-02 21:44:06 +0100 | [diff] [blame] | 5543 | err = 0; |
| 5544 | /* need to do this before rcu_read_unlock() */ |
| 5545 | if (!try_module_get(owner)) |
| 5546 | err = -EPROTONOSUPPORT; |
| 5547 | |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 5548 | rcu_read_unlock(); |
| 5549 | |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 5550 | rtnl = net->rtnl; |
Florian Westphal | e420251 | 2017-12-02 21:44:06 +0100 | [diff] [blame] | 5551 | if (err == 0) { |
Pablo Neira Ayuso | 80d326f | 2012-02-24 14:30:15 +0000 | [diff] [blame] | 5552 | struct netlink_dump_control c = { |
| 5553 | .dump = dumpit, |
| 5554 | .min_dump_alloc = min_dump_alloc, |
Florian Westphal | e420251 | 2017-12-02 21:44:06 +0100 | [diff] [blame] | 5555 | .module = owner, |
Pablo Neira Ayuso | 80d326f | 2012-02-24 14:30:15 +0000 | [diff] [blame] | 5556 | }; |
| 5557 | err = netlink_dump_start(rtnl, skb, nlh, &c); |
Florian Westphal | e420251 | 2017-12-02 21:44:06 +0100 | [diff] [blame] | 5558 | /* netlink_dump_start() will keep a reference on |
| 5559 | * module if dump is still in progress. |
| 5560 | */ |
| 5561 | module_put(owner); |
Pablo Neira Ayuso | 80d326f | 2012-02-24 14:30:15 +0000 | [diff] [blame] | 5562 | } |
Eric Dumazet | 2907c35 | 2011-05-25 07:34:04 +0000 | [diff] [blame] | 5563 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5564 | } |
| 5565 | |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 5566 | link = rtnl_get_link(family, type); |
| 5567 | if (!link || !link->doit) { |
Florian Westphal | 8caa38b | 2017-08-10 16:53:01 +0200 | [diff] [blame] | 5568 | family = PF_UNSPEC; |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 5569 | link = rtnl_get_link(PF_UNSPEC, type); |
| 5570 | if (!link || !link->doit) |
| 5571 | goto out_unlock; |
Florian Westphal | 8caa38b | 2017-08-10 16:53:01 +0200 | [diff] [blame] | 5572 | } |
| 5573 | |
Florian Westphal | e420251 | 2017-12-02 21:44:06 +0100 | [diff] [blame] | 5574 | owner = link->owner; |
| 5575 | if (!try_module_get(owner)) { |
| 5576 | err = -EPROTONOSUPPORT; |
| 5577 | goto out_unlock; |
| 5578 | } |
| 5579 | |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 5580 | flags = link->flags; |
Florian Westphal | 62256f9 | 2017-08-09 20:41:52 +0200 | [diff] [blame] | 5581 | if (flags & RTNL_FLAG_DOIT_UNLOCKED) { |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 5582 | doit = link->doit; |
Florian Westphal | 62256f9 | 2017-08-09 20:41:52 +0200 | [diff] [blame] | 5583 | rcu_read_unlock(); |
| 5584 | if (doit) |
| 5585 | err = doit(skb, nlh, extack); |
Florian Westphal | e420251 | 2017-12-02 21:44:06 +0100 | [diff] [blame] | 5586 | module_put(owner); |
Florian Westphal | 62256f9 | 2017-08-09 20:41:52 +0200 | [diff] [blame] | 5587 | return err; |
| 5588 | } |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 5589 | rcu_read_unlock(); |
| 5590 | |
Florian Westphal | 0cc0902 | 2017-08-09 20:41:50 +0200 | [diff] [blame] | 5591 | rtnl_lock(); |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 5592 | link = rtnl_get_link(family, type); |
| 5593 | if (link && link->doit) |
| 5594 | err = link->doit(skb, nlh, extack); |
Florian Westphal | 0cc0902 | 2017-08-09 20:41:50 +0200 | [diff] [blame] | 5595 | rtnl_unlock(); |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 5596 | |
Florian Westphal | e420251 | 2017-12-02 21:44:06 +0100 | [diff] [blame] | 5597 | module_put(owner); |
| 5598 | |
Florian Westphal | addf9b9 | 2017-12-02 21:44:05 +0100 | [diff] [blame] | 5599 | return err; |
| 5600 | |
| 5601 | out_unlock: |
| 5602 | rcu_read_unlock(); |
Florian Westphal | 0cc0902 | 2017-08-09 20:41:50 +0200 | [diff] [blame] | 5603 | return err; |
| 5604 | |
| 5605 | err_unlock: |
Florian Westphal | 6853dd4 | 2017-08-09 20:41:51 +0200 | [diff] [blame] | 5606 | rcu_read_unlock(); |
Florian Westphal | 0cc0902 | 2017-08-09 20:41:50 +0200 | [diff] [blame] | 5607 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5608 | } |
| 5609 | |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 5610 | static void rtnetlink_rcv(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5611 | { |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 5612 | netlink_rcv_skb(skb, &rtnetlink_rcv_msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5613 | } |
| 5614 | |
Julien Gomes | 5f729ea | 2017-06-20 13:54:16 -0700 | [diff] [blame] | 5615 | static int rtnetlink_bind(struct net *net, int group) |
| 5616 | { |
| 5617 | switch (group) { |
| 5618 | case RTNLGRP_IPV4_MROUTE_R: |
| 5619 | case RTNLGRP_IPV6_MROUTE_R: |
| 5620 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
| 5621 | return -EPERM; |
| 5622 | break; |
| 5623 | } |
| 5624 | return 0; |
| 5625 | } |
| 5626 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5627 | static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 5628 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 5629 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 5630 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5631 | switch (event) { |
Vlad Yasevich | 5138e86 | 2017-04-04 09:23:41 -0400 | [diff] [blame] | 5632 | case NETDEV_REBOOT: |
Xin Long | 8a21258 | 2017-10-15 18:13:41 +0800 | [diff] [blame] | 5633 | case NETDEV_CHANGEMTU: |
David Ahern | 3753654 | 2017-07-19 10:22:40 -0700 | [diff] [blame] | 5634 | case NETDEV_CHANGEADDR: |
Vlad Yasevich | 5138e86 | 2017-04-04 09:23:41 -0400 | [diff] [blame] | 5635 | case NETDEV_CHANGENAME: |
| 5636 | case NETDEV_FEAT_CHANGE: |
| 5637 | case NETDEV_BONDING_FAILOVER: |
Xin Long | e6e6659 | 2017-10-15 18:13:43 +0800 | [diff] [blame] | 5638 | case NETDEV_POST_TYPE_CHANGE: |
Vlad Yasevich | 5138e86 | 2017-04-04 09:23:41 -0400 | [diff] [blame] | 5639 | case NETDEV_NOTIFY_PEERS: |
Xin Long | dc709f3 | 2017-10-15 18:13:44 +0800 | [diff] [blame] | 5640 | case NETDEV_CHANGEUPPER: |
Vlad Yasevich | 5138e86 | 2017-04-04 09:23:41 -0400 | [diff] [blame] | 5641 | case NETDEV_RESEND_IGMP: |
Vlad Yasevich | 5138e86 | 2017-04-04 09:23:41 -0400 | [diff] [blame] | 5642 | case NETDEV_CHANGEINFODATA: |
Xin Long | eeda3fb | 2017-10-24 13:54:19 +0800 | [diff] [blame] | 5643 | case NETDEV_CHANGELOWERSTATE: |
Xin Long | ebdcf04 | 2017-10-15 18:13:42 +0800 | [diff] [blame] | 5644 | case NETDEV_CHANGE_TX_QUEUE_LEN: |
Vlad Yasevich | 3d3ea5a | 2017-05-27 10:14:34 -0400 | [diff] [blame] | 5645 | rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event), |
Nicolas Dichtel | 38e01b3 | 2018-01-25 15:01:39 +0100 | [diff] [blame] | 5646 | GFP_KERNEL, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5647 | break; |
| 5648 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5649 | break; |
| 5650 | } |
| 5651 | return NOTIFY_DONE; |
| 5652 | } |
| 5653 | |
| 5654 | static struct notifier_block rtnetlink_dev_notifier = { |
| 5655 | .notifier_call = rtnetlink_event, |
| 5656 | }; |
| 5657 | |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 5658 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 5659 | static int __net_init rtnetlink_net_init(struct net *net) |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 5660 | { |
| 5661 | struct sock *sk; |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 5662 | struct netlink_kernel_cfg cfg = { |
| 5663 | .groups = RTNLGRP_MAX, |
| 5664 | .input = rtnetlink_rcv, |
| 5665 | .cb_mutex = &rtnl_mutex, |
Pablo Neira Ayuso | 9785e10 | 2012-09-08 02:53:53 +0000 | [diff] [blame] | 5666 | .flags = NL_CFG_F_NONROOT_RECV, |
Julien Gomes | 5f729ea | 2017-06-20 13:54:16 -0700 | [diff] [blame] | 5667 | .bind = rtnetlink_bind, |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 5668 | }; |
| 5669 | |
Pablo Neira Ayuso | 9f00d97 | 2012-09-08 02:53:54 +0000 | [diff] [blame] | 5670 | sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 5671 | if (!sk) |
| 5672 | return -ENOMEM; |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 5673 | net->rtnl = sk; |
| 5674 | return 0; |
| 5675 | } |
| 5676 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 5677 | static void __net_exit rtnetlink_net_exit(struct net *net) |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 5678 | { |
Denis V. Lunev | 775516b | 2008-01-18 23:55:19 -0800 | [diff] [blame] | 5679 | netlink_kernel_release(net->rtnl); |
| 5680 | net->rtnl = NULL; |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 5681 | } |
| 5682 | |
| 5683 | static struct pernet_operations rtnetlink_net_ops = { |
| 5684 | .init = rtnetlink_net_init, |
| 5685 | .exit = rtnetlink_net_exit, |
| 5686 | }; |
| 5687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5688 | void __init rtnetlink_init(void) |
| 5689 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 5690 | if (register_pernet_subsys(&rtnetlink_net_ops)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5691 | panic("rtnetlink_init: cannot initialize rtnetlink\n"); |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 5692 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5693 | register_netdevice_notifier(&rtnetlink_dev_notifier); |
Thomas Graf | 340d17f | 2007-03-22 11:49:22 -0700 | [diff] [blame] | 5694 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 5695 | rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 5696 | rtnl_dump_ifinfo, 0); |
| 5697 | rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0); |
| 5698 | rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0); |
| 5699 | rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0); |
Thomas Graf | 687ad8c | 2007-03-22 11:59:42 -0700 | [diff] [blame] | 5700 | |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 5701 | rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0); |
| 5702 | rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0); |
| 5703 | rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 5704 | |
Jiri Pirko | 36fbf1e | 2019-09-30 11:48:16 +0200 | [diff] [blame] | 5705 | rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0); |
| 5706 | rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0); |
| 5707 | |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 5708 | rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0); |
| 5709 | rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0); |
Roopa Prabhu | 5b2f94b | 2018-12-15 22:35:08 -0800 | [diff] [blame] | 5710 | 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] | 5711 | |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 5712 | rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0); |
| 5713 | rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0); |
| 5714 | rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0); |
Roopa Prabhu | 10c9ead | 2016-04-20 08:43:43 -0700 | [diff] [blame] | 5715 | |
| 5716 | rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump, |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 5717 | 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5718 | } |