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