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