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