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