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