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