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