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