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