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 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 811 | /* All VF info */ |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 812 | static inline int rtnl_vfinfo_size(const struct net_device *dev, |
| 813 | u32 ext_filter_mask) |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 814 | { |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 815 | if (dev->dev.parent && dev_is_pci(dev->dev.parent) && |
| 816 | (ext_filter_mask & RTEXT_FILTER_VF)) { |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 817 | int num_vfs = dev_num_vf(dev->dev.parent); |
Scott Feldman | 045de01 | 2010-05-28 03:42:43 -0700 | [diff] [blame] | 818 | size_t size = nla_total_size(sizeof(struct nlattr)); |
| 819 | size += nla_total_size(num_vfs * sizeof(struct nlattr)); |
| 820 | size += num_vfs * |
| 821 | (nla_total_size(sizeof(struct ifla_vf_mac)) + |
| 822 | nla_total_size(sizeof(struct ifla_vf_vlan)) + |
Sucheta Chakraborty | ed61668 | 2014-05-22 09:59:05 -0400 | [diff] [blame] | 823 | nla_total_size(sizeof(struct ifla_vf_spoofchk)) + |
Jiri Benc | 945a367 | 2014-08-08 16:44:32 +0200 | [diff] [blame] | 824 | nla_total_size(sizeof(struct ifla_vf_rate)) + |
Vlad Zolotarov | 01a3d79 | 2015-03-30 21:35:23 +0300 | [diff] [blame] | 825 | nla_total_size(sizeof(struct ifla_vf_link_state)) + |
Eran Ben Elisha | 3b766cd | 2015-06-15 17:59:07 +0300 | [diff] [blame] | 826 | nla_total_size(sizeof(struct ifla_vf_rss_query_en)) + |
| 827 | /* IFLA_VF_STATS_RX_PACKETS */ |
| 828 | nla_total_size(sizeof(__u64)) + |
| 829 | /* IFLA_VF_STATS_TX_PACKETS */ |
| 830 | nla_total_size(sizeof(__u64)) + |
| 831 | /* IFLA_VF_STATS_RX_BYTES */ |
| 832 | nla_total_size(sizeof(__u64)) + |
| 833 | /* IFLA_VF_STATS_TX_BYTES */ |
| 834 | nla_total_size(sizeof(__u64)) + |
| 835 | /* IFLA_VF_STATS_BROADCAST */ |
| 836 | nla_total_size(sizeof(__u64)) + |
| 837 | /* IFLA_VF_STATS_MULTICAST */ |
Hiroshi Shimamoto | dd461d6 | 2015-08-28 06:57:55 +0000 | [diff] [blame] | 838 | nla_total_size(sizeof(__u64)) + |
| 839 | nla_total_size(sizeof(struct ifla_vf_trust))); |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 840 | return size; |
| 841 | } else |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 842 | return 0; |
| 843 | } |
| 844 | |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 845 | static size_t rtnl_port_size(const struct net_device *dev, |
| 846 | u32 ext_filter_mask) |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 847 | { |
| 848 | size_t port_size = nla_total_size(4) /* PORT_VF */ |
| 849 | + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ |
| 850 | + nla_total_size(sizeof(struct ifla_port_vsi)) |
| 851 | /* PORT_VSI_TYPE */ |
| 852 | + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ |
| 853 | + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ |
| 854 | + nla_total_size(1) /* PROT_VDP_REQUEST */ |
| 855 | + nla_total_size(2); /* PORT_VDP_RESPONSE */ |
| 856 | size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); |
| 857 | size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) |
| 858 | + port_size; |
| 859 | size_t port_self_size = nla_total_size(sizeof(struct nlattr)) |
| 860 | + port_size; |
| 861 | |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 862 | if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || |
| 863 | !(ext_filter_mask & RTEXT_FILTER_VF)) |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 864 | return 0; |
| 865 | if (dev_num_vf(dev->dev.parent)) |
| 866 | return port_self_size + vf_ports_size + |
| 867 | vf_port_size * dev_num_vf(dev->dev.parent); |
| 868 | else |
| 869 | return port_self_size; |
| 870 | } |
| 871 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 872 | static noinline size_t if_nlmsg_size(const struct net_device *dev, |
| 873 | u32 ext_filter_mask) |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 874 | { |
| 875 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) |
| 876 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 877 | + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 878 | + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ |
| 879 | + nla_total_size(sizeof(struct rtnl_link_ifmap)) |
| 880 | + nla_total_size(sizeof(struct rtnl_link_stats)) |
David S. Miller | 35c5845 | 2016-04-19 19:49:29 -0400 | [diff] [blame^] | 881 | + nla_total_size_64bit(sizeof(struct rtnl_link_stats64)) |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 882 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ |
| 883 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ |
| 884 | + nla_total_size(4) /* IFLA_TXQLEN */ |
| 885 | + nla_total_size(4) /* IFLA_WEIGHT */ |
| 886 | + nla_total_size(4) /* IFLA_MTU */ |
| 887 | + nla_total_size(4) /* IFLA_LINK */ |
| 888 | + nla_total_size(4) /* IFLA_MASTER */ |
Jiri Pirko | 9a57247 | 2012-12-27 23:49:39 +0000 | [diff] [blame] | 889 | + nla_total_size(1) /* IFLA_CARRIER */ |
Ben Greear | edbc0bb | 2012-03-29 12:51:30 +0000 | [diff] [blame] | 890 | + nla_total_size(4) /* IFLA_PROMISCUITY */ |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 891 | + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */ |
| 892 | + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */ |
Eric Dumazet | c70ce02 | 2016-03-21 09:55:10 -0700 | [diff] [blame] | 893 | + nla_total_size(4) /* IFLA_MAX_GSO_SEGS */ |
| 894 | + nla_total_size(4) /* IFLA_MAX_GSO_SIZE */ |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 895 | + nla_total_size(1) /* IFLA_OPERSTATE */ |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 896 | + nla_total_size(1) /* IFLA_LINKMODE */ |
david decotigny | 2d3b479 | 2014-03-29 09:48:35 -0700 | [diff] [blame] | 897 | + nla_total_size(4) /* IFLA_CARRIER_CHANGES */ |
Nicolas Dichtel | d37512a | 2015-01-15 15:11:16 +0100 | [diff] [blame] | 898 | + nla_total_size(4) /* IFLA_LINK_NETNSID */ |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 899 | + nla_total_size(ext_filter_mask |
| 900 | & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ |
| 901 | + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 902 | + 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] | 903 | + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ |
Arad, Ronen | b1974ed | 2015-10-19 09:23:28 -0700 | [diff] [blame] | 904 | + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */ |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 905 | + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */ |
Anuradha Karuppiah | 88d6378 | 2015-07-14 13:43:20 -0700 | [diff] [blame] | 906 | + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */ |
Nicolas Dichtel | c57c7a9 | 2016-03-31 18:10:31 +0200 | [diff] [blame] | 907 | + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */ |
Anuradha Karuppiah | 88d6378 | 2015-07-14 13:43:20 -0700 | [diff] [blame] | 908 | + nla_total_size(1); /* IFLA_PROTO_DOWN */ |
| 909 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 910 | } |
| 911 | |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 912 | static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) |
| 913 | { |
| 914 | struct nlattr *vf_ports; |
| 915 | struct nlattr *vf_port; |
| 916 | int vf; |
| 917 | int err; |
| 918 | |
| 919 | vf_ports = nla_nest_start(skb, IFLA_VF_PORTS); |
| 920 | if (!vf_ports) |
| 921 | return -EMSGSIZE; |
| 922 | |
| 923 | for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { |
| 924 | vf_port = nla_nest_start(skb, IFLA_VF_PORT); |
Scott Feldman | 8ca9418 | 2010-05-28 03:42:18 -0700 | [diff] [blame] | 925 | if (!vf_port) |
| 926 | goto nla_put_failure; |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 927 | if (nla_put_u32(skb, IFLA_PORT_VF, vf)) |
| 928 | goto nla_put_failure; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 929 | err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); |
Scott Feldman | 8ca9418 | 2010-05-28 03:42:18 -0700 | [diff] [blame] | 930 | if (err == -EMSGSIZE) |
| 931 | goto nla_put_failure; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 932 | if (err) { |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 933 | nla_nest_cancel(skb, vf_port); |
| 934 | continue; |
| 935 | } |
| 936 | nla_nest_end(skb, vf_port); |
| 937 | } |
| 938 | |
| 939 | nla_nest_end(skb, vf_ports); |
| 940 | |
| 941 | return 0; |
Scott Feldman | 8ca9418 | 2010-05-28 03:42:18 -0700 | [diff] [blame] | 942 | |
| 943 | nla_put_failure: |
| 944 | nla_nest_cancel(skb, vf_ports); |
| 945 | return -EMSGSIZE; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) |
| 949 | { |
| 950 | struct nlattr *port_self; |
| 951 | int err; |
| 952 | |
| 953 | port_self = nla_nest_start(skb, IFLA_PORT_SELF); |
| 954 | if (!port_self) |
| 955 | return -EMSGSIZE; |
| 956 | |
| 957 | err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); |
| 958 | if (err) { |
| 959 | nla_nest_cancel(skb, port_self); |
Scott Feldman | 8ca9418 | 2010-05-28 03:42:18 -0700 | [diff] [blame] | 960 | return (err == -EMSGSIZE) ? err : 0; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | nla_nest_end(skb, port_self); |
| 964 | |
| 965 | return 0; |
| 966 | } |
| 967 | |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 968 | static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, |
| 969 | u32 ext_filter_mask) |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 970 | { |
| 971 | int err; |
| 972 | |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 973 | if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || |
| 974 | !(ext_filter_mask & RTEXT_FILTER_VF)) |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 975 | return 0; |
| 976 | |
| 977 | err = rtnl_port_self_fill(skb, dev); |
| 978 | if (err) |
| 979 | return err; |
| 980 | |
| 981 | if (dev_num_vf(dev->dev.parent)) { |
| 982 | err = rtnl_vf_ports_fill(skb, dev); |
| 983 | if (err) |
| 984 | return err; |
| 985 | } |
| 986 | |
| 987 | return 0; |
| 988 | } |
| 989 | |
Jiri Pirko | 66cae9e | 2013-07-29 18:16:50 +0200 | [diff] [blame] | 990 | static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev) |
| 991 | { |
| 992 | int err; |
Jiri Pirko | 02637fc | 2014-11-28 14:34:16 +0100 | [diff] [blame] | 993 | struct netdev_phys_item_id ppid; |
Jiri Pirko | 66cae9e | 2013-07-29 18:16:50 +0200 | [diff] [blame] | 994 | |
| 995 | err = dev_get_phys_port_id(dev, &ppid); |
| 996 | if (err) { |
| 997 | if (err == -EOPNOTSUPP) |
| 998 | return 0; |
| 999 | return err; |
| 1000 | } |
| 1001 | |
| 1002 | if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id)) |
| 1003 | return -EMSGSIZE; |
| 1004 | |
| 1005 | return 0; |
| 1006 | } |
| 1007 | |
David Ahern | db24a90 | 2015-03-17 20:23:15 -0600 | [diff] [blame] | 1008 | static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev) |
| 1009 | { |
| 1010 | char name[IFNAMSIZ]; |
| 1011 | int err; |
| 1012 | |
| 1013 | err = dev_get_phys_port_name(dev, name, sizeof(name)); |
| 1014 | if (err) { |
| 1015 | if (err == -EOPNOTSUPP) |
| 1016 | return 0; |
| 1017 | return err; |
| 1018 | } |
| 1019 | |
| 1020 | if (nla_put(skb, IFLA_PHYS_PORT_NAME, strlen(name), name)) |
| 1021 | return -EMSGSIZE; |
| 1022 | |
| 1023 | return 0; |
| 1024 | } |
| 1025 | |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1026 | static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) |
| 1027 | { |
| 1028 | int err; |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 1029 | struct switchdev_attr attr = { |
Ido Schimmel | 6ff64f6 | 2015-12-15 16:03:35 +0100 | [diff] [blame] | 1030 | .orig_dev = dev, |
Jiri Pirko | 1f86839 | 2015-10-01 11:03:42 +0200 | [diff] [blame] | 1031 | .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID, |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 1032 | .flags = SWITCHDEV_F_NO_RECURSE, |
| 1033 | }; |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1034 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 1035 | err = switchdev_port_attr_get(dev, &attr); |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1036 | if (err) { |
| 1037 | if (err == -EOPNOTSUPP) |
| 1038 | return 0; |
| 1039 | return err; |
| 1040 | } |
| 1041 | |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 1042 | if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len, |
| 1043 | attr.u.ppid.id)) |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1044 | return -EMSGSIZE; |
| 1045 | |
| 1046 | return 0; |
| 1047 | } |
| 1048 | |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1049 | static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb, |
| 1050 | struct net_device *dev) |
| 1051 | { |
Roopa Prabhu | 550bce5 | 2016-04-15 20:36:25 -0700 | [diff] [blame] | 1052 | struct rtnl_link_stats64 *sp; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1053 | struct nlattr *attr; |
David S. Miller | 35c5845 | 2016-04-19 19:49:29 -0400 | [diff] [blame^] | 1054 | int err; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1055 | |
David S. Miller | 35c5845 | 2016-04-19 19:49:29 -0400 | [diff] [blame^] | 1056 | err = nla_align_64bit(skb, IFLA_PAD); |
| 1057 | if (err) |
| 1058 | return err; |
David S. Miller | 1840284 | 2016-04-19 14:30:10 -0400 | [diff] [blame] | 1059 | |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1060 | attr = nla_reserve(skb, IFLA_STATS64, |
| 1061 | sizeof(struct rtnl_link_stats64)); |
| 1062 | if (!attr) |
| 1063 | return -EMSGSIZE; |
| 1064 | |
Roopa Prabhu | 550bce5 | 2016-04-15 20:36:25 -0700 | [diff] [blame] | 1065 | sp = nla_data(attr); |
| 1066 | dev_get_stats(dev, sp); |
| 1067 | |
| 1068 | attr = nla_reserve(skb, IFLA_STATS, |
| 1069 | sizeof(struct rtnl_link_stats)); |
| 1070 | if (!attr) |
| 1071 | return -EMSGSIZE; |
| 1072 | |
| 1073 | copy_rtnl_link_stats(nla_data(attr), sp); |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1074 | |
| 1075 | return 0; |
| 1076 | } |
| 1077 | |
| 1078 | static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, |
| 1079 | struct net_device *dev, |
| 1080 | int vfs_num, |
| 1081 | struct nlattr *vfinfo) |
| 1082 | { |
| 1083 | struct ifla_vf_rss_query_en vf_rss_query_en; |
| 1084 | struct ifla_vf_link_state vf_linkstate; |
| 1085 | struct ifla_vf_spoofchk vf_spoofchk; |
| 1086 | struct ifla_vf_tx_rate vf_tx_rate; |
| 1087 | struct ifla_vf_stats vf_stats; |
| 1088 | struct ifla_vf_trust vf_trust; |
| 1089 | struct ifla_vf_vlan vf_vlan; |
| 1090 | struct ifla_vf_rate vf_rate; |
| 1091 | struct nlattr *vf, *vfstats; |
| 1092 | struct ifla_vf_mac vf_mac; |
| 1093 | struct ifla_vf_info ivi; |
| 1094 | |
| 1095 | /* Not all SR-IOV capable drivers support the |
| 1096 | * spoofcheck and "RSS query enable" query. Preset to |
| 1097 | * -1 so the user space tool can detect that the driver |
| 1098 | * didn't report anything. |
| 1099 | */ |
| 1100 | ivi.spoofchk = -1; |
| 1101 | ivi.rss_query_en = -1; |
| 1102 | ivi.trusted = -1; |
| 1103 | memset(ivi.mac, 0, sizeof(ivi.mac)); |
| 1104 | /* The default value for VF link state is "auto" |
| 1105 | * IFLA_VF_LINK_STATE_AUTO which equals zero |
| 1106 | */ |
| 1107 | ivi.linkstate = 0; |
| 1108 | if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi)) |
| 1109 | return 0; |
| 1110 | |
| 1111 | vf_mac.vf = |
| 1112 | vf_vlan.vf = |
| 1113 | vf_rate.vf = |
| 1114 | vf_tx_rate.vf = |
| 1115 | vf_spoofchk.vf = |
| 1116 | vf_linkstate.vf = |
| 1117 | vf_rss_query_en.vf = |
| 1118 | vf_trust.vf = ivi.vf; |
| 1119 | |
| 1120 | memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); |
| 1121 | vf_vlan.vlan = ivi.vlan; |
| 1122 | vf_vlan.qos = ivi.qos; |
| 1123 | vf_tx_rate.rate = ivi.max_tx_rate; |
| 1124 | vf_rate.min_tx_rate = ivi.min_tx_rate; |
| 1125 | vf_rate.max_tx_rate = ivi.max_tx_rate; |
| 1126 | vf_spoofchk.setting = ivi.spoofchk; |
| 1127 | vf_linkstate.link_state = ivi.linkstate; |
| 1128 | vf_rss_query_en.setting = ivi.rss_query_en; |
| 1129 | vf_trust.setting = ivi.trusted; |
| 1130 | vf = nla_nest_start(skb, IFLA_VF_INFO); |
| 1131 | if (!vf) { |
| 1132 | nla_nest_cancel(skb, vfinfo); |
| 1133 | return -EMSGSIZE; |
| 1134 | } |
| 1135 | if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || |
| 1136 | nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || |
| 1137 | nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), |
| 1138 | &vf_rate) || |
| 1139 | nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), |
| 1140 | &vf_tx_rate) || |
| 1141 | nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), |
| 1142 | &vf_spoofchk) || |
| 1143 | nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), |
| 1144 | &vf_linkstate) || |
| 1145 | nla_put(skb, IFLA_VF_RSS_QUERY_EN, |
| 1146 | sizeof(vf_rss_query_en), |
| 1147 | &vf_rss_query_en) || |
| 1148 | nla_put(skb, IFLA_VF_TRUST, |
| 1149 | sizeof(vf_trust), &vf_trust)) |
| 1150 | return -EMSGSIZE; |
| 1151 | memset(&vf_stats, 0, sizeof(vf_stats)); |
| 1152 | if (dev->netdev_ops->ndo_get_vf_stats) |
| 1153 | dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, |
| 1154 | &vf_stats); |
| 1155 | vfstats = nla_nest_start(skb, IFLA_VF_STATS); |
| 1156 | if (!vfstats) { |
| 1157 | nla_nest_cancel(skb, vf); |
| 1158 | nla_nest_cancel(skb, vfinfo); |
| 1159 | return -EMSGSIZE; |
| 1160 | } |
| 1161 | if (nla_put_u64(skb, IFLA_VF_STATS_RX_PACKETS, |
| 1162 | vf_stats.rx_packets) || |
| 1163 | nla_put_u64(skb, IFLA_VF_STATS_TX_PACKETS, |
| 1164 | vf_stats.tx_packets) || |
| 1165 | nla_put_u64(skb, IFLA_VF_STATS_RX_BYTES, |
| 1166 | vf_stats.rx_bytes) || |
| 1167 | nla_put_u64(skb, IFLA_VF_STATS_TX_BYTES, |
| 1168 | vf_stats.tx_bytes) || |
| 1169 | nla_put_u64(skb, IFLA_VF_STATS_BROADCAST, |
| 1170 | vf_stats.broadcast) || |
| 1171 | nla_put_u64(skb, IFLA_VF_STATS_MULTICAST, |
| 1172 | vf_stats.multicast)) |
| 1173 | return -EMSGSIZE; |
| 1174 | nla_nest_end(skb, vfstats); |
| 1175 | nla_nest_end(skb, vf); |
| 1176 | return 0; |
| 1177 | } |
| 1178 | |
| 1179 | static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev) |
| 1180 | { |
| 1181 | struct rtnl_link_ifmap map = { |
| 1182 | .mem_start = dev->mem_start, |
| 1183 | .mem_end = dev->mem_end, |
| 1184 | .base_addr = dev->base_addr, |
| 1185 | .irq = dev->irq, |
| 1186 | .dma = dev->dma, |
| 1187 | .port = dev->if_port, |
| 1188 | }; |
| 1189 | if (nla_put(skb, IFLA_MAP, sizeof(map), &map)) |
| 1190 | return -EMSGSIZE; |
| 1191 | |
| 1192 | return 0; |
| 1193 | } |
| 1194 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1195 | 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] | 1196 | int type, u32 pid, u32 seq, u32 change, |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1197 | unsigned int flags, u32 ext_filter_mask) |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1198 | { |
| 1199 | struct ifinfomsg *ifm; |
| 1200 | struct nlmsghdr *nlh; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1201 | struct nlattr *af_spec; |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 1202 | struct rtnl_af_ops *af_ops; |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1203 | struct net_device *upper_dev = netdev_master_upper_dev_get(dev); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1204 | |
Eric Dumazet | 2907c35 | 2011-05-25 07:34:04 +0000 | [diff] [blame] | 1205 | ASSERT_RTNL(); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1206 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); |
| 1207 | if (nlh == NULL) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 1208 | return -EMSGSIZE; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1209 | |
| 1210 | ifm = nlmsg_data(nlh); |
| 1211 | ifm->ifi_family = AF_UNSPEC; |
| 1212 | ifm->__ifi_pad = 0; |
| 1213 | ifm->ifi_type = dev->type; |
| 1214 | ifm->ifi_index = dev->ifindex; |
| 1215 | ifm->ifi_flags = dev_get_flags(dev); |
| 1216 | ifm->ifi_change = change; |
| 1217 | |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1218 | if (nla_put_string(skb, IFLA_IFNAME, dev->name) || |
| 1219 | nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || |
| 1220 | nla_put_u8(skb, IFLA_OPERSTATE, |
| 1221 | netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || |
| 1222 | nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || |
| 1223 | nla_put_u32(skb, IFLA_MTU, dev->mtu) || |
| 1224 | nla_put_u32(skb, IFLA_GROUP, dev->group) || |
Ben Greear | edbc0bb | 2012-03-29 12:51:30 +0000 | [diff] [blame] | 1225 | nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 1226 | nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || |
Eric Dumazet | c70ce02 | 2016-03-21 09:55:10 -0700 | [diff] [blame] | 1227 | nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) || |
| 1228 | 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] | 1229 | #ifdef CONFIG_RPS |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 1230 | 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] | 1231 | #endif |
Nicolas Dichtel | a54acb3 | 2015-04-02 17:07:00 +0200 | [diff] [blame] | 1232 | (dev->ifindex != dev_get_iflink(dev) && |
| 1233 | nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) || |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1234 | (upper_dev && |
| 1235 | nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) || |
Jiri Pirko | 9a57247 | 2012-12-27 23:49:39 +0000 | [diff] [blame] | 1236 | nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1237 | (dev->qdisc && |
| 1238 | nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) || |
| 1239 | (dev->ifalias && |
david decotigny | 2d3b479 | 2014-03-29 09:48:35 -0700 | [diff] [blame] | 1240 | nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) || |
| 1241 | nla_put_u32(skb, IFLA_CARRIER_CHANGES, |
Anuradha Karuppiah | 88d6378 | 2015-07-14 13:43:20 -0700 | [diff] [blame] | 1242 | atomic_read(&dev->carrier_changes)) || |
| 1243 | nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down)) |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1244 | goto nla_put_failure; |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 1245 | |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1246 | if (rtnl_fill_link_ifmap(skb, dev)) |
| 1247 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | |
| 1249 | if (dev->addr_len) { |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1250 | if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || |
| 1251 | nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) |
| 1252 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | } |
| 1254 | |
Jiri Pirko | 66cae9e | 2013-07-29 18:16:50 +0200 | [diff] [blame] | 1255 | if (rtnl_phys_port_id_fill(skb, dev)) |
| 1256 | goto nla_put_failure; |
| 1257 | |
David Ahern | db24a90 | 2015-03-17 20:23:15 -0600 | [diff] [blame] | 1258 | if (rtnl_phys_port_name_fill(skb, dev)) |
| 1259 | goto nla_put_failure; |
| 1260 | |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1261 | if (rtnl_phys_switch_id_fill(skb, dev)) |
| 1262 | goto nla_put_failure; |
| 1263 | |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1264 | if (rtnl_fill_stats(skb, dev)) |
Pavel Emelyanov | 96e7408 | 2008-05-21 14:12:46 -0700 | [diff] [blame] | 1265 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | |
David S. Miller | a657434 | 2012-04-01 20:12:00 -0400 | [diff] [blame] | 1267 | if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) && |
| 1268 | nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent))) |
| 1269 | goto nla_put_failure; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1270 | |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1271 | if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent && |
| 1272 | ext_filter_mask & RTEXT_FILTER_VF) { |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 1273 | int i; |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1274 | struct nlattr *vfinfo; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1275 | int num_vfs = dev_num_vf(dev->dev.parent); |
| 1276 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1277 | vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST); |
| 1278 | if (!vfinfo) |
| 1279 | goto nla_put_failure; |
| 1280 | for (i = 0; i < num_vfs; i++) { |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1281 | if (rtnl_fill_vfinfo(skb, dev, i, vfinfo)) |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1282 | goto nla_put_failure; |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 1283 | } |
Hannes Frederic Sowa | b22b941 | 2015-11-17 14:16:52 +0100 | [diff] [blame] | 1284 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1285 | nla_nest_end(skb, vfinfo); |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 1286 | } |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1287 | |
David Gibson | c53864f | 2014-04-24 10:22:36 +1000 | [diff] [blame] | 1288 | if (rtnl_port_fill(skb, dev, ext_filter_mask)) |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1289 | goto nla_put_failure; |
| 1290 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 1291 | if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1292 | if (rtnl_link_fill(skb, dev) < 0) |
| 1293 | goto nla_put_failure; |
| 1294 | } |
| 1295 | |
Nicolas Dichtel | d37512a | 2015-01-15 15:11:16 +0100 | [diff] [blame] | 1296 | if (dev->rtnl_link_ops && |
| 1297 | dev->rtnl_link_ops->get_link_net) { |
| 1298 | struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); |
| 1299 | |
| 1300 | if (!net_eq(dev_net(dev), link_net)) { |
Nicolas Dichtel | 7a0877d | 2015-05-07 11:02:49 +0200 | [diff] [blame] | 1301 | int id = peernet2id_alloc(dev_net(dev), link_net); |
Nicolas Dichtel | d37512a | 2015-01-15 15:11:16 +0100 | [diff] [blame] | 1302 | |
| 1303 | if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) |
| 1304 | goto nla_put_failure; |
| 1305 | } |
| 1306 | } |
| 1307 | |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 1308 | if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC))) |
| 1309 | goto nla_put_failure; |
| 1310 | |
| 1311 | list_for_each_entry(af_ops, &rtnl_af_ops, list) { |
| 1312 | if (af_ops->fill_link_af) { |
| 1313 | struct nlattr *af; |
| 1314 | int err; |
| 1315 | |
| 1316 | if (!(af = nla_nest_start(skb, af_ops->family))) |
| 1317 | goto nla_put_failure; |
| 1318 | |
Sowmini Varadhan | d5566fd | 2015-09-11 16:48:48 -0400 | [diff] [blame] | 1319 | err = af_ops->fill_link_af(skb, dev, ext_filter_mask); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 1320 | |
| 1321 | /* |
| 1322 | * Caller may return ENODATA to indicate that there |
| 1323 | * was no data to be dumped. This is not an error, it |
| 1324 | * means we should trim the attribute header and |
| 1325 | * continue. |
| 1326 | */ |
| 1327 | if (err == -ENODATA) |
| 1328 | nla_nest_cancel(skb, af); |
| 1329 | else if (err < 0) |
| 1330 | goto nla_put_failure; |
| 1331 | |
| 1332 | nla_nest_end(skb, af); |
| 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | nla_nest_end(skb, af_spec); |
| 1337 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 1338 | nlmsg_end(skb, nlh); |
| 1339 | return 0; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1340 | |
| 1341 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 1342 | nlmsg_cancel(skb, nlh); |
| 1343 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1346 | static const struct nla_policy ifla_policy[IFLA_MAX+1] = { |
| 1347 | [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, |
| 1348 | [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, |
| 1349 | [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, |
| 1350 | [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, |
| 1351 | [IFLA_MTU] = { .type = NLA_U32 }, |
| 1352 | [IFLA_LINK] = { .type = NLA_U32 }, |
| 1353 | [IFLA_MASTER] = { .type = NLA_U32 }, |
| 1354 | [IFLA_CARRIER] = { .type = NLA_U8 }, |
| 1355 | [IFLA_TXQLEN] = { .type = NLA_U32 }, |
| 1356 | [IFLA_WEIGHT] = { .type = NLA_U32 }, |
| 1357 | [IFLA_OPERSTATE] = { .type = NLA_U8 }, |
| 1358 | [IFLA_LINKMODE] = { .type = NLA_U8 }, |
| 1359 | [IFLA_LINKINFO] = { .type = NLA_NESTED }, |
| 1360 | [IFLA_NET_NS_PID] = { .type = NLA_U32 }, |
| 1361 | [IFLA_NET_NS_FD] = { .type = NLA_U32 }, |
| 1362 | [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 }, |
| 1363 | [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, |
| 1364 | [IFLA_VF_PORTS] = { .type = NLA_NESTED }, |
| 1365 | [IFLA_PORT_SELF] = { .type = NLA_NESTED }, |
| 1366 | [IFLA_AF_SPEC] = { .type = NLA_NESTED }, |
| 1367 | [IFLA_EXT_MASK] = { .type = NLA_U32 }, |
| 1368 | [IFLA_PROMISCUITY] = { .type = NLA_U32 }, |
| 1369 | [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, |
| 1370 | [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, |
Jiri Pirko | 02637fc | 2014-11-28 14:34:16 +0100 | [diff] [blame] | 1371 | [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] | 1372 | [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ |
Jiri Pirko | 82f2841 | 2014-11-28 14:34:18 +0100 | [diff] [blame] | 1373 | [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] | 1374 | [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, |
Anuradha Karuppiah | 88d6378 | 2015-07-14 13:43:20 -0700 | [diff] [blame] | 1375 | [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1376 | }; |
| 1377 | |
| 1378 | static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { |
| 1379 | [IFLA_INFO_KIND] = { .type = NLA_STRING }, |
| 1380 | [IFLA_INFO_DATA] = { .type = NLA_NESTED }, |
| 1381 | [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, |
| 1382 | [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, |
| 1383 | }; |
| 1384 | |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1385 | static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { |
Daniel Borkmann | 364d571 | 2015-02-05 18:44:04 +0100 | [diff] [blame] | 1386 | [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, |
| 1387 | [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, |
| 1388 | [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, |
| 1389 | [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, |
| 1390 | [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, |
| 1391 | [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, |
Vlad Zolotarov | 01a3d79 | 2015-03-30 21:35:23 +0300 | [diff] [blame] | 1392 | [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] | 1393 | [IFLA_VF_STATS] = { .type = NLA_NESTED }, |
Hiroshi Shimamoto | dd461d6 | 2015-08-28 06:57:55 +0000 | [diff] [blame] | 1394 | [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, |
Eli Cohen | cc8e27c | 2016-03-11 22:58:34 +0200 | [diff] [blame] | 1395 | [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) }, |
| 1396 | [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) }, |
Eran Ben Elisha | 3b766cd | 2015-06-15 17:59:07 +0300 | [diff] [blame] | 1397 | }; |
| 1398 | |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1399 | static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { |
| 1400 | [IFLA_PORT_VF] = { .type = NLA_U32 }, |
| 1401 | [IFLA_PORT_PROFILE] = { .type = NLA_STRING, |
| 1402 | .len = PORT_PROFILE_MAX }, |
| 1403 | [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, |
| 1404 | .len = sizeof(struct ifla_port_vsi)}, |
| 1405 | [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, |
| 1406 | .len = PORT_UUID_MAX }, |
| 1407 | [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, |
| 1408 | .len = PORT_UUID_MAX }, |
| 1409 | [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, |
| 1410 | [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, |
| 1411 | }; |
| 1412 | |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 1413 | static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla) |
| 1414 | { |
| 1415 | const struct rtnl_link_ops *ops = NULL; |
| 1416 | struct nlattr *linfo[IFLA_INFO_MAX + 1]; |
| 1417 | |
| 1418 | if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla, ifla_info_policy) < 0) |
| 1419 | return NULL; |
| 1420 | |
| 1421 | if (linfo[IFLA_INFO_KIND]) { |
| 1422 | char kind[MODULE_NAME_LEN]; |
| 1423 | |
| 1424 | nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind)); |
| 1425 | ops = rtnl_link_ops_get(kind); |
| 1426 | } |
| 1427 | |
| 1428 | return ops; |
| 1429 | } |
| 1430 | |
| 1431 | static bool link_master_filtered(struct net_device *dev, int master_idx) |
| 1432 | { |
| 1433 | struct net_device *master; |
| 1434 | |
| 1435 | if (!master_idx) |
| 1436 | return false; |
| 1437 | |
| 1438 | master = netdev_master_upper_dev_get(dev); |
| 1439 | if (!master || master->ifindex != master_idx) |
| 1440 | return true; |
| 1441 | |
| 1442 | return false; |
| 1443 | } |
| 1444 | |
| 1445 | static bool link_kind_filtered(const struct net_device *dev, |
| 1446 | const struct rtnl_link_ops *kind_ops) |
| 1447 | { |
| 1448 | if (kind_ops && dev->rtnl_link_ops != kind_ops) |
| 1449 | return true; |
| 1450 | |
| 1451 | return false; |
| 1452 | } |
| 1453 | |
| 1454 | static bool link_dump_filtered(struct net_device *dev, |
| 1455 | int master_idx, |
| 1456 | const struct rtnl_link_ops *kind_ops) |
| 1457 | { |
| 1458 | if (link_master_filtered(dev, master_idx) || |
| 1459 | link_kind_filtered(dev, kind_ops)) |
| 1460 | return true; |
| 1461 | |
| 1462 | return false; |
| 1463 | } |
| 1464 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1465 | 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] | 1466 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1467 | struct net *net = sock_net(skb->sk); |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1468 | int h, s_h; |
| 1469 | int idx = 0, s_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | struct net_device *dev; |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1471 | struct hlist_head *head; |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1472 | struct nlattr *tb[IFLA_MAX+1]; |
| 1473 | u32 ext_filter_mask = 0; |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 1474 | const struct rtnl_link_ops *kind_ops = NULL; |
| 1475 | unsigned int flags = NLM_F_MULTI; |
| 1476 | int master_idx = 0; |
David Gibson | 973462b | 2014-04-24 10:22:35 +1000 | [diff] [blame] | 1477 | int err; |
Michal Schmidt | e5eca6d | 2014-05-28 14:15:19 +0200 | [diff] [blame] | 1478 | int hdrlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1480 | s_h = cb->args[0]; |
| 1481 | s_idx = cb->args[1]; |
| 1482 | |
Thomas Graf | 4e985ad | 2011-06-21 03:11:20 +0000 | [diff] [blame] | 1483 | cb->seq = net->dev_base_seq; |
| 1484 | |
Michal Schmidt | e5eca6d | 2014-05-28 14:15:19 +0200 | [diff] [blame] | 1485 | /* A hack to preserve kernel<->userspace interface. |
| 1486 | * The correct header is ifinfomsg. It is consistent with rtnl_getlink. |
| 1487 | * However, before Linux v3.9 the code here assumed rtgenmsg and that's |
| 1488 | * what iproute2 < v3.9.0 used. |
| 1489 | * We can detect the old iproute2. Even including the IFLA_EXT_MASK |
| 1490 | * attribute, its netlink message is shorter than struct ifinfomsg. |
| 1491 | */ |
| 1492 | hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ? |
| 1493 | sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); |
| 1494 | |
| 1495 | if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) { |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1496 | |
Eric Dumazet | a4b64fb | 2012-03-04 12:32:10 +0000 | [diff] [blame] | 1497 | if (tb[IFLA_EXT_MASK]) |
| 1498 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 1499 | |
| 1500 | if (tb[IFLA_MASTER]) |
| 1501 | master_idx = nla_get_u32(tb[IFLA_MASTER]); |
| 1502 | |
| 1503 | if (tb[IFLA_LINKINFO]) |
| 1504 | kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]); |
| 1505 | |
| 1506 | if (master_idx || kind_ops) |
| 1507 | flags |= NLM_F_DUMP_FILTERED; |
Eric Dumazet | a4b64fb | 2012-03-04 12:32:10 +0000 | [diff] [blame] | 1508 | } |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1509 | |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1510 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { |
| 1511 | idx = 0; |
| 1512 | head = &net->dev_index_head[h]; |
Eric Dumazet | cac5e65 | 2015-02-27 09:42:50 -0800 | [diff] [blame] | 1513 | hlist_for_each_entry(dev, head, index_hlist) { |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 1514 | if (link_dump_filtered(dev, master_idx, kind_ops)) |
| 1515 | continue; |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1516 | if (idx < s_idx) |
| 1517 | goto cont; |
David Gibson | 973462b | 2014-04-24 10:22:35 +1000 | [diff] [blame] | 1518 | err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK, |
| 1519 | NETLINK_CB(cb->skb).portid, |
| 1520 | cb->nlh->nlmsg_seq, 0, |
David Ahern | dc599f7 | 2016-02-02 08:17:07 -0800 | [diff] [blame] | 1521 | flags, |
David Gibson | 973462b | 2014-04-24 10:22:35 +1000 | [diff] [blame] | 1522 | ext_filter_mask); |
| 1523 | /* If we ran out of room on the first message, |
| 1524 | * we're in trouble |
| 1525 | */ |
| 1526 | WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); |
| 1527 | |
David S. Miller | 7b46a64 | 2015-01-18 23:36:08 -0500 | [diff] [blame] | 1528 | if (err < 0) |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1529 | goto out; |
Thomas Graf | 4e985ad | 2011-06-21 03:11:20 +0000 | [diff] [blame] | 1530 | |
| 1531 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 1532 | cont: |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1533 | idx++; |
| 1534 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | } |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1536 | out: |
| 1537 | cb->args[1] = idx; |
| 1538 | cb->args[0] = h; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | |
| 1540 | return skb->len; |
| 1541 | } |
| 1542 | |
Jiri Pirko | f7b1260 | 2014-02-18 20:53:18 +0100 | [diff] [blame] | 1543 | int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len) |
| 1544 | { |
| 1545 | return nla_parse(tb, IFLA_MAX, head, len, ifla_policy); |
| 1546 | } |
| 1547 | EXPORT_SYMBOL(rtnl_nla_parse_ifla); |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1548 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1549 | struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) |
| 1550 | { |
| 1551 | struct net *net; |
| 1552 | /* Examine the link attributes and figure out which |
| 1553 | * network namespace we are talking about. |
| 1554 | */ |
| 1555 | if (tb[IFLA_NET_NS_PID]) |
| 1556 | 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] | 1557 | else if (tb[IFLA_NET_NS_FD]) |
| 1558 | 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] | 1559 | else |
| 1560 | net = get_net(src_net); |
| 1561 | return net; |
| 1562 | } |
| 1563 | EXPORT_SYMBOL(rtnl_link_get_net); |
| 1564 | |
Thomas Graf | 1840bb1 | 2008-02-23 19:54:36 -0800 | [diff] [blame] | 1565 | static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) |
| 1566 | { |
| 1567 | if (dev) { |
| 1568 | if (tb[IFLA_ADDRESS] && |
| 1569 | nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) |
| 1570 | return -EINVAL; |
| 1571 | |
| 1572 | if (tb[IFLA_BROADCAST] && |
| 1573 | nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) |
| 1574 | return -EINVAL; |
| 1575 | } |
| 1576 | |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 1577 | if (tb[IFLA_AF_SPEC]) { |
| 1578 | struct nlattr *af; |
| 1579 | int rem, err; |
| 1580 | |
| 1581 | nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { |
| 1582 | const struct rtnl_af_ops *af_ops; |
| 1583 | |
| 1584 | if (!(af_ops = rtnl_af_lookup(nla_type(af)))) |
| 1585 | return -EAFNOSUPPORT; |
| 1586 | |
| 1587 | if (!af_ops->set_link_af) |
| 1588 | return -EOPNOTSUPP; |
| 1589 | |
| 1590 | if (af_ops->validate_link_af) { |
Kurt Van Dijck | 6d3a9a6 | 2011-01-26 04:55:24 +0000 | [diff] [blame] | 1591 | err = af_ops->validate_link_af(dev, af); |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 1592 | if (err < 0) |
| 1593 | return err; |
| 1594 | } |
| 1595 | } |
| 1596 | } |
| 1597 | |
Thomas Graf | 1840bb1 | 2008-02-23 19:54:36 -0800 | [diff] [blame] | 1598 | return 0; |
| 1599 | } |
| 1600 | |
Eli Cohen | cc8e27c | 2016-03-11 22:58:34 +0200 | [diff] [blame] | 1601 | static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt, |
| 1602 | int guid_type) |
| 1603 | { |
| 1604 | const struct net_device_ops *ops = dev->netdev_ops; |
| 1605 | |
| 1606 | return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type); |
| 1607 | } |
| 1608 | |
| 1609 | static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type) |
| 1610 | { |
| 1611 | if (dev->type != ARPHRD_INFINIBAND) |
| 1612 | return -EOPNOTSUPP; |
| 1613 | |
| 1614 | return handle_infiniband_guid(dev, ivt, guid_type); |
| 1615 | } |
| 1616 | |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1617 | static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1618 | { |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1619 | const struct net_device_ops *ops = dev->netdev_ops; |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1620 | int err = -EINVAL; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1621 | |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1622 | if (tb[IFLA_VF_MAC]) { |
| 1623 | struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); |
Vlad Zolotarov | 01a3d79 | 2015-03-30 21:35:23 +0300 | [diff] [blame] | 1624 | |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1625 | err = -EOPNOTSUPP; |
| 1626 | if (ops->ndo_set_vf_mac) |
| 1627 | err = ops->ndo_set_vf_mac(dev, ivm->vf, |
| 1628 | ivm->mac); |
| 1629 | if (err < 0) |
| 1630 | return err; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1631 | } |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1632 | |
| 1633 | if (tb[IFLA_VF_VLAN]) { |
| 1634 | struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); |
| 1635 | |
| 1636 | err = -EOPNOTSUPP; |
| 1637 | if (ops->ndo_set_vf_vlan) |
| 1638 | err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, |
| 1639 | ivv->qos); |
| 1640 | if (err < 0) |
| 1641 | return err; |
| 1642 | } |
| 1643 | |
| 1644 | if (tb[IFLA_VF_TX_RATE]) { |
| 1645 | struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); |
| 1646 | struct ifla_vf_info ivf; |
| 1647 | |
| 1648 | err = -EOPNOTSUPP; |
| 1649 | if (ops->ndo_get_vf_config) |
| 1650 | err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); |
| 1651 | if (err < 0) |
| 1652 | return err; |
| 1653 | |
| 1654 | err = -EOPNOTSUPP; |
| 1655 | if (ops->ndo_set_vf_rate) |
| 1656 | err = ops->ndo_set_vf_rate(dev, ivt->vf, |
| 1657 | ivf.min_tx_rate, |
| 1658 | ivt->rate); |
| 1659 | if (err < 0) |
| 1660 | return err; |
| 1661 | } |
| 1662 | |
| 1663 | if (tb[IFLA_VF_RATE]) { |
| 1664 | struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); |
| 1665 | |
| 1666 | err = -EOPNOTSUPP; |
| 1667 | if (ops->ndo_set_vf_rate) |
| 1668 | err = ops->ndo_set_vf_rate(dev, ivt->vf, |
| 1669 | ivt->min_tx_rate, |
| 1670 | ivt->max_tx_rate); |
| 1671 | if (err < 0) |
| 1672 | return err; |
| 1673 | } |
| 1674 | |
| 1675 | if (tb[IFLA_VF_SPOOFCHK]) { |
| 1676 | struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); |
| 1677 | |
| 1678 | err = -EOPNOTSUPP; |
| 1679 | if (ops->ndo_set_vf_spoofchk) |
| 1680 | err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, |
| 1681 | ivs->setting); |
| 1682 | if (err < 0) |
| 1683 | return err; |
| 1684 | } |
| 1685 | |
| 1686 | if (tb[IFLA_VF_LINK_STATE]) { |
| 1687 | struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); |
| 1688 | |
| 1689 | err = -EOPNOTSUPP; |
| 1690 | if (ops->ndo_set_vf_link_state) |
| 1691 | err = ops->ndo_set_vf_link_state(dev, ivl->vf, |
| 1692 | ivl->link_state); |
| 1693 | if (err < 0) |
| 1694 | return err; |
| 1695 | } |
| 1696 | |
| 1697 | if (tb[IFLA_VF_RSS_QUERY_EN]) { |
| 1698 | struct ifla_vf_rss_query_en *ivrssq_en; |
| 1699 | |
| 1700 | err = -EOPNOTSUPP; |
| 1701 | ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); |
| 1702 | if (ops->ndo_set_vf_rss_query_en) |
| 1703 | err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, |
| 1704 | ivrssq_en->setting); |
| 1705 | if (err < 0) |
| 1706 | return err; |
| 1707 | } |
| 1708 | |
Hiroshi Shimamoto | dd461d6 | 2015-08-28 06:57:55 +0000 | [diff] [blame] | 1709 | if (tb[IFLA_VF_TRUST]) { |
| 1710 | struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); |
| 1711 | |
| 1712 | err = -EOPNOTSUPP; |
| 1713 | if (ops->ndo_set_vf_trust) |
| 1714 | err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); |
| 1715 | if (err < 0) |
| 1716 | return err; |
| 1717 | } |
| 1718 | |
Eli Cohen | cc8e27c | 2016-03-11 22:58:34 +0200 | [diff] [blame] | 1719 | if (tb[IFLA_VF_IB_NODE_GUID]) { |
| 1720 | struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]); |
| 1721 | |
| 1722 | if (!ops->ndo_set_vf_guid) |
| 1723 | return -EOPNOTSUPP; |
| 1724 | |
| 1725 | return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID); |
| 1726 | } |
| 1727 | |
| 1728 | if (tb[IFLA_VF_IB_PORT_GUID]) { |
| 1729 | struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]); |
| 1730 | |
| 1731 | if (!ops->ndo_set_vf_guid) |
| 1732 | return -EOPNOTSUPP; |
| 1733 | |
| 1734 | return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID); |
| 1735 | } |
| 1736 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1737 | return err; |
| 1738 | } |
| 1739 | |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1740 | static int do_set_master(struct net_device *dev, int ifindex) |
| 1741 | { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1742 | struct net_device *upper_dev = netdev_master_upper_dev_get(dev); |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1743 | const struct net_device_ops *ops; |
| 1744 | int err; |
| 1745 | |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1746 | if (upper_dev) { |
| 1747 | if (upper_dev->ifindex == ifindex) |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1748 | return 0; |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1749 | ops = upper_dev->netdev_ops; |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1750 | if (ops->ndo_del_slave) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1751 | err = ops->ndo_del_slave(upper_dev, dev); |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1752 | if (err) |
| 1753 | return err; |
| 1754 | } else { |
| 1755 | return -EOPNOTSUPP; |
| 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | if (ifindex) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1760 | upper_dev = __dev_get_by_index(dev_net(dev), ifindex); |
| 1761 | if (!upper_dev) |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1762 | return -EINVAL; |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1763 | ops = upper_dev->netdev_ops; |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1764 | if (ops->ndo_add_slave) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 1765 | err = ops->ndo_add_slave(upper_dev, dev); |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1766 | if (err) |
| 1767 | return err; |
| 1768 | } else { |
| 1769 | return -EOPNOTSUPP; |
| 1770 | } |
| 1771 | } |
| 1772 | return 0; |
| 1773 | } |
| 1774 | |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1775 | #define DO_SETLINK_MODIFIED 0x01 |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1776 | /* notify flag means notify + modified. */ |
| 1777 | #define DO_SETLINK_NOTIFY 0x03 |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 1778 | static int do_setlink(const struct sk_buff *skb, |
| 1779 | struct net_device *dev, struct ifinfomsg *ifm, |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1780 | struct nlattr **tb, char *ifname, int status) |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1781 | { |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 1782 | const struct net_device_ops *ops = dev->netdev_ops; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1783 | int err; |
| 1784 | |
Eric W. Biederman | f063052 | 2011-05-04 17:51:50 -0700 | [diff] [blame] | 1785 | if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) { |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1786 | struct net *net = rtnl_link_get_net(dev_net(dev), tb); |
Eric W. Biederman | d8a5ec6 | 2007-09-12 13:57:04 +0200 | [diff] [blame] | 1787 | if (IS_ERR(net)) { |
| 1788 | err = PTR_ERR(net); |
| 1789 | goto errout; |
| 1790 | } |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 1791 | if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) { |
Nicolas Dichtel | e0ebde0 | 2014-11-27 10:16:15 +0100 | [diff] [blame] | 1792 | put_net(net); |
Eric W. Biederman | b51642f | 2012-11-16 03:03:11 +0000 | [diff] [blame] | 1793 | err = -EPERM; |
| 1794 | goto errout; |
| 1795 | } |
Eric W. Biederman | d8a5ec6 | 2007-09-12 13:57:04 +0200 | [diff] [blame] | 1796 | err = dev_change_net_namespace(dev, net, ifname); |
| 1797 | put_net(net); |
| 1798 | if (err) |
| 1799 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1800 | status |= DO_SETLINK_MODIFIED; |
Eric W. Biederman | d8a5ec6 | 2007-09-12 13:57:04 +0200 | [diff] [blame] | 1801 | } |
| 1802 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1803 | if (tb[IFLA_MAP]) { |
| 1804 | struct rtnl_link_ifmap *u_map; |
| 1805 | struct ifmap k_map; |
| 1806 | |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 1807 | if (!ops->ndo_set_config) { |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1808 | err = -EOPNOTSUPP; |
| 1809 | goto errout; |
| 1810 | } |
| 1811 | |
| 1812 | if (!netif_device_present(dev)) { |
| 1813 | err = -ENODEV; |
| 1814 | goto errout; |
| 1815 | } |
| 1816 | |
| 1817 | u_map = nla_data(tb[IFLA_MAP]); |
| 1818 | k_map.mem_start = (unsigned long) u_map->mem_start; |
| 1819 | k_map.mem_end = (unsigned long) u_map->mem_end; |
| 1820 | k_map.base_addr = (unsigned short) u_map->base_addr; |
| 1821 | k_map.irq = (unsigned char) u_map->irq; |
| 1822 | k_map.dma = (unsigned char) u_map->dma; |
| 1823 | k_map.port = (unsigned char) u_map->port; |
| 1824 | |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 1825 | err = ops->ndo_set_config(dev, &k_map); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1826 | if (err < 0) |
| 1827 | goto errout; |
| 1828 | |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1829 | status |= DO_SETLINK_NOTIFY; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1830 | } |
| 1831 | |
| 1832 | if (tb[IFLA_ADDRESS]) { |
| 1833 | struct sockaddr *sa; |
| 1834 | int len; |
| 1835 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1836 | len = sizeof(sa_family_t) + dev->addr_len; |
| 1837 | sa = kmalloc(len, GFP_KERNEL); |
| 1838 | if (!sa) { |
| 1839 | err = -ENOMEM; |
| 1840 | goto errout; |
| 1841 | } |
| 1842 | sa->sa_family = dev->type; |
| 1843 | memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), |
| 1844 | dev->addr_len); |
Jiri Pirko | e7c3273 | 2013-01-01 03:30:13 +0000 | [diff] [blame] | 1845 | err = dev_set_mac_address(dev, sa); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1846 | kfree(sa); |
| 1847 | if (err) |
| 1848 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1849 | status |= DO_SETLINK_MODIFIED; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | if (tb[IFLA_MTU]) { |
| 1853 | err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); |
| 1854 | if (err < 0) |
| 1855 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1856 | status |= DO_SETLINK_MODIFIED; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1857 | } |
| 1858 | |
Vlad Dogaru | cbda10f | 2011-01-13 23:38:30 +0000 | [diff] [blame] | 1859 | if (tb[IFLA_GROUP]) { |
| 1860 | dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1861 | status |= DO_SETLINK_NOTIFY; |
Vlad Dogaru | cbda10f | 2011-01-13 23:38:30 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1864 | /* |
| 1865 | * Interface selected by interface index but interface |
| 1866 | * name provided implies that a name change has been |
| 1867 | * requested. |
| 1868 | */ |
| 1869 | if (ifm->ifi_index > 0 && ifname[0]) { |
| 1870 | err = dev_change_name(dev, ifname); |
| 1871 | if (err < 0) |
| 1872 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1873 | status |= DO_SETLINK_MODIFIED; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1874 | } |
| 1875 | |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 1876 | if (tb[IFLA_IFALIAS]) { |
| 1877 | err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), |
| 1878 | nla_len(tb[IFLA_IFALIAS])); |
| 1879 | if (err < 0) |
| 1880 | goto errout; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1881 | status |= DO_SETLINK_NOTIFY; |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 1882 | } |
| 1883 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1884 | if (tb[IFLA_BROADCAST]) { |
| 1885 | nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); |
Jiri Pirko | e7c3273 | 2013-01-01 03:30:13 +0000 | [diff] [blame] | 1886 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | if (ifm->ifi_flags || ifm->ifi_change) { |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 1890 | err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); |
Johannes Berg | 5f9021c | 2008-11-16 23:20:31 -0800 | [diff] [blame] | 1891 | if (err < 0) |
| 1892 | goto errout; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1893 | } |
| 1894 | |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1895 | if (tb[IFLA_MASTER]) { |
| 1896 | err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER])); |
| 1897 | if (err) |
| 1898 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1899 | status |= DO_SETLINK_MODIFIED; |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
Jiri Pirko | 9a57247 | 2012-12-27 23:49:39 +0000 | [diff] [blame] | 1902 | if (tb[IFLA_CARRIER]) { |
| 1903 | err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); |
| 1904 | if (err) |
| 1905 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1906 | status |= DO_SETLINK_MODIFIED; |
Jiri Pirko | 9a57247 | 2012-12-27 23:49:39 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
Nicolas Dichtel | 5d1180f | 2014-09-01 16:07:26 +0200 | [diff] [blame] | 1909 | if (tb[IFLA_TXQLEN]) { |
| 1910 | unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]); |
| 1911 | |
| 1912 | if (dev->tx_queue_len ^ value) |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1913 | status |= DO_SETLINK_NOTIFY; |
Nicolas Dichtel | 5d1180f | 2014-09-01 16:07:26 +0200 | [diff] [blame] | 1914 | |
| 1915 | dev->tx_queue_len = value; |
| 1916 | } |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1917 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1918 | if (tb[IFLA_OPERSTATE]) |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 1919 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1920 | |
| 1921 | if (tb[IFLA_LINKMODE]) { |
Nicolas Dichtel | 1889b0e | 2014-09-01 16:07:27 +0200 | [diff] [blame] | 1922 | unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); |
| 1923 | |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 1924 | write_lock_bh(&dev_base_lock); |
Nicolas Dichtel | 1889b0e | 2014-09-01 16:07:27 +0200 | [diff] [blame] | 1925 | if (dev->link_mode ^ value) |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1926 | status |= DO_SETLINK_NOTIFY; |
Nicolas Dichtel | 1889b0e | 2014-09-01 16:07:27 +0200 | [diff] [blame] | 1927 | dev->link_mode = value; |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 1928 | write_unlock_bh(&dev_base_lock); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1929 | } |
| 1930 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1931 | if (tb[IFLA_VFINFO_LIST]) { |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1932 | struct nlattr *vfinfo[IFLA_VF_MAX + 1]; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1933 | struct nlattr *attr; |
| 1934 | int rem; |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1935 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1936 | nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1937 | if (nla_type(attr) != IFLA_VF_INFO || |
| 1938 | nla_len(attr) < NLA_HDRLEN) { |
David Howells | 253683b | 2010-05-21 02:25:27 +0000 | [diff] [blame] | 1939 | err = -EINVAL; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1940 | goto errout; |
David Howells | 253683b | 2010-05-21 02:25:27 +0000 | [diff] [blame] | 1941 | } |
Daniel Borkmann | 4f7d2cd | 2015-07-07 00:07:52 +0200 | [diff] [blame] | 1942 | err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr, |
| 1943 | ifla_vf_policy); |
| 1944 | if (err < 0) |
| 1945 | goto errout; |
| 1946 | err = do_setvfinfo(dev, vfinfo); |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1947 | if (err < 0) |
| 1948 | goto errout; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1949 | status |= DO_SETLINK_NOTIFY; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1950 | } |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 1951 | } |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1952 | err = 0; |
| 1953 | |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1954 | if (tb[IFLA_VF_PORTS]) { |
| 1955 | struct nlattr *port[IFLA_PORT_MAX+1]; |
| 1956 | struct nlattr *attr; |
| 1957 | int vf; |
| 1958 | int rem; |
| 1959 | |
| 1960 | err = -EOPNOTSUPP; |
| 1961 | if (!ops->ndo_set_vf_port) |
| 1962 | goto errout; |
| 1963 | |
| 1964 | nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { |
Daniel Borkmann | 035d210 | 2015-07-13 00:06:02 +0200 | [diff] [blame] | 1965 | if (nla_type(attr) != IFLA_VF_PORT || |
| 1966 | nla_len(attr) < NLA_HDRLEN) { |
| 1967 | err = -EINVAL; |
| 1968 | goto errout; |
| 1969 | } |
| 1970 | err = nla_parse_nested(port, IFLA_PORT_MAX, attr, |
| 1971 | ifla_port_policy); |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1972 | if (err < 0) |
| 1973 | goto errout; |
| 1974 | if (!port[IFLA_PORT_VF]) { |
| 1975 | err = -EOPNOTSUPP; |
| 1976 | goto errout; |
| 1977 | } |
| 1978 | vf = nla_get_u32(port[IFLA_PORT_VF]); |
| 1979 | err = ops->ndo_set_vf_port(dev, vf, port); |
| 1980 | if (err < 0) |
| 1981 | goto errout; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1982 | status |= DO_SETLINK_NOTIFY; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1983 | } |
| 1984 | } |
| 1985 | err = 0; |
| 1986 | |
| 1987 | if (tb[IFLA_PORT_SELF]) { |
| 1988 | struct nlattr *port[IFLA_PORT_MAX+1]; |
| 1989 | |
| 1990 | err = nla_parse_nested(port, IFLA_PORT_MAX, |
| 1991 | tb[IFLA_PORT_SELF], ifla_port_policy); |
| 1992 | if (err < 0) |
| 1993 | goto errout; |
| 1994 | |
| 1995 | err = -EOPNOTSUPP; |
| 1996 | if (ops->ndo_set_vf_port) |
| 1997 | err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); |
| 1998 | if (err < 0) |
| 1999 | goto errout; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2000 | status |= DO_SETLINK_NOTIFY; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 2001 | } |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 2002 | |
| 2003 | if (tb[IFLA_AF_SPEC]) { |
| 2004 | struct nlattr *af; |
| 2005 | int rem; |
| 2006 | |
| 2007 | nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { |
| 2008 | const struct rtnl_af_ops *af_ops; |
| 2009 | |
| 2010 | if (!(af_ops = rtnl_af_lookup(nla_type(af)))) |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 2011 | BUG(); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 2012 | |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 2013 | err = af_ops->set_link_af(dev, af); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 2014 | if (err < 0) |
| 2015 | goto errout; |
| 2016 | |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2017 | status |= DO_SETLINK_NOTIFY; |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 2018 | } |
| 2019 | } |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 2020 | err = 0; |
| 2021 | |
Anuradha Karuppiah | 88d6378 | 2015-07-14 13:43:20 -0700 | [diff] [blame] | 2022 | if (tb[IFLA_PROTO_DOWN]) { |
| 2023 | err = dev_change_proto_down(dev, |
| 2024 | nla_get_u8(tb[IFLA_PROTO_DOWN])); |
| 2025 | if (err) |
| 2026 | goto errout; |
| 2027 | status |= DO_SETLINK_NOTIFY; |
| 2028 | } |
| 2029 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2030 | errout: |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2031 | if (status & DO_SETLINK_MODIFIED) { |
| 2032 | if (status & DO_SETLINK_NOTIFY) |
| 2033 | netdev_state_change(dev); |
| 2034 | |
| 2035 | if (err < 0) |
| 2036 | 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", |
| 2037 | dev->name); |
| 2038 | } |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2039 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2040 | return err; |
| 2041 | } |
| 2042 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 2043 | static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2045 | struct net *net = sock_net(skb->sk); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2046 | struct ifinfomsg *ifm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | struct net_device *dev; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 2048 | int err; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2049 | struct nlattr *tb[IFLA_MAX+1]; |
| 2050 | char ifname[IFNAMSIZ]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2052 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 2053 | if (err < 0) |
| 2054 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 2056 | if (tb[IFLA_IFNAME]) |
| 2057 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
Patrick McHardy | 78e5b891 | 2006-09-13 20:35:36 -0700 | [diff] [blame] | 2058 | else |
| 2059 | ifname[0] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | err = -EINVAL; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2062 | ifm = nlmsg_data(nlh); |
Patrick McHardy | 51055be | 2007-06-05 12:40:01 -0700 | [diff] [blame] | 2063 | if (ifm->ifi_index > 0) |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2064 | dev = __dev_get_by_index(net, ifm->ifi_index); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2065 | else if (tb[IFLA_IFNAME]) |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2066 | dev = __dev_get_by_name(net, ifname); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2067 | else |
| 2068 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2070 | if (dev == NULL) { |
| 2071 | err = -ENODEV; |
| 2072 | goto errout; |
| 2073 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 2075 | err = validate_linkmsg(dev, tb); |
| 2076 | if (err < 0) |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2077 | goto errout; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2078 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 2079 | err = do_setlink(skb, dev, ifm, tb, ifname, 0); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 2080 | errout: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | return err; |
| 2082 | } |
| 2083 | |
WANG Cong | 66400d5 | 2015-03-24 11:53:31 -0700 | [diff] [blame] | 2084 | static int rtnl_group_dellink(const struct net *net, int group) |
| 2085 | { |
| 2086 | struct net_device *dev, *aux; |
| 2087 | LIST_HEAD(list_kill); |
| 2088 | bool found = false; |
| 2089 | |
| 2090 | if (!group) |
| 2091 | return -EPERM; |
| 2092 | |
| 2093 | for_each_netdev(net, dev) { |
| 2094 | if (dev->group == group) { |
| 2095 | const struct rtnl_link_ops *ops; |
| 2096 | |
| 2097 | found = true; |
| 2098 | ops = dev->rtnl_link_ops; |
| 2099 | if (!ops || !ops->dellink) |
| 2100 | return -EOPNOTSUPP; |
| 2101 | } |
| 2102 | } |
| 2103 | |
| 2104 | if (!found) |
| 2105 | return -ENODEV; |
| 2106 | |
| 2107 | for_each_netdev_safe(net, dev, aux) { |
| 2108 | if (dev->group == group) { |
| 2109 | const struct rtnl_link_ops *ops; |
| 2110 | |
| 2111 | ops = dev->rtnl_link_ops; |
| 2112 | ops->dellink(dev, &list_kill); |
| 2113 | } |
| 2114 | } |
| 2115 | unregister_netdevice_many(&list_kill); |
| 2116 | |
| 2117 | return 0; |
| 2118 | } |
| 2119 | |
Thomas Graf | 614732e | 2015-07-21 10:44:06 +0200 | [diff] [blame] | 2120 | int rtnl_delete_link(struct net_device *dev) |
| 2121 | { |
| 2122 | const struct rtnl_link_ops *ops; |
| 2123 | LIST_HEAD(list_kill); |
| 2124 | |
| 2125 | ops = dev->rtnl_link_ops; |
| 2126 | if (!ops || !ops->dellink) |
| 2127 | return -EOPNOTSUPP; |
| 2128 | |
| 2129 | ops->dellink(dev, &list_kill); |
| 2130 | unregister_netdevice_many(&list_kill); |
| 2131 | |
| 2132 | return 0; |
| 2133 | } |
| 2134 | EXPORT_SYMBOL_GPL(rtnl_delete_link); |
| 2135 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 2136 | static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2137 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2138 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2139 | struct net_device *dev; |
| 2140 | struct ifinfomsg *ifm; |
| 2141 | char ifname[IFNAMSIZ]; |
| 2142 | struct nlattr *tb[IFLA_MAX+1]; |
| 2143 | int err; |
| 2144 | |
| 2145 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 2146 | if (err < 0) |
| 2147 | return err; |
| 2148 | |
| 2149 | if (tb[IFLA_IFNAME]) |
| 2150 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
| 2151 | |
| 2152 | ifm = nlmsg_data(nlh); |
| 2153 | if (ifm->ifi_index > 0) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 2154 | dev = __dev_get_by_index(net, ifm->ifi_index); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2155 | else if (tb[IFLA_IFNAME]) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 2156 | dev = __dev_get_by_name(net, ifname); |
WANG Cong | 66400d5 | 2015-03-24 11:53:31 -0700 | [diff] [blame] | 2157 | else if (tb[IFLA_GROUP]) |
| 2158 | return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP])); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2159 | else |
| 2160 | return -EINVAL; |
| 2161 | |
| 2162 | if (!dev) |
| 2163 | return -ENODEV; |
| 2164 | |
Thomas Graf | 614732e | 2015-07-21 10:44:06 +0200 | [diff] [blame] | 2165 | return rtnl_delete_link(dev); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2166 | } |
| 2167 | |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 2168 | int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) |
| 2169 | { |
| 2170 | unsigned int old_flags; |
| 2171 | int err; |
| 2172 | |
| 2173 | old_flags = dev->flags; |
| 2174 | if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { |
| 2175 | err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); |
| 2176 | if (err < 0) |
| 2177 | return err; |
| 2178 | } |
| 2179 | |
| 2180 | dev->rtnl_link_state = RTNL_LINK_INITIALIZED; |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 2181 | |
Nicolas Dichtel | a528c21 | 2013-09-25 12:02:44 +0200 | [diff] [blame] | 2182 | __dev_notify_flags(dev, old_flags, ~0U); |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 2183 | return 0; |
| 2184 | } |
| 2185 | EXPORT_SYMBOL(rtnl_configure_link); |
| 2186 | |
Rami Rosen | c071356 | 2012-11-30 01:08:47 +0000 | [diff] [blame] | 2187 | struct net_device *rtnl_create_link(struct net *net, |
Thomas Graf | 78ebb0d | 2015-04-10 01:45:53 +0200 | [diff] [blame] | 2188 | const char *ifname, unsigned char name_assign_type, |
Tom Gundersen | 5517750 | 2014-07-14 16:37:25 +0200 | [diff] [blame] | 2189 | const struct rtnl_link_ops *ops, struct nlattr *tb[]) |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2190 | { |
| 2191 | int err; |
| 2192 | struct net_device *dev; |
Jiri Pirko | d40156a | 2012-07-20 02:28:47 +0000 | [diff] [blame] | 2193 | unsigned int num_tx_queues = 1; |
| 2194 | unsigned int num_rx_queues = 1; |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2195 | |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 2196 | if (tb[IFLA_NUM_TX_QUEUES]) |
| 2197 | num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); |
| 2198 | else if (ops->get_num_tx_queues) |
Jiri Pirko | d40156a | 2012-07-20 02:28:47 +0000 | [diff] [blame] | 2199 | num_tx_queues = ops->get_num_tx_queues(); |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 2200 | |
| 2201 | if (tb[IFLA_NUM_RX_QUEUES]) |
| 2202 | num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); |
| 2203 | else if (ops->get_num_rx_queues) |
Jiri Pirko | d40156a | 2012-07-20 02:28:47 +0000 | [diff] [blame] | 2204 | num_rx_queues = ops->get_num_rx_queues(); |
stephen hemminger | efacb30 | 2012-04-10 18:34:43 +0000 | [diff] [blame] | 2205 | |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2206 | err = -ENOMEM; |
Tom Gundersen | 5517750 | 2014-07-14 16:37:25 +0200 | [diff] [blame] | 2207 | dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type, |
Tom Gundersen | c835a67 | 2014-07-14 16:37:24 +0200 | [diff] [blame] | 2208 | ops->setup, num_tx_queues, num_rx_queues); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2209 | if (!dev) |
| 2210 | goto err; |
| 2211 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 2212 | dev_net_set(dev, net); |
| 2213 | dev->rtnl_link_ops = ops; |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 2214 | dev->rtnl_link_state = RTNL_LINK_INITIALIZING; |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 2215 | |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2216 | if (tb[IFLA_MTU]) |
| 2217 | dev->mtu = nla_get_u32(tb[IFLA_MTU]); |
Jiri Pirko | 2afb9b5 | 2013-01-06 12:41:57 +0000 | [diff] [blame] | 2218 | if (tb[IFLA_ADDRESS]) { |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2219 | memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), |
| 2220 | nla_len(tb[IFLA_ADDRESS])); |
Jiri Pirko | 2afb9b5 | 2013-01-06 12:41:57 +0000 | [diff] [blame] | 2221 | dev->addr_assign_type = NET_ADDR_SET; |
| 2222 | } |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2223 | if (tb[IFLA_BROADCAST]) |
| 2224 | memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), |
| 2225 | nla_len(tb[IFLA_BROADCAST])); |
| 2226 | if (tb[IFLA_TXQLEN]) |
| 2227 | dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); |
| 2228 | if (tb[IFLA_OPERSTATE]) |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 2229 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2230 | if (tb[IFLA_LINKMODE]) |
| 2231 | dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); |
Patrick McHardy | ffa934f | 2011-01-20 03:00:42 +0000 | [diff] [blame] | 2232 | if (tb[IFLA_GROUP]) |
| 2233 | dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2234 | |
| 2235 | return dev; |
| 2236 | |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2237 | err: |
| 2238 | return ERR_PTR(err); |
| 2239 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 2240 | EXPORT_SYMBOL(rtnl_create_link); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2241 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 2242 | static int rtnl_group_changelink(const struct sk_buff *skb, |
| 2243 | struct net *net, int group, |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 2244 | struct ifinfomsg *ifm, |
| 2245 | struct nlattr **tb) |
| 2246 | { |
WANG Cong | d079535 | 2015-03-23 16:31:09 -0700 | [diff] [blame] | 2247 | struct net_device *dev, *aux; |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 2248 | int err; |
| 2249 | |
WANG Cong | d079535 | 2015-03-23 16:31:09 -0700 | [diff] [blame] | 2250 | for_each_netdev_safe(net, dev, aux) { |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 2251 | if (dev->group == group) { |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 2252 | err = do_setlink(skb, dev, ifm, tb, NULL, 0); |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 2253 | if (err < 0) |
| 2254 | return err; |
| 2255 | } |
| 2256 | } |
| 2257 | |
| 2258 | return 0; |
| 2259 | } |
| 2260 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 2261 | static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2262 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2263 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2264 | const struct rtnl_link_ops *ops; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 2265 | const struct rtnl_link_ops *m_ops = NULL; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2266 | struct net_device *dev; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 2267 | struct net_device *master_dev = NULL; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2268 | struct ifinfomsg *ifm; |
| 2269 | char kind[MODULE_NAME_LEN]; |
| 2270 | char ifname[IFNAMSIZ]; |
| 2271 | struct nlattr *tb[IFLA_MAX+1]; |
| 2272 | struct nlattr *linkinfo[IFLA_INFO_MAX+1]; |
Tom Gundersen | 5517750 | 2014-07-14 16:37:25 +0200 | [diff] [blame] | 2273 | unsigned char name_assign_type = NET_NAME_USER; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2274 | int err; |
| 2275 | |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 2276 | #ifdef CONFIG_MODULES |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2277 | replay: |
Thomas Graf | 8072f08 | 2007-07-31 14:13:50 -0700 | [diff] [blame] | 2278 | #endif |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2279 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 2280 | if (err < 0) |
| 2281 | return err; |
| 2282 | |
| 2283 | if (tb[IFLA_IFNAME]) |
| 2284 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
| 2285 | else |
| 2286 | ifname[0] = '\0'; |
| 2287 | |
| 2288 | ifm = nlmsg_data(nlh); |
| 2289 | if (ifm->ifi_index > 0) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 2290 | dev = __dev_get_by_index(net, ifm->ifi_index); |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 2291 | else { |
| 2292 | if (ifname[0]) |
| 2293 | dev = __dev_get_by_name(net, ifname); |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 2294 | else |
| 2295 | dev = NULL; |
| 2296 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2297 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 2298 | if (dev) { |
| 2299 | master_dev = netdev_master_upper_dev_get(dev); |
| 2300 | if (master_dev) |
| 2301 | m_ops = master_dev->rtnl_link_ops; |
| 2302 | } |
| 2303 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 2304 | err = validate_linkmsg(dev, tb); |
| 2305 | if (err < 0) |
Thomas Graf | 1840bb1 | 2008-02-23 19:54:36 -0800 | [diff] [blame] | 2306 | return err; |
| 2307 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2308 | if (tb[IFLA_LINKINFO]) { |
| 2309 | err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, |
| 2310 | tb[IFLA_LINKINFO], ifla_info_policy); |
| 2311 | if (err < 0) |
| 2312 | return err; |
| 2313 | } else |
| 2314 | memset(linkinfo, 0, sizeof(linkinfo)); |
| 2315 | |
| 2316 | if (linkinfo[IFLA_INFO_KIND]) { |
| 2317 | nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); |
| 2318 | ops = rtnl_link_ops_get(kind); |
| 2319 | } else { |
| 2320 | kind[0] = '\0'; |
| 2321 | ops = NULL; |
| 2322 | } |
| 2323 | |
| 2324 | if (1) { |
Sasha Levin | 4e10fd5 | 2015-02-24 14:14:35 -0500 | [diff] [blame] | 2325 | struct nlattr *attr[ops ? ops->maxtype + 1 : 1]; |
| 2326 | struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1]; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 2327 | struct nlattr **data = NULL; |
| 2328 | struct nlattr **slave_data = NULL; |
Nicolas Dichtel | 317f481 | 2015-01-15 15:11:18 +0100 | [diff] [blame] | 2329 | struct net *dest_net, *link_net = NULL; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2330 | |
| 2331 | if (ops) { |
| 2332 | if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { |
| 2333 | err = nla_parse_nested(attr, ops->maxtype, |
| 2334 | linkinfo[IFLA_INFO_DATA], |
| 2335 | ops->policy); |
| 2336 | if (err < 0) |
| 2337 | return err; |
| 2338 | data = attr; |
| 2339 | } |
| 2340 | if (ops->validate) { |
| 2341 | err = ops->validate(tb, data); |
| 2342 | if (err < 0) |
| 2343 | return err; |
| 2344 | } |
| 2345 | } |
| 2346 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 2347 | if (m_ops) { |
| 2348 | if (m_ops->slave_maxtype && |
| 2349 | linkinfo[IFLA_INFO_SLAVE_DATA]) { |
| 2350 | err = nla_parse_nested(slave_attr, |
| 2351 | m_ops->slave_maxtype, |
| 2352 | linkinfo[IFLA_INFO_SLAVE_DATA], |
| 2353 | m_ops->slave_policy); |
| 2354 | if (err < 0) |
| 2355 | return err; |
| 2356 | slave_data = slave_attr; |
| 2357 | } |
| 2358 | if (m_ops->slave_validate) { |
| 2359 | err = m_ops->slave_validate(tb, slave_data); |
| 2360 | if (err < 0) |
| 2361 | return err; |
| 2362 | } |
| 2363 | } |
| 2364 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2365 | if (dev) { |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 2366 | int status = 0; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2367 | |
| 2368 | if (nlh->nlmsg_flags & NLM_F_EXCL) |
| 2369 | return -EEXIST; |
| 2370 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
| 2371 | return -EOPNOTSUPP; |
| 2372 | |
| 2373 | if (linkinfo[IFLA_INFO_DATA]) { |
| 2374 | if (!ops || ops != dev->rtnl_link_ops || |
| 2375 | !ops->changelink) |
| 2376 | return -EOPNOTSUPP; |
| 2377 | |
| 2378 | err = ops->changelink(dev, tb, data); |
| 2379 | if (err < 0) |
| 2380 | return err; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2381 | status |= DO_SETLINK_NOTIFY; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2382 | } |
| 2383 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 2384 | if (linkinfo[IFLA_INFO_SLAVE_DATA]) { |
| 2385 | if (!m_ops || !m_ops->slave_changelink) |
| 2386 | return -EOPNOTSUPP; |
| 2387 | |
| 2388 | err = m_ops->slave_changelink(master_dev, dev, |
| 2389 | tb, slave_data); |
| 2390 | if (err < 0) |
| 2391 | return err; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2392 | status |= DO_SETLINK_NOTIFY; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 2393 | } |
| 2394 | |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 2395 | return do_setlink(skb, dev, ifm, tb, ifname, status); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2396 | } |
| 2397 | |
Patrick McHardy | ffa934f | 2011-01-20 03:00:42 +0000 | [diff] [blame] | 2398 | if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { |
| 2399 | if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 2400 | return rtnl_group_changelink(skb, net, |
Patrick McHardy | ffa934f | 2011-01-20 03:00:42 +0000 | [diff] [blame] | 2401 | nla_get_u32(tb[IFLA_GROUP]), |
| 2402 | ifm, tb); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2403 | return -ENODEV; |
Patrick McHardy | ffa934f | 2011-01-20 03:00:42 +0000 | [diff] [blame] | 2404 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2405 | |
Patrick McHardy | 0e06877 | 2007-07-11 19:42:31 -0700 | [diff] [blame] | 2406 | if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO]) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2407 | return -EOPNOTSUPP; |
| 2408 | |
| 2409 | if (!ops) { |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 2410 | #ifdef CONFIG_MODULES |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2411 | if (kind[0]) { |
| 2412 | __rtnl_unlock(); |
| 2413 | request_module("rtnl-link-%s", kind); |
| 2414 | rtnl_lock(); |
| 2415 | ops = rtnl_link_ops_get(kind); |
| 2416 | if (ops) |
| 2417 | goto replay; |
| 2418 | } |
| 2419 | #endif |
| 2420 | return -EOPNOTSUPP; |
| 2421 | } |
| 2422 | |
Jiri Pirko | b0ab2fa | 2014-06-26 09:58:25 +0200 | [diff] [blame] | 2423 | if (!ops->setup) |
| 2424 | return -EOPNOTSUPP; |
| 2425 | |
Tom Gundersen | 5517750 | 2014-07-14 16:37:25 +0200 | [diff] [blame] | 2426 | if (!ifname[0]) { |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2427 | snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); |
Tom Gundersen | 5517750 | 2014-07-14 16:37:25 +0200 | [diff] [blame] | 2428 | name_assign_type = NET_NAME_ENUM; |
| 2429 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2430 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 2431 | dest_net = rtnl_link_get_net(net, tb); |
Eric W. Biederman | 13ad177 | 2011-01-29 14:57:22 +0000 | [diff] [blame] | 2432 | if (IS_ERR(dest_net)) |
| 2433 | return PTR_ERR(dest_net); |
| 2434 | |
Eric W. Biederman | 505ce41 | 2015-02-26 16:19:00 -0600 | [diff] [blame] | 2435 | err = -EPERM; |
| 2436 | if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN)) |
| 2437 | goto out; |
| 2438 | |
Nicolas Dichtel | 317f481 | 2015-01-15 15:11:18 +0100 | [diff] [blame] | 2439 | if (tb[IFLA_LINK_NETNSID]) { |
| 2440 | int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); |
| 2441 | |
| 2442 | link_net = get_net_ns_by_id(dest_net, id); |
| 2443 | if (!link_net) { |
| 2444 | err = -EINVAL; |
| 2445 | goto out; |
| 2446 | } |
Eric W. Biederman | 06615be | 2015-02-26 16:20:07 -0600 | [diff] [blame] | 2447 | err = -EPERM; |
| 2448 | if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) |
| 2449 | goto out; |
Nicolas Dichtel | 317f481 | 2015-01-15 15:11:18 +0100 | [diff] [blame] | 2450 | } |
| 2451 | |
| 2452 | dev = rtnl_create_link(link_net ? : dest_net, ifname, |
| 2453 | name_assign_type, ops, tb); |
Pavel Emelyanov | 9c7dafb | 2012-08-08 21:52:46 +0000 | [diff] [blame] | 2454 | if (IS_ERR(dev)) { |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2455 | err = PTR_ERR(dev); |
Pavel Emelyanov | 9c7dafb | 2012-08-08 21:52:46 +0000 | [diff] [blame] | 2456 | goto out; |
| 2457 | } |
| 2458 | |
| 2459 | dev->ifindex = ifm->ifi_index; |
| 2460 | |
Cong Wang | 0e0eee2 | 2014-02-11 15:51:30 -0800 | [diff] [blame] | 2461 | if (ops->newlink) { |
Nicolas Dichtel | 7b4ce69 | 2015-01-27 11:13:08 +0100 | [diff] [blame] | 2462 | err = ops->newlink(link_net ? : net, dev, tb, data); |
Cong Wang | 0e0eee2 | 2014-02-11 15:51:30 -0800 | [diff] [blame] | 2463 | /* Drivers should call free_netdev() in ->destructor |
Cong Wang | e51fb15 | 2014-06-03 16:40:47 -0700 | [diff] [blame] | 2464 | * and unregister it on failure after registration |
| 2465 | * so that device could be finally freed in rtnl_unlock. |
Cong Wang | 0e0eee2 | 2014-02-11 15:51:30 -0800 | [diff] [blame] | 2466 | */ |
Cong Wang | e51fb15 | 2014-06-03 16:40:47 -0700 | [diff] [blame] | 2467 | if (err < 0) { |
| 2468 | /* If device is not registered at all, free it now */ |
| 2469 | if (dev->reg_state == NETREG_UNINITIALIZED) |
| 2470 | free_netdev(dev); |
Cong Wang | 0e0eee2 | 2014-02-11 15:51:30 -0800 | [diff] [blame] | 2471 | goto out; |
Cong Wang | e51fb15 | 2014-06-03 16:40:47 -0700 | [diff] [blame] | 2472 | } |
Cong Wang | 0e0eee2 | 2014-02-11 15:51:30 -0800 | [diff] [blame] | 2473 | } else { |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 2474 | err = register_netdevice(dev); |
Cong Wang | 0e0eee2 | 2014-02-11 15:51:30 -0800 | [diff] [blame] | 2475 | if (err < 0) { |
| 2476 | free_netdev(dev); |
| 2477 | goto out; |
| 2478 | } |
Dan Carpenter | fce9b9b | 2013-08-14 12:35:42 +0300 | [diff] [blame] | 2479 | } |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 2480 | err = rtnl_configure_link(dev, ifm); |
David S. Miller | 4363890 | 2015-03-10 21:58:32 -0400 | [diff] [blame] | 2481 | if (err < 0) |
| 2482 | goto out_unregister; |
Nicolas Dichtel | bdef279 | 2015-01-20 15:15:42 +0100 | [diff] [blame] | 2483 | if (link_net) { |
Nicolas Dichtel | 317f481 | 2015-01-15 15:11:18 +0100 | [diff] [blame] | 2484 | err = dev_change_net_namespace(dev, dest_net, ifname); |
Nicolas Dichtel | bdef279 | 2015-01-20 15:15:42 +0100 | [diff] [blame] | 2485 | if (err < 0) |
David S. Miller | 4363890 | 2015-03-10 21:58:32 -0400 | [diff] [blame] | 2486 | goto out_unregister; |
Nicolas Dichtel | bdef279 | 2015-01-20 15:15:42 +0100 | [diff] [blame] | 2487 | } |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 2488 | out: |
Nicolas Dichtel | 317f481 | 2015-01-15 15:11:18 +0100 | [diff] [blame] | 2489 | if (link_net) |
| 2490 | put_net(link_net); |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 2491 | put_net(dest_net); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2492 | return err; |
David S. Miller | 4363890 | 2015-03-10 21:58:32 -0400 | [diff] [blame] | 2493 | out_unregister: |
| 2494 | if (ops->newlink) { |
| 2495 | LIST_HEAD(list_kill); |
| 2496 | |
| 2497 | ops->dellink(dev, &list_kill); |
| 2498 | unregister_netdevice_many(&list_kill); |
| 2499 | } else { |
| 2500 | unregister_netdevice(dev); |
| 2501 | } |
| 2502 | goto out; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2503 | } |
| 2504 | } |
| 2505 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 2506 | static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh) |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2507 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2508 | struct net *net = sock_net(skb->sk); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2509 | struct ifinfomsg *ifm; |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2510 | char ifname[IFNAMSIZ]; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2511 | struct nlattr *tb[IFLA_MAX+1]; |
| 2512 | struct net_device *dev = NULL; |
| 2513 | struct sk_buff *nskb; |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 2514 | int err; |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2515 | u32 ext_filter_mask = 0; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2516 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2517 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 2518 | if (err < 0) |
Eric Sesterhenn | 9918f23 | 2006-09-26 23:26:38 -0700 | [diff] [blame] | 2519 | return err; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2520 | |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2521 | if (tb[IFLA_IFNAME]) |
| 2522 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
| 2523 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2524 | if (tb[IFLA_EXT_MASK]) |
| 2525 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); |
| 2526 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2527 | ifm = nlmsg_data(nlh); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2528 | if (ifm->ifi_index > 0) |
| 2529 | dev = __dev_get_by_index(net, ifm->ifi_index); |
| 2530 | else if (tb[IFLA_IFNAME]) |
| 2531 | dev = __dev_get_by_name(net, ifname); |
| 2532 | else |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2533 | return -EINVAL; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2534 | |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2535 | if (dev == NULL) |
| 2536 | return -ENODEV; |
| 2537 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2538 | nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2539 | if (nskb == NULL) |
| 2540 | return -ENOBUFS; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2541 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2542 | err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid, |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2543 | nlh->nlmsg_seq, 0, 0, ext_filter_mask); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 2544 | if (err < 0) { |
| 2545 | /* -EMSGSIZE implies BUG in if_nlmsg_size */ |
| 2546 | WARN_ON(err == -EMSGSIZE); |
| 2547 | kfree_skb(nskb); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2548 | } else |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2549 | err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2550 | |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2551 | return err; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2552 | } |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2553 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2554 | static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2555 | { |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2556 | struct net *net = sock_net(skb->sk); |
| 2557 | struct net_device *dev; |
| 2558 | struct nlattr *tb[IFLA_MAX+1]; |
| 2559 | u32 ext_filter_mask = 0; |
| 2560 | u16 min_ifinfo_dump_size = 0; |
Michal Schmidt | e5eca6d | 2014-05-28 14:15:19 +0200 | [diff] [blame] | 2561 | int hdrlen; |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2562 | |
Michal Schmidt | e5eca6d | 2014-05-28 14:15:19 +0200 | [diff] [blame] | 2563 | /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ |
| 2564 | hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? |
| 2565 | sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); |
| 2566 | |
| 2567 | if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) { |
Eric Dumazet | a4b64fb | 2012-03-04 12:32:10 +0000 | [diff] [blame] | 2568 | if (tb[IFLA_EXT_MASK]) |
| 2569 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); |
| 2570 | } |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2571 | |
| 2572 | if (!ext_filter_mask) |
| 2573 | return NLMSG_GOODSIZE; |
| 2574 | /* |
| 2575 | * traverse the list of net devices and compute the minimum |
| 2576 | * buffer size based upon the filter mask. |
| 2577 | */ |
| 2578 | list_for_each_entry(dev, &net->dev_base_head, dev_list) { |
| 2579 | min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size, |
| 2580 | if_nlmsg_size(dev, |
| 2581 | ext_filter_mask)); |
| 2582 | } |
| 2583 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2584 | return min_ifinfo_dump_size; |
| 2585 | } |
| 2586 | |
Adrian Bunk | 42bad1d | 2007-04-26 00:57:41 -0700 | [diff] [blame] | 2587 | 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] | 2588 | { |
| 2589 | int idx; |
| 2590 | int s_idx = cb->family; |
| 2591 | |
| 2592 | if (s_idx == 0) |
| 2593 | s_idx = 1; |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 2594 | for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2595 | int type = cb->nlh->nlmsg_type-RTM_BASE; |
| 2596 | if (idx < s_idx || idx == PF_PACKET) |
| 2597 | continue; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 2598 | if (rtnl_msg_handlers[idx] == NULL || |
| 2599 | rtnl_msg_handlers[idx][type].dumpit == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2600 | continue; |
Nicolas Dichtel | 0465277 | 2013-03-22 06:28:42 +0000 | [diff] [blame] | 2601 | if (idx > s_idx) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | memset(&cb->args[0], 0, sizeof(cb->args)); |
Nicolas Dichtel | 0465277 | 2013-03-22 06:28:42 +0000 | [diff] [blame] | 2603 | cb->prev_seq = 0; |
| 2604 | cb->seq = 0; |
| 2605 | } |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 2606 | if (rtnl_msg_handlers[idx][type].dumpit(skb, cb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2607 | break; |
| 2608 | } |
| 2609 | cb->family = idx; |
| 2610 | |
| 2611 | return skb->len; |
| 2612 | } |
| 2613 | |
Mahesh Bandewar | 395eea6 | 2014-12-03 13:46:24 -0800 | [diff] [blame] | 2614 | struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, |
| 2615 | unsigned int change, gfp_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2616 | { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2617 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2618 | struct sk_buff *skb; |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 2619 | int err = -ENOBUFS; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2620 | size_t if_info_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | |
Alexei Starovoitov | 7f29405 | 2013-10-23 16:02:42 -0700 | [diff] [blame] | 2622 | skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags); |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 2623 | if (skb == NULL) |
| 2624 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2625 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2626 | err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 2627 | if (err < 0) { |
| 2628 | /* -EMSGSIZE implies BUG in if_nlmsg_size() */ |
| 2629 | WARN_ON(err == -EMSGSIZE); |
| 2630 | kfree_skb(skb); |
| 2631 | goto errout; |
| 2632 | } |
Mahesh Bandewar | 395eea6 | 2014-12-03 13:46:24 -0800 | [diff] [blame] | 2633 | return skb; |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 2634 | errout: |
| 2635 | if (err < 0) |
Eric W. Biederman | 4b3da70 | 2007-11-19 22:27:40 -0800 | [diff] [blame] | 2636 | rtnl_set_sk_err(net, RTNLGRP_LINK, err); |
Mahesh Bandewar | 395eea6 | 2014-12-03 13:46:24 -0800 | [diff] [blame] | 2637 | return NULL; |
| 2638 | } |
| 2639 | |
| 2640 | void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags) |
| 2641 | { |
| 2642 | struct net *net = dev_net(dev); |
| 2643 | |
| 2644 | rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); |
| 2645 | } |
| 2646 | |
| 2647 | void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, |
| 2648 | gfp_t flags) |
| 2649 | { |
| 2650 | struct sk_buff *skb; |
| 2651 | |
Nicolas Dichtel | ed2a80a | 2015-05-13 14:19:42 +0200 | [diff] [blame] | 2652 | if (dev->reg_state != NETREG_REGISTERED) |
| 2653 | return; |
| 2654 | |
Mahesh Bandewar | 395eea6 | 2014-12-03 13:46:24 -0800 | [diff] [blame] | 2655 | skb = rtmsg_ifinfo_build_skb(type, dev, change, flags); |
| 2656 | if (skb) |
| 2657 | rtmsg_ifinfo_send(skb, dev, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2658 | } |
Jiri Pirko | 471cb5a | 2013-01-03 22:49:01 +0000 | [diff] [blame] | 2659 | EXPORT_SYMBOL(rtmsg_ifinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2660 | |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2661 | static int nlmsg_populate_fdb_fill(struct sk_buff *skb, |
| 2662 | struct net_device *dev, |
Hubert Sokolowski | 1e53d5b | 2015-04-09 12:16:17 +0000 | [diff] [blame] | 2663 | u8 *addr, u16 vid, u32 pid, u32 seq, |
Nicolas Dichtel | 1c104a6 | 2014-03-19 17:47:49 +0100 | [diff] [blame] | 2664 | int type, unsigned int flags, |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 2665 | int nlflags, u16 ndm_state) |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2666 | { |
| 2667 | struct nlmsghdr *nlh; |
| 2668 | struct ndmsg *ndm; |
| 2669 | |
Nicolas Dichtel | 1c104a6 | 2014-03-19 17:47:49 +0100 | [diff] [blame] | 2670 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2671 | if (!nlh) |
| 2672 | return -EMSGSIZE; |
| 2673 | |
| 2674 | ndm = nlmsg_data(nlh); |
| 2675 | ndm->ndm_family = AF_BRIDGE; |
| 2676 | ndm->ndm_pad1 = 0; |
| 2677 | ndm->ndm_pad2 = 0; |
| 2678 | ndm->ndm_flags = flags; |
| 2679 | ndm->ndm_type = 0; |
| 2680 | ndm->ndm_ifindex = dev->ifindex; |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 2681 | ndm->ndm_state = ndm_state; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2682 | |
| 2683 | if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) |
| 2684 | goto nla_put_failure; |
Hubert Sokolowski | 1e53d5b | 2015-04-09 12:16:17 +0000 | [diff] [blame] | 2685 | if (vid) |
| 2686 | if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) |
| 2687 | goto nla_put_failure; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2688 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 2689 | nlmsg_end(skb, nlh); |
| 2690 | return 0; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2691 | |
| 2692 | nla_put_failure: |
| 2693 | nlmsg_cancel(skb, nlh); |
| 2694 | return -EMSGSIZE; |
| 2695 | } |
| 2696 | |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2697 | static inline size_t rtnl_fdb_nlmsg_size(void) |
| 2698 | { |
| 2699 | return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN); |
| 2700 | } |
| 2701 | |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 2702 | static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, |
| 2703 | u16 ndm_state) |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2704 | { |
| 2705 | struct net *net = dev_net(dev); |
| 2706 | struct sk_buff *skb; |
| 2707 | int err = -ENOBUFS; |
| 2708 | |
| 2709 | skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); |
| 2710 | if (!skb) |
| 2711 | goto errout; |
| 2712 | |
Hubert Sokolowski | 1e53d5b | 2015-04-09 12:16:17 +0000 | [diff] [blame] | 2713 | err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 2714 | 0, 0, type, NTF_SELF, 0, ndm_state); |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2715 | if (err < 0) { |
| 2716 | kfree_skb(skb); |
| 2717 | goto errout; |
| 2718 | } |
| 2719 | |
| 2720 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); |
| 2721 | return; |
| 2722 | errout: |
| 2723 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); |
| 2724 | } |
| 2725 | |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2726 | /** |
| 2727 | * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry |
| 2728 | */ |
| 2729 | int ndo_dflt_fdb_add(struct ndmsg *ndm, |
| 2730 | struct nlattr *tb[], |
| 2731 | struct net_device *dev, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2732 | const unsigned char *addr, u16 vid, |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2733 | u16 flags) |
| 2734 | { |
| 2735 | int err = -EINVAL; |
| 2736 | |
| 2737 | /* If aging addresses are supported device will need to |
| 2738 | * implement its own handler for this. |
| 2739 | */ |
| 2740 | if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { |
| 2741 | pr_info("%s: FDB only supports static addresses\n", dev->name); |
| 2742 | return err; |
| 2743 | } |
| 2744 | |
Or Gerlitz | 65891fe | 2014-12-14 18:19:05 +0200 | [diff] [blame] | 2745 | if (vid) { |
| 2746 | pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name); |
| 2747 | return err; |
| 2748 | } |
| 2749 | |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2750 | if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) |
| 2751 | err = dev_uc_add_excl(dev, addr); |
| 2752 | else if (is_multicast_ether_addr(addr)) |
| 2753 | err = dev_mc_add_excl(dev, addr); |
| 2754 | |
| 2755 | /* Only return duplicate errors if NLM_F_EXCL is set */ |
| 2756 | if (err == -EEXIST && !(flags & NLM_F_EXCL)) |
| 2757 | err = 0; |
| 2758 | |
| 2759 | return err; |
| 2760 | } |
| 2761 | EXPORT_SYMBOL(ndo_dflt_fdb_add); |
| 2762 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2763 | static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid) |
| 2764 | { |
| 2765 | u16 vid = 0; |
| 2766 | |
| 2767 | if (vlan_attr) { |
| 2768 | if (nla_len(vlan_attr) != sizeof(u16)) { |
| 2769 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n"); |
| 2770 | return -EINVAL; |
| 2771 | } |
| 2772 | |
| 2773 | vid = nla_get_u16(vlan_attr); |
| 2774 | |
| 2775 | if (!vid || vid >= VLAN_VID_MASK) { |
| 2776 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n", |
| 2777 | vid); |
| 2778 | return -EINVAL; |
| 2779 | } |
| 2780 | } |
| 2781 | *p_vid = vid; |
| 2782 | return 0; |
| 2783 | } |
| 2784 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 2785 | static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh) |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2786 | { |
| 2787 | struct net *net = sock_net(skb->sk); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2788 | struct ndmsg *ndm; |
| 2789 | struct nlattr *tb[NDA_MAX+1]; |
| 2790 | struct net_device *dev; |
| 2791 | u8 *addr; |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2792 | u16 vid; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2793 | int err; |
| 2794 | |
| 2795 | err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL); |
| 2796 | if (err < 0) |
| 2797 | return err; |
| 2798 | |
| 2799 | ndm = nlmsg_data(nlh); |
| 2800 | if (ndm->ndm_ifindex == 0) { |
| 2801 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n"); |
| 2802 | return -EINVAL; |
| 2803 | } |
| 2804 | |
| 2805 | dev = __dev_get_by_index(net, ndm->ndm_ifindex); |
| 2806 | if (dev == NULL) { |
| 2807 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n"); |
| 2808 | return -ENODEV; |
| 2809 | } |
| 2810 | |
| 2811 | if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { |
| 2812 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n"); |
| 2813 | return -EINVAL; |
| 2814 | } |
| 2815 | |
| 2816 | addr = nla_data(tb[NDA_LLADDR]); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2817 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2818 | err = fdb_vid_parse(tb[NDA_VLAN], &vid); |
| 2819 | if (err) |
| 2820 | return err; |
| 2821 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2822 | err = -EOPNOTSUPP; |
| 2823 | |
| 2824 | /* Support fdb on master device the net/bridge default case */ |
| 2825 | if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && |
| 2826 | (dev->priv_flags & IFF_BRIDGE_PORT)) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2827 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
| 2828 | const struct net_device_ops *ops = br_dev->netdev_ops; |
| 2829 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2830 | err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, |
| 2831 | nlh->nlmsg_flags); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2832 | if (err) |
| 2833 | goto out; |
| 2834 | else |
| 2835 | ndm->ndm_flags &= ~NTF_MASTER; |
| 2836 | } |
| 2837 | |
| 2838 | /* Embedded bridge, macvlan, and any other device support */ |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2839 | if ((ndm->ndm_flags & NTF_SELF)) { |
| 2840 | if (dev->netdev_ops->ndo_fdb_add) |
| 2841 | err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2842 | vid, |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2843 | nlh->nlmsg_flags); |
| 2844 | else |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2845 | err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2846 | nlh->nlmsg_flags); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2847 | |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2848 | if (!err) { |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 2849 | rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, |
| 2850 | ndm->ndm_state); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2851 | ndm->ndm_flags &= ~NTF_SELF; |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2852 | } |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2853 | } |
| 2854 | out: |
| 2855 | return err; |
| 2856 | } |
| 2857 | |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2858 | /** |
| 2859 | * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry |
| 2860 | */ |
| 2861 | int ndo_dflt_fdb_del(struct ndmsg *ndm, |
| 2862 | struct nlattr *tb[], |
| 2863 | struct net_device *dev, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2864 | const unsigned char *addr, u16 vid) |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2865 | { |
Alexander Duyck | c8a89c4 | 2014-07-15 15:15:20 -0700 | [diff] [blame] | 2866 | int err = -EINVAL; |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2867 | |
| 2868 | /* If aging addresses are supported device will need to |
| 2869 | * implement its own handler for this. |
| 2870 | */ |
Sridhar Samudrala | 6453599 | 2013-08-08 15:19:48 -0700 | [diff] [blame] | 2871 | if (!(ndm->ndm_state & NUD_PERMANENT)) { |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2872 | pr_info("%s: FDB only supports static addresses\n", dev->name); |
Alexander Duyck | c8a89c4 | 2014-07-15 15:15:20 -0700 | [diff] [blame] | 2873 | return err; |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2874 | } |
| 2875 | |
| 2876 | if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) |
| 2877 | err = dev_uc_del(dev, addr); |
| 2878 | else if (is_multicast_ether_addr(addr)) |
| 2879 | err = dev_mc_del(dev, addr); |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2880 | |
| 2881 | return err; |
| 2882 | } |
| 2883 | EXPORT_SYMBOL(ndo_dflt_fdb_del); |
| 2884 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 2885 | static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh) |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2886 | { |
| 2887 | struct net *net = sock_net(skb->sk); |
| 2888 | struct ndmsg *ndm; |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 2889 | struct nlattr *tb[NDA_MAX+1]; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2890 | struct net_device *dev; |
| 2891 | int err = -EINVAL; |
| 2892 | __u8 *addr; |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2893 | u16 vid; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2894 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 2895 | if (!netlink_capable(skb, CAP_NET_ADMIN)) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 2896 | return -EPERM; |
| 2897 | |
| 2898 | err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL); |
| 2899 | if (err < 0) |
| 2900 | return err; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2901 | |
| 2902 | ndm = nlmsg_data(nlh); |
| 2903 | if (ndm->ndm_ifindex == 0) { |
| 2904 | pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n"); |
| 2905 | return -EINVAL; |
| 2906 | } |
| 2907 | |
| 2908 | dev = __dev_get_by_index(net, ndm->ndm_ifindex); |
| 2909 | if (dev == NULL) { |
| 2910 | pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n"); |
| 2911 | return -ENODEV; |
| 2912 | } |
| 2913 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 2914 | if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { |
| 2915 | pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n"); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2916 | return -EINVAL; |
| 2917 | } |
| 2918 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 2919 | addr = nla_data(tb[NDA_LLADDR]); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 2920 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2921 | err = fdb_vid_parse(tb[NDA_VLAN], &vid); |
| 2922 | if (err) |
| 2923 | return err; |
| 2924 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2925 | err = -EOPNOTSUPP; |
| 2926 | |
| 2927 | /* Support fdb on master device the net/bridge default case */ |
| 2928 | if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && |
| 2929 | (dev->priv_flags & IFF_BRIDGE_PORT)) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2930 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
| 2931 | const struct net_device_ops *ops = br_dev->netdev_ops; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2932 | |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2933 | if (ops->ndo_fdb_del) |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2934 | err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2935 | |
| 2936 | if (err) |
| 2937 | goto out; |
| 2938 | else |
| 2939 | ndm->ndm_flags &= ~NTF_MASTER; |
| 2940 | } |
| 2941 | |
| 2942 | /* Embedded bridge, macvlan, and any other device support */ |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2943 | if (ndm->ndm_flags & NTF_SELF) { |
| 2944 | if (dev->netdev_ops->ndo_fdb_del) |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2945 | err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, |
| 2946 | vid); |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2947 | else |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2948 | err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2949 | |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2950 | if (!err) { |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 2951 | rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, |
| 2952 | ndm->ndm_state); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2953 | ndm->ndm_flags &= ~NTF_SELF; |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2954 | } |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2955 | } |
| 2956 | out: |
| 2957 | return err; |
| 2958 | } |
| 2959 | |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2960 | static int nlmsg_populate_fdb(struct sk_buff *skb, |
| 2961 | struct netlink_callback *cb, |
| 2962 | struct net_device *dev, |
| 2963 | int *idx, |
| 2964 | struct netdev_hw_addr_list *list) |
| 2965 | { |
| 2966 | struct netdev_hw_addr *ha; |
| 2967 | int err; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2968 | u32 portid, seq; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2969 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2970 | portid = NETLINK_CB(cb->skb).portid; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2971 | seq = cb->nlh->nlmsg_seq; |
| 2972 | |
| 2973 | list_for_each_entry(ha, &list->list, list) { |
| 2974 | if (*idx < cb->args[0]) |
| 2975 | goto skip; |
| 2976 | |
Hubert Sokolowski | 1e53d5b | 2015-04-09 12:16:17 +0000 | [diff] [blame] | 2977 | err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, |
John Fastabend | a7a558f | 2012-11-01 16:23:10 +0000 | [diff] [blame] | 2978 | portid, seq, |
Nicolas Dichtel | 1c104a6 | 2014-03-19 17:47:49 +0100 | [diff] [blame] | 2979 | RTM_NEWNEIGH, NTF_SELF, |
Hubert Sokolowski | b337904 | 2015-12-15 13:20:30 +0000 | [diff] [blame] | 2980 | NLM_F_MULTI, NUD_PERMANENT); |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2981 | if (err < 0) |
| 2982 | return err; |
| 2983 | skip: |
| 2984 | *idx += 1; |
| 2985 | } |
| 2986 | return 0; |
| 2987 | } |
| 2988 | |
| 2989 | /** |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 2990 | * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2991 | * @nlh: netlink message header |
| 2992 | * @dev: netdevice |
| 2993 | * |
| 2994 | * Default netdevice operation to dump the existing unicast address list. |
John Fastabend | 91f3e7b | 2013-03-29 08:18:37 +0000 | [diff] [blame] | 2995 | * Returns number of addresses from list put in skb. |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2996 | */ |
| 2997 | int ndo_dflt_fdb_dump(struct sk_buff *skb, |
| 2998 | struct netlink_callback *cb, |
| 2999 | struct net_device *dev, |
Jamal Hadi Salim | 5d5eacb | 2014-07-10 07:01:58 -0400 | [diff] [blame] | 3000 | struct net_device *filter_dev, |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 3001 | int idx) |
| 3002 | { |
| 3003 | int err; |
| 3004 | |
| 3005 | netif_addr_lock_bh(dev); |
| 3006 | err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc); |
| 3007 | if (err) |
| 3008 | goto out; |
| 3009 | nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc); |
| 3010 | out: |
| 3011 | netif_addr_unlock_bh(dev); |
MINOURA Makoto / 箕浦 真 | 472681d | 2016-02-25 14:20:48 +0900 | [diff] [blame] | 3012 | cb->args[1] = err; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 3013 | return idx; |
| 3014 | } |
| 3015 | EXPORT_SYMBOL(ndo_dflt_fdb_dump); |
| 3016 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3017 | static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 3018 | { |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3019 | struct net_device *dev; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3020 | struct nlattr *tb[IFLA_MAX+1]; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3021 | struct net_device *br_dev = NULL; |
| 3022 | const struct net_device_ops *ops = NULL; |
| 3023 | const struct net_device_ops *cops = NULL; |
| 3024 | struct ifinfomsg *ifm = nlmsg_data(cb->nlh); |
| 3025 | struct net *net = sock_net(skb->sk); |
| 3026 | int brport_idx = 0; |
| 3027 | int br_idx = 0; |
| 3028 | int idx = 0; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3029 | |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3030 | if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX, |
| 3031 | ifla_policy) == 0) { |
| 3032 | if (tb[IFLA_MASTER]) |
| 3033 | br_idx = nla_get_u32(tb[IFLA_MASTER]); |
| 3034 | } |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3035 | |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3036 | brport_idx = ifm->ifi_index; |
| 3037 | |
| 3038 | if (br_idx) { |
| 3039 | br_dev = __dev_get_by_index(net, br_idx); |
| 3040 | if (!br_dev) |
| 3041 | return -ENODEV; |
| 3042 | |
| 3043 | ops = br_dev->netdev_ops; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3044 | } |
| 3045 | |
MINOURA Makoto / 箕浦 真 | 472681d | 2016-02-25 14:20:48 +0900 | [diff] [blame] | 3046 | cb->args[1] = 0; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3047 | for_each_netdev(net, dev) { |
| 3048 | if (brport_idx && (dev->ifindex != brport_idx)) |
| 3049 | continue; |
| 3050 | |
| 3051 | if (!br_idx) { /* user did not specify a specific bridge */ |
| 3052 | if (dev->priv_flags & IFF_BRIDGE_PORT) { |
| 3053 | br_dev = netdev_master_upper_dev_get(dev); |
| 3054 | cops = br_dev->netdev_ops; |
| 3055 | } |
| 3056 | |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3057 | } else { |
| 3058 | if (dev != br_dev && |
| 3059 | !(dev->priv_flags & IFF_BRIDGE_PORT)) |
| 3060 | continue; |
| 3061 | |
| 3062 | if (br_dev != netdev_master_upper_dev_get(dev) && |
| 3063 | !(dev->priv_flags & IFF_EBRIDGE)) |
| 3064 | continue; |
| 3065 | |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3066 | cops = ops; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3067 | } |
| 3068 | |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3069 | if (dev->priv_flags & IFF_BRIDGE_PORT) { |
| 3070 | if (cops && cops->ndo_fdb_dump) |
| 3071 | idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev, |
| 3072 | idx); |
| 3073 | } |
MINOURA Makoto / 箕浦 真 | 472681d | 2016-02-25 14:20:48 +0900 | [diff] [blame] | 3074 | if (cb->args[1] == -EMSGSIZE) |
| 3075 | break; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3076 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3077 | if (dev->netdev_ops->ndo_fdb_dump) |
Hubert Sokolowski | 6cb6974 | 2015-01-05 17:29:21 +0000 | [diff] [blame] | 3078 | idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, NULL, |
Jamal Hadi Salim | 5d5eacb | 2014-07-10 07:01:58 -0400 | [diff] [blame] | 3079 | idx); |
Hubert Sokolowski | 6cb6974 | 2015-01-05 17:29:21 +0000 | [diff] [blame] | 3080 | else |
| 3081 | idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx); |
MINOURA Makoto / 箕浦 真 | 472681d | 2016-02-25 14:20:48 +0900 | [diff] [blame] | 3082 | if (cb->args[1] == -EMSGSIZE) |
| 3083 | break; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 3084 | |
| 3085 | cops = NULL; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3086 | } |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3087 | |
| 3088 | cb->args[0] = idx; |
| 3089 | return skb->len; |
| 3090 | } |
| 3091 | |
Scott Feldman | 2c3c031 | 2014-11-28 14:34:25 +0100 | [diff] [blame] | 3092 | static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, |
| 3093 | unsigned int attrnum, unsigned int flag) |
| 3094 | { |
| 3095 | if (mask & flag) |
| 3096 | return nla_put_u8(skb, attrnum, !!(flags & flag)); |
| 3097 | return 0; |
| 3098 | } |
| 3099 | |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3100 | 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] | 3101 | struct net_device *dev, u16 mode, |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 3102 | u32 flags, u32 mask, int nlflags, |
| 3103 | u32 filter_mask, |
| 3104 | int (*vlan_fill)(struct sk_buff *skb, |
| 3105 | struct net_device *dev, |
| 3106 | u32 filter_mask)) |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3107 | { |
| 3108 | struct nlmsghdr *nlh; |
| 3109 | struct ifinfomsg *ifm; |
| 3110 | struct nlattr *br_afspec; |
Scott Feldman | 2c3c031 | 2014-11-28 14:34:25 +0100 | [diff] [blame] | 3111 | struct nlattr *protinfo; |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3112 | u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 3113 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 3114 | int err = 0; |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3115 | |
Nicolas Dichtel | 46c264d | 2015-04-28 18:33:49 +0200 | [diff] [blame] | 3116 | nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3117 | if (nlh == NULL) |
| 3118 | return -EMSGSIZE; |
| 3119 | |
| 3120 | ifm = nlmsg_data(nlh); |
| 3121 | ifm->ifi_family = AF_BRIDGE; |
| 3122 | ifm->__ifi_pad = 0; |
| 3123 | ifm->ifi_type = dev->type; |
| 3124 | ifm->ifi_index = dev->ifindex; |
| 3125 | ifm->ifi_flags = dev_get_flags(dev); |
| 3126 | ifm->ifi_change = 0; |
| 3127 | |
| 3128 | |
| 3129 | if (nla_put_string(skb, IFLA_IFNAME, dev->name) || |
| 3130 | nla_put_u32(skb, IFLA_MTU, dev->mtu) || |
| 3131 | nla_put_u8(skb, IFLA_OPERSTATE, operstate) || |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 3132 | (br_dev && |
| 3133 | nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3134 | (dev->addr_len && |
| 3135 | nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || |
Nicolas Dichtel | a54acb3 | 2015-04-02 17:07:00 +0200 | [diff] [blame] | 3136 | (dev->ifindex != dev_get_iflink(dev) && |
| 3137 | nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3138 | goto nla_put_failure; |
| 3139 | |
| 3140 | br_afspec = nla_nest_start(skb, IFLA_AF_SPEC); |
| 3141 | if (!br_afspec) |
| 3142 | goto nla_put_failure; |
| 3143 | |
Roopa Prabhu | 1d460b9 | 2014-12-08 14:04:20 -0800 | [diff] [blame] | 3144 | if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3145 | nla_nest_cancel(skb, br_afspec); |
| 3146 | goto nla_put_failure; |
| 3147 | } |
Roopa Prabhu | 1d460b9 | 2014-12-08 14:04:20 -0800 | [diff] [blame] | 3148 | |
| 3149 | if (mode != BRIDGE_MODE_UNDEF) { |
| 3150 | if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { |
| 3151 | nla_nest_cancel(skb, br_afspec); |
| 3152 | goto nla_put_failure; |
| 3153 | } |
| 3154 | } |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 3155 | if (vlan_fill) { |
| 3156 | err = vlan_fill(skb, dev, filter_mask); |
| 3157 | if (err) { |
| 3158 | nla_nest_cancel(skb, br_afspec); |
| 3159 | goto nla_put_failure; |
| 3160 | } |
| 3161 | } |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3162 | nla_nest_end(skb, br_afspec); |
| 3163 | |
Scott Feldman | 2c3c031 | 2014-11-28 14:34:25 +0100 | [diff] [blame] | 3164 | protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED); |
| 3165 | if (!protinfo) |
| 3166 | goto nla_put_failure; |
| 3167 | |
| 3168 | if (brport_nla_put_flag(skb, flags, mask, |
| 3169 | IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || |
| 3170 | brport_nla_put_flag(skb, flags, mask, |
| 3171 | IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || |
| 3172 | brport_nla_put_flag(skb, flags, mask, |
| 3173 | IFLA_BRPORT_FAST_LEAVE, |
| 3174 | BR_MULTICAST_FAST_LEAVE) || |
| 3175 | brport_nla_put_flag(skb, flags, mask, |
| 3176 | IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || |
| 3177 | brport_nla_put_flag(skb, flags, mask, |
| 3178 | IFLA_BRPORT_LEARNING, BR_LEARNING) || |
| 3179 | brport_nla_put_flag(skb, flags, mask, |
| 3180 | IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || |
| 3181 | brport_nla_put_flag(skb, flags, mask, |
| 3182 | IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || |
| 3183 | brport_nla_put_flag(skb, flags, mask, |
| 3184 | IFLA_BRPORT_PROXYARP, BR_PROXYARP)) { |
| 3185 | nla_nest_cancel(skb, protinfo); |
| 3186 | goto nla_put_failure; |
| 3187 | } |
| 3188 | |
| 3189 | nla_nest_end(skb, protinfo); |
| 3190 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 3191 | nlmsg_end(skb, nlh); |
| 3192 | return 0; |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3193 | nla_put_failure: |
| 3194 | nlmsg_cancel(skb, nlh); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 3195 | return err ? err : -EMSGSIZE; |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3196 | } |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 3197 | EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 3198 | |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3199 | static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) |
| 3200 | { |
| 3201 | struct net *net = sock_net(skb->sk); |
| 3202 | struct net_device *dev; |
| 3203 | int idx = 0; |
| 3204 | u32 portid = NETLINK_CB(cb->skb).portid; |
| 3205 | u32 seq = cb->nlh->nlmsg_seq; |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 3206 | u32 filter_mask = 0; |
Roopa Prabhu | d64f69b | 2015-09-15 14:44:29 -0700 | [diff] [blame] | 3207 | int err; |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 3208 | |
Thomas Graf | aa68c20 | 2014-11-26 13:42:20 +0100 | [diff] [blame] | 3209 | if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) { |
| 3210 | struct nlattr *extfilt; |
| 3211 | |
| 3212 | extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg), |
| 3213 | IFLA_EXT_MASK); |
| 3214 | if (extfilt) { |
| 3215 | if (nla_len(extfilt) < sizeof(filter_mask)) |
| 3216 | return -EINVAL; |
| 3217 | |
| 3218 | filter_mask = nla_get_u32(extfilt); |
| 3219 | } |
| 3220 | } |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3221 | |
| 3222 | rcu_read_lock(); |
| 3223 | for_each_netdev_rcu(net, dev) { |
| 3224 | const struct net_device_ops *ops = dev->netdev_ops; |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 3225 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3226 | |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 3227 | if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { |
Roopa Prabhu | d64f69b | 2015-09-15 14:44:29 -0700 | [diff] [blame] | 3228 | if (idx >= cb->args[0]) { |
| 3229 | err = br_dev->netdev_ops->ndo_bridge_getlink( |
| 3230 | skb, portid, seq, dev, |
| 3231 | filter_mask, NLM_F_MULTI); |
| 3232 | if (err < 0 && err != -EOPNOTSUPP) |
| 3233 | break; |
| 3234 | } |
Ben Hutchings | 25b1e67 | 2012-11-02 12:56:52 +0000 | [diff] [blame] | 3235 | idx++; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3236 | } |
| 3237 | |
| 3238 | if (ops->ndo_bridge_getlink) { |
Roopa Prabhu | d64f69b | 2015-09-15 14:44:29 -0700 | [diff] [blame] | 3239 | if (idx >= cb->args[0]) { |
| 3240 | err = ops->ndo_bridge_getlink(skb, portid, |
| 3241 | seq, dev, |
| 3242 | filter_mask, |
| 3243 | NLM_F_MULTI); |
| 3244 | if (err < 0 && err != -EOPNOTSUPP) |
| 3245 | break; |
| 3246 | } |
Ben Hutchings | 25b1e67 | 2012-11-02 12:56:52 +0000 | [diff] [blame] | 3247 | idx++; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3248 | } |
| 3249 | } |
| 3250 | rcu_read_unlock(); |
| 3251 | cb->args[0] = idx; |
| 3252 | |
| 3253 | return skb->len; |
| 3254 | } |
| 3255 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3256 | static inline size_t bridge_nlmsg_size(void) |
| 3257 | { |
| 3258 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) |
| 3259 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ |
| 3260 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ |
| 3261 | + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ |
| 3262 | + nla_total_size(sizeof(u32)) /* IFLA_MTU */ |
| 3263 | + nla_total_size(sizeof(u32)) /* IFLA_LINK */ |
| 3264 | + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ |
| 3265 | + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ |
| 3266 | + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ |
| 3267 | + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ |
| 3268 | + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ |
| 3269 | } |
| 3270 | |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 3271 | static int rtnl_bridge_notify(struct net_device *dev) |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3272 | { |
| 3273 | struct net *net = dev_net(dev); |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3274 | struct sk_buff *skb; |
| 3275 | int err = -EOPNOTSUPP; |
| 3276 | |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 3277 | if (!dev->netdev_ops->ndo_bridge_getlink) |
| 3278 | return 0; |
| 3279 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3280 | skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); |
| 3281 | if (!skb) { |
| 3282 | err = -ENOMEM; |
| 3283 | goto errout; |
| 3284 | } |
| 3285 | |
Nicolas Dichtel | 46c264d | 2015-04-28 18:33:49 +0200 | [diff] [blame] | 3286 | 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] | 3287 | if (err < 0) |
| 3288 | goto errout; |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3289 | |
Roopa Prabhu | 59ccaaa | 2015-01-28 16:23:11 -0800 | [diff] [blame] | 3290 | if (!skb->len) |
| 3291 | goto errout; |
| 3292 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3293 | rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); |
| 3294 | return 0; |
| 3295 | errout: |
| 3296 | WARN_ON(err == -EMSGSIZE); |
| 3297 | kfree_skb(skb); |
Roopa Prabhu | 59ccaaa | 2015-01-28 16:23:11 -0800 | [diff] [blame] | 3298 | if (err) |
| 3299 | rtnl_set_sk_err(net, RTNLGRP_LINK, err); |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3300 | return err; |
| 3301 | } |
| 3302 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 3303 | static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh) |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3304 | { |
| 3305 | struct net *net = sock_net(skb->sk); |
| 3306 | struct ifinfomsg *ifm; |
| 3307 | struct net_device *dev; |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3308 | struct nlattr *br_spec, *attr = NULL; |
| 3309 | int rem, err = -EOPNOTSUPP; |
Rosen, Rami | 4de8b41 | 2015-01-19 11:45:04 +0200 | [diff] [blame] | 3310 | u16 flags = 0; |
John Fastabend | c38e01b | 2012-11-02 16:32:36 +0000 | [diff] [blame] | 3311 | bool have_flags = false; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3312 | |
| 3313 | if (nlmsg_len(nlh) < sizeof(*ifm)) |
| 3314 | return -EINVAL; |
| 3315 | |
| 3316 | ifm = nlmsg_data(nlh); |
| 3317 | if (ifm->ifi_family != AF_BRIDGE) |
| 3318 | return -EPFNOSUPPORT; |
| 3319 | |
| 3320 | dev = __dev_get_by_index(net, ifm->ifi_index); |
| 3321 | if (!dev) { |
| 3322 | pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n"); |
| 3323 | return -ENODEV; |
| 3324 | } |
| 3325 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3326 | br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); |
| 3327 | if (br_spec) { |
| 3328 | nla_for_each_nested(attr, br_spec, rem) { |
| 3329 | if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { |
Thomas Graf | 6e8d1c5 | 2014-11-26 13:42:16 +0100 | [diff] [blame] | 3330 | if (nla_len(attr) < sizeof(flags)) |
| 3331 | return -EINVAL; |
| 3332 | |
John Fastabend | c38e01b | 2012-11-02 16:32:36 +0000 | [diff] [blame] | 3333 | have_flags = true; |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3334 | flags = nla_get_u16(attr); |
| 3335 | break; |
| 3336 | } |
| 3337 | } |
| 3338 | } |
| 3339 | |
| 3340 | if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 3341 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
| 3342 | |
| 3343 | if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3344 | err = -EOPNOTSUPP; |
| 3345 | goto out; |
| 3346 | } |
| 3347 | |
Roopa Prabhu | add511b | 2015-01-29 22:40:12 -0800 | [diff] [blame] | 3348 | err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3349 | if (err) |
| 3350 | goto out; |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3351 | |
| 3352 | flags &= ~BRIDGE_FLAGS_MASTER; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3353 | } |
| 3354 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3355 | if ((flags & BRIDGE_FLAGS_SELF)) { |
| 3356 | if (!dev->netdev_ops->ndo_bridge_setlink) |
| 3357 | err = -EOPNOTSUPP; |
| 3358 | else |
Roopa Prabhu | add511b | 2015-01-29 22:40:12 -0800 | [diff] [blame] | 3359 | err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, |
| 3360 | flags); |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 3361 | if (!err) { |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3362 | flags &= ~BRIDGE_FLAGS_SELF; |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 3363 | |
| 3364 | /* Generate event to notify upper layer of bridge |
| 3365 | * change |
| 3366 | */ |
| 3367 | err = rtnl_bridge_notify(dev); |
| 3368 | } |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3369 | } |
| 3370 | |
John Fastabend | c38e01b | 2012-11-02 16:32:36 +0000 | [diff] [blame] | 3371 | if (have_flags) |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 3372 | memcpy(nla_data(attr), &flags, sizeof(flags)); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3373 | out: |
| 3374 | return err; |
| 3375 | } |
| 3376 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 3377 | static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh) |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3378 | { |
| 3379 | struct net *net = sock_net(skb->sk); |
| 3380 | struct ifinfomsg *ifm; |
| 3381 | struct net_device *dev; |
| 3382 | struct nlattr *br_spec, *attr = NULL; |
| 3383 | int rem, err = -EOPNOTSUPP; |
Rosen, Rami | 4de8b41 | 2015-01-19 11:45:04 +0200 | [diff] [blame] | 3384 | u16 flags = 0; |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3385 | bool have_flags = false; |
| 3386 | |
| 3387 | if (nlmsg_len(nlh) < sizeof(*ifm)) |
| 3388 | return -EINVAL; |
| 3389 | |
| 3390 | ifm = nlmsg_data(nlh); |
| 3391 | if (ifm->ifi_family != AF_BRIDGE) |
| 3392 | return -EPFNOSUPPORT; |
| 3393 | |
| 3394 | dev = __dev_get_by_index(net, ifm->ifi_index); |
| 3395 | if (!dev) { |
| 3396 | pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n"); |
| 3397 | return -ENODEV; |
| 3398 | } |
| 3399 | |
| 3400 | br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); |
| 3401 | if (br_spec) { |
| 3402 | nla_for_each_nested(attr, br_spec, rem) { |
| 3403 | if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { |
Thomas Graf | 6e8d1c5 | 2014-11-26 13:42:16 +0100 | [diff] [blame] | 3404 | if (nla_len(attr) < sizeof(flags)) |
| 3405 | return -EINVAL; |
| 3406 | |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3407 | have_flags = true; |
| 3408 | flags = nla_get_u16(attr); |
| 3409 | break; |
| 3410 | } |
| 3411 | } |
| 3412 | } |
| 3413 | |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3414 | if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { |
| 3415 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
| 3416 | |
| 3417 | if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { |
| 3418 | err = -EOPNOTSUPP; |
| 3419 | goto out; |
| 3420 | } |
| 3421 | |
Roopa Prabhu | add511b | 2015-01-29 22:40:12 -0800 | [diff] [blame] | 3422 | err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3423 | if (err) |
| 3424 | goto out; |
| 3425 | |
| 3426 | flags &= ~BRIDGE_FLAGS_MASTER; |
| 3427 | } |
| 3428 | |
| 3429 | if ((flags & BRIDGE_FLAGS_SELF)) { |
| 3430 | if (!dev->netdev_ops->ndo_bridge_dellink) |
| 3431 | err = -EOPNOTSUPP; |
| 3432 | else |
Roopa Prabhu | add511b | 2015-01-29 22:40:12 -0800 | [diff] [blame] | 3433 | err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, |
| 3434 | flags); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3435 | |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 3436 | if (!err) { |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3437 | flags &= ~BRIDGE_FLAGS_SELF; |
Roopa Prabhu | 02dba43 | 2015-01-14 20:02:25 -0800 | [diff] [blame] | 3438 | |
| 3439 | /* Generate event to notify upper layer of bridge |
| 3440 | * change |
| 3441 | */ |
| 3442 | err = rtnl_bridge_notify(dev); |
| 3443 | } |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3444 | } |
| 3445 | |
| 3446 | if (have_flags) |
| 3447 | memcpy(nla_data(attr), &flags, sizeof(flags)); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3448 | out: |
| 3449 | return err; |
| 3450 | } |
| 3451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3452 | /* Process one rtnetlink message. */ |
| 3453 | |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 3454 | static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3455 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 3456 | struct net *net = sock_net(skb->sk); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 3457 | rtnl_doit_func doit; |
Alexander Kuleshov | 617cfc7 | 2016-01-10 21:26:57 +0600 | [diff] [blame] | 3458 | int kind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3459 | int family; |
| 3460 | int type; |
Eric Dumazet | 2907c35 | 2011-05-25 07:34:04 +0000 | [diff] [blame] | 3461 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3463 | type = nlh->nlmsg_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3464 | if (type > RTM_MAX) |
Thomas Graf | 038890f | 2007-04-05 14:35:52 -0700 | [diff] [blame] | 3465 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3466 | |
| 3467 | type -= RTM_BASE; |
| 3468 | |
| 3469 | /* All the messages must have at least 1 byte length */ |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 3470 | if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3471 | return 0; |
| 3472 | |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 3473 | family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3474 | kind = type&3; |
| 3475 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 3476 | if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 3477 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3478 | |
David S. Miller | b8f3ab4 | 2011-01-18 12:40:38 -0800 | [diff] [blame] | 3479 | if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3480 | struct sock *rtnl; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 3481 | rtnl_dumpit_func dumpit; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 3482 | rtnl_calcit_func calcit; |
| 3483 | u16 min_dump_alloc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3484 | |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 3485 | dumpit = rtnl_get_dumpit(family, type); |
| 3486 | if (dumpit == NULL) |
Thomas Graf | 038890f | 2007-04-05 14:35:52 -0700 | [diff] [blame] | 3487 | return -EOPNOTSUPP; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 3488 | calcit = rtnl_get_calcit(family, type); |
| 3489 | if (calcit) |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 3490 | min_dump_alloc = calcit(skb, nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3491 | |
Eric Dumazet | 2907c35 | 2011-05-25 07:34:04 +0000 | [diff] [blame] | 3492 | __rtnl_unlock(); |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3493 | rtnl = net->rtnl; |
Pablo Neira Ayuso | 80d326f | 2012-02-24 14:30:15 +0000 | [diff] [blame] | 3494 | { |
| 3495 | struct netlink_dump_control c = { |
| 3496 | .dump = dumpit, |
| 3497 | .min_dump_alloc = min_dump_alloc, |
| 3498 | }; |
| 3499 | err = netlink_dump_start(rtnl, skb, nlh, &c); |
| 3500 | } |
Eric Dumazet | 2907c35 | 2011-05-25 07:34:04 +0000 | [diff] [blame] | 3501 | rtnl_lock(); |
| 3502 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3503 | } |
| 3504 | |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 3505 | doit = rtnl_get_doit(family, type); |
| 3506 | if (doit == NULL) |
Thomas Graf | 038890f | 2007-04-05 14:35:52 -0700 | [diff] [blame] | 3507 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3508 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 3509 | return doit(skb, nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3510 | } |
| 3511 | |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 3512 | static void rtnetlink_rcv(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3513 | { |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 3514 | rtnl_lock(); |
| 3515 | netlink_rcv_skb(skb, &rtnetlink_rcv_msg); |
| 3516 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3517 | } |
| 3518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3519 | static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 3520 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 3521 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 3522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3523 | switch (event) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3524 | case NETDEV_UP: |
| 3525 | case NETDEV_DOWN: |
Patrick McHardy | 10de05a | 2010-02-26 06:34:50 +0000 | [diff] [blame] | 3526 | case NETDEV_PRE_UP: |
Eric W. Biederman | d90a909 | 2009-12-12 22:11:15 +0000 | [diff] [blame] | 3527 | case NETDEV_POST_INIT: |
| 3528 | case NETDEV_REGISTER: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3529 | case NETDEV_CHANGE: |
Patrick McHardy | 755d0e7 | 2010-03-19 04:42:24 +0000 | [diff] [blame] | 3530 | case NETDEV_PRE_TYPE_CHANGE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3531 | case NETDEV_GOING_DOWN: |
Patrick McHardy | a283576 | 2010-02-26 06:34:51 +0000 | [diff] [blame] | 3532 | case NETDEV_UNREGISTER: |
Eric Dumazet | 0115e8e | 2012-08-22 17:19:46 +0000 | [diff] [blame] | 3533 | case NETDEV_UNREGISTER_FINAL: |
Amerigo Wang | ac3d3f8 | 2011-05-19 23:06:32 +0000 | [diff] [blame] | 3534 | case NETDEV_RELEASE: |
| 3535 | case NETDEV_JOIN: |
Moni Shoua | 61bd385 | 2015-02-03 16:48:29 +0200 | [diff] [blame] | 3536 | case NETDEV_BONDING_INFO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3537 | break; |
| 3538 | default: |
Alexei Starovoitov | 7f29405 | 2013-10-23 16:02:42 -0700 | [diff] [blame] | 3539 | rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3540 | break; |
| 3541 | } |
| 3542 | return NOTIFY_DONE; |
| 3543 | } |
| 3544 | |
| 3545 | static struct notifier_block rtnetlink_dev_notifier = { |
| 3546 | .notifier_call = rtnetlink_event, |
| 3547 | }; |
| 3548 | |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3549 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 3550 | static int __net_init rtnetlink_net_init(struct net *net) |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3551 | { |
| 3552 | struct sock *sk; |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 3553 | struct netlink_kernel_cfg cfg = { |
| 3554 | .groups = RTNLGRP_MAX, |
| 3555 | .input = rtnetlink_rcv, |
| 3556 | .cb_mutex = &rtnl_mutex, |
Pablo Neira Ayuso | 9785e10 | 2012-09-08 02:53:53 +0000 | [diff] [blame] | 3557 | .flags = NL_CFG_F_NONROOT_RECV, |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 3558 | }; |
| 3559 | |
Pablo Neira Ayuso | 9f00d97 | 2012-09-08 02:53:54 +0000 | [diff] [blame] | 3560 | sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3561 | if (!sk) |
| 3562 | return -ENOMEM; |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3563 | net->rtnl = sk; |
| 3564 | return 0; |
| 3565 | } |
| 3566 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 3567 | static void __net_exit rtnetlink_net_exit(struct net *net) |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3568 | { |
Denis V. Lunev | 775516b | 2008-01-18 23:55:19 -0800 | [diff] [blame] | 3569 | netlink_kernel_release(net->rtnl); |
| 3570 | net->rtnl = NULL; |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3571 | } |
| 3572 | |
| 3573 | static struct pernet_operations rtnetlink_net_ops = { |
| 3574 | .init = rtnetlink_net_init, |
| 3575 | .exit = rtnetlink_net_exit, |
| 3576 | }; |
| 3577 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3578 | void __init rtnetlink_init(void) |
| 3579 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3580 | if (register_pernet_subsys(&rtnetlink_net_ops)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3581 | panic("rtnetlink_init: cannot initialize rtnetlink\n"); |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3582 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3583 | register_netdevice_notifier(&rtnetlink_dev_notifier); |
Thomas Graf | 340d17f | 2007-03-22 11:49:22 -0700 | [diff] [blame] | 3584 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 3585 | rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, |
| 3586 | rtnl_dump_ifinfo, rtnl_calcit); |
| 3587 | rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL); |
| 3588 | rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL); |
| 3589 | rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL); |
Thomas Graf | 687ad8c | 2007-03-22 11:59:42 -0700 | [diff] [blame] | 3590 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 3591 | rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL); |
| 3592 | rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3593 | |
| 3594 | rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL); |
| 3595 | rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL); |
| 3596 | rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3597 | |
| 3598 | rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3599 | rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3600 | rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3601 | } |