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