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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/errno.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/types.h> |
| 22 | #include <linux/socket.h> |
| 23 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/timer.h> |
| 25 | #include <linux/string.h> |
| 26 | #include <linux/sockios.h> |
| 27 | #include <linux/net.h> |
| 28 | #include <linux/fcntl.h> |
| 29 | #include <linux/mm.h> |
| 30 | #include <linux/slab.h> |
| 31 | #include <linux/interrupt.h> |
| 32 | #include <linux/capability.h> |
| 33 | #include <linux/skbuff.h> |
| 34 | #include <linux/init.h> |
| 35 | #include <linux/security.h> |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 36 | #include <linux/mutex.h> |
Thomas Graf | 1823730 | 2006-08-04 23:04:54 -0700 | [diff] [blame] | 37 | #include <linux/if_addr.h> |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 38 | #include <linux/if_bridge.h> |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 39 | #include <linux/if_vlan.h> |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 40 | #include <linux/pci.h> |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 41 | #include <linux/etherdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | #include <linux/inet.h> |
| 46 | #include <linux/netdevice.h> |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 47 | #include <net/switchdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <net/ip.h> |
| 49 | #include <net/protocol.h> |
| 50 | #include <net/arp.h> |
| 51 | #include <net/route.h> |
| 52 | #include <net/udp.h> |
Daniel Borkmann | ea69763 | 2015-01-05 23:57:47 +0100 | [diff] [blame] | 53 | #include <net/tcp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #include <net/sock.h> |
| 55 | #include <net/pkt_sched.h> |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 56 | #include <net/fib_rules.h> |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 57 | #include <net/rtnetlink.h> |
Johannes Berg | 30ffee8 | 2009-07-10 09:51:35 +0000 | [diff] [blame] | 58 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 60 | struct rtnl_link { |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 61 | rtnl_doit_func doit; |
| 62 | rtnl_dumpit_func dumpit; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 63 | rtnl_calcit_func calcit; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 66 | static DEFINE_MUTEX(rtnl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | void rtnl_lock(void) |
| 69 | { |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 70 | mutex_lock(&rtnl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 72 | EXPORT_SYMBOL(rtnl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 74 | void __rtnl_unlock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 76 | mutex_unlock(&rtnl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 78 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | void rtnl_unlock(void) |
| 80 | { |
Herbert Xu | 58ec3b4 | 2008-10-07 15:50:03 -0700 | [diff] [blame] | 81 | /* This fellow will unlock it for us. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | netdev_run_todo(); |
| 83 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 84 | EXPORT_SYMBOL(rtnl_unlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 86 | int rtnl_trylock(void) |
| 87 | { |
| 88 | return mutex_trylock(&rtnl_mutex); |
| 89 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 90 | EXPORT_SYMBOL(rtnl_trylock); |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 91 | |
Patrick McHardy | c9c1014 | 2008-04-23 22:10:48 -0700 | [diff] [blame] | 92 | int rtnl_is_locked(void) |
| 93 | { |
| 94 | return mutex_is_locked(&rtnl_mutex); |
| 95 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 96 | EXPORT_SYMBOL(rtnl_is_locked); |
Patrick McHardy | c9c1014 | 2008-04-23 22:10:48 -0700 | [diff] [blame] | 97 | |
Paul E. McKenney | a898def | 2010-02-22 17:04:49 -0800 | [diff] [blame] | 98 | #ifdef CONFIG_PROVE_LOCKING |
Yaowei Bai | 0cbf334 | 2015-10-08 21:29:02 +0800 | [diff] [blame] | 99 | bool lockdep_rtnl_is_held(void) |
Paul E. McKenney | a898def | 2010-02-22 17:04:49 -0800 | [diff] [blame] | 100 | { |
| 101 | return lockdep_is_held(&rtnl_mutex); |
| 102 | } |
| 103 | EXPORT_SYMBOL(lockdep_rtnl_is_held); |
| 104 | #endif /* #ifdef CONFIG_PROVE_LOCKING */ |
| 105 | |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 106 | static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1]; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 107 | |
| 108 | static inline int rtm_msgindex(int msgtype) |
| 109 | { |
| 110 | int msgindex = msgtype - RTM_BASE; |
| 111 | |
| 112 | /* |
| 113 | * msgindex < 0 implies someone tried to register a netlink |
| 114 | * control code. msgindex >= RTM_NR_MSGTYPES may indicate that |
| 115 | * the message type has not been added to linux/rtnetlink.h |
| 116 | */ |
| 117 | BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); |
| 118 | |
| 119 | return msgindex; |
| 120 | } |
| 121 | |
| 122 | static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex) |
| 123 | { |
| 124 | struct rtnl_link *tab; |
| 125 | |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 126 | if (protocol <= RTNL_FAMILY_MAX) |
Patrick McHardy | 0f87b1d | 2010-04-13 05:03:17 +0000 | [diff] [blame] | 127 | tab = rtnl_msg_handlers[protocol]; |
| 128 | else |
| 129 | tab = NULL; |
| 130 | |
Thomas Graf | 51057f2 | 2007-03-22 21:41:06 -0700 | [diff] [blame] | 131 | if (tab == NULL || tab[msgindex].doit == NULL) |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 132 | tab = rtnl_msg_handlers[PF_UNSPEC]; |
| 133 | |
Hans Zhang | c80bbea | 2012-10-22 22:21:23 +0000 | [diff] [blame] | 134 | return tab[msgindex].doit; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex) |
| 138 | { |
| 139 | struct rtnl_link *tab; |
| 140 | |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 141 | if (protocol <= RTNL_FAMILY_MAX) |
Patrick McHardy | 0f87b1d | 2010-04-13 05:03:17 +0000 | [diff] [blame] | 142 | tab = rtnl_msg_handlers[protocol]; |
| 143 | else |
| 144 | tab = NULL; |
| 145 | |
Thomas Graf | 51057f2 | 2007-03-22 21:41:06 -0700 | [diff] [blame] | 146 | if (tab == NULL || tab[msgindex].dumpit == NULL) |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 147 | tab = rtnl_msg_handlers[PF_UNSPEC]; |
| 148 | |
Hans Zhang | c80bbea | 2012-10-22 22:21:23 +0000 | [diff] [blame] | 149 | return tab[msgindex].dumpit; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 152 | static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex) |
| 153 | { |
| 154 | struct rtnl_link *tab; |
| 155 | |
| 156 | if (protocol <= RTNL_FAMILY_MAX) |
| 157 | tab = rtnl_msg_handlers[protocol]; |
| 158 | else |
| 159 | tab = NULL; |
| 160 | |
| 161 | if (tab == NULL || tab[msgindex].calcit == NULL) |
| 162 | tab = rtnl_msg_handlers[PF_UNSPEC]; |
| 163 | |
Hans Zhang | c80bbea | 2012-10-22 22:21:23 +0000 | [diff] [blame] | 164 | return tab[msgindex].calcit; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 167 | /** |
| 168 | * __rtnl_register - Register a rtnetlink message type |
| 169 | * @protocol: Protocol family or PF_UNSPEC |
| 170 | * @msgtype: rtnetlink message type |
| 171 | * @doit: Function pointer called for each request message |
| 172 | * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 173 | * @calcit: Function pointer to calc size of dump message |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 174 | * |
| 175 | * Registers the specified function pointers (at least one of them has |
| 176 | * to be non-NULL) to be called whenever a request message for the |
| 177 | * specified protocol family and message type is received. |
| 178 | * |
| 179 | * The special protocol family PF_UNSPEC may be used to define fallback |
| 180 | * function pointers for the case when no entry for the specific protocol |
| 181 | * family exists. |
| 182 | * |
| 183 | * Returns 0 on success or a negative error code. |
| 184 | */ |
| 185 | int __rtnl_register(int protocol, int msgtype, |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 186 | rtnl_doit_func doit, rtnl_dumpit_func dumpit, |
| 187 | rtnl_calcit_func calcit) |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 188 | { |
| 189 | struct rtnl_link *tab; |
| 190 | int msgindex; |
| 191 | |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 192 | BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 193 | msgindex = rtm_msgindex(msgtype); |
| 194 | |
| 195 | tab = rtnl_msg_handlers[protocol]; |
| 196 | if (tab == NULL) { |
| 197 | tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL); |
| 198 | if (tab == NULL) |
| 199 | return -ENOBUFS; |
| 200 | |
| 201 | rtnl_msg_handlers[protocol] = tab; |
| 202 | } |
| 203 | |
| 204 | if (doit) |
| 205 | tab[msgindex].doit = doit; |
| 206 | |
| 207 | if (dumpit) |
| 208 | tab[msgindex].dumpit = dumpit; |
| 209 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 210 | if (calcit) |
| 211 | tab[msgindex].calcit = calcit; |
| 212 | |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 213 | return 0; |
| 214 | } |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 215 | EXPORT_SYMBOL_GPL(__rtnl_register); |
| 216 | |
| 217 | /** |
| 218 | * rtnl_register - Register a rtnetlink message type |
| 219 | * |
| 220 | * Identical to __rtnl_register() but panics on failure. This is useful |
| 221 | * as failure of this function is very unlikely, it can only happen due |
| 222 | * to lack of memory when allocating the chain to store all message |
| 223 | * handlers for a protocol. Meant for use in init functions where lack |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 224 | * of memory implies no sense in continuing. |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 225 | */ |
| 226 | void rtnl_register(int protocol, int msgtype, |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 227 | rtnl_doit_func doit, rtnl_dumpit_func dumpit, |
| 228 | rtnl_calcit_func calcit) |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 229 | { |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 230 | if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0) |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 231 | panic("Unable to register rtnetlink message handler, " |
| 232 | "protocol = %d, message type = %d\n", |
| 233 | protocol, msgtype); |
| 234 | } |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 235 | EXPORT_SYMBOL_GPL(rtnl_register); |
| 236 | |
| 237 | /** |
| 238 | * rtnl_unregister - Unregister a rtnetlink message type |
| 239 | * @protocol: Protocol family or PF_UNSPEC |
| 240 | * @msgtype: rtnetlink message type |
| 241 | * |
| 242 | * Returns 0 on success or a negative error code. |
| 243 | */ |
| 244 | int rtnl_unregister(int protocol, int msgtype) |
| 245 | { |
| 246 | int msgindex; |
| 247 | |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 248 | BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 249 | msgindex = rtm_msgindex(msgtype); |
| 250 | |
| 251 | if (rtnl_msg_handlers[protocol] == NULL) |
| 252 | return -ENOENT; |
| 253 | |
| 254 | rtnl_msg_handlers[protocol][msgindex].doit = NULL; |
| 255 | rtnl_msg_handlers[protocol][msgindex].dumpit = NULL; |
| 256 | |
| 257 | return 0; |
| 258 | } |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 259 | EXPORT_SYMBOL_GPL(rtnl_unregister); |
| 260 | |
| 261 | /** |
| 262 | * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol |
| 263 | * @protocol : Protocol family or PF_UNSPEC |
| 264 | * |
| 265 | * Identical to calling rtnl_unregster() for all registered message types |
| 266 | * of a certain protocol family. |
| 267 | */ |
| 268 | void rtnl_unregister_all(int protocol) |
| 269 | { |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 270 | BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 271 | |
| 272 | kfree(rtnl_msg_handlers[protocol]); |
| 273 | rtnl_msg_handlers[protocol] = NULL; |
| 274 | } |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 275 | EXPORT_SYMBOL_GPL(rtnl_unregister_all); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 277 | static LIST_HEAD(link_ops); |
| 278 | |
Eric Dumazet | c63044f | 2011-12-13 11:38:00 +0000 | [diff] [blame] | 279 | static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) |
| 280 | { |
| 281 | const struct rtnl_link_ops *ops; |
| 282 | |
| 283 | list_for_each_entry(ops, &link_ops, list) { |
| 284 | if (!strcmp(ops->kind, kind)) |
| 285 | return ops; |
| 286 | } |
| 287 | return NULL; |
| 288 | } |
| 289 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 290 | /** |
| 291 | * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. |
| 292 | * @ops: struct rtnl_link_ops * to register |
| 293 | * |
| 294 | * The caller must hold the rtnl_mutex. This function should be used |
| 295 | * by drivers that create devices during module initialization. It |
| 296 | * must be called before registering the devices. |
| 297 | * |
| 298 | * Returns 0 on success or a negative error code. |
| 299 | */ |
| 300 | int __rtnl_link_register(struct rtnl_link_ops *ops) |
| 301 | { |
Eric Dumazet | c63044f | 2011-12-13 11:38:00 +0000 | [diff] [blame] | 302 | if (rtnl_link_ops_get(ops->kind)) |
| 303 | return -EEXIST; |
| 304 | |
Jiri Pirko | b0ab2fa | 2014-06-26 09:58:25 +0200 | [diff] [blame] | 305 | /* The check for setup is here because if ops |
| 306 | * does not have that filled up, it is not possible |
| 307 | * to use the ops for creating device. So do not |
| 308 | * fill up dellink as well. That disables rtnl_dellink. |
| 309 | */ |
| 310 | if (ops->setup && !ops->dellink) |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 311 | ops->dellink = unregister_netdevice_queue; |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 312 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 313 | list_add_tail(&ops->list, &link_ops); |
| 314 | return 0; |
| 315 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 316 | EXPORT_SYMBOL_GPL(__rtnl_link_register); |
| 317 | |
| 318 | /** |
| 319 | * rtnl_link_register - Register rtnl_link_ops with rtnetlink. |
| 320 | * @ops: struct rtnl_link_ops * to register |
| 321 | * |
| 322 | * Returns 0 on success or a negative error code. |
| 323 | */ |
| 324 | int rtnl_link_register(struct rtnl_link_ops *ops) |
| 325 | { |
| 326 | int err; |
| 327 | |
| 328 | rtnl_lock(); |
| 329 | err = __rtnl_link_register(ops); |
| 330 | rtnl_unlock(); |
| 331 | return err; |
| 332 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 333 | EXPORT_SYMBOL_GPL(rtnl_link_register); |
| 334 | |
Pavel Emelyanov | 669f87b | 2008-04-16 00:46:52 -0700 | [diff] [blame] | 335 | static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) |
| 336 | { |
| 337 | struct net_device *dev; |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 338 | LIST_HEAD(list_kill); |
| 339 | |
Pavel Emelyanov | 669f87b | 2008-04-16 00:46:52 -0700 | [diff] [blame] | 340 | for_each_netdev(net, dev) { |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 341 | if (dev->rtnl_link_ops == ops) |
| 342 | ops->dellink(dev, &list_kill); |
Pavel Emelyanov | 669f87b | 2008-04-16 00:46:52 -0700 | [diff] [blame] | 343 | } |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 344 | unregister_netdevice_many(&list_kill); |
Pavel Emelyanov | 669f87b | 2008-04-16 00:46:52 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 347 | /** |
| 348 | * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. |
| 349 | * @ops: struct rtnl_link_ops * to unregister |
| 350 | * |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 351 | * The caller must hold the rtnl_mutex. |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 352 | */ |
| 353 | void __rtnl_link_unregister(struct rtnl_link_ops *ops) |
| 354 | { |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 355 | struct net *net; |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 356 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 357 | for_each_net(net) { |
Pavel Emelyanov | 669f87b | 2008-04-16 00:46:52 -0700 | [diff] [blame] | 358 | __rtnl_kill_links(net, ops); |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 359 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 360 | list_del(&ops->list); |
| 361 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 362 | EXPORT_SYMBOL_GPL(__rtnl_link_unregister); |
| 363 | |
Cong Wang | 200b916 | 2014-05-12 15:11:20 -0700 | [diff] [blame] | 364 | /* Return with the rtnl_lock held when there are no network |
| 365 | * devices unregistering in any network namespace. |
| 366 | */ |
| 367 | static void rtnl_lock_unregistering_all(void) |
| 368 | { |
| 369 | struct net *net; |
| 370 | bool unregistering; |
Peter Zijlstra | ff960a7 | 2014-10-29 17:04:56 +0100 | [diff] [blame] | 371 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
Cong Wang | 200b916 | 2014-05-12 15:11:20 -0700 | [diff] [blame] | 372 | |
Peter Zijlstra | ff960a7 | 2014-10-29 17:04:56 +0100 | [diff] [blame] | 373 | add_wait_queue(&netdev_unregistering_wq, &wait); |
Cong Wang | 200b916 | 2014-05-12 15:11:20 -0700 | [diff] [blame] | 374 | for (;;) { |
Cong Wang | 200b916 | 2014-05-12 15:11:20 -0700 | [diff] [blame] | 375 | unregistering = false; |
| 376 | rtnl_lock(); |
| 377 | for_each_net(net) { |
| 378 | if (net->dev_unreg_count > 0) { |
| 379 | unregistering = true; |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | if (!unregistering) |
| 384 | break; |
| 385 | __rtnl_unlock(); |
Peter Zijlstra | ff960a7 | 2014-10-29 17:04:56 +0100 | [diff] [blame] | 386 | |
| 387 | wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
Cong Wang | 200b916 | 2014-05-12 15:11:20 -0700 | [diff] [blame] | 388 | } |
Peter Zijlstra | ff960a7 | 2014-10-29 17:04:56 +0100 | [diff] [blame] | 389 | remove_wait_queue(&netdev_unregistering_wq, &wait); |
Cong Wang | 200b916 | 2014-05-12 15:11:20 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 392 | /** |
| 393 | * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. |
| 394 | * @ops: struct rtnl_link_ops * to unregister |
| 395 | */ |
| 396 | void rtnl_link_unregister(struct rtnl_link_ops *ops) |
| 397 | { |
Cong Wang | 200b916 | 2014-05-12 15:11:20 -0700 | [diff] [blame] | 398 | /* Close the race with cleanup_net() */ |
| 399 | mutex_lock(&net_mutex); |
| 400 | rtnl_lock_unregistering_all(); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 401 | __rtnl_link_unregister(ops); |
| 402 | rtnl_unlock(); |
Cong Wang | 200b916 | 2014-05-12 15:11:20 -0700 | [diff] [blame] | 403 | mutex_unlock(&net_mutex); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 404 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 405 | EXPORT_SYMBOL_GPL(rtnl_link_unregister); |
| 406 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 407 | static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev) |
| 408 | { |
| 409 | struct net_device *master_dev; |
| 410 | const struct rtnl_link_ops *ops; |
| 411 | |
| 412 | master_dev = netdev_master_upper_dev_get((struct net_device *) dev); |
| 413 | if (!master_dev) |
| 414 | return 0; |
| 415 | ops = master_dev->rtnl_link_ops; |
Fernando Luis Vazquez Cao | 6049f25 | 2014-02-04 19:35:02 +0900 | [diff] [blame] | 416 | if (!ops || !ops->get_slave_size) |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 417 | return 0; |
| 418 | /* IFLA_INFO_SLAVE_DATA + nested data */ |
| 419 | return nla_total_size(sizeof(struct nlattr)) + |
| 420 | ops->get_slave_size(master_dev, dev); |
| 421 | } |
| 422 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 423 | static size_t rtnl_link_get_size(const struct net_device *dev) |
| 424 | { |
| 425 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; |
| 426 | size_t size; |
| 427 | |
| 428 | if (!ops) |
| 429 | return 0; |
| 430 | |
Thomas Graf | 369cf77 | 2010-11-11 15:47:59 +0000 | [diff] [blame] | 431 | size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ |
| 432 | nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 433 | |
| 434 | if (ops->get_size) |
| 435 | /* IFLA_INFO_DATA + nested data */ |
Thomas Graf | 369cf77 | 2010-11-11 15:47:59 +0000 | [diff] [blame] | 436 | size += nla_total_size(sizeof(struct nlattr)) + |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 437 | ops->get_size(dev); |
| 438 | |
| 439 | if (ops->get_xstats_size) |
Thomas Graf | 369cf77 | 2010-11-11 15:47:59 +0000 | [diff] [blame] | 440 | /* IFLA_INFO_XSTATS */ |
| 441 | size += nla_total_size(ops->get_xstats_size(dev)); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 442 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 443 | size += rtnl_link_get_slave_info_data_size(dev); |
| 444 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 445 | return size; |
| 446 | } |
| 447 | |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 448 | static LIST_HEAD(rtnl_af_ops); |
| 449 | |
| 450 | static const struct rtnl_af_ops *rtnl_af_lookup(const int family) |
| 451 | { |
| 452 | const struct rtnl_af_ops *ops; |
| 453 | |
| 454 | list_for_each_entry(ops, &rtnl_af_ops, list) { |
| 455 | if (ops->family == family) |
| 456 | return ops; |
| 457 | } |
| 458 | |
| 459 | return NULL; |
| 460 | } |
| 461 | |
| 462 | /** |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 463 | * rtnl_af_register - Register rtnl_af_ops with rtnetlink. |
| 464 | * @ops: struct rtnl_af_ops * to register |
| 465 | * |
| 466 | * Returns 0 on success or a negative error code. |
| 467 | */ |
stephen hemminger | 3678a9d | 2013-12-30 10:41:32 -0800 | [diff] [blame] | 468 | void rtnl_af_register(struct rtnl_af_ops *ops) |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 469 | { |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 470 | rtnl_lock(); |
stephen hemminger | 3678a9d | 2013-12-30 10:41:32 -0800 | [diff] [blame] | 471 | list_add_tail(&ops->list, &rtnl_af_ops); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 472 | rtnl_unlock(); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 473 | } |
| 474 | EXPORT_SYMBOL_GPL(rtnl_af_register); |
| 475 | |
| 476 | /** |
| 477 | * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. |
| 478 | * @ops: struct rtnl_af_ops * to unregister |
| 479 | * |
| 480 | * The caller must hold the rtnl_mutex. |
| 481 | */ |
| 482 | void __rtnl_af_unregister(struct rtnl_af_ops *ops) |
| 483 | { |
| 484 | list_del(&ops->list); |
| 485 | } |
| 486 | EXPORT_SYMBOL_GPL(__rtnl_af_unregister); |
| 487 | |
| 488 | /** |
| 489 | * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. |
| 490 | * @ops: struct rtnl_af_ops * to unregister |
| 491 | */ |
| 492 | void rtnl_af_unregister(struct rtnl_af_ops *ops) |
| 493 | { |
| 494 | rtnl_lock(); |
| 495 | __rtnl_af_unregister(ops); |
| 496 | rtnl_unlock(); |
| 497 | } |
| 498 | EXPORT_SYMBOL_GPL(rtnl_af_unregister); |
| 499 | |
Arad, Ronen | b1974ed | 2015-10-19 09:23:28 -0700 | [diff] [blame] | 500 | static size_t rtnl_link_get_af_size(const struct net_device *dev, |
| 501 | u32 ext_filter_mask) |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 502 | { |
| 503 | struct rtnl_af_ops *af_ops; |
| 504 | size_t size; |
| 505 | |
| 506 | /* IFLA_AF_SPEC */ |
| 507 | size = nla_total_size(sizeof(struct nlattr)); |
| 508 | |
| 509 | list_for_each_entry(af_ops, &rtnl_af_ops, list) { |
| 510 | if (af_ops->get_link_af_size) { |
| 511 | /* AF_* + nested data */ |
| 512 | size += nla_total_size(sizeof(struct nlattr)) + |
Arad, Ronen | b1974ed | 2015-10-19 09:23:28 -0700 | [diff] [blame] | 513 | af_ops->get_link_af_size(dev, ext_filter_mask); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
| 517 | return size; |
| 518 | } |
| 519 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 520 | static bool rtnl_have_link_slave_info(const struct net_device *dev) |
| 521 | { |
| 522 | struct net_device *master_dev; |
| 523 | |
| 524 | master_dev = netdev_master_upper_dev_get((struct net_device *) dev); |
Jiri Pirko | 813f020 | 2014-01-23 19:19:21 +0100 | [diff] [blame] | 525 | if (master_dev && master_dev->rtnl_link_ops) |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 526 | return true; |
| 527 | return false; |
| 528 | } |
| 529 | |
| 530 | static int rtnl_link_slave_info_fill(struct sk_buff *skb, |
| 531 | const struct net_device *dev) |
| 532 | { |
| 533 | struct net_device *master_dev; |
| 534 | const struct rtnl_link_ops *ops; |
| 535 | struct nlattr *slave_data; |
| 536 | int err; |
| 537 | |
| 538 | master_dev = netdev_master_upper_dev_get((struct net_device *) dev); |
| 539 | if (!master_dev) |
| 540 | return 0; |
| 541 | ops = master_dev->rtnl_link_ops; |
| 542 | if (!ops) |
| 543 | return 0; |
| 544 | if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0) |
| 545 | return -EMSGSIZE; |
| 546 | if (ops->fill_slave_info) { |
| 547 | slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA); |
| 548 | if (!slave_data) |
| 549 | return -EMSGSIZE; |
| 550 | err = ops->fill_slave_info(skb, master_dev, dev); |
| 551 | if (err < 0) |
| 552 | goto err_cancel_slave_data; |
| 553 | nla_nest_end(skb, slave_data); |
| 554 | } |
| 555 | return 0; |
| 556 | |
| 557 | err_cancel_slave_data: |
| 558 | nla_nest_cancel(skb, slave_data); |
| 559 | return err; |
| 560 | } |
| 561 | |
| 562 | static int rtnl_link_info_fill(struct sk_buff *skb, |
| 563 | const struct net_device *dev) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 564 | { |
| 565 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 566 | struct nlattr *data; |
| 567 | int err; |
| 568 | |
| 569 | if (!ops) |
| 570 | return 0; |
| 571 | if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) |
| 572 | return -EMSGSIZE; |
| 573 | if (ops->fill_xstats) { |
| 574 | err = ops->fill_xstats(skb, dev); |
| 575 | if (err < 0) |
| 576 | return err; |
| 577 | } |
| 578 | if (ops->fill_info) { |
| 579 | data = nla_nest_start(skb, IFLA_INFO_DATA); |
| 580 | if (data == NULL) |
| 581 | return -EMSGSIZE; |
| 582 | err = ops->fill_info(skb, dev); |
| 583 | if (err < 0) |
| 584 | goto err_cancel_data; |
| 585 | nla_nest_end(skb, data); |
| 586 | } |
| 587 | return 0; |
| 588 | |
| 589 | err_cancel_data: |
| 590 | nla_nest_cancel(skb, data); |
| 591 | return err; |
| 592 | } |
| 593 | |
| 594 | static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) |
| 595 | { |
| 596 | struct nlattr *linkinfo; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 597 | int err = -EMSGSIZE; |
| 598 | |
| 599 | linkinfo = nla_nest_start(skb, IFLA_LINKINFO); |
| 600 | if (linkinfo == NULL) |
| 601 | goto out; |
| 602 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 603 | err = rtnl_link_info_fill(skb, dev); |
| 604 | if (err < 0) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 605 | goto err_cancel_link; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 606 | |
| 607 | err = rtnl_link_slave_info_fill(skb, dev); |
| 608 | if (err < 0) |
| 609 | goto err_cancel_link; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 610 | |
| 611 | nla_nest_end(skb, linkinfo); |
| 612 | return 0; |
| 613 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 614 | err_cancel_link: |
| 615 | nla_nest_cancel(skb, linkinfo); |
| 616 | out: |
| 617 | return err; |
| 618 | } |
| 619 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 620 | 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] | 621 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 622 | struct sock *rtnl = net->rtnl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | int err = 0; |
| 624 | |
Patrick McHardy | ac6d439 | 2005-08-14 19:29:52 -0700 | [diff] [blame] | 625 | NETLINK_CB(skb).dst_group = group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | if (echo) |
| 627 | atomic_inc(&skb->users); |
| 628 | netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); |
| 629 | if (echo) |
| 630 | err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); |
| 631 | return err; |
| 632 | } |
| 633 | |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 634 | int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) |
Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 635 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 636 | struct sock *rtnl = net->rtnl; |
| 637 | |
Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 638 | return nlmsg_unicast(rtnl, skb, pid); |
| 639 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 640 | EXPORT_SYMBOL(rtnl_unicast); |
Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 641 | |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 642 | void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, |
| 643 | struct nlmsghdr *nlh, gfp_t flags) |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 644 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 645 | struct sock *rtnl = net->rtnl; |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 646 | int report = 0; |
| 647 | |
| 648 | if (nlh) |
| 649 | report = nlmsg_report(nlh); |
| 650 | |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 651 | nlmsg_notify(rtnl, skb, pid, group, report, flags); |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 652 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 653 | EXPORT_SYMBOL(rtnl_notify); |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 654 | |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 655 | void rtnl_set_sk_err(struct net *net, u32 group, int error) |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 656 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 657 | struct sock *rtnl = net->rtnl; |
| 658 | |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 659 | netlink_set_err(rtnl, 0, group, error); |
| 660 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 661 | EXPORT_SYMBOL(rtnl_set_sk_err); |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) |
| 664 | { |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 665 | struct nlattr *mx; |
| 666 | int i, valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 668 | mx = nla_nest_start(skb, RTA_METRICS); |
| 669 | if (mx == NULL) |
| 670 | return -ENOBUFS; |
| 671 | |
| 672 | for (i = 0; i < RTAX_MAX; i++) { |
| 673 | if (metrics[i]) { |
Daniel Borkmann | ea69763 | 2015-01-05 23:57:47 +0100 | [diff] [blame] | 674 | if (i == RTAX_CC_ALGO - 1) { |
| 675 | char tmp[TCP_CA_NAME_MAX], *name; |
| 676 | |
| 677 | name = tcp_ca_get_name_by_key(metrics[i], tmp); |
| 678 | if (!name) |
| 679 | continue; |
| 680 | if (nla_put_string(skb, i + 1, name)) |
| 681 | goto nla_put_failure; |
Daniel Borkmann | c3a8d94 | 2015-08-31 15:58:47 +0200 | [diff] [blame] | 682 | } else if (i == RTAX_FEATURES - 1) { |
| 683 | u32 user_features = metrics[i] & RTAX_FEATURE_MASK; |
| 684 | |
| 685 | BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK); |
| 686 | if (nla_put_u32(skb, i + 1, user_features)) |
| 687 | goto nla_put_failure; |
Daniel Borkmann | ea69763 | 2015-01-05 23:57:47 +0100 | [diff] [blame] | 688 | } else { |
| 689 | if (nla_put_u32(skb, i + 1, metrics[i])) |
| 690 | goto nla_put_failure; |
| 691 | } |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 692 | valid++; |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 693 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | |
David S. Miller | a57d27f | 2006-08-22 22:20:14 -0700 | [diff] [blame] | 696 | if (!valid) { |
| 697 | nla_nest_cancel(skb, mx); |
| 698 | return 0; |
| 699 | } |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 700 | |
| 701 | return nla_nest_end(skb, mx); |
| 702 | |
| 703 | nla_put_failure: |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 704 | nla_nest_cancel(skb, mx); |
| 705 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 707 | EXPORT_SYMBOL(rtnetlink_put_metrics); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 709 | 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] | 710 | long expires, u32 error) |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 711 | { |
| 712 | struct rta_cacheinfo ci = { |
Eric Dumazet | a399a80 | 2012-08-08 21:13:53 +0000 | [diff] [blame] | 713 | .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse), |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 714 | .rta_used = dst->__use, |
| 715 | .rta_clntref = atomic_read(&(dst->__refcnt)), |
| 716 | .rta_error = error, |
| 717 | .rta_id = id, |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 718 | }; |
| 719 | |
Li Wei | 8253947 | 2012-07-29 16:01:30 +0000 | [diff] [blame] | 720 | if (expires) { |
| 721 | unsigned long clock; |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 722 | |
Li Wei | 8253947 | 2012-07-29 16:01:30 +0000 | [diff] [blame] | 723 | clock = jiffies_to_clock_t(abs(expires)); |
| 724 | clock = min_t(unsigned long, clock, INT_MAX); |
| 725 | ci.rta_expires = (expires > 0) ? clock : -clock; |
| 726 | } |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 727 | return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); |
| 728 | } |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 729 | EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 731 | static void set_operstate(struct net_device *dev, unsigned char transition) |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 732 | { |
| 733 | unsigned char operstate = dev->operstate; |
| 734 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 735 | switch (transition) { |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 736 | case IF_OPER_UP: |
| 737 | if ((operstate == IF_OPER_DORMANT || |
| 738 | operstate == IF_OPER_UNKNOWN) && |
| 739 | !netif_dormant(dev)) |
| 740 | operstate = IF_OPER_UP; |
| 741 | break; |
| 742 | |
| 743 | case IF_OPER_DORMANT: |
| 744 | if (operstate == IF_OPER_UP || |
| 745 | operstate == IF_OPER_UNKNOWN) |
| 746 | operstate = IF_OPER_DORMANT; |
| 747 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 748 | } |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 749 | |
| 750 | if (dev->operstate != operstate) { |
| 751 | write_lock_bh(&dev_base_lock); |
| 752 | dev->operstate = operstate; |
| 753 | write_unlock_bh(&dev_base_lock); |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 754 | netdev_state_change(dev); |
| 755 | } |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 756 | } |
| 757 | |
Jiri Benc | b1beb68 | 2012-07-27 02:58:22 +0000 | [diff] [blame] | 758 | static unsigned int rtnl_dev_get_flags(const struct net_device *dev) |
| 759 | { |
| 760 | return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) | |
| 761 | (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI)); |
| 762 | } |
| 763 | |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 764 | static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, |
| 765 | const struct ifinfomsg *ifm) |
| 766 | { |
| 767 | unsigned int flags = ifm->ifi_flags; |
| 768 | |
| 769 | /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ |
| 770 | if (ifm->ifi_change) |
| 771 | flags = (flags & ifm->ifi_change) | |
Jiri Benc | b1beb68 | 2012-07-27 02:58:22 +0000 | [diff] [blame] | 772 | (rtnl_dev_get_flags(dev) & ~ifm->ifi_change); |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 773 | |
| 774 | return flags; |
| 775 | } |
| 776 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 777 | static void copy_rtnl_link_stats(struct rtnl_link_stats *a, |
Ben Hutchings | be1f3c2 | 2010-06-08 07:19:54 +0000 | [diff] [blame] | 778 | const struct rtnl_link_stats64 *b) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | { |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 780 | a->rx_packets = b->rx_packets; |
| 781 | a->tx_packets = b->tx_packets; |
| 782 | a->rx_bytes = b->rx_bytes; |
| 783 | a->tx_bytes = b->tx_bytes; |
| 784 | a->rx_errors = b->rx_errors; |
| 785 | a->tx_errors = b->tx_errors; |
| 786 | a->rx_dropped = b->rx_dropped; |
| 787 | a->tx_dropped = b->tx_dropped; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 789 | a->multicast = b->multicast; |
| 790 | a->collisions = b->collisions; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 792 | a->rx_length_errors = b->rx_length_errors; |
| 793 | a->rx_over_errors = b->rx_over_errors; |
| 794 | a->rx_crc_errors = b->rx_crc_errors; |
| 795 | a->rx_frame_errors = b->rx_frame_errors; |
| 796 | a->rx_fifo_errors = b->rx_fifo_errors; |
| 797 | a->rx_missed_errors = b->rx_missed_errors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 799 | a->tx_aborted_errors = b->tx_aborted_errors; |
| 800 | a->tx_carrier_errors = b->tx_carrier_errors; |
| 801 | a->tx_fifo_errors = b->tx_fifo_errors; |
| 802 | a->tx_heartbeat_errors = b->tx_heartbeat_errors; |
| 803 | a->tx_window_errors = b->tx_window_errors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 805 | a->rx_compressed = b->rx_compressed; |
| 806 | a->tx_compressed = b->tx_compressed; |
Jarod Wilson | 6e7333d | 2016-02-01 18:51:05 -0500 | [diff] [blame] | 807 | |
| 808 | a->rx_nohandler = b->rx_nohandler; |
Jan Engelhardt | 10708f3 | 2010-03-11 09:57:29 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Ben Hutchings | be1f3c2 | 2010-06-08 07:19:54 +0000 | [diff] [blame] | 811 | static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b) |
Jan Engelhardt | 10708f3 | 2010-03-11 09:57:29 +0000 | [diff] [blame] | 812 | { |
Eric Dumazet | afdcba3 | 2010-08-23 07:14:36 +0000 | [diff] [blame] | 813 | memcpy(v, b, sizeof(*b)); |
Jan Engelhardt | 10708f3 | 2010-03-11 09:57:29 +0000 | [diff] [blame] | 814 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 816 | /* All VF info */ |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 817 | static inline int rtnl_vfinfo_size(const struct net_device *dev, |
| 818 | u32 ext_filter_mask) |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 819 | { |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 820 | if (dev->dev.parent && dev_is_pci(dev->dev.parent) && |
| 821 | (ext_filter_mask & RTEXT_FILTER_VF)) { |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 822 | int num_vfs = dev_num_vf(dev->dev.parent); |
Scott Feldman | 045de01 | 2010-05-28 03:42:43 -0700 | [diff] [blame] | 823 | size_t size = nla_total_size(sizeof(struct nlattr)); |
| 824 | size += nla_total_size(num_vfs * sizeof(struct nlattr)); |
| 825 | size += num_vfs * |
| 826 | (nla_total_size(sizeof(struct ifla_vf_mac)) + |
| 827 | nla_total_size(sizeof(struct ifla_vf_vlan)) + |
Sucheta Chakraborty | ed61668 | 2014-05-22 09:59:05 -0400 | [diff] [blame] | 828 | nla_total_size(sizeof(struct ifla_vf_spoofchk)) + |
Jiri Benc | 945a367 | 2014-08-08 16:44:32 +0200 | [diff] [blame] | 829 | nla_total_size(sizeof(struct ifla_vf_rate)) + |
Vlad Zolotarov | 01a3d79 | 2015-03-30 21:35:23 +0300 | [diff] [blame] | 830 | nla_total_size(sizeof(struct ifla_vf_link_state)) + |
Eran Ben Elisha | 3b766cd | 2015-06-15 17:59:07 +0300 | [diff] [blame] | 831 | nla_total_size(sizeof(struct ifla_vf_rss_query_en)) + |
| 832 | /* IFLA_VF_STATS_RX_PACKETS */ |
| 833 | nla_total_size(sizeof(__u64)) + |
| 834 | /* IFLA_VF_STATS_TX_PACKETS */ |
| 835 | nla_total_size(sizeof(__u64)) + |
| 836 | /* IFLA_VF_STATS_RX_BYTES */ |
| 837 | nla_total_size(sizeof(__u64)) + |
| 838 | /* IFLA_VF_STATS_TX_BYTES */ |
| 839 | nla_total_size(sizeof(__u64)) + |
| 840 | /* IFLA_VF_STATS_BROADCAST */ |
| 841 | nla_total_size(sizeof(__u64)) + |
| 842 | /* IFLA_VF_STATS_MULTICAST */ |
Hiroshi Shimamoto | dd461d6 | 2015-08-28 06:57:55 +0000 | [diff] [blame] | 843 | nla_total_size(sizeof(__u64)) + |
| 844 | nla_total_size(sizeof(struct ifla_vf_trust))); |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 845 | return size; |
| 846 | } else |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 847 | return 0; |
| 848 | } |
| 849 | |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 850 | static size_t rtnl_port_size(const struct net_device *dev, |
| 851 | u32 ext_filter_mask) |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 852 | { |
| 853 | size_t port_size = nla_total_size(4) /* PORT_VF */ |
| 854 | + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ |
| 855 | + nla_total_size(sizeof(struct ifla_port_vsi)) |
| 856 | /* PORT_VSI_TYPE */ |
| 857 | + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ |
| 858 | + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ |
| 859 | + nla_total_size(1) /* PROT_VDP_REQUEST */ |
| 860 | + nla_total_size(2); /* PORT_VDP_RESPONSE */ |
| 861 | size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); |
| 862 | size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) |
| 863 | + port_size; |
| 864 | size_t port_self_size = nla_total_size(sizeof(struct nlattr)) |
| 865 | + port_size; |
| 866 | |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 867 | if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || |
| 868 | !(ext_filter_mask & RTEXT_FILTER_VF)) |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 869 | return 0; |
| 870 | if (dev_num_vf(dev->dev.parent)) |
| 871 | return port_self_size + vf_ports_size + |
| 872 | vf_port_size * dev_num_vf(dev->dev.parent); |
| 873 | else |
| 874 | return port_self_size; |
| 875 | } |
| 876 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 877 | static noinline size_t if_nlmsg_size(const struct net_device *dev, |
| 878 | u32 ext_filter_mask) |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 879 | { |
| 880 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) |
| 881 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 882 | + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 883 | + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ |
| 884 | + nla_total_size(sizeof(struct rtnl_link_ifmap)) |
| 885 | + nla_total_size(sizeof(struct rtnl_link_stats)) |
Jan Engelhardt | adcfe19 | 2010-03-27 17:15:29 -0700 | [diff] [blame] | 886 | + nla_total_size(sizeof(struct rtnl_link_stats64)) |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 887 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ |
| 888 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ |
| 889 | + nla_total_size(4) /* IFLA_TXQLEN */ |
| 890 | + nla_total_size(4) /* IFLA_WEIGHT */ |
| 891 | + nla_total_size(4) /* IFLA_MTU */ |
| 892 | + nla_total_size(4) /* IFLA_LINK */ |
| 893 | + nla_total_size(4) /* IFLA_MASTER */ |
Jiri Pirko | 9a57247 | 2012-12-27 23:49:39 +0000 | [diff] [blame] | 894 | + nla_total_size(1) /* IFLA_CARRIER */ |
Ben Greear | edbc0bb | 2012-03-29 12:51:30 +0000 | [diff] [blame] | 895 | + nla_total_size(4) /* IFLA_PROMISCUITY */ |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 896 | + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */ |
| 897 | + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */ |
Eric Dumazet | c70ce02 | 2016-03-21 09:55:10 -0700 | [diff] [blame] | 898 | + nla_total_size(4) /* IFLA_MAX_GSO_SEGS */ |
| 899 | + nla_total_size(4) /* IFLA_MAX_GSO_SIZE */ |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 900 | + nla_total_size(1) /* IFLA_OPERSTATE */ |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 901 | + nla_total_size(1) /* IFLA_LINKMODE */ |
david decotigny | 2d3b479 | 2014-03-29 09:48:35 -0700 | [diff] [blame] | 902 | + nla_total_size(4) /* IFLA_CARRIER_CHANGES */ |
Nicolas Dichtel | d37512a | 2015-01-15 15:11:16 +0100 | [diff] [blame] | 903 | + nla_total_size(4) /* IFLA_LINK_NETNSID */ |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 904 | + nla_total_size(ext_filter_mask |
| 905 | & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ |
| 906 | + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 907 | + 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] | 908 | + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ |
Arad, Ronen | b1974ed | 2015-10-19 09:23:28 -0700 | [diff] [blame] | 909 | + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */ |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 910 | + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */ |
Anuradha Karuppiah | 88d6378 | 2015-07-14 13:43:20 -0700 | [diff] [blame] | 911 | + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */ |
| 912 | + nla_total_size(1); /* IFLA_PROTO_DOWN */ |
| 913 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 914 | } |
| 915 | |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 916 | static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) |
| 917 | { |
| 918 | struct nlattr *vf_ports; |
| 919 | struct nlattr *vf_port; |
| 920 | int vf; |
| 921 | int err; |
| 922 | |
| 923 | vf_ports = nla_nest_start(skb, IFLA_VF_PORTS); |
| 924 | if (!vf_ports) |
| 925 | return -EMSGSIZE; |
| 926 | |
| 927 | for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { |
| 928 | vf_port = nla_nest_start(skb, IFLA_VF_PORT); |
Scott Feldman | 8ca9418 | 2010-05-28 03:42:18 -0700 | [diff] [blame] | 929 | if (!vf_port) |
| 930 | goto nla_put_failure; |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 931 | if (nla_put_u32(skb, IFLA_PORT_VF, vf)) |
| 932 | goto nla_put_failure; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 933 | err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); |
Scott Feldman | 8ca9418 | 2010-05-28 03:42:18 -0700 | [diff] [blame] | 934 | if (err == -EMSGSIZE) |
| 935 | goto nla_put_failure; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 936 | if (err) { |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 937 | nla_nest_cancel(skb, vf_port); |
| 938 | continue; |
| 939 | } |
| 940 | nla_nest_end(skb, vf_port); |
| 941 | } |
| 942 | |
| 943 | nla_nest_end(skb, vf_ports); |
| 944 | |
| 945 | return 0; |
Scott Feldman | 8ca9418 | 2010-05-28 03:42:18 -0700 | [diff] [blame] | 946 | |
| 947 | nla_put_failure: |
| 948 | nla_nest_cancel(skb, vf_ports); |
| 949 | return -EMSGSIZE; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) |
| 953 | { |
| 954 | struct nlattr *port_self; |
| 955 | int err; |
| 956 | |
| 957 | port_self = nla_nest_start(skb, IFLA_PORT_SELF); |
| 958 | if (!port_self) |
| 959 | return -EMSGSIZE; |
| 960 | |
| 961 | err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); |
| 962 | if (err) { |
| 963 | nla_nest_cancel(skb, port_self); |
Scott Feldman | 8ca9418 | 2010-05-28 03:42:18 -0700 | [diff] [blame] | 964 | return (err == -EMSGSIZE) ? err : 0; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | nla_nest_end(skb, port_self); |
| 968 | |
| 969 | return 0; |
| 970 | } |
| 971 | |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 972 | static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, |
| 973 | u32 ext_filter_mask) |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 974 | { |
| 975 | int err; |
| 976 | |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 977 | if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || |
| 978 | !(ext_filter_mask & RTEXT_FILTER_VF)) |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 979 | return 0; |
| 980 | |
| 981 | err = rtnl_port_self_fill(skb, dev); |
| 982 | if (err) |
| 983 | return err; |
| 984 | |
| 985 | if (dev_num_vf(dev->dev.parent)) { |
| 986 | err = rtnl_vf_ports_fill(skb, dev); |
| 987 | if (err) |
| 988 | return err; |
| 989 | } |
| 990 | |
| 991 | return 0; |
| 992 | } |
| 993 | |
Jiri Pirko | 66cae9e | 2013-07-29 18:16:50 +0200 | [diff] [blame] | 994 | static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev) |
| 995 | { |
| 996 | int err; |
Jiri Pirko | 02637fc | 2014-11-28 14:34:16 +0100 | [diff] [blame] | 997 | struct netdev_phys_item_id ppid; |
Jiri Pirko | 66cae9e | 2013-07-29 18:16:50 +0200 | [diff] [blame] | 998 | |
| 999 | err = dev_get_phys_port_id(dev, &ppid); |
| 1000 | if (err) { |
| 1001 | if (err == -EOPNOTSUPP) |
| 1002 | return 0; |
| 1003 | return err; |
| 1004 | } |
| 1005 | |
| 1006 | if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id)) |
| 1007 | return -EMSGSIZE; |
| 1008 | |
| 1009 | return 0; |
| 1010 | } |
| 1011 | |
David Ahern | db24a90 | 2015-03-17 20:23:15 -0600 | [diff] [blame] | 1012 | static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev) |
| 1013 | { |
| 1014 | char name[IFNAMSIZ]; |
| 1015 | int err; |
| 1016 | |
| 1017 | err = dev_get_phys_port_name(dev, name, sizeof(name)); |
| 1018 | if (err) { |
| 1019 | if (err == -EOPNOTSUPP) |
| 1020 | return 0; |
| 1021 | return err; |
| 1022 | } |
| 1023 | |
| 1024 | if (nla_put(skb, IFLA_PHYS_PORT_NAME, strlen(name), name)) |
| 1025 | return -EMSGSIZE; |
| 1026 | |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1030 | static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) |
| 1031 | { |
| 1032 | int err; |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 1033 | struct switchdev_attr attr = { |
Ido Schimmel | 6ff64f6 | 2015-12-15 16:03:35 +0100 | [diff] [blame] | 1034 | .orig_dev = dev, |
Jiri Pirko | 1f86839 | 2015-10-01 11:03:42 +0200 | [diff] [blame] | 1035 | .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID, |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 1036 | .flags = SWITCHDEV_F_NO_RECURSE, |
| 1037 | }; |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1038 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 1039 | err = switchdev_port_attr_get(dev, &attr); |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1040 | if (err) { |
| 1041 | if (err == -EOPNOTSUPP) |
| 1042 | return 0; |
| 1043 | return err; |
| 1044 | } |
| 1045 | |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 1046 | if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len, |
| 1047 | attr.u.ppid.id)) |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1048 | return -EMSGSIZE; |
| 1049 | |
| 1050 | return 0; |
| 1051 | } |
| 1052 | |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1053 | static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb, |
| 1054 | struct net_device *dev) |
| 1055 | { |
| 1056 | const struct rtnl_link_stats64 *stats; |
| 1057 | struct rtnl_link_stats64 temp; |
| 1058 | struct nlattr *attr; |
| 1059 | |
| 1060 | stats = dev_get_stats(dev, &temp); |
| 1061 | |
| 1062 | attr = nla_reserve(skb, IFLA_STATS, |
| 1063 | sizeof(struct rtnl_link_stats)); |
| 1064 | if (!attr) |
| 1065 | return -EMSGSIZE; |
| 1066 | |
| 1067 | copy_rtnl_link_stats(nla_data(attr), stats); |
| 1068 | |
| 1069 | attr = nla_reserve(skb, IFLA_STATS64, |
| 1070 | sizeof(struct rtnl_link_stats64)); |
| 1071 | if (!attr) |
| 1072 | return -EMSGSIZE; |
| 1073 | |
| 1074 | copy_rtnl_link_stats64(nla_data(attr), stats); |
| 1075 | |
| 1076 | return 0; |
| 1077 | } |
| 1078 | |
| 1079 | static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, |
| 1080 | struct net_device *dev, |
| 1081 | int vfs_num, |
| 1082 | struct nlattr *vfinfo) |
| 1083 | { |
| 1084 | struct ifla_vf_rss_query_en vf_rss_query_en; |
| 1085 | struct ifla_vf_link_state vf_linkstate; |
| 1086 | struct ifla_vf_spoofchk vf_spoofchk; |
| 1087 | struct ifla_vf_tx_rate vf_tx_rate; |
| 1088 | struct ifla_vf_stats vf_stats; |
| 1089 | struct ifla_vf_trust vf_trust; |
| 1090 | struct ifla_vf_vlan vf_vlan; |
| 1091 | struct ifla_vf_rate vf_rate; |
| 1092 | struct nlattr *vf, *vfstats; |
| 1093 | struct ifla_vf_mac vf_mac; |
| 1094 | struct ifla_vf_info ivi; |
| 1095 | |
| 1096 | /* Not all SR-IOV capable drivers support the |
| 1097 | * spoofcheck and "RSS query enable" query. Preset to |
| 1098 | * -1 so the user space tool can detect that the driver |
| 1099 | * didn't report anything. |
| 1100 | */ |
| 1101 | ivi.spoofchk = -1; |
| 1102 | ivi.rss_query_en = -1; |
| 1103 | ivi.trusted = -1; |
| 1104 | memset(ivi.mac, 0, sizeof(ivi.mac)); |
| 1105 | /* The default value for VF link state is "auto" |
| 1106 | * IFLA_VF_LINK_STATE_AUTO which equals zero |
| 1107 | */ |
| 1108 | ivi.linkstate = 0; |
| 1109 | if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi)) |
| 1110 | return 0; |
| 1111 | |
| 1112 | vf_mac.vf = |
| 1113 | vf_vlan.vf = |
| 1114 | vf_rate.vf = |
| 1115 | vf_tx_rate.vf = |
| 1116 | vf_spoofchk.vf = |
| 1117 | vf_linkstate.vf = |
| 1118 | vf_rss_query_en.vf = |
| 1119 | vf_trust.vf = ivi.vf; |
| 1120 | |
| 1121 | memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); |
| 1122 | vf_vlan.vlan = ivi.vlan; |
| 1123 | vf_vlan.qos = ivi.qos; |
| 1124 | vf_tx_rate.rate = ivi.max_tx_rate; |
| 1125 | vf_rate.min_tx_rate = ivi.min_tx_rate; |
| 1126 | vf_rate.max_tx_rate = ivi.max_tx_rate; |
| 1127 | vf_spoofchk.setting = ivi.spoofchk; |
| 1128 | vf_linkstate.link_state = ivi.linkstate; |
| 1129 | vf_rss_query_en.setting = ivi.rss_query_en; |
| 1130 | vf_trust.setting = ivi.trusted; |
| 1131 | vf = nla_nest_start(skb, IFLA_VF_INFO); |
| 1132 | if (!vf) { |
| 1133 | nla_nest_cancel(skb, vfinfo); |
| 1134 | return -EMSGSIZE; |
| 1135 | } |
| 1136 | if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || |
| 1137 | nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || |
| 1138 | nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), |
| 1139 | &vf_rate) || |
| 1140 | nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), |
| 1141 | &vf_tx_rate) || |
| 1142 | nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), |
| 1143 | &vf_spoofchk) || |
| 1144 | nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), |
| 1145 | &vf_linkstate) || |
| 1146 | nla_put(skb, IFLA_VF_RSS_QUERY_EN, |
| 1147 | sizeof(vf_rss_query_en), |
| 1148 | &vf_rss_query_en) || |
| 1149 | nla_put(skb, IFLA_VF_TRUST, |
| 1150 | sizeof(vf_trust), &vf_trust)) |
| 1151 | return -EMSGSIZE; |
| 1152 | memset(&vf_stats, 0, sizeof(vf_stats)); |
| 1153 | if (dev->netdev_ops->ndo_get_vf_stats) |
| 1154 | dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, |
| 1155 | &vf_stats); |
| 1156 | vfstats = nla_nest_start(skb, IFLA_VF_STATS); |
| 1157 | if (!vfstats) { |
| 1158 | nla_nest_cancel(skb, vf); |
| 1159 | nla_nest_cancel(skb, vfinfo); |
| 1160 | return -EMSGSIZE; |
| 1161 | } |
| 1162 | if (nla_put_u64(skb, IFLA_VF_STATS_RX_PACKETS, |
| 1163 | vf_stats.rx_packets) || |
| 1164 | nla_put_u64(skb, IFLA_VF_STATS_TX_PACKETS, |
| 1165 | vf_stats.tx_packets) || |
| 1166 | nla_put_u64(skb, IFLA_VF_STATS_RX_BYTES, |
| 1167 | vf_stats.rx_bytes) || |
| 1168 | nla_put_u64(skb, IFLA_VF_STATS_TX_BYTES, |
| 1169 | vf_stats.tx_bytes) || |
| 1170 | nla_put_u64(skb, IFLA_VF_STATS_BROADCAST, |
| 1171 | vf_stats.broadcast) || |
| 1172 | nla_put_u64(skb, IFLA_VF_STATS_MULTICAST, |
| 1173 | vf_stats.multicast)) |
| 1174 | return -EMSGSIZE; |
| 1175 | nla_nest_end(skb, vfstats); |
| 1176 | nla_nest_end(skb, vf); |
| 1177 | return 0; |
| 1178 | } |
| 1179 | |
| 1180 | static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev) |
| 1181 | { |
| 1182 | struct rtnl_link_ifmap map = { |
| 1183 | .mem_start = dev->mem_start, |
| 1184 | .mem_end = dev->mem_end, |
| 1185 | .base_addr = dev->base_addr, |
| 1186 | .irq = dev->irq, |
| 1187 | .dma = dev->dma, |
| 1188 | .port = dev->if_port, |
| 1189 | }; |
| 1190 | if (nla_put(skb, IFLA_MAP, sizeof(map), &map)) |
| 1191 | return -EMSGSIZE; |
| 1192 | |
| 1193 | return 0; |
| 1194 | } |
| 1195 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1196 | static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev, |
Patrick McHardy | 575c3e2 | 2007-05-22 17:00:49 -0700 | [diff] [blame] | 1197 | int type, u32 pid, u32 seq, u32 change, |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1198 | unsigned int flags, u32 ext_filter_mask) |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1199 | { |
| 1200 | struct ifinfomsg *ifm; |
| 1201 | struct nlmsghdr *nlh; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1202 | struct nlattr *af_spec; |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 1203 | struct rtnl_af_ops *af_ops; |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1204 | struct net_device *upper_dev = netdev_master_upper_dev_get(dev); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1205 | |
Eric Dumazet | 2907c35 | 2011-05-25 07:34:04 +0000 | [diff] [blame] | 1206 | ASSERT_RTNL(); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1207 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); |
| 1208 | if (nlh == NULL) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 1209 | return -EMSGSIZE; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1210 | |
| 1211 | ifm = nlmsg_data(nlh); |
| 1212 | ifm->ifi_family = AF_UNSPEC; |
| 1213 | ifm->__ifi_pad = 0; |
| 1214 | ifm->ifi_type = dev->type; |
| 1215 | ifm->ifi_index = dev->ifindex; |
| 1216 | ifm->ifi_flags = dev_get_flags(dev); |
| 1217 | ifm->ifi_change = change; |
| 1218 | |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1219 | if (nla_put_string(skb, IFLA_IFNAME, dev->name) || |
| 1220 | nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || |
| 1221 | nla_put_u8(skb, IFLA_OPERSTATE, |
| 1222 | netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || |
| 1223 | nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || |
| 1224 | nla_put_u32(skb, IFLA_MTU, dev->mtu) || |
| 1225 | nla_put_u32(skb, IFLA_GROUP, dev->group) || |
Ben Greear | edbc0bb | 2012-03-29 12:51:30 +0000 | [diff] [blame] | 1226 | nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 1227 | nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || |
Eric Dumazet | c70ce02 | 2016-03-21 09:55:10 -0700 | [diff] [blame] | 1228 | nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) || |
| 1229 | 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] | 1230 | #ifdef CONFIG_RPS |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 1231 | 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] | 1232 | #endif |
Nicolas Dichtel | a54acb3 | 2015-04-02 17:07:00 +0200 | [diff] [blame] | 1233 | (dev->ifindex != dev_get_iflink(dev) && |
| 1234 | nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) || |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1235 | (upper_dev && |
| 1236 | nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) || |
Jiri Pirko | 9a57247 | 2012-12-27 23:49:39 +0000 | [diff] [blame] | 1237 | nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1238 | (dev->qdisc && |
| 1239 | nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) || |
| 1240 | (dev->ifalias && |
david decotigny | 2d3b479 | 2014-03-29 09:48:35 -0700 | [diff] [blame] | 1241 | nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) || |
| 1242 | nla_put_u32(skb, IFLA_CARRIER_CHANGES, |
Anuradha Karuppiah | 88d6378 | 2015-07-14 13:43:20 -0700 | [diff] [blame] | 1243 | atomic_read(&dev->carrier_changes)) || |
| 1244 | nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down)) |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1245 | goto nla_put_failure; |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 1246 | |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1247 | if (rtnl_fill_link_ifmap(skb, dev)) |
| 1248 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | |
| 1250 | if (dev->addr_len) { |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1251 | if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || |
| 1252 | nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) |
| 1253 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | } |
| 1255 | |
Jiri Pirko | 66cae9e | 2013-07-29 18:16:50 +0200 | [diff] [blame] | 1256 | if (rtnl_phys_port_id_fill(skb, dev)) |
| 1257 | goto nla_put_failure; |
| 1258 | |
David Ahern | db24a90 | 2015-03-17 20:23:15 -0600 | [diff] [blame] | 1259 | if (rtnl_phys_port_name_fill(skb, dev)) |
| 1260 | goto nla_put_failure; |
| 1261 | |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1262 | if (rtnl_phys_switch_id_fill(skb, dev)) |
| 1263 | goto nla_put_failure; |
| 1264 | |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1265 | if (rtnl_fill_stats(skb, dev)) |
Pavel Emelyanov | 96e7408 | 2008-05-21 14:12:46 -0700 | [diff] [blame] | 1266 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1268 | if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) && |
| 1269 | nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent))) |
| 1270 | goto nla_put_failure; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1271 | |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1272 | if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent && |
| 1273 | ext_filter_mask & RTEXT_FILTER_VF) { |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 1274 | int i; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1275 | struct nlattr *vfinfo; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1276 | int num_vfs = dev_num_vf(dev->dev.parent); |
| 1277 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1278 | vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST); |
| 1279 | if (!vfinfo) |
| 1280 | goto nla_put_failure; |
| 1281 | for (i = 0; i < num_vfs; i++) { |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1282 | if (rtnl_fill_vfinfo(skb, dev, i, vfinfo)) |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1283 | goto nla_put_failure; |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 1284 | } |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1285 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1286 | nla_nest_end(skb, vfinfo); |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 1287 | } |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1288 | |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 1289 | if (rtnl_port_fill(skb, dev, ext_filter_mask)) |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1290 | goto nla_put_failure; |
| 1291 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 1292 | if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1293 | if (rtnl_link_fill(skb, dev) < 0) |
| 1294 | goto nla_put_failure; |
| 1295 | } |
| 1296 | |
Nicolas Dichtel | d37512a | 2015-01-15 15:11:16 +0100 | [diff] [blame] | 1297 | if (dev->rtnl_link_ops && |
| 1298 | dev->rtnl_link_ops->get_link_net) { |
| 1299 | struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); |
| 1300 | |
| 1301 | if (!net_eq(dev_net(dev), link_net)) { |
Nicolas Dichtel | 7a0877d | 2015-05-07 11:02:49 +0200 | [diff] [blame] | 1302 | int id = peernet2id_alloc(dev_net(dev), link_net); |
Nicolas Dichtel | d37512a | 2015-01-15 15:11:16 +0100 | [diff] [blame] | 1303 | |
| 1304 | if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) |
| 1305 | goto nla_put_failure; |
| 1306 | } |
| 1307 | } |
| 1308 | |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 1309 | if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC))) |
| 1310 | goto nla_put_failure; |
| 1311 | |
| 1312 | list_for_each_entry(af_ops, &rtnl_af_ops, list) { |
| 1313 | if (af_ops->fill_link_af) { |
| 1314 | struct nlattr *af; |
| 1315 | int err; |
| 1316 | |
| 1317 | if (!(af = nla_nest_start(skb, af_ops->family))) |
| 1318 | goto nla_put_failure; |
| 1319 | |
Sowmini Varadhan | d5566fd | 2015-09-11 16:48:48 -0400 | [diff] [blame] | 1320 | err = af_ops->fill_link_af(skb, dev, ext_filter_mask); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 1321 | |
| 1322 | /* |
| 1323 | * Caller may return ENODATA to indicate that there |
| 1324 | * was no data to be dumped. This is not an error, it |
| 1325 | * means we should trim the attribute header and |
| 1326 | * continue. |
| 1327 | */ |
| 1328 | if (err == -ENODATA) |
| 1329 | nla_nest_cancel(skb, af); |
| 1330 | else if (err < 0) |
| 1331 | goto nla_put_failure; |
| 1332 | |
| 1333 | nla_nest_end(skb, af); |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | nla_nest_end(skb, af_spec); |
| 1338 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 1339 | nlmsg_end(skb, nlh); |
| 1340 | return 0; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1341 | |
| 1342 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 1343 | nlmsg_cancel(skb, nlh); |
| 1344 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1347 | static const struct nla_policy ifla_policy[IFLA_MAX+1] = { |
| 1348 | [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, |
| 1349 | [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, |
| 1350 | [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, |
| 1351 | [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, |
| 1352 | [IFLA_MTU] = { .type = NLA_U32 }, |
| 1353 | [IFLA_LINK] = { .type = NLA_U32 }, |
| 1354 | [IFLA_MASTER] = { .type = NLA_U32 }, |
| 1355 | [IFLA_CARRIER] = { .type = NLA_U8 }, |
| 1356 | [IFLA_TXQLEN] = { .type = NLA_U32 }, |
| 1357 | [IFLA_WEIGHT] = { .type = NLA_U32 }, |
| 1358 | [IFLA_OPERSTATE] = { .type = NLA_U8 }, |
| 1359 | [IFLA_LINKMODE] = { .type = NLA_U8 }, |
| 1360 | [IFLA_LINKINFO] = { .type = NLA_NESTED }, |
| 1361 | [IFLA_NET_NS_PID] = { .type = NLA_U32 }, |
| 1362 | [IFLA_NET_NS_FD] = { .type = NLA_U32 }, |
| 1363 | [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 }, |
| 1364 | [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, |
| 1365 | [IFLA_VF_PORTS] = { .type = NLA_NESTED }, |
| 1366 | [IFLA_PORT_SELF] = { .type = NLA_NESTED }, |
| 1367 | [IFLA_AF_SPEC] = { .type = NLA_NESTED }, |
| 1368 | [IFLA_EXT_MASK] = { .type = NLA_U32 }, |
| 1369 | [IFLA_PROMISCUITY] = { .type = NLA_U32 }, |
| 1370 | [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, |
| 1371 | [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, |
Jiri Pirko | 02637fc | 2014-11-28 14:34:16 +0100 | [diff] [blame] | 1372 | [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] | 1373 | [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1374 | [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] | 1375 | [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, |
Anuradha Karuppiah | 88d6378 | 2015-07-14 13:43:20 -0700 | [diff] [blame] | 1376 | [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1377 | }; |
| 1378 | |
| 1379 | static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { |
| 1380 | [IFLA_INFO_KIND] = { .type = NLA_STRING }, |
| 1381 | [IFLA_INFO_DATA] = { .type = NLA_NESTED }, |
| 1382 | [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, |
| 1383 | [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, |
| 1384 | }; |
| 1385 | |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1386 | static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { |
Daniel Borkmann | 364d571 | 2015-02-05 18:44:04 +0100 | [diff] [blame] | 1387 | [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, |
| 1388 | [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, |
| 1389 | [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, |
| 1390 | [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, |
| 1391 | [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, |
| 1392 | [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, |
Vlad Zolotarov | 01a3d79 | 2015-03-30 21:35:23 +0300 | [diff] [blame] | 1393 | [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] | 1394 | [IFLA_VF_STATS] = { .type = NLA_NESTED }, |
Hiroshi Shimamoto | dd461d6 | 2015-08-28 06:57:55 +0000 | [diff] [blame] | 1395 | [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, |
Eli Cohen | cc8e27c | 2016-03-11 22:58:34 +0200 | [diff] [blame] | 1396 | [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) }, |
| 1397 | [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) }, |
Eran Ben Elisha | 3b766cd | 2015-06-15 17:59:07 +0300 | [diff] [blame] | 1398 | }; |
| 1399 | |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1400 | static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { |
| 1401 | [IFLA_PORT_VF] = { .type = NLA_U32 }, |
| 1402 | [IFLA_PORT_PROFILE] = { .type = NLA_STRING, |
| 1403 | .len = PORT_PROFILE_MAX }, |
| 1404 | [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, |
| 1405 | .len = sizeof(struct ifla_port_vsi)}, |
| 1406 | [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, |
| 1407 | .len = PORT_UUID_MAX }, |
| 1408 | [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, |
| 1409 | .len = PORT_UUID_MAX }, |
| 1410 | [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, |
| 1411 | [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, |
| 1412 | }; |
| 1413 | |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 1414 | static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla) |
| 1415 | { |
| 1416 | const struct rtnl_link_ops *ops = NULL; |
| 1417 | struct nlattr *linfo[IFLA_INFO_MAX + 1]; |
| 1418 | |
| 1419 | if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla, ifla_info_policy) < 0) |
| 1420 | return NULL; |
| 1421 | |
| 1422 | if (linfo[IFLA_INFO_KIND]) { |
| 1423 | char kind[MODULE_NAME_LEN]; |
| 1424 | |
| 1425 | nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind)); |
| 1426 | ops = rtnl_link_ops_get(kind); |
| 1427 | } |
| 1428 | |
| 1429 | return ops; |
| 1430 | } |
| 1431 | |
| 1432 | static bool link_master_filtered(struct net_device *dev, int master_idx) |
| 1433 | { |
| 1434 | struct net_device *master; |
| 1435 | |
| 1436 | if (!master_idx) |
| 1437 | return false; |
| 1438 | |
| 1439 | master = netdev_master_upper_dev_get(dev); |
| 1440 | if (!master || master->ifindex != master_idx) |
| 1441 | return true; |
| 1442 | |
| 1443 | return false; |
| 1444 | } |
| 1445 | |
| 1446 | static bool link_kind_filtered(const struct net_device *dev, |
| 1447 | const struct rtnl_link_ops *kind_ops) |
| 1448 | { |
| 1449 | if (kind_ops && dev->rtnl_link_ops != kind_ops) |
| 1450 | return true; |
| 1451 | |
| 1452 | return false; |
| 1453 | } |
| 1454 | |
| 1455 | static bool link_dump_filtered(struct net_device *dev, |
| 1456 | int master_idx, |
| 1457 | const struct rtnl_link_ops *kind_ops) |
| 1458 | { |
| 1459 | if (link_master_filtered(dev, master_idx) || |
| 1460 | link_kind_filtered(dev, kind_ops)) |
| 1461 | return true; |
| 1462 | |
| 1463 | return false; |
| 1464 | } |
| 1465 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1466 | 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] | 1467 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1468 | struct net *net = sock_net(skb->sk); |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1469 | int h, s_h; |
| 1470 | int idx = 0, s_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | struct net_device *dev; |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1472 | struct hlist_head *head; |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1473 | struct nlattr *tb[IFLA_MAX+1]; |
| 1474 | u32 ext_filter_mask = 0; |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 1475 | const struct rtnl_link_ops *kind_ops = NULL; |
| 1476 | unsigned int flags = NLM_F_MULTI; |
| 1477 | int master_idx = 0; |
David Gibson | 973462b | 2014-04-24 10:22:35 +1000 | [diff] [blame] | 1478 | int err; |
Michal Schmidt | e5eca6d | 2014-05-28 14:15:19 +0200 | [diff] [blame] | 1479 | int hdrlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1481 | s_h = cb->args[0]; |
| 1482 | s_idx = cb->args[1]; |
| 1483 | |
Thomas Graf | 4e985ad | 2011-06-21 03:11:20 +0000 | [diff] [blame] | 1484 | cb->seq = net->dev_base_seq; |
| 1485 | |
Michal Schmidt | e5eca6d | 2014-05-28 14:15:19 +0200 | [diff] [blame] | 1486 | /* A hack to preserve kernel<->userspace interface. |
| 1487 | * The correct header is ifinfomsg. It is consistent with rtnl_getlink. |
| 1488 | * However, before Linux v3.9 the code here assumed rtgenmsg and that's |
| 1489 | * what iproute2 < v3.9.0 used. |
| 1490 | * We can detect the old iproute2. Even including the IFLA_EXT_MASK |
| 1491 | * attribute, its netlink message is shorter than struct ifinfomsg. |
| 1492 | */ |
| 1493 | hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ? |
| 1494 | sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); |
| 1495 | |
| 1496 | if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) { |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1497 | |
Eric Dumazet | a4b64fb | 2012-03-04 12:32:10 +0000 | [diff] [blame] | 1498 | if (tb[IFLA_EXT_MASK]) |
| 1499 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 1500 | |
| 1501 | if (tb[IFLA_MASTER]) |
| 1502 | master_idx = nla_get_u32(tb[IFLA_MASTER]); |
| 1503 | |
| 1504 | if (tb[IFLA_LINKINFO]) |
| 1505 | kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]); |
| 1506 | |
| 1507 | if (master_idx || kind_ops) |
| 1508 | flags |= NLM_F_DUMP_FILTERED; |
Eric Dumazet | a4b64fb | 2012-03-04 12:32:10 +0000 | [diff] [blame] | 1509 | } |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1510 | |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1511 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { |
| 1512 | idx = 0; |
| 1513 | head = &net->dev_index_head[h]; |
Eric Dumazet | cac5e65 | 2015-02-27 09:42:50 -0800 | [diff] [blame] | 1514 | hlist_for_each_entry(dev, head, index_hlist) { |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 1515 | if (link_dump_filtered(dev, master_idx, kind_ops)) |
| 1516 | continue; |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1517 | if (idx < s_idx) |
| 1518 | goto cont; |
David Gibson | 973462b | 2014-04-24 10:22:35 +1000 | [diff] [blame] | 1519 | err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK, |
| 1520 | NETLINK_CB(cb->skb).portid, |
| 1521 | cb->nlh->nlmsg_seq, 0, |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 1522 | flags, |
David Gibson | 973462b | 2014-04-24 10:22:35 +1000 | [diff] [blame] | 1523 | ext_filter_mask); |
| 1524 | /* If we ran out of room on the first message, |
| 1525 | * we're in trouble |
| 1526 | */ |
| 1527 | WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); |
| 1528 | |
David S. Miller | 7b46a64 | 2015-01-18 23:36:08 -0500 | [diff] [blame] | 1529 | if (err < 0) |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1530 | goto out; |
Thomas Graf | 4e985ad | 2011-06-21 03:11:20 +0000 | [diff] [blame] | 1531 | |
| 1532 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 1533 | cont: |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1534 | idx++; |
| 1535 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | } |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1537 | out: |
| 1538 | cb->args[1] = idx; |
| 1539 | cb->args[0] = h; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | |
| 1541 | return skb->len; |
| 1542 | } |
| 1543 | |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1544 | int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len) |
| 1545 | { |
| 1546 | return nla_parse(tb, IFLA_MAX, head, len, ifla_policy); |
| 1547 | } |
| 1548 | EXPORT_SYMBOL(rtnl_nla_parse_ifla); |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1549 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1550 | struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) |
| 1551 | { |
| 1552 | struct net *net; |
| 1553 | /* Examine the link attributes and figure out which |
| 1554 | * network namespace we are talking about. |
| 1555 | */ |
| 1556 | if (tb[IFLA_NET_NS_PID]) |
| 1557 | 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] | 1558 | else if (tb[IFLA_NET_NS_FD]) |
| 1559 | 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] | 1560 | else |
| 1561 | net = get_net(src_net); |
| 1562 | return net; |
| 1563 | } |
| 1564 | EXPORT_SYMBOL(rtnl_link_get_net); |
| 1565 | |
Thomas Graf | 1840bb1 | 2008-02-23 19:54:36 -0800 | [diff] [blame] | 1566 | static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) |
| 1567 | { |
| 1568 | if (dev) { |
| 1569 | if (tb[IFLA_ADDRESS] && |
| 1570 | nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) |
| 1571 | return -EINVAL; |
| 1572 | |
| 1573 | if (tb[IFLA_BROADCAST] && |
| 1574 | nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) |
| 1575 | return -EINVAL; |
| 1576 | } |
| 1577 | |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 1578 | if (tb[IFLA_AF_SPEC]) { |
| 1579 | struct nlattr *af; |
| 1580 | int rem, err; |
| 1581 | |
| 1582 | nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { |
| 1583 | const struct rtnl_af_ops *af_ops; |
| 1584 | |
| 1585 | if (!(af_ops = rtnl_af_lookup(nla_type(af)))) |
| 1586 | return -EAFNOSUPPORT; |
| 1587 | |
| 1588 | if (!af_ops->set_link_af) |
| 1589 | return -EOPNOTSUPP; |
| 1590 | |
| 1591 | if (af_ops->validate_link_af) { |
Kurt Van Dijck | 6d3a9a6 | 2011-01-26 04:55:24 +0000 | [diff] [blame] | 1592 | err = af_ops->validate_link_af(dev, af); |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 1593 | if (err < 0) |
| 1594 | return err; |
| 1595 | } |
| 1596 | } |
| 1597 | } |
| 1598 | |
Thomas Graf | 1840bb1 | 2008-02-23 19:54:36 -0800 | [diff] [blame] | 1599 | return 0; |
| 1600 | } |
| 1601 | |
Eli Cohen | cc8e27c | 2016-03-11 22:58:34 +0200 | [diff] [blame] | 1602 | static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt, |
| 1603 | int guid_type) |
| 1604 | { |
| 1605 | const struct net_device_ops *ops = dev->netdev_ops; |
| 1606 | |
| 1607 | return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type); |
| 1608 | } |
| 1609 | |
| 1610 | static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type) |
| 1611 | { |
| 1612 | if (dev->type != ARPHRD_INFINIBAND) |
| 1613 | return -EOPNOTSUPP; |
| 1614 | |
| 1615 | return handle_infiniband_guid(dev, ivt, guid_type); |
| 1616 | } |
| 1617 | |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1618 | static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1619 | { |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1620 | const struct net_device_ops *ops = dev->netdev_ops; |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1621 | int err = -EINVAL; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1622 | |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1623 | if (tb[IFLA_VF_MAC]) { |
| 1624 | struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); |
Vlad Zolotarov | 01a3d79 | 2015-03-30 21:35:23 +0300 | [diff] [blame] | 1625 | |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1626 | err = -EOPNOTSUPP; |
| 1627 | if (ops->ndo_set_vf_mac) |
| 1628 | err = ops->ndo_set_vf_mac(dev, ivm->vf, |
| 1629 | ivm->mac); |
| 1630 | if (err < 0) |
| 1631 | return err; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1632 | } |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1633 | |
| 1634 | if (tb[IFLA_VF_VLAN]) { |
| 1635 | struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); |
| 1636 | |
| 1637 | err = -EOPNOTSUPP; |
| 1638 | if (ops->ndo_set_vf_vlan) |
| 1639 | err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, |
| 1640 | ivv->qos); |
| 1641 | if (err < 0) |
| 1642 | return err; |
| 1643 | } |
| 1644 | |
| 1645 | if (tb[IFLA_VF_TX_RATE]) { |
| 1646 | struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); |
| 1647 | struct ifla_vf_info ivf; |
| 1648 | |
| 1649 | err = -EOPNOTSUPP; |
| 1650 | if (ops->ndo_get_vf_config) |
| 1651 | err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); |
| 1652 | if (err < 0) |
| 1653 | return err; |
| 1654 | |
| 1655 | err = -EOPNOTSUPP; |
| 1656 | if (ops->ndo_set_vf_rate) |
| 1657 | err = ops->ndo_set_vf_rate(dev, ivt->vf, |
| 1658 | ivf.min_tx_rate, |
| 1659 | ivt->rate); |
| 1660 | if (err < 0) |
| 1661 | return err; |
| 1662 | } |
| 1663 | |
| 1664 | if (tb[IFLA_VF_RATE]) { |
| 1665 | struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); |
| 1666 | |
| 1667 | err = -EOPNOTSUPP; |
| 1668 | if (ops->ndo_set_vf_rate) |
| 1669 | err = ops->ndo_set_vf_rate(dev, ivt->vf, |
| 1670 | ivt->min_tx_rate, |
| 1671 | ivt->max_tx_rate); |
| 1672 | if (err < 0) |
| 1673 | return err; |
| 1674 | } |
| 1675 | |
| 1676 | if (tb[IFLA_VF_SPOOFCHK]) { |
| 1677 | struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); |
| 1678 | |
| 1679 | err = -EOPNOTSUPP; |
| 1680 | if (ops->ndo_set_vf_spoofchk) |
| 1681 | err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, |
| 1682 | ivs->setting); |
| 1683 | if (err < 0) |
| 1684 | return err; |
| 1685 | } |
| 1686 | |
| 1687 | if (tb[IFLA_VF_LINK_STATE]) { |
| 1688 | struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); |
| 1689 | |
| 1690 | err = -EOPNOTSUPP; |
| 1691 | if (ops->ndo_set_vf_link_state) |
| 1692 | err = ops->ndo_set_vf_link_state(dev, ivl->vf, |
| 1693 | ivl->link_state); |
| 1694 | if (err < 0) |
| 1695 | return err; |
| 1696 | } |
| 1697 | |
| 1698 | if (tb[IFLA_VF_RSS_QUERY_EN]) { |
| 1699 | struct ifla_vf_rss_query_en *ivrssq_en; |
| 1700 | |
| 1701 | err = -EOPNOTSUPP; |
| 1702 | ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); |
| 1703 | if (ops->ndo_set_vf_rss_query_en) |
| 1704 | err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, |
| 1705 | ivrssq_en->setting); |
| 1706 | if (err < 0) |
| 1707 | return err; |
| 1708 | } |
| 1709 | |
Hiroshi Shimamoto | dd461d6 | 2015-08-28 06:57:55 +0000 | [diff] [blame] | 1710 | if (tb[IFLA_VF_TRUST]) { |
| 1711 | struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); |
| 1712 | |
| 1713 | err = -EOPNOTSUPP; |
| 1714 | if (ops->ndo_set_vf_trust) |
| 1715 | err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); |
| 1716 | if (err < 0) |
| 1717 | return err; |
| 1718 | } |
| 1719 | |
Eli Cohen | cc8e27c | 2016-03-11 22:58:34 +0200 | [diff] [blame] | 1720 | if (tb[IFLA_VF_IB_NODE_GUID]) { |
| 1721 | struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]); |
| 1722 | |
| 1723 | if (!ops->ndo_set_vf_guid) |
| 1724 | return -EOPNOTSUPP; |
| 1725 | |
| 1726 | return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID); |
| 1727 | } |
| 1728 | |
| 1729 | if (tb[IFLA_VF_IB_PORT_GUID]) { |
| 1730 | struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]); |
| 1731 | |
| 1732 | if (!ops->ndo_set_vf_guid) |
| 1733 | return -EOPNOTSUPP; |
| 1734 | |
| 1735 | return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID); |
| 1736 | } |
| 1737 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1738 | return err; |
| 1739 | } |
| 1740 | |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1741 | static int do_set_master(struct net_device *dev, int ifindex) |
| 1742 | { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1743 | struct net_device *upper_dev = netdev_master_upper_dev_get(dev); |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1744 | const struct net_device_ops *ops; |
| 1745 | int err; |
| 1746 | |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1747 | if (upper_dev) { |
| 1748 | if (upper_dev->ifindex == ifindex) |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1749 | return 0; |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1750 | ops = upper_dev->netdev_ops; |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1751 | if (ops->ndo_del_slave) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1752 | err = ops->ndo_del_slave(upper_dev, dev); |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1753 | if (err) |
| 1754 | return err; |
| 1755 | } else { |
| 1756 | return -EOPNOTSUPP; |
| 1757 | } |
| 1758 | } |
| 1759 | |
| 1760 | if (ifindex) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1761 | upper_dev = __dev_get_by_index(dev_net(dev), ifindex); |
| 1762 | if (!upper_dev) |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1763 | return -EINVAL; |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1764 | ops = upper_dev->netdev_ops; |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1765 | if (ops->ndo_add_slave) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1766 | err = ops->ndo_add_slave(upper_dev, dev); |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1767 | if (err) |
| 1768 | return err; |
| 1769 | } else { |
| 1770 | return -EOPNOTSUPP; |
| 1771 | } |
| 1772 | } |
| 1773 | return 0; |
| 1774 | } |
| 1775 | |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1776 | #define DO_SETLINK_MODIFIED 0x01 |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1777 | /* notify flag means notify + modified. */ |
| 1778 | #define DO_SETLINK_NOTIFY 0x03 |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 1779 | static int do_setlink(const struct sk_buff *skb, |
| 1780 | struct net_device *dev, struct ifinfomsg *ifm, |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1781 | struct nlattr **tb, char *ifname, int status) |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1782 | { |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 1783 | const struct net_device_ops *ops = dev->netdev_ops; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1784 | int err; |
| 1785 | |
Eric W. Biederman | f063052 | 2011-05-04 17:51:50 -0700 | [diff] [blame] | 1786 | if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) { |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1787 | struct net *net = rtnl_link_get_net(dev_net(dev), tb); |
Eric W. Biederman | d8a5ec6 | 2007-09-12 13:57:04 +0200 | [diff] [blame] | 1788 | if (IS_ERR(net)) { |
| 1789 | err = PTR_ERR(net); |
| 1790 | goto errout; |
| 1791 | } |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 1792 | if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) { |
Nicolas Dichtel | e0ebde0 | 2014-11-27 10:16:15 +0100 | [diff] [blame] | 1793 | put_net(net); |
Eric W. Biederman | b51642f | 2012-11-16 03:03:11 +0000 | [diff] [blame] | 1794 | err = -EPERM; |
| 1795 | goto errout; |
| 1796 | } |
Eric W. Biederman | d8a5ec6 | 2007-09-12 13:57:04 +0200 | [diff] [blame] | 1797 | err = dev_change_net_namespace(dev, net, ifname); |
| 1798 | put_net(net); |
| 1799 | if (err) |
| 1800 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1801 | status |= DO_SETLINK_MODIFIED; |
Eric W. Biederman | d8a5ec6 | 2007-09-12 13:57:04 +0200 | [diff] [blame] | 1802 | } |
| 1803 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1804 | if (tb[IFLA_MAP]) { |
| 1805 | struct rtnl_link_ifmap *u_map; |
| 1806 | struct ifmap k_map; |
| 1807 | |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 1808 | if (!ops->ndo_set_config) { |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1809 | err = -EOPNOTSUPP; |
| 1810 | goto errout; |
| 1811 | } |
| 1812 | |
| 1813 | if (!netif_device_present(dev)) { |
| 1814 | err = -ENODEV; |
| 1815 | goto errout; |
| 1816 | } |
| 1817 | |
| 1818 | u_map = nla_data(tb[IFLA_MAP]); |
| 1819 | k_map.mem_start = (unsigned long) u_map->mem_start; |
| 1820 | k_map.mem_end = (unsigned long) u_map->mem_end; |
| 1821 | k_map.base_addr = (unsigned short) u_map->base_addr; |
| 1822 | k_map.irq = (unsigned char) u_map->irq; |
| 1823 | k_map.dma = (unsigned char) u_map->dma; |
| 1824 | k_map.port = (unsigned char) u_map->port; |
| 1825 | |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 1826 | err = ops->ndo_set_config(dev, &k_map); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1827 | if (err < 0) |
| 1828 | goto errout; |
| 1829 | |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1830 | status |= DO_SETLINK_NOTIFY; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1831 | } |
| 1832 | |
| 1833 | if (tb[IFLA_ADDRESS]) { |
| 1834 | struct sockaddr *sa; |
| 1835 | int len; |
| 1836 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1837 | len = sizeof(sa_family_t) + dev->addr_len; |
| 1838 | sa = kmalloc(len, GFP_KERNEL); |
| 1839 | if (!sa) { |
| 1840 | err = -ENOMEM; |
| 1841 | goto errout; |
| 1842 | } |
| 1843 | sa->sa_family = dev->type; |
| 1844 | memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), |
| 1845 | dev->addr_len); |
Jiri Pirko | e7c3273 | 2013-01-01 03:30:13 +0000 | [diff] [blame] | 1846 | err = dev_set_mac_address(dev, sa); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1847 | kfree(sa); |
| 1848 | if (err) |
| 1849 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1850 | status |= DO_SETLINK_MODIFIED; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | if (tb[IFLA_MTU]) { |
| 1854 | err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); |
| 1855 | if (err < 0) |
| 1856 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1857 | status |= DO_SETLINK_MODIFIED; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1858 | } |
| 1859 | |
Vlad Dogaru | cbda10f | 2011-01-13 23:38:30 +0000 | [diff] [blame] | 1860 | if (tb[IFLA_GROUP]) { |
| 1861 | dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1862 | status |= DO_SETLINK_NOTIFY; |
Vlad Dogaru | cbda10f | 2011-01-13 23:38:30 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1865 | /* |
| 1866 | * Interface selected by interface index but interface |
| 1867 | * name provided implies that a name change has been |
| 1868 | * requested. |
| 1869 | */ |
| 1870 | if (ifm->ifi_index > 0 && ifname[0]) { |
| 1871 | err = dev_change_name(dev, ifname); |
| 1872 | if (err < 0) |
| 1873 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1874 | status |= DO_SETLINK_MODIFIED; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1875 | } |
| 1876 | |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 1877 | if (tb[IFLA_IFALIAS]) { |
| 1878 | err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), |
| 1879 | nla_len(tb[IFLA_IFALIAS])); |
| 1880 | if (err < 0) |
| 1881 | goto errout; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1882 | status |= DO_SETLINK_NOTIFY; |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 1883 | } |
| 1884 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1885 | if (tb[IFLA_BROADCAST]) { |
| 1886 | nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); |
Jiri Pirko | e7c3273 | 2013-01-01 03:30:13 +0000 | [diff] [blame] | 1887 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | if (ifm->ifi_flags || ifm->ifi_change) { |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 1891 | err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); |
Johannes Berg | 5f9021c | 2008-11-16 23:20:31 -0800 | [diff] [blame] | 1892 | if (err < 0) |
| 1893 | goto errout; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1894 | } |
| 1895 | |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1896 | if (tb[IFLA_MASTER]) { |
| 1897 | err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER])); |
| 1898 | if (err) |
| 1899 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1900 | status |= DO_SETLINK_MODIFIED; |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
Jiri Pirko | 9a57247 | 2012-12-27 23:49:39 +0000 | [diff] [blame] | 1903 | if (tb[IFLA_CARRIER]) { |
| 1904 | err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); |
| 1905 | if (err) |
| 1906 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1907 | status |= DO_SETLINK_MODIFIED; |
Jiri Pirko | 9a57247 | 2012-12-27 23:49:39 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
Nicolas Dichtel | 5d1180f | 2014-09-01 16:07:26 +0200 | [diff] [blame] | 1910 | if (tb[IFLA_TXQLEN]) { |
| 1911 | unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]); |
| 1912 | |
| 1913 | if (dev->tx_queue_len ^ value) |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1914 | status |= DO_SETLINK_NOTIFY; |
Nicolas Dichtel | 5d1180f | 2014-09-01 16:07:26 +0200 | [diff] [blame] | 1915 | |
| 1916 | dev->tx_queue_len = value; |
| 1917 | } |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1918 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1919 | if (tb[IFLA_OPERSTATE]) |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 1920 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1921 | |
| 1922 | if (tb[IFLA_LINKMODE]) { |
Nicolas Dichtel | 1889b0e | 2014-09-01 16:07:27 +0200 | [diff] [blame] | 1923 | unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); |
| 1924 | |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 1925 | write_lock_bh(&dev_base_lock); |
Nicolas Dichtel | 1889b0e | 2014-09-01 16:07:27 +0200 | [diff] [blame] | 1926 | if (dev->link_mode ^ value) |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1927 | status |= DO_SETLINK_NOTIFY; |
Nicolas Dichtel | 1889b0e | 2014-09-01 16:07:27 +0200 | [diff] [blame] | 1928 | dev->link_mode = value; |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 1929 | write_unlock_bh(&dev_base_lock); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1930 | } |
| 1931 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1932 | if (tb[IFLA_VFINFO_LIST]) { |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1933 | struct nlattr *vfinfo[IFLA_VF_MAX + 1]; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1934 | struct nlattr *attr; |
| 1935 | int rem; |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1936 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1937 | nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1938 | if (nla_type(attr) != IFLA_VF_INFO || |
| 1939 | nla_len(attr) < NLA_HDRLEN) { |
David Howells | 253683b | 2010-05-21 02:25:27 +0000 | [diff] [blame] | 1940 | err = -EINVAL; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1941 | goto errout; |
David Howells | 253683b | 2010-05-21 02:25:27 +0000 | [diff] [blame] | 1942 | } |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1943 | err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr, |
| 1944 | ifla_vf_policy); |
| 1945 | if (err < 0) |
| 1946 | goto errout; |
| 1947 | err = do_setvfinfo(dev, vfinfo); |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1948 | if (err < 0) |
| 1949 | goto errout; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1950 | status |= DO_SETLINK_NOTIFY; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1951 | } |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 1952 | } |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1953 | err = 0; |
| 1954 | |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1955 | if (tb[IFLA_VF_PORTS]) { |
| 1956 | struct nlattr *port[IFLA_PORT_MAX+1]; |
| 1957 | struct nlattr *attr; |
| 1958 | int vf; |
| 1959 | int rem; |
| 1960 | |
| 1961 | err = -EOPNOTSUPP; |
| 1962 | if (!ops->ndo_set_vf_port) |
| 1963 | goto errout; |
| 1964 | |
| 1965 | nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { |
Daniel Borkmann | 035d210 | 2015-07-13 00:06:02 +0200 | [diff] [blame] | 1966 | if (nla_type(attr) != IFLA_VF_PORT || |
| 1967 | nla_len(attr) < NLA_HDRLEN) { |
| 1968 | err = -EINVAL; |
| 1969 | goto errout; |
| 1970 | } |
| 1971 | err = nla_parse_nested(port, IFLA_PORT_MAX, attr, |
| 1972 | ifla_port_policy); |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1973 | if (err < 0) |
| 1974 | goto errout; |
| 1975 | if (!port[IFLA_PORT_VF]) { |
| 1976 | err = -EOPNOTSUPP; |
| 1977 | goto errout; |
| 1978 | } |
| 1979 | vf = nla_get_u32(port[IFLA_PORT_VF]); |
| 1980 | err = ops->ndo_set_vf_port(dev, vf, port); |
| 1981 | if (err < 0) |
| 1982 | goto errout; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1983 | status |= DO_SETLINK_NOTIFY; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1984 | } |
| 1985 | } |
| 1986 | err = 0; |
| 1987 | |
| 1988 | if (tb[IFLA_PORT_SELF]) { |
| 1989 | struct nlattr *port[IFLA_PORT_MAX+1]; |
| 1990 | |
| 1991 | err = nla_parse_nested(port, IFLA_PORT_MAX, |
| 1992 | tb[IFLA_PORT_SELF], ifla_port_policy); |
| 1993 | if (err < 0) |
| 1994 | goto errout; |
| 1995 | |
| 1996 | err = -EOPNOTSUPP; |
| 1997 | if (ops->ndo_set_vf_port) |
| 1998 | err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); |
| 1999 | if (err < 0) |
| 2000 | goto errout; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2001 | status |= DO_SETLINK_NOTIFY; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 2002 | } |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 2003 | |
| 2004 | if (tb[IFLA_AF_SPEC]) { |
| 2005 | struct nlattr *af; |
| 2006 | int rem; |
| 2007 | |
| 2008 | nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { |
| 2009 | const struct rtnl_af_ops *af_ops; |
| 2010 | |
| 2011 | if (!(af_ops = rtnl_af_lookup(nla_type(af)))) |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 2012 | BUG(); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 2013 | |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 2014 | err = af_ops->set_link_af(dev, af); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 2015 | if (err < 0) |
| 2016 | goto errout; |
| 2017 | |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2018 | status |= DO_SETLINK_NOTIFY; |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 2019 | } |
| 2020 | } |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 2021 | err = 0; |
| 2022 | |
Anuradha Karuppiah | 88d6378 | 2015-07-14 13:43:20 -0700 | [diff] [blame] | 2023 | if (tb[IFLA_PROTO_DOWN]) { |
| 2024 | err = dev_change_proto_down(dev, |
| 2025 | nla_get_u8(tb[IFLA_PROTO_DOWN])); |
| 2026 | if (err) |
| 2027 | goto errout; |
| 2028 | status |= DO_SETLINK_NOTIFY; |
| 2029 | } |
| 2030 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2031 | errout: |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2032 | if (status & DO_SETLINK_MODIFIED) { |
| 2033 | if (status & DO_SETLINK_NOTIFY) |
| 2034 | netdev_state_change(dev); |
| 2035 | |
| 2036 | if (err < 0) |
| 2037 | 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", |
| 2038 | dev->name); |
| 2039 | } |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2040 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2041 | return err; |
| 2042 | } |
| 2043 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 2044 | static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2046 | struct net *net = sock_net(skb->sk); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2047 | struct ifinfomsg *ifm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | struct net_device *dev; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2049 | int err; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2050 | struct nlattr *tb[IFLA_MAX+1]; |
| 2051 | char ifname[IFNAMSIZ]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2053 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 2054 | if (err < 0) |
| 2055 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 2057 | if (tb[IFLA_IFNAME]) |
| 2058 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
Patrick McHardy | 78e5b891 | 2006-09-13 20:35:36 -0700 | [diff] [blame] | 2059 | else |
| 2060 | ifname[0] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | err = -EINVAL; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2063 | ifm = nlmsg_data(nlh); |
Patrick McHardy | 51055be | 2007-06-05 12:40:01 -0700 | [diff] [blame] | 2064 | if (ifm->ifi_index > 0) |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2065 | dev = __dev_get_by_index(net, ifm->ifi_index); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2066 | else if (tb[IFLA_IFNAME]) |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2067 | dev = __dev_get_by_name(net, ifname); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2068 | else |
| 2069 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2071 | if (dev == NULL) { |
| 2072 | err = -ENODEV; |
| 2073 | goto errout; |
| 2074 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 2076 | err = validate_linkmsg(dev, tb); |
| 2077 | if (err < 0) |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2078 | goto errout; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2079 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 2080 | err = do_setlink(skb, dev, ifm, tb, ifname, 0); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2081 | errout: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | return err; |
| 2083 | } |
| 2084 | |
WANG Cong | 66400d5 | 2015-03-24 11:53:31 -0700 | [diff] [blame] | 2085 | static int rtnl_group_dellink(const struct net *net, int group) |
| 2086 | { |
| 2087 | struct net_device *dev, *aux; |
| 2088 | LIST_HEAD(list_kill); |
| 2089 | bool found = false; |
| 2090 | |
| 2091 | if (!group) |
| 2092 | return -EPERM; |
| 2093 | |
| 2094 | for_each_netdev(net, dev) { |
| 2095 | if (dev->group == group) { |
| 2096 | const struct rtnl_link_ops *ops; |
| 2097 | |
| 2098 | found = true; |
| 2099 | ops = dev->rtnl_link_ops; |
| 2100 | if (!ops || !ops->dellink) |
| 2101 | return -EOPNOTSUPP; |
| 2102 | } |
| 2103 | } |
| 2104 | |
| 2105 | if (!found) |
| 2106 | return -ENODEV; |
| 2107 | |
| 2108 | for_each_netdev_safe(net, dev, aux) { |
| 2109 | if (dev->group == group) { |
| 2110 | const struct rtnl_link_ops *ops; |
| 2111 | |
| 2112 | ops = dev->rtnl_link_ops; |
| 2113 | ops->dellink(dev, &list_kill); |
| 2114 | } |
| 2115 | } |
| 2116 | unregister_netdevice_many(&list_kill); |
| 2117 | |
| 2118 | return 0; |
| 2119 | } |
| 2120 | |
Thomas Graf | 614732e | 2015-07-21 10:44:06 +0200 | [diff] [blame] | 2121 | int rtnl_delete_link(struct net_device *dev) |
| 2122 | { |
| 2123 | const struct rtnl_link_ops *ops; |
| 2124 | LIST_HEAD(list_kill); |
| 2125 | |
| 2126 | ops = dev->rtnl_link_ops; |
| 2127 | if (!ops || !ops->dellink) |
| 2128 | return -EOPNOTSUPP; |
| 2129 | |
| 2130 | ops->dellink(dev, &list_kill); |
| 2131 | unregister_netdevice_many(&list_kill); |
| 2132 | |
| 2133 | return 0; |
| 2134 | } |
| 2135 | EXPORT_SYMBOL_GPL(rtnl_delete_link); |
| 2136 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 2137 | static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2138 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2139 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2140 | struct net_device *dev; |
| 2141 | struct ifinfomsg *ifm; |
| 2142 | char ifname[IFNAMSIZ]; |
| 2143 | struct nlattr *tb[IFLA_MAX+1]; |
| 2144 | int err; |
| 2145 | |
| 2146 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 2147 | if (err < 0) |
| 2148 | return err; |
| 2149 | |
| 2150 | if (tb[IFLA_IFNAME]) |
| 2151 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
| 2152 | |
| 2153 | ifm = nlmsg_data(nlh); |
| 2154 | if (ifm->ifi_index > 0) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 2155 | dev = __dev_get_by_index(net, ifm->ifi_index); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2156 | else if (tb[IFLA_IFNAME]) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 2157 | dev = __dev_get_by_name(net, ifname); |
WANG Cong | 66400d5 | 2015-03-24 11:53:31 -0700 | [diff] [blame] | 2158 | else if (tb[IFLA_GROUP]) |
| 2159 | return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP])); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2160 | else |
| 2161 | return -EINVAL; |
| 2162 | |
| 2163 | if (!dev) |
| 2164 | return -ENODEV; |
| 2165 | |
Thomas Graf | 614732e | 2015-07-21 10:44:06 +0200 | [diff] [blame] | 2166 | return rtnl_delete_link(dev); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2167 | } |
| 2168 | |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 2169 | int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) |
| 2170 | { |
| 2171 | unsigned int old_flags; |
| 2172 | int err; |
| 2173 | |
| 2174 | old_flags = dev->flags; |
| 2175 | if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { |
| 2176 | err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); |
| 2177 | if (err < 0) |
| 2178 | return err; |
| 2179 | } |
| 2180 | |
| 2181 | dev->rtnl_link_state = RTNL_LINK_INITIALIZED; |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 2182 | |
Nicolas Dichtel | a528c21 | 2013-09-25 12:02:44 +0200 | [diff] [blame] | 2183 | __dev_notify_flags(dev, old_flags, ~0U); |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 2184 | return 0; |
| 2185 | } |
| 2186 | EXPORT_SYMBOL(rtnl_configure_link); |
| 2187 | |
Rami Rosen | c071356 | 2012-11-30 01:08:47 +0000 | [diff] [blame] | 2188 | struct net_device *rtnl_create_link(struct net *net, |
Thomas Graf | 78ebb0d | 2015-04-10 01:45:53 +0200 | [diff] [blame] | 2189 | const char *ifname, unsigned char name_assign_type, |
Tom Gundersen | 5517750 | 2014-07-14 16:37:25 +0200 | [diff] [blame] | 2190 | const struct rtnl_link_ops *ops, struct nlattr *tb[]) |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2191 | { |
| 2192 | int err; |
| 2193 | struct net_device *dev; |
Jiri Pirko | d40156a | 2012-07-20 02:28:47 +0000 | [diff] [blame] | 2194 | unsigned int num_tx_queues = 1; |
| 2195 | unsigned int num_rx_queues = 1; |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2196 | |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 2197 | if (tb[IFLA_NUM_TX_QUEUES]) |
| 2198 | num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); |
| 2199 | else if (ops->get_num_tx_queues) |
Jiri Pirko | d40156a | 2012-07-20 02:28:47 +0000 | [diff] [blame] | 2200 | num_tx_queues = ops->get_num_tx_queues(); |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 2201 | |
| 2202 | if (tb[IFLA_NUM_RX_QUEUES]) |
| 2203 | num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); |
| 2204 | else if (ops->get_num_rx_queues) |
Jiri Pirko | d40156a | 2012-07-20 02:28:47 +0000 | [diff] [blame] | 2205 | num_rx_queues = ops->get_num_rx_queues(); |
stephen hemminger | efacb30 | 2012-04-10 18:34:43 +0000 | [diff] [blame] | 2206 | |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2207 | err = -ENOMEM; |
Tom Gundersen | 5517750 | 2014-07-14 16:37:25 +0200 | [diff] [blame] | 2208 | dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type, |
Tom Gundersen | c835a67 | 2014-07-14 16:37:24 +0200 | [diff] [blame] | 2209 | ops->setup, num_tx_queues, num_rx_queues); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2210 | if (!dev) |
| 2211 | goto err; |
| 2212 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 2213 | dev_net_set(dev, net); |
| 2214 | dev->rtnl_link_ops = ops; |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 2215 | dev->rtnl_link_state = RTNL_LINK_INITIALIZING; |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 2216 | |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2217 | if (tb[IFLA_MTU]) |
| 2218 | dev->mtu = nla_get_u32(tb[IFLA_MTU]); |
Jiri Pirko | 2afb9b5 | 2013-01-06 12:41:57 +0000 | [diff] [blame] | 2219 | if (tb[IFLA_ADDRESS]) { |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2220 | memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), |
| 2221 | nla_len(tb[IFLA_ADDRESS])); |
Jiri Pirko | 2afb9b5 | 2013-01-06 12:41:57 +0000 | [diff] [blame] | 2222 | dev->addr_assign_type = NET_ADDR_SET; |
| 2223 | } |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2224 | if (tb[IFLA_BROADCAST]) |
| 2225 | memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), |
| 2226 | nla_len(tb[IFLA_BROADCAST])); |
| 2227 | if (tb[IFLA_TXQLEN]) |
| 2228 | dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); |
| 2229 | if (tb[IFLA_OPERSTATE]) |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 2230 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2231 | if (tb[IFLA_LINKMODE]) |
| 2232 | dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); |
Patrick McHardy | ffa934f | 2011-01-20 03:00:42 +0000 | [diff] [blame] | 2233 | if (tb[IFLA_GROUP]) |
| 2234 | dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2235 | |
| 2236 | return dev; |
| 2237 | |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2238 | err: |
| 2239 | return ERR_PTR(err); |
| 2240 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 2241 | EXPORT_SYMBOL(rtnl_create_link); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2242 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 2243 | static int rtnl_group_changelink(const struct sk_buff *skb, |
| 2244 | struct net *net, int group, |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 2245 | struct ifinfomsg *ifm, |
| 2246 | struct nlattr **tb) |
| 2247 | { |
WANG Cong | d079535 | 2015-03-23 16:31:09 -0700 | [diff] [blame] | 2248 | struct net_device *dev, *aux; |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 2249 | int err; |
| 2250 | |
WANG Cong | d079535 | 2015-03-23 16:31:09 -0700 | [diff] [blame] | 2251 | for_each_netdev_safe(net, dev, aux) { |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 2252 | if (dev->group == group) { |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 2253 | err = do_setlink(skb, dev, ifm, tb, NULL, 0); |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 2254 | if (err < 0) |
| 2255 | return err; |
| 2256 | } |
| 2257 | } |
| 2258 | |
| 2259 | return 0; |
| 2260 | } |
| 2261 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 2262 | static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2263 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2264 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2265 | const struct rtnl_link_ops *ops; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 2266 | const struct rtnl_link_ops *m_ops = NULL; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2267 | struct net_device *dev; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 2268 | struct net_device *master_dev = NULL; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2269 | struct ifinfomsg *ifm; |
| 2270 | char kind[MODULE_NAME_LEN]; |
| 2271 | char ifname[IFNAMSIZ]; |
| 2272 | struct nlattr *tb[IFLA_MAX+1]; |
| 2273 | struct nlattr *linkinfo[IFLA_INFO_MAX+1]; |
Tom Gundersen | 5517750 | 2014-07-14 16:37:25 +0200 | [diff] [blame] | 2274 | unsigned char name_assign_type = NET_NAME_USER; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2275 | int err; |
| 2276 | |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 2277 | #ifdef CONFIG_MODULES |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2278 | replay: |
Thomas Graf | 8072f08 | 2007-07-31 14:13:50 -0700 | [diff] [blame] | 2279 | #endif |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2280 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 2281 | if (err < 0) |
| 2282 | return err; |
| 2283 | |
| 2284 | if (tb[IFLA_IFNAME]) |
| 2285 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
| 2286 | else |
| 2287 | ifname[0] = '\0'; |
| 2288 | |
| 2289 | ifm = nlmsg_data(nlh); |
| 2290 | if (ifm->ifi_index > 0) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 2291 | dev = __dev_get_by_index(net, ifm->ifi_index); |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 2292 | else { |
| 2293 | if (ifname[0]) |
| 2294 | dev = __dev_get_by_name(net, ifname); |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 2295 | else |
| 2296 | dev = NULL; |
| 2297 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2298 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 2299 | if (dev) { |
| 2300 | master_dev = netdev_master_upper_dev_get(dev); |
| 2301 | if (master_dev) |
| 2302 | m_ops = master_dev->rtnl_link_ops; |
| 2303 | } |
| 2304 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 2305 | err = validate_linkmsg(dev, tb); |
| 2306 | if (err < 0) |
Thomas Graf | 1840bb1 | 2008-02-23 19:54:36 -0800 | [diff] [blame] | 2307 | return err; |
| 2308 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2309 | if (tb[IFLA_LINKINFO]) { |
| 2310 | err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, |
| 2311 | tb[IFLA_LINKINFO], ifla_info_policy); |
| 2312 | if (err < 0) |
| 2313 | return err; |
| 2314 | } else |
| 2315 | memset(linkinfo, 0, sizeof(linkinfo)); |
| 2316 | |
| 2317 | if (linkinfo[IFLA_INFO_KIND]) { |
| 2318 | nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); |
| 2319 | ops = rtnl_link_ops_get(kind); |
| 2320 | } else { |
| 2321 | kind[0] = '\0'; |
| 2322 | ops = NULL; |
| 2323 | } |
| 2324 | |
| 2325 | if (1) { |
Sasha Levin | 4e10fd5 | 2015-02-24 14:14:35 -0500 | [diff] [blame] | 2326 | struct nlattr *attr[ops ? ops->maxtype + 1 : 1]; |
| 2327 | struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1]; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 2328 | struct nlattr **data = NULL; |
| 2329 | struct nlattr **slave_data = NULL; |
Nicolas Dichtel | 317f481 | 2015-01-15 15:11:18 +0100 | [diff] [blame] | 2330 | struct net *dest_net, *link_net = NULL; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2331 | |
| 2332 | if (ops) { |
| 2333 | if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { |
| 2334 | err = nla_parse_nested(attr, ops->maxtype, |
| 2335 | linkinfo[IFLA_INFO_DATA], |
| 2336 | ops->policy); |
| 2337 | if (err < 0) |
| 2338 | return err; |
| 2339 | data = attr; |
| 2340 | } |
| 2341 | if (ops->validate) { |
| 2342 | err = ops->validate(tb, data); |
| 2343 | if (err < 0) |
| 2344 | return err; |
| 2345 | } |
| 2346 | } |
| 2347 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 2348 | if (m_ops) { |
| 2349 | if (m_ops->slave_maxtype && |
| 2350 | linkinfo[IFLA_INFO_SLAVE_DATA]) { |
| 2351 | err = nla_parse_nested(slave_attr, |
| 2352 | m_ops->slave_maxtype, |
| 2353 | linkinfo[IFLA_INFO_SLAVE_DATA], |
| 2354 | m_ops->slave_policy); |
| 2355 | if (err < 0) |
| 2356 | return err; |
| 2357 | slave_data = slave_attr; |
| 2358 | } |
| 2359 | if (m_ops->slave_validate) { |
| 2360 | err = m_ops->slave_validate(tb, slave_data); |
| 2361 | if (err < 0) |
| 2362 | return err; |
| 2363 | } |
| 2364 | } |
| 2365 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2366 | if (dev) { |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 2367 | int status = 0; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2368 | |
| 2369 | if (nlh->nlmsg_flags & NLM_F_EXCL) |
| 2370 | return -EEXIST; |
| 2371 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
| 2372 | return -EOPNOTSUPP; |
| 2373 | |
| 2374 | if (linkinfo[IFLA_INFO_DATA]) { |
| 2375 | if (!ops || ops != dev->rtnl_link_ops || |
| 2376 | !ops->changelink) |
| 2377 | return -EOPNOTSUPP; |
| 2378 | |
| 2379 | err = ops->changelink(dev, tb, data); |
| 2380 | if (err < 0) |
| 2381 | return err; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2382 | status |= DO_SETLINK_NOTIFY; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2383 | } |
| 2384 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 2385 | if (linkinfo[IFLA_INFO_SLAVE_DATA]) { |
| 2386 | if (!m_ops || !m_ops->slave_changelink) |
| 2387 | return -EOPNOTSUPP; |
| 2388 | |
| 2389 | err = m_ops->slave_changelink(master_dev, dev, |
| 2390 | tb, slave_data); |
| 2391 | if (err < 0) |
| 2392 | return err; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2393 | status |= DO_SETLINK_NOTIFY; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 2394 | } |
| 2395 | |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 2396 | return do_setlink(skb, dev, ifm, tb, ifname, status); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2397 | } |
| 2398 | |
Patrick McHardy | ffa934f | 2011-01-20 03:00:42 +0000 | [diff] [blame] | 2399 | if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { |
| 2400 | if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 2401 | return rtnl_group_changelink(skb, net, |
Patrick McHardy | ffa934f | 2011-01-20 03:00:42 +0000 | [diff] [blame] | 2402 | nla_get_u32(tb[IFLA_GROUP]), |
| 2403 | ifm, tb); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2404 | return -ENODEV; |
Patrick McHardy | ffa934f | 2011-01-20 03:00:42 +0000 | [diff] [blame] | 2405 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2406 | |
Patrick McHardy | 0e06877 | 2007-07-11 19:42:31 -0700 | [diff] [blame] | 2407 | if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO]) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2408 | return -EOPNOTSUPP; |
| 2409 | |
| 2410 | if (!ops) { |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 2411 | #ifdef CONFIG_MODULES |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2412 | if (kind[0]) { |
| 2413 | __rtnl_unlock(); |
| 2414 | request_module("rtnl-link-%s", kind); |
| 2415 | rtnl_lock(); |
| 2416 | ops = rtnl_link_ops_get(kind); |
| 2417 | if (ops) |
| 2418 | goto replay; |
| 2419 | } |
| 2420 | #endif |
| 2421 | return -EOPNOTSUPP; |
| 2422 | } |
| 2423 | |
Jiri Pirko | b0ab2fa | 2014-06-26 09:58:25 +0200 | [diff] [blame] | 2424 | if (!ops->setup) |
| 2425 | return -EOPNOTSUPP; |
| 2426 | |
Tom Gundersen | 5517750 | 2014-07-14 16:37:25 +0200 | [diff] [blame] | 2427 | if (!ifname[0]) { |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2428 | snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); |
Tom Gundersen | 5517750 | 2014-07-14 16:37:25 +0200 | [diff] [blame] | 2429 | name_assign_type = NET_NAME_ENUM; |
| 2430 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2431 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 2432 | dest_net = rtnl_link_get_net(net, tb); |
Eric W. Biederman | 13ad177 | 2011-01-29 14:57:22 +0000 | [diff] [blame] | 2433 | if (IS_ERR(dest_net)) |
| 2434 | return PTR_ERR(dest_net); |
| 2435 | |
Eric W. Biederman | 505ce41 | 2015-02-26 16:19:00 -0600 | [diff] [blame] | 2436 | err = -EPERM; |
| 2437 | if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN)) |
| 2438 | goto out; |
| 2439 | |
Nicolas Dichtel | 317f481 | 2015-01-15 15:11:18 +0100 | [diff] [blame] | 2440 | if (tb[IFLA_LINK_NETNSID]) { |
| 2441 | int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); |
| 2442 | |
| 2443 | link_net = get_net_ns_by_id(dest_net, id); |
| 2444 | if (!link_net) { |
| 2445 | err = -EINVAL; |
| 2446 | goto out; |
| 2447 | } |
Eric W. Biederman | 06615be | 2015-02-26 16:20:07 -0600 | [diff] [blame] | 2448 | err = -EPERM; |
| 2449 | if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) |
| 2450 | goto out; |
Nicolas Dichtel | 317f481 | 2015-01-15 15:11:18 +0100 | [diff] [blame] | 2451 | } |
| 2452 | |
| 2453 | dev = rtnl_create_link(link_net ? : dest_net, ifname, |
| 2454 | name_assign_type, ops, tb); |
Pavel Emelyanov | 9c7dafb | 2012-08-08 21:52:46 +0000 | [diff] [blame] | 2455 | if (IS_ERR(dev)) { |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2456 | err = PTR_ERR(dev); |
Pavel Emelyanov | 9c7dafb | 2012-08-08 21:52:46 +0000 | [diff] [blame] | 2457 | goto out; |
| 2458 | } |
| 2459 | |
| 2460 | dev->ifindex = ifm->ifi_index; |
| 2461 | |
Cong Wang | 0e0eee2 | 2014-02-11 15:51:30 -0800 | [diff] [blame] | 2462 | if (ops->newlink) { |
Nicolas Dichtel | 7b4ce69 | 2015-01-27 11:13:08 +0100 | [diff] [blame] | 2463 | err = ops->newlink(link_net ? : net, dev, tb, data); |
Cong Wang | 0e0eee2 | 2014-02-11 15:51:30 -0800 | [diff] [blame] | 2464 | /* Drivers should call free_netdev() in ->destructor |
Cong Wang | e51fb15 | 2014-06-03 16:40:47 -0700 | [diff] [blame] | 2465 | * and unregister it on failure after registration |
| 2466 | * so that device could be finally freed in rtnl_unlock. |
Cong Wang | 0e0eee2 | 2014-02-11 15:51:30 -0800 | [diff] [blame] | 2467 | */ |
Cong Wang | e51fb15 | 2014-06-03 16:40:47 -0700 | [diff] [blame] | 2468 | if (err < 0) { |
| 2469 | /* If device is not registered at all, free it now */ |
| 2470 | if (dev->reg_state == NETREG_UNINITIALIZED) |
| 2471 | free_netdev(dev); |
Cong Wang | 0e0eee2 | 2014-02-11 15:51:30 -0800 | [diff] [blame] | 2472 | goto out; |
Cong Wang | e51fb15 | 2014-06-03 16:40:47 -0700 | [diff] [blame] | 2473 | } |
Cong Wang | 0e0eee2 | 2014-02-11 15:51:30 -0800 | [diff] [blame] | 2474 | } else { |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 2475 | err = register_netdevice(dev); |
Cong Wang | 0e0eee2 | 2014-02-11 15:51:30 -0800 | [diff] [blame] | 2476 | if (err < 0) { |
| 2477 | free_netdev(dev); |
| 2478 | goto out; |
| 2479 | } |
Dan Carpenter | fce9b9b | 2013-08-14 12:35:42 +0300 | [diff] [blame] | 2480 | } |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 2481 | err = rtnl_configure_link(dev, ifm); |
David S. Miller | 4363890 | 2015-03-10 21:58:32 -0400 | [diff] [blame] | 2482 | if (err < 0) |
| 2483 | goto out_unregister; |
Nicolas Dichtel | bdef279 | 2015-01-20 15:15:42 +0100 | [diff] [blame] | 2484 | if (link_net) { |
Nicolas Dichtel | 317f481 | 2015-01-15 15:11:18 +0100 | [diff] [blame] | 2485 | err = dev_change_net_namespace(dev, dest_net, ifname); |
Nicolas Dichtel | bdef279 | 2015-01-20 15:15:42 +0100 | [diff] [blame] | 2486 | if (err < 0) |
David S. Miller | 4363890 | 2015-03-10 21:58:32 -0400 | [diff] [blame] | 2487 | goto out_unregister; |
Nicolas Dichtel | bdef279 | 2015-01-20 15:15:42 +0100 | [diff] [blame] | 2488 | } |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 2489 | out: |
Nicolas Dichtel | 317f481 | 2015-01-15 15:11:18 +0100 | [diff] [blame] | 2490 | if (link_net) |
| 2491 | put_net(link_net); |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 2492 | put_net(dest_net); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2493 | return err; |
David S. Miller | 4363890 | 2015-03-10 21:58:32 -0400 | [diff] [blame] | 2494 | out_unregister: |
| 2495 | if (ops->newlink) { |
| 2496 | LIST_HEAD(list_kill); |
| 2497 | |
| 2498 | ops->dellink(dev, &list_kill); |
| 2499 | unregister_netdevice_many(&list_kill); |
| 2500 | } else { |
| 2501 | unregister_netdevice(dev); |
| 2502 | } |
| 2503 | goto out; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2504 | } |
| 2505 | } |
| 2506 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 2507 | static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh) |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2508 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2509 | struct net *net = sock_net(skb->sk); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2510 | struct ifinfomsg *ifm; |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2511 | char ifname[IFNAMSIZ]; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2512 | struct nlattr *tb[IFLA_MAX+1]; |
| 2513 | struct net_device *dev = NULL; |
| 2514 | struct sk_buff *nskb; |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 2515 | int err; |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2516 | u32 ext_filter_mask = 0; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2517 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2518 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 2519 | if (err < 0) |
Eric Sesterhenn | 9918f23 | 2006-09-26 23:26:38 -0700 | [diff] [blame] | 2520 | return err; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2521 | |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2522 | if (tb[IFLA_IFNAME]) |
| 2523 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
| 2524 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2525 | if (tb[IFLA_EXT_MASK]) |
| 2526 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); |
| 2527 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2528 | ifm = nlmsg_data(nlh); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2529 | if (ifm->ifi_index > 0) |
| 2530 | dev = __dev_get_by_index(net, ifm->ifi_index); |
| 2531 | else if (tb[IFLA_IFNAME]) |
| 2532 | dev = __dev_get_by_name(net, ifname); |
| 2533 | else |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2534 | return -EINVAL; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2535 | |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2536 | if (dev == NULL) |
| 2537 | return -ENODEV; |
| 2538 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2539 | nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2540 | if (nskb == NULL) |
| 2541 | return -ENOBUFS; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2542 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2543 | err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid, |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2544 | nlh->nlmsg_seq, 0, 0, ext_filter_mask); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 2545 | if (err < 0) { |
| 2546 | /* -EMSGSIZE implies BUG in if_nlmsg_size */ |
| 2547 | WARN_ON(err == -EMSGSIZE); |
| 2548 | kfree_skb(nskb); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2549 | } else |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2550 | err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2551 | |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2552 | return err; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2553 | } |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2554 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2555 | static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2556 | { |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2557 | struct net *net = sock_net(skb->sk); |
| 2558 | struct net_device *dev; |
| 2559 | struct nlattr *tb[IFLA_MAX+1]; |
| 2560 | u32 ext_filter_mask = 0; |
| 2561 | u16 min_ifinfo_dump_size = 0; |
Michal Schmidt | e5eca6d | 2014-05-28 14:15:19 +0200 | [diff] [blame] | 2562 | int hdrlen; |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2563 | |
Michal Schmidt | e5eca6d | 2014-05-28 14:15:19 +0200 | [diff] [blame] | 2564 | /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ |
| 2565 | hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? |
| 2566 | sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); |
| 2567 | |
| 2568 | if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) { |
Eric Dumazet | a4b64fb | 2012-03-04 12:32:10 +0000 | [diff] [blame] | 2569 | if (tb[IFLA_EXT_MASK]) |
| 2570 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); |
| 2571 | } |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2572 | |
| 2573 | if (!ext_filter_mask) |
| 2574 | return NLMSG_GOODSIZE; |
| 2575 | /* |
| 2576 | * traverse the list of net devices and compute the minimum |
| 2577 | * buffer size based upon the filter mask. |
| 2578 | */ |
| 2579 | list_for_each_entry(dev, &net->dev_base_head, dev_list) { |
| 2580 | min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size, |
| 2581 | if_nlmsg_size(dev, |
| 2582 | ext_filter_mask)); |
| 2583 | } |
| 2584 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2585 | return min_ifinfo_dump_size; |
| 2586 | } |
| 2587 | |
Adrian Bunk | 42bad1d | 2007-04-26 00:57:41 -0700 | [diff] [blame] | 2588 | 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] | 2589 | { |
| 2590 | int idx; |
| 2591 | int s_idx = cb->family; |
| 2592 | |
| 2593 | if (s_idx == 0) |
| 2594 | s_idx = 1; |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 2595 | for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2596 | int type = cb->nlh->nlmsg_type-RTM_BASE; |
| 2597 | if (idx < s_idx || idx == PF_PACKET) |
| 2598 | continue; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 2599 | if (rtnl_msg_handlers[idx] == NULL || |
| 2600 | rtnl_msg_handlers[idx][type].dumpit == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2601 | continue; |
Nicolas Dichtel | 0465277 | 2013-03-22 06:28:42 +0000 | [diff] [blame] | 2602 | if (idx > s_idx) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2603 | memset(&cb->args[0], 0, sizeof(cb->args)); |
Nicolas Dichtel | 0465277 | 2013-03-22 06:28:42 +0000 | [diff] [blame] | 2604 | cb->prev_seq = 0; |
| 2605 | cb->seq = 0; |
| 2606 | } |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 2607 | if (rtnl_msg_handlers[idx][type].dumpit(skb, cb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2608 | break; |
| 2609 | } |
| 2610 | cb->family = idx; |
| 2611 | |
| 2612 | return skb->len; |
| 2613 | } |
| 2614 | |
Mahesh Bandewar | 395eea6 | 2014-12-03 13:46:24 -0800 | [diff] [blame] | 2615 | struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, |
| 2616 | unsigned int change, gfp_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2617 | { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2618 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2619 | struct sk_buff *skb; |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 2620 | int err = -ENOBUFS; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2621 | size_t if_info_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2622 | |
Alexei Starovoitov | 7f29405 | 2013-10-23 16:02:42 -0700 | [diff] [blame] | 2623 | skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags); |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 2624 | if (skb == NULL) |
| 2625 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2626 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2627 | err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 2628 | if (err < 0) { |
| 2629 | /* -EMSGSIZE implies BUG in if_nlmsg_size() */ |
| 2630 | WARN_ON(err == -EMSGSIZE); |
| 2631 | kfree_skb(skb); |
| 2632 | goto errout; |
| 2633 | } |
Mahesh Bandewar | 395eea6 | 2014-12-03 13:46:24 -0800 | [diff] [blame] | 2634 | return skb; |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 2635 | errout: |
| 2636 | if (err < 0) |
Eric W. Biederman | 4b3da70 | 2007-11-19 22:27:40 -0800 | [diff] [blame] | 2637 | rtnl_set_sk_err(net, RTNLGRP_LINK, err); |
Mahesh Bandewar | 395eea6 | 2014-12-03 13:46:24 -0800 | [diff] [blame] | 2638 | return NULL; |
| 2639 | } |
| 2640 | |
| 2641 | void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags) |
| 2642 | { |
| 2643 | struct net *net = dev_net(dev); |
| 2644 | |
| 2645 | rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); |
| 2646 | } |
| 2647 | |
| 2648 | void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, |
| 2649 | gfp_t flags) |
| 2650 | { |
| 2651 | struct sk_buff *skb; |
| 2652 | |
Nicolas Dichtel | ed2a80a | 2015-05-13 14:19:42 +0200 | [diff] [blame] | 2653 | if (dev->reg_state != NETREG_REGISTERED) |
| 2654 | return; |
| 2655 | |
Mahesh Bandewar | 395eea6 | 2014-12-03 13:46:24 -0800 | [diff] [blame] | 2656 | skb = rtmsg_ifinfo_build_skb(type, dev, change, flags); |
| 2657 | if (skb) |
| 2658 | rtmsg_ifinfo_send(skb, dev, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2659 | } |
Jiri Pirko | 471cb5a | 2013-01-03 22:49:01 +0000 | [diff] [blame] | 2660 | EXPORT_SYMBOL(rtmsg_ifinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2661 | |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2662 | static int nlmsg_populate_fdb_fill(struct sk_buff *skb, |
| 2663 | struct net_device *dev, |
Hubert Sokolowski | 1e53d5b | 2015-04-09 12:16:17 +0000 | [diff] [blame] | 2664 | u8 *addr, u16 vid, u32 pid, u32 seq, |
Nicolas Dichtel | 1c104a6 | 2014-03-19 17:47:49 +0100 | [diff] [blame] | 2665 | int type, unsigned int flags, |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 2666 | int nlflags, u16 ndm_state) |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2667 | { |
| 2668 | struct nlmsghdr *nlh; |
| 2669 | struct ndmsg *ndm; |
| 2670 | |
Nicolas Dichtel | 1c104a6 | 2014-03-19 17:47:49 +0100 | [diff] [blame] | 2671 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2672 | if (!nlh) |
| 2673 | return -EMSGSIZE; |
| 2674 | |
| 2675 | ndm = nlmsg_data(nlh); |
| 2676 | ndm->ndm_family = AF_BRIDGE; |
| 2677 | ndm->ndm_pad1 = 0; |
| 2678 | ndm->ndm_pad2 = 0; |
| 2679 | ndm->ndm_flags = flags; |
| 2680 | ndm->ndm_type = 0; |
| 2681 | ndm->ndm_ifindex = dev->ifindex; |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 2682 | ndm->ndm_state = ndm_state; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2683 | |
| 2684 | if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) |
| 2685 | goto nla_put_failure; |
Hubert Sokolowski | 1e53d5b | 2015-04-09 12:16:17 +0000 | [diff] [blame] | 2686 | if (vid) |
| 2687 | if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) |
| 2688 | goto nla_put_failure; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2689 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 2690 | nlmsg_end(skb, nlh); |
| 2691 | return 0; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2692 | |
| 2693 | nla_put_failure: |
| 2694 | nlmsg_cancel(skb, nlh); |
| 2695 | return -EMSGSIZE; |
| 2696 | } |
| 2697 | |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2698 | static inline size_t rtnl_fdb_nlmsg_size(void) |
| 2699 | { |
| 2700 | return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN); |
| 2701 | } |
| 2702 | |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 2703 | static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, |
| 2704 | u16 ndm_state) |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2705 | { |
| 2706 | struct net *net = dev_net(dev); |
| 2707 | struct sk_buff *skb; |
| 2708 | int err = -ENOBUFS; |
| 2709 | |
| 2710 | skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); |
| 2711 | if (!skb) |
| 2712 | goto errout; |
| 2713 | |
Hubert Sokolowski | 1e53d5b | 2015-04-09 12:16:17 +0000 | [diff] [blame] | 2714 | err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 2715 | 0, 0, type, NTF_SELF, 0, ndm_state); |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2716 | if (err < 0) { |
| 2717 | kfree_skb(skb); |
| 2718 | goto errout; |
| 2719 | } |
| 2720 | |
| 2721 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); |
| 2722 | return; |
| 2723 | errout: |
| 2724 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); |
| 2725 | } |
| 2726 | |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2727 | /** |
| 2728 | * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry |
| 2729 | */ |
| 2730 | int ndo_dflt_fdb_add(struct ndmsg *ndm, |
| 2731 | struct nlattr *tb[], |
| 2732 | struct net_device *dev, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2733 | const unsigned char *addr, u16 vid, |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2734 | u16 flags) |
| 2735 | { |
| 2736 | int err = -EINVAL; |
| 2737 | |
| 2738 | /* If aging addresses are supported device will need to |
| 2739 | * implement its own handler for this. |
| 2740 | */ |
| 2741 | if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { |
| 2742 | pr_info("%s: FDB only supports static addresses\n", dev->name); |
| 2743 | return err; |
| 2744 | } |
| 2745 | |
Or Gerlitz | 65891fe | 2014-12-14 18:19:05 +0200 | [diff] [blame] | 2746 | if (vid) { |
| 2747 | pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name); |
| 2748 | return err; |
| 2749 | } |
| 2750 | |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2751 | if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) |
| 2752 | err = dev_uc_add_excl(dev, addr); |
| 2753 | else if (is_multicast_ether_addr(addr)) |
| 2754 | err = dev_mc_add_excl(dev, addr); |
| 2755 | |
| 2756 | /* Only return duplicate errors if NLM_F_EXCL is set */ |
| 2757 | if (err == -EEXIST && !(flags & NLM_F_EXCL)) |
| 2758 | err = 0; |
| 2759 | |
| 2760 | return err; |
| 2761 | } |
| 2762 | EXPORT_SYMBOL(ndo_dflt_fdb_add); |
| 2763 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2764 | static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid) |
| 2765 | { |
| 2766 | u16 vid = 0; |
| 2767 | |
| 2768 | if (vlan_attr) { |
| 2769 | if (nla_len(vlan_attr) != sizeof(u16)) { |
| 2770 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n"); |
| 2771 | return -EINVAL; |
| 2772 | } |
| 2773 | |
| 2774 | vid = nla_get_u16(vlan_attr); |
| 2775 | |
| 2776 | if (!vid || vid >= VLAN_VID_MASK) { |
| 2777 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n", |
| 2778 | vid); |
| 2779 | return -EINVAL; |
| 2780 | } |
| 2781 | } |
| 2782 | *p_vid = vid; |
| 2783 | return 0; |
| 2784 | } |
| 2785 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 2786 | static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh) |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2787 | { |
| 2788 | struct net *net = sock_net(skb->sk); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2789 | struct ndmsg *ndm; |
| 2790 | struct nlattr *tb[NDA_MAX+1]; |
| 2791 | struct net_device *dev; |
| 2792 | u8 *addr; |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2793 | u16 vid; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2794 | int err; |
| 2795 | |
| 2796 | err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL); |
| 2797 | if (err < 0) |
| 2798 | return err; |
| 2799 | |
| 2800 | ndm = nlmsg_data(nlh); |
| 2801 | if (ndm->ndm_ifindex == 0) { |
| 2802 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n"); |
| 2803 | return -EINVAL; |
| 2804 | } |
| 2805 | |
| 2806 | dev = __dev_get_by_index(net, ndm->ndm_ifindex); |
| 2807 | if (dev == NULL) { |
| 2808 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n"); |
| 2809 | return -ENODEV; |
| 2810 | } |
| 2811 | |
| 2812 | if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { |
| 2813 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n"); |
| 2814 | return -EINVAL; |
| 2815 | } |
| 2816 | |
| 2817 | addr = nla_data(tb[NDA_LLADDR]); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2818 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2819 | err = fdb_vid_parse(tb[NDA_VLAN], &vid); |
| 2820 | if (err) |
| 2821 | return err; |
| 2822 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2823 | err = -EOPNOTSUPP; |
| 2824 | |
| 2825 | /* Support fdb on master device the net/bridge default case */ |
| 2826 | if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && |
| 2827 | (dev->priv_flags & IFF_BRIDGE_PORT)) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2828 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
| 2829 | const struct net_device_ops *ops = br_dev->netdev_ops; |
| 2830 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2831 | err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, |
| 2832 | nlh->nlmsg_flags); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2833 | if (err) |
| 2834 | goto out; |
| 2835 | else |
| 2836 | ndm->ndm_flags &= ~NTF_MASTER; |
| 2837 | } |
| 2838 | |
| 2839 | /* Embedded bridge, macvlan, and any other device support */ |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2840 | if ((ndm->ndm_flags & NTF_SELF)) { |
| 2841 | if (dev->netdev_ops->ndo_fdb_add) |
| 2842 | err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2843 | vid, |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2844 | nlh->nlmsg_flags); |
| 2845 | else |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2846 | err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2847 | nlh->nlmsg_flags); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2848 | |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2849 | if (!err) { |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 2850 | rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, |
| 2851 | ndm->ndm_state); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2852 | ndm->ndm_flags &= ~NTF_SELF; |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2853 | } |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2854 | } |
| 2855 | out: |
| 2856 | return err; |
| 2857 | } |
| 2858 | |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2859 | /** |
| 2860 | * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry |
| 2861 | */ |
| 2862 | int ndo_dflt_fdb_del(struct ndmsg *ndm, |
| 2863 | struct nlattr *tb[], |
| 2864 | struct net_device *dev, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2865 | const unsigned char *addr, u16 vid) |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2866 | { |
Alexander Duyck | c8a89c4 | 2014-07-15 15:15:20 -0700 | [diff] [blame] | 2867 | int err = -EINVAL; |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2868 | |
| 2869 | /* If aging addresses are supported device will need to |
| 2870 | * implement its own handler for this. |
| 2871 | */ |
Sridhar Samudrala | 6453599 | 2013-08-08 15:19:48 -0700 | [diff] [blame] | 2872 | if (!(ndm->ndm_state & NUD_PERMANENT)) { |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2873 | pr_info("%s: FDB only supports static addresses\n", dev->name); |
Alexander Duyck | c8a89c4 | 2014-07-15 15:15:20 -0700 | [diff] [blame] | 2874 | return err; |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2875 | } |
| 2876 | |
| 2877 | if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) |
| 2878 | err = dev_uc_del(dev, addr); |
| 2879 | else if (is_multicast_ether_addr(addr)) |
| 2880 | err = dev_mc_del(dev, addr); |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2881 | |
| 2882 | return err; |
| 2883 | } |
| 2884 | EXPORT_SYMBOL(ndo_dflt_fdb_del); |
| 2885 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 2886 | static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh) |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2887 | { |
| 2888 | struct net *net = sock_net(skb->sk); |
| 2889 | struct ndmsg *ndm; |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 2890 | struct nlattr *tb[NDA_MAX+1]; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2891 | struct net_device *dev; |
| 2892 | int err = -EINVAL; |
| 2893 | __u8 *addr; |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2894 | u16 vid; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2895 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 2896 | if (!netlink_capable(skb, CAP_NET_ADMIN)) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 2897 | return -EPERM; |
| 2898 | |
| 2899 | err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL); |
| 2900 | if (err < 0) |
| 2901 | return err; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2902 | |
| 2903 | ndm = nlmsg_data(nlh); |
| 2904 | if (ndm->ndm_ifindex == 0) { |
| 2905 | pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n"); |
| 2906 | return -EINVAL; |
| 2907 | } |
| 2908 | |
| 2909 | dev = __dev_get_by_index(net, ndm->ndm_ifindex); |
| 2910 | if (dev == NULL) { |
| 2911 | pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n"); |
| 2912 | return -ENODEV; |
| 2913 | } |
| 2914 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 2915 | if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { |
| 2916 | pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n"); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2917 | return -EINVAL; |
| 2918 | } |
| 2919 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 2920 | addr = nla_data(tb[NDA_LLADDR]); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 2921 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2922 | err = fdb_vid_parse(tb[NDA_VLAN], &vid); |
| 2923 | if (err) |
| 2924 | return err; |
| 2925 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2926 | err = -EOPNOTSUPP; |
| 2927 | |
| 2928 | /* Support fdb on master device the net/bridge default case */ |
| 2929 | if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && |
| 2930 | (dev->priv_flags & IFF_BRIDGE_PORT)) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2931 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
| 2932 | const struct net_device_ops *ops = br_dev->netdev_ops; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2933 | |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2934 | if (ops->ndo_fdb_del) |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2935 | err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2936 | |
| 2937 | if (err) |
| 2938 | goto out; |
| 2939 | else |
| 2940 | ndm->ndm_flags &= ~NTF_MASTER; |
| 2941 | } |
| 2942 | |
| 2943 | /* Embedded bridge, macvlan, and any other device support */ |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2944 | if (ndm->ndm_flags & NTF_SELF) { |
| 2945 | if (dev->netdev_ops->ndo_fdb_del) |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2946 | err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, |
| 2947 | vid); |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2948 | else |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2949 | err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2950 | |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2951 | if (!err) { |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 2952 | rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, |
| 2953 | ndm->ndm_state); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2954 | ndm->ndm_flags &= ~NTF_SELF; |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2955 | } |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2956 | } |
| 2957 | out: |
| 2958 | return err; |
| 2959 | } |
| 2960 | |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2961 | static int nlmsg_populate_fdb(struct sk_buff *skb, |
| 2962 | struct netlink_callback *cb, |
| 2963 | struct net_device *dev, |
| 2964 | int *idx, |
| 2965 | struct netdev_hw_addr_list *list) |
| 2966 | { |
| 2967 | struct netdev_hw_addr *ha; |
| 2968 | int err; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2969 | u32 portid, seq; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2970 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2971 | portid = NETLINK_CB(cb->skb).portid; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2972 | seq = cb->nlh->nlmsg_seq; |
| 2973 | |
| 2974 | list_for_each_entry(ha, &list->list, list) { |
| 2975 | if (*idx < cb->args[0]) |
| 2976 | goto skip; |
| 2977 | |
Hubert Sokolowski | 1e53d5b | 2015-04-09 12:16:17 +0000 | [diff] [blame] | 2978 | err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, |
John Fastabend | a7a558f | 2012-11-01 16:23:10 +0000 | [diff] [blame] | 2979 | portid, seq, |
Nicolas Dichtel | 1c104a6 | 2014-03-19 17:47:49 +0100 | [diff] [blame] | 2980 | RTM_NEWNEIGH, NTF_SELF, |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 2981 | NLM_F_MULTI, NUD_PERMANENT); |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2982 | if (err < 0) |
| 2983 | return err; |
| 2984 | skip: |
| 2985 | *idx += 1; |
| 2986 | } |
| 2987 | return 0; |
| 2988 | } |
| 2989 | |
| 2990 | /** |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 2991 | * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2992 | * @nlh: netlink message header |
| 2993 | * @dev: netdevice |
| 2994 | * |
| 2995 | * Default netdevice operation to dump the existing unicast address list. |
John Fastabend | 91f3e7b | 2013-03-29 08:18:37 +0000 | [diff] [blame] | 2996 | * Returns number of addresses from list put in skb. |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2997 | */ |
| 2998 | int ndo_dflt_fdb_dump(struct sk_buff *skb, |
| 2999 | struct netlink_callback *cb, |
| 3000 | struct net_device *dev, |
Jamal Hadi Salim | 5d5eacb | 2014-07-10 07:01:58 -0400 | [diff] [blame] | 3001 | struct net_device *filter_dev, |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 3002 | int idx) |
| 3003 | { |
| 3004 | int err; |
| 3005 | |
| 3006 | netif_addr_lock_bh(dev); |
| 3007 | err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc); |
| 3008 | if (err) |
| 3009 | goto out; |
| 3010 | nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc); |
| 3011 | out: |
| 3012 | netif_addr_unlock_bh(dev); |
MINOURA Makoto / 箕浦 真 | 472681d | 2016-02-25 14:20:48 +0900 | [diff] [blame] | 3013 | cb->args[1] = err; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 3014 | return idx; |
| 3015 | } |
| 3016 | EXPORT_SYMBOL(ndo_dflt_fdb_dump); |
| 3017 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3018 | static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 3019 | { |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3020 | struct net_device *dev; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3021 | struct nlattr *tb[IFLA_MAX+1]; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3022 | struct net_device *br_dev = NULL; |
| 3023 | const struct net_device_ops *ops = NULL; |
| 3024 | const struct net_device_ops *cops = NULL; |
| 3025 | struct ifinfomsg *ifm = nlmsg_data(cb->nlh); |
| 3026 | struct net *net = sock_net(skb->sk); |
| 3027 | int brport_idx = 0; |
| 3028 | int br_idx = 0; |
| 3029 | int idx = 0; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3030 | |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3031 | if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX, |
| 3032 | ifla_policy) == 0) { |
| 3033 | if (tb[IFLA_MASTER]) |
| 3034 | br_idx = nla_get_u32(tb[IFLA_MASTER]); |
| 3035 | } |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3036 | |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3037 | brport_idx = ifm->ifi_index; |
| 3038 | |
| 3039 | if (br_idx) { |
| 3040 | br_dev = __dev_get_by_index(net, br_idx); |
| 3041 | if (!br_dev) |
| 3042 | return -ENODEV; |
| 3043 | |
| 3044 | ops = br_dev->netdev_ops; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3045 | } |
| 3046 | |
MINOURA Makoto / 箕浦 真 | 472681d | 2016-02-25 14:20:48 +0900 | [diff] [blame] | 3047 | cb->args[1] = 0; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3048 | for_each_netdev(net, dev) { |
| 3049 | if (brport_idx && (dev->ifindex != brport_idx)) |
| 3050 | continue; |
| 3051 | |
| 3052 | if (!br_idx) { /* user did not specify a specific bridge */ |
| 3053 | if (dev->priv_flags & IFF_BRIDGE_PORT) { |
| 3054 | br_dev = netdev_master_upper_dev_get(dev); |
| 3055 | cops = br_dev->netdev_ops; |
| 3056 | } |
| 3057 | |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3058 | } else { |
| 3059 | if (dev != br_dev && |
| 3060 | !(dev->priv_flags & IFF_BRIDGE_PORT)) |
| 3061 | continue; |
| 3062 | |
| 3063 | if (br_dev != netdev_master_upper_dev_get(dev) && |
| 3064 | !(dev->priv_flags & IFF_EBRIDGE)) |
| 3065 | continue; |
| 3066 | |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3067 | cops = ops; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3070 | if (dev->priv_flags & IFF_BRIDGE_PORT) { |
| 3071 | if (cops && cops->ndo_fdb_dump) |
| 3072 | idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev, |
| 3073 | idx); |
| 3074 | } |
MINOURA Makoto / 箕浦 真 | 472681d | 2016-02-25 14:20:48 +0900 | [diff] [blame] | 3075 | if (cb->args[1] == -EMSGSIZE) |
| 3076 | break; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3077 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3078 | if (dev->netdev_ops->ndo_fdb_dump) |
Hubert Sokolowski | 6cb6974 | 2015-01-05 17:29:21 +0000 | [diff] [blame] | 3079 | idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, NULL, |
Jamal Hadi Salim | 5d5eacb | 2014-07-10 07:01:58 -0400 | [diff] [blame] | 3080 | idx); |
Hubert Sokolowski | 6cb6974 | 2015-01-05 17:29:21 +0000 | [diff] [blame] | 3081 | else |
| 3082 | idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx); |
MINOURA Makoto / 箕浦 真 | 472681d | 2016-02-25 14:20:48 +0900 | [diff] [blame] | 3083 | if (cb->args[1] == -EMSGSIZE) |
| 3084 | break; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3085 | |
| 3086 | cops = NULL; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3087 | } |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3088 | |
| 3089 | cb->args[0] = idx; |
| 3090 | return skb->len; |
| 3091 | } |
| 3092 | |
Scott Feldman | 2c3c031 | 2014-11-28 14:34:25 +0100 | [diff] [blame] | 3093 | static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, |
| 3094 | unsigned int attrnum, unsigned int flag) |
| 3095 | { |
| 3096 | if (mask & flag) |
| 3097 | return nla_put_u8(skb, attrnum, !!(flags & flag)); |
| 3098 | return 0; |
| 3099 | } |
| 3100 | |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3101 | 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] | 3102 | struct net_device *dev, u16 mode, |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 3103 | u32 flags, u32 mask, int nlflags, |
| 3104 | u32 filter_mask, |
| 3105 | int (*vlan_fill)(struct sk_buff *skb, |
| 3106 | struct net_device *dev, |
| 3107 | u32 filter_mask)) |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3108 | { |
| 3109 | struct nlmsghdr *nlh; |
| 3110 | struct ifinfomsg *ifm; |
| 3111 | struct nlattr *br_afspec; |
Scott Feldman | 2c3c031 | 2014-11-28 14:34:25 +0100 | [diff] [blame] | 3112 | struct nlattr *protinfo; |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3113 | u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 3114 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 3115 | int err = 0; |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3116 | |
Nicolas Dichtel | 46c264d | 2015-04-28 18:33:49 +0200 | [diff] [blame] | 3117 | nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3118 | if (nlh == NULL) |
| 3119 | return -EMSGSIZE; |
| 3120 | |
| 3121 | ifm = nlmsg_data(nlh); |
| 3122 | ifm->ifi_family = AF_BRIDGE; |
| 3123 | ifm->__ifi_pad = 0; |
| 3124 | ifm->ifi_type = dev->type; |
| 3125 | ifm->ifi_index = dev->ifindex; |
| 3126 | ifm->ifi_flags = dev_get_flags(dev); |
| 3127 | ifm->ifi_change = 0; |
| 3128 | |
| 3129 | |
| 3130 | if (nla_put_string(skb, IFLA_IFNAME, dev->name) || |
| 3131 | nla_put_u32(skb, IFLA_MTU, dev->mtu) || |
| 3132 | nla_put_u8(skb, IFLA_OPERSTATE, operstate) || |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 3133 | (br_dev && |
| 3134 | nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3135 | (dev->addr_len && |
| 3136 | nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || |
Nicolas Dichtel | a54acb3 | 2015-04-02 17:07:00 +0200 | [diff] [blame] | 3137 | (dev->ifindex != dev_get_iflink(dev) && |
| 3138 | nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3139 | goto nla_put_failure; |
| 3140 | |
| 3141 | br_afspec = nla_nest_start(skb, IFLA_AF_SPEC); |
| 3142 | if (!br_afspec) |
| 3143 | goto nla_put_failure; |
| 3144 | |
Roopa Prabhu | 1d460b9 | 2014-12-08 14:04:20 -0800 | [diff] [blame] | 3145 | if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3146 | nla_nest_cancel(skb, br_afspec); |
| 3147 | goto nla_put_failure; |
| 3148 | } |
Roopa Prabhu | 1d460b9 | 2014-12-08 14:04:20 -0800 | [diff] [blame] | 3149 | |
| 3150 | if (mode != BRIDGE_MODE_UNDEF) { |
| 3151 | if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { |
| 3152 | nla_nest_cancel(skb, br_afspec); |
| 3153 | goto nla_put_failure; |
| 3154 | } |
| 3155 | } |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 3156 | if (vlan_fill) { |
| 3157 | err = vlan_fill(skb, dev, filter_mask); |
| 3158 | if (err) { |
| 3159 | nla_nest_cancel(skb, br_afspec); |
| 3160 | goto nla_put_failure; |
| 3161 | } |
| 3162 | } |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3163 | nla_nest_end(skb, br_afspec); |
| 3164 | |
Scott Feldman | 2c3c031 | 2014-11-28 14:34:25 +0100 | [diff] [blame] | 3165 | protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED); |
| 3166 | if (!protinfo) |
| 3167 | goto nla_put_failure; |
| 3168 | |
| 3169 | if (brport_nla_put_flag(skb, flags, mask, |
| 3170 | IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || |
| 3171 | brport_nla_put_flag(skb, flags, mask, |
| 3172 | IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || |
| 3173 | brport_nla_put_flag(skb, flags, mask, |
| 3174 | IFLA_BRPORT_FAST_LEAVE, |
| 3175 | BR_MULTICAST_FAST_LEAVE) || |
| 3176 | brport_nla_put_flag(skb, flags, mask, |
| 3177 | IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || |
| 3178 | brport_nla_put_flag(skb, flags, mask, |
| 3179 | IFLA_BRPORT_LEARNING, BR_LEARNING) || |
| 3180 | brport_nla_put_flag(skb, flags, mask, |
| 3181 | IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || |
| 3182 | brport_nla_put_flag(skb, flags, mask, |
| 3183 | IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || |
| 3184 | brport_nla_put_flag(skb, flags, mask, |
| 3185 | IFLA_BRPORT_PROXYARP, BR_PROXYARP)) { |
| 3186 | nla_nest_cancel(skb, protinfo); |
| 3187 | goto nla_put_failure; |
| 3188 | } |
| 3189 | |
| 3190 | nla_nest_end(skb, protinfo); |
| 3191 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 3192 | nlmsg_end(skb, nlh); |
| 3193 | return 0; |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3194 | nla_put_failure: |
| 3195 | nlmsg_cancel(skb, nlh); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 3196 | return err ? err : -EMSGSIZE; |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3197 | } |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 3198 | EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3199 | |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3200 | static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) |
| 3201 | { |
| 3202 | struct net *net = sock_net(skb->sk); |
| 3203 | struct net_device *dev; |
| 3204 | int idx = 0; |
| 3205 | u32 portid = NETLINK_CB(cb->skb).portid; |
| 3206 | u32 seq = cb->nlh->nlmsg_seq; |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 3207 | u32 filter_mask = 0; |
Roopa Prabhu | d64f69b | 2015-09-15 14:44:29 -0700 | [diff] [blame] | 3208 | int err; |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 3209 | |
Thomas Graf | aa68c20 | 2014-11-26 13:42:20 +0100 | [diff] [blame] | 3210 | if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) { |
| 3211 | struct nlattr *extfilt; |
| 3212 | |
| 3213 | extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg), |
| 3214 | IFLA_EXT_MASK); |
| 3215 | if (extfilt) { |
| 3216 | if (nla_len(extfilt) < sizeof(filter_mask)) |
| 3217 | return -EINVAL; |
| 3218 | |
| 3219 | filter_mask = nla_get_u32(extfilt); |
| 3220 | } |
| 3221 | } |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3222 | |
| 3223 | rcu_read_lock(); |
| 3224 | for_each_netdev_rcu(net, dev) { |
| 3225 | const struct net_device_ops *ops = dev->netdev_ops; |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 3226 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3227 | |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 3228 | if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { |
Roopa Prabhu | d64f69b | 2015-09-15 14:44:29 -0700 | [diff] [blame] | 3229 | if (idx >= cb->args[0]) { |
| 3230 | err = br_dev->netdev_ops->ndo_bridge_getlink( |
| 3231 | skb, portid, seq, dev, |
| 3232 | filter_mask, NLM_F_MULTI); |
| 3233 | if (err < 0 && err != -EOPNOTSUPP) |
| 3234 | break; |
| 3235 | } |
Ben Hutchings | 25b1e67 | 2012-11-02 12:56:52 +0000 | [diff] [blame] | 3236 | idx++; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3237 | } |
| 3238 | |
| 3239 | if (ops->ndo_bridge_getlink) { |
Roopa Prabhu | d64f69b | 2015-09-15 14:44:29 -0700 | [diff] [blame] | 3240 | if (idx >= cb->args[0]) { |
| 3241 | err = ops->ndo_bridge_getlink(skb, portid, |
| 3242 | seq, dev, |
| 3243 | filter_mask, |
| 3244 | NLM_F_MULTI); |
| 3245 | if (err < 0 && err != -EOPNOTSUPP) |
| 3246 | break; |
| 3247 | } |
Ben Hutchings | 25b1e67 | 2012-11-02 12:56:52 +0000 | [diff] [blame] | 3248 | idx++; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3249 | } |
| 3250 | } |
| 3251 | rcu_read_unlock(); |
| 3252 | cb->args[0] = idx; |
| 3253 | |
| 3254 | return skb->len; |
| 3255 | } |
| 3256 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3257 | static inline size_t bridge_nlmsg_size(void) |
| 3258 | { |
| 3259 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) |
| 3260 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ |
| 3261 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ |
| 3262 | + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ |
| 3263 | + nla_total_size(sizeof(u32)) /* IFLA_MTU */ |
| 3264 | + nla_total_size(sizeof(u32)) /* IFLA_LINK */ |
| 3265 | + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ |
| 3266 | + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ |
| 3267 | + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ |
| 3268 | + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ |
| 3269 | + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ |
| 3270 | } |
| 3271 | |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 3272 | static int rtnl_bridge_notify(struct net_device *dev) |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3273 | { |
| 3274 | struct net *net = dev_net(dev); |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3275 | struct sk_buff *skb; |
| 3276 | int err = -EOPNOTSUPP; |
| 3277 | |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 3278 | if (!dev->netdev_ops->ndo_bridge_getlink) |
| 3279 | return 0; |
| 3280 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3281 | skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); |
| 3282 | if (!skb) { |
| 3283 | err = -ENOMEM; |
| 3284 | goto errout; |
| 3285 | } |
| 3286 | |
Nicolas Dichtel | 46c264d | 2015-04-28 18:33:49 +0200 | [diff] [blame] | 3287 | 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] | 3288 | if (err < 0) |
| 3289 | goto errout; |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3290 | |
Roopa Prabhu | 59ccaaa | 2015-01-28 16:23:11 -0800 | [diff] [blame] | 3291 | if (!skb->len) |
| 3292 | goto errout; |
| 3293 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3294 | rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); |
| 3295 | return 0; |
| 3296 | errout: |
| 3297 | WARN_ON(err == -EMSGSIZE); |
| 3298 | kfree_skb(skb); |
Roopa Prabhu | 59ccaaa | 2015-01-28 16:23:11 -0800 | [diff] [blame] | 3299 | if (err) |
| 3300 | rtnl_set_sk_err(net, RTNLGRP_LINK, err); |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3301 | return err; |
| 3302 | } |
| 3303 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 3304 | static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh) |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3305 | { |
| 3306 | struct net *net = sock_net(skb->sk); |
| 3307 | struct ifinfomsg *ifm; |
| 3308 | struct net_device *dev; |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3309 | struct nlattr *br_spec, *attr = NULL; |
| 3310 | int rem, err = -EOPNOTSUPP; |
Rosen, Rami | 4de8b41 | 2015-01-19 11:45:04 +0200 | [diff] [blame] | 3311 | u16 flags = 0; |
John Fastabend | c38e01b | 2012-11-02 16:32:36 +0000 | [diff] [blame] | 3312 | bool have_flags = false; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3313 | |
| 3314 | if (nlmsg_len(nlh) < sizeof(*ifm)) |
| 3315 | return -EINVAL; |
| 3316 | |
| 3317 | ifm = nlmsg_data(nlh); |
| 3318 | if (ifm->ifi_family != AF_BRIDGE) |
| 3319 | return -EPFNOSUPPORT; |
| 3320 | |
| 3321 | dev = __dev_get_by_index(net, ifm->ifi_index); |
| 3322 | if (!dev) { |
| 3323 | pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n"); |
| 3324 | return -ENODEV; |
| 3325 | } |
| 3326 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3327 | br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); |
| 3328 | if (br_spec) { |
| 3329 | nla_for_each_nested(attr, br_spec, rem) { |
| 3330 | if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { |
Thomas Graf | 6e8d1c5 | 2014-11-26 13:42:16 +0100 | [diff] [blame] | 3331 | if (nla_len(attr) < sizeof(flags)) |
| 3332 | return -EINVAL; |
| 3333 | |
John Fastabend | c38e01b | 2012-11-02 16:32:36 +0000 | [diff] [blame] | 3334 | have_flags = true; |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3335 | flags = nla_get_u16(attr); |
| 3336 | break; |
| 3337 | } |
| 3338 | } |
| 3339 | } |
| 3340 | |
| 3341 | if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 3342 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
| 3343 | |
| 3344 | if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3345 | err = -EOPNOTSUPP; |
| 3346 | goto out; |
| 3347 | } |
| 3348 | |
Roopa Prabhu | add511b | 2015-01-29 22:40:12 -0800 | [diff] [blame] | 3349 | err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3350 | if (err) |
| 3351 | goto out; |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3352 | |
| 3353 | flags &= ~BRIDGE_FLAGS_MASTER; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3354 | } |
| 3355 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3356 | if ((flags & BRIDGE_FLAGS_SELF)) { |
| 3357 | if (!dev->netdev_ops->ndo_bridge_setlink) |
| 3358 | err = -EOPNOTSUPP; |
| 3359 | else |
Roopa Prabhu | add511b | 2015-01-29 22:40:12 -0800 | [diff] [blame] | 3360 | err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, |
| 3361 | flags); |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 3362 | if (!err) { |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3363 | flags &= ~BRIDGE_FLAGS_SELF; |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 3364 | |
| 3365 | /* Generate event to notify upper layer of bridge |
| 3366 | * change |
| 3367 | */ |
| 3368 | err = rtnl_bridge_notify(dev); |
| 3369 | } |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3370 | } |
| 3371 | |
John Fastabend | c38e01b | 2012-11-02 16:32:36 +0000 | [diff] [blame] | 3372 | if (have_flags) |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3373 | memcpy(nla_data(attr), &flags, sizeof(flags)); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3374 | out: |
| 3375 | return err; |
| 3376 | } |
| 3377 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 3378 | static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh) |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3379 | { |
| 3380 | struct net *net = sock_net(skb->sk); |
| 3381 | struct ifinfomsg *ifm; |
| 3382 | struct net_device *dev; |
| 3383 | struct nlattr *br_spec, *attr = NULL; |
| 3384 | int rem, err = -EOPNOTSUPP; |
Rosen, Rami | 4de8b41 | 2015-01-19 11:45:04 +0200 | [diff] [blame] | 3385 | u16 flags = 0; |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3386 | bool have_flags = false; |
| 3387 | |
| 3388 | if (nlmsg_len(nlh) < sizeof(*ifm)) |
| 3389 | return -EINVAL; |
| 3390 | |
| 3391 | ifm = nlmsg_data(nlh); |
| 3392 | if (ifm->ifi_family != AF_BRIDGE) |
| 3393 | return -EPFNOSUPPORT; |
| 3394 | |
| 3395 | dev = __dev_get_by_index(net, ifm->ifi_index); |
| 3396 | if (!dev) { |
| 3397 | pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n"); |
| 3398 | return -ENODEV; |
| 3399 | } |
| 3400 | |
| 3401 | br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); |
| 3402 | if (br_spec) { |
| 3403 | nla_for_each_nested(attr, br_spec, rem) { |
| 3404 | if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { |
Thomas Graf | 6e8d1c5 | 2014-11-26 13:42:16 +0100 | [diff] [blame] | 3405 | if (nla_len(attr) < sizeof(flags)) |
| 3406 | return -EINVAL; |
| 3407 | |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3408 | have_flags = true; |
| 3409 | flags = nla_get_u16(attr); |
| 3410 | break; |
| 3411 | } |
| 3412 | } |
| 3413 | } |
| 3414 | |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3415 | if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { |
| 3416 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
| 3417 | |
| 3418 | if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { |
| 3419 | err = -EOPNOTSUPP; |
| 3420 | goto out; |
| 3421 | } |
| 3422 | |
Roopa Prabhu | add511b | 2015-01-29 22:40:12 -0800 | [diff] [blame] | 3423 | err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3424 | if (err) |
| 3425 | goto out; |
| 3426 | |
| 3427 | flags &= ~BRIDGE_FLAGS_MASTER; |
| 3428 | } |
| 3429 | |
| 3430 | if ((flags & BRIDGE_FLAGS_SELF)) { |
| 3431 | if (!dev->netdev_ops->ndo_bridge_dellink) |
| 3432 | err = -EOPNOTSUPP; |
| 3433 | else |
Roopa Prabhu | add511b | 2015-01-29 22:40:12 -0800 | [diff] [blame] | 3434 | err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, |
| 3435 | flags); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3436 | |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 3437 | if (!err) { |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3438 | flags &= ~BRIDGE_FLAGS_SELF; |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 3439 | |
| 3440 | /* Generate event to notify upper layer of bridge |
| 3441 | * change |
| 3442 | */ |
| 3443 | err = rtnl_bridge_notify(dev); |
| 3444 | } |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3445 | } |
| 3446 | |
| 3447 | if (have_flags) |
| 3448 | memcpy(nla_data(attr), &flags, sizeof(flags)); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3449 | out: |
| 3450 | return err; |
| 3451 | } |
| 3452 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3453 | /* Process one rtnetlink message. */ |
| 3454 | |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 3455 | static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3456 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 3457 | struct net *net = sock_net(skb->sk); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 3458 | rtnl_doit_func doit; |
Alexander Kuleshov | 617cfc7 | 2016-01-10 21:26:57 +0600 | [diff] [blame] | 3459 | int kind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3460 | int family; |
| 3461 | int type; |
Eric Dumazet | 2907c35 | 2011-05-25 07:34:04 +0000 | [diff] [blame] | 3462 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3464 | type = nlh->nlmsg_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3465 | if (type > RTM_MAX) |
Thomas Graf | 038890f | 2007-04-05 14:35:52 -0700 | [diff] [blame] | 3466 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3467 | |
| 3468 | type -= RTM_BASE; |
| 3469 | |
| 3470 | /* All the messages must have at least 1 byte length */ |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 3471 | if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3472 | return 0; |
| 3473 | |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 3474 | family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3475 | kind = type&3; |
| 3476 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 3477 | if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 3478 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3479 | |
David S. Miller | b8f3ab4 | 2011-01-18 12:40:38 -0800 | [diff] [blame] | 3480 | if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3481 | struct sock *rtnl; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 3482 | rtnl_dumpit_func dumpit; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 3483 | rtnl_calcit_func calcit; |
| 3484 | u16 min_dump_alloc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3485 | |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 3486 | dumpit = rtnl_get_dumpit(family, type); |
| 3487 | if (dumpit == NULL) |
Thomas Graf | 038890f | 2007-04-05 14:35:52 -0700 | [diff] [blame] | 3488 | return -EOPNOTSUPP; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 3489 | calcit = rtnl_get_calcit(family, type); |
| 3490 | if (calcit) |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 3491 | min_dump_alloc = calcit(skb, nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3492 | |
Eric Dumazet | 2907c35 | 2011-05-25 07:34:04 +0000 | [diff] [blame] | 3493 | __rtnl_unlock(); |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3494 | rtnl = net->rtnl; |
Pablo Neira Ayuso | 80d326f | 2012-02-24 14:30:15 +0000 | [diff] [blame] | 3495 | { |
| 3496 | struct netlink_dump_control c = { |
| 3497 | .dump = dumpit, |
| 3498 | .min_dump_alloc = min_dump_alloc, |
| 3499 | }; |
| 3500 | err = netlink_dump_start(rtnl, skb, nlh, &c); |
| 3501 | } |
Eric Dumazet | 2907c35 | 2011-05-25 07:34:04 +0000 | [diff] [blame] | 3502 | rtnl_lock(); |
| 3503 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3504 | } |
| 3505 | |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 3506 | doit = rtnl_get_doit(family, type); |
| 3507 | if (doit == NULL) |
Thomas Graf | 038890f | 2007-04-05 14:35:52 -0700 | [diff] [blame] | 3508 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3509 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 3510 | return doit(skb, nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3511 | } |
| 3512 | |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 3513 | static void rtnetlink_rcv(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3514 | { |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 3515 | rtnl_lock(); |
| 3516 | netlink_rcv_skb(skb, &rtnetlink_rcv_msg); |
| 3517 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3518 | } |
| 3519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3520 | static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 3521 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 3522 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 3523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3524 | switch (event) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3525 | case NETDEV_UP: |
| 3526 | case NETDEV_DOWN: |
Patrick McHardy | 10de05a | 2010-02-26 06:34:50 +0000 | [diff] [blame] | 3527 | case NETDEV_PRE_UP: |
Eric W. Biederman | d90a909 | 2009-12-12 22:11:15 +0000 | [diff] [blame] | 3528 | case NETDEV_POST_INIT: |
| 3529 | case NETDEV_REGISTER: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3530 | case NETDEV_CHANGE: |
Patrick McHardy | 755d0e7 | 2010-03-19 04:42:24 +0000 | [diff] [blame] | 3531 | case NETDEV_PRE_TYPE_CHANGE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3532 | case NETDEV_GOING_DOWN: |
Patrick McHardy | a283576 | 2010-02-26 06:34:51 +0000 | [diff] [blame] | 3533 | case NETDEV_UNREGISTER: |
Eric Dumazet | 0115e8e | 2012-08-22 17:19:46 +0000 | [diff] [blame] | 3534 | case NETDEV_UNREGISTER_FINAL: |
Amerigo Wang | ac3d3f8 | 2011-05-19 23:06:32 +0000 | [diff] [blame] | 3535 | case NETDEV_RELEASE: |
| 3536 | case NETDEV_JOIN: |
Moni Shoua | 61bd385 | 2015-02-03 16:48:29 +0200 | [diff] [blame] | 3537 | case NETDEV_BONDING_INFO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3538 | break; |
| 3539 | default: |
Alexei Starovoitov | 7f29405 | 2013-10-23 16:02:42 -0700 | [diff] [blame] | 3540 | rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3541 | break; |
| 3542 | } |
| 3543 | return NOTIFY_DONE; |
| 3544 | } |
| 3545 | |
| 3546 | static struct notifier_block rtnetlink_dev_notifier = { |
| 3547 | .notifier_call = rtnetlink_event, |
| 3548 | }; |
| 3549 | |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3550 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 3551 | static int __net_init rtnetlink_net_init(struct net *net) |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3552 | { |
| 3553 | struct sock *sk; |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 3554 | struct netlink_kernel_cfg cfg = { |
| 3555 | .groups = RTNLGRP_MAX, |
| 3556 | .input = rtnetlink_rcv, |
| 3557 | .cb_mutex = &rtnl_mutex, |
Pablo Neira Ayuso | 9785e10 | 2012-09-08 02:53:53 +0000 | [diff] [blame] | 3558 | .flags = NL_CFG_F_NONROOT_RECV, |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 3559 | }; |
| 3560 | |
Pablo Neira Ayuso | 9f00d97 | 2012-09-08 02:53:54 +0000 | [diff] [blame] | 3561 | sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3562 | if (!sk) |
| 3563 | return -ENOMEM; |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3564 | net->rtnl = sk; |
| 3565 | return 0; |
| 3566 | } |
| 3567 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 3568 | static void __net_exit rtnetlink_net_exit(struct net *net) |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3569 | { |
Denis V. Lunev | 775516b | 2008-01-18 23:55:19 -0800 | [diff] [blame] | 3570 | netlink_kernel_release(net->rtnl); |
| 3571 | net->rtnl = NULL; |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3572 | } |
| 3573 | |
| 3574 | static struct pernet_operations rtnetlink_net_ops = { |
| 3575 | .init = rtnetlink_net_init, |
| 3576 | .exit = rtnetlink_net_exit, |
| 3577 | }; |
| 3578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3579 | void __init rtnetlink_init(void) |
| 3580 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3581 | if (register_pernet_subsys(&rtnetlink_net_ops)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3582 | panic("rtnetlink_init: cannot initialize rtnetlink\n"); |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3584 | register_netdevice_notifier(&rtnetlink_dev_notifier); |
Thomas Graf | 340d17f | 2007-03-22 11:49:22 -0700 | [diff] [blame] | 3585 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 3586 | rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, |
| 3587 | rtnl_dump_ifinfo, rtnl_calcit); |
| 3588 | rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL); |
| 3589 | rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL); |
| 3590 | rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL); |
Thomas Graf | 687ad8c | 2007-03-22 11:59:42 -0700 | [diff] [blame] | 3591 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 3592 | rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL); |
| 3593 | rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3594 | |
| 3595 | rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL); |
| 3596 | rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL); |
| 3597 | rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3598 | |
| 3599 | rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3600 | rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3601 | rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3602 | } |