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; |
| 370 | DEFINE_WAIT(wait); |
| 371 | |
| 372 | for (;;) { |
| 373 | prepare_to_wait(&netdev_unregistering_wq, &wait, |
| 374 | TASK_UNINTERRUPTIBLE); |
| 375 | unregistering = false; |
| 376 | rtnl_lock(); |
| 377 | for_each_net(net) { |
| 378 | if (net->dev_unreg_count > 0) { |
| 379 | unregistering = true; |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | if (!unregistering) |
| 384 | break; |
| 385 | __rtnl_unlock(); |
| 386 | schedule(); |
| 387 | } |
| 388 | finish_wait(&netdev_unregistering_wq, &wait); |
| 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)) { |
Eric W. Biederman | b51642f | 2012-11-16 03:03:11 +0000 | [diff] [blame] | 1526 | err = -EPERM; |
| 1527 | goto errout; |
| 1528 | } |
Eric W. Biederman | d8a5ec6 | 2007-09-12 13:57:04 +0200 | [diff] [blame] | 1529 | err = dev_change_net_namespace(dev, net, ifname); |
| 1530 | put_net(net); |
| 1531 | if (err) |
| 1532 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1533 | status |= DO_SETLINK_MODIFIED; |
Eric W. Biederman | d8a5ec6 | 2007-09-12 13:57:04 +0200 | [diff] [blame] | 1534 | } |
| 1535 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1536 | if (tb[IFLA_MAP]) { |
| 1537 | struct rtnl_link_ifmap *u_map; |
| 1538 | struct ifmap k_map; |
| 1539 | |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 1540 | if (!ops->ndo_set_config) { |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1541 | err = -EOPNOTSUPP; |
| 1542 | goto errout; |
| 1543 | } |
| 1544 | |
| 1545 | if (!netif_device_present(dev)) { |
| 1546 | err = -ENODEV; |
| 1547 | goto errout; |
| 1548 | } |
| 1549 | |
| 1550 | u_map = nla_data(tb[IFLA_MAP]); |
| 1551 | k_map.mem_start = (unsigned long) u_map->mem_start; |
| 1552 | k_map.mem_end = (unsigned long) u_map->mem_end; |
| 1553 | k_map.base_addr = (unsigned short) u_map->base_addr; |
| 1554 | k_map.irq = (unsigned char) u_map->irq; |
| 1555 | k_map.dma = (unsigned char) u_map->dma; |
| 1556 | k_map.port = (unsigned char) u_map->port; |
| 1557 | |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 1558 | err = ops->ndo_set_config(dev, &k_map); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1559 | if (err < 0) |
| 1560 | goto errout; |
| 1561 | |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1562 | status |= DO_SETLINK_NOTIFY; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | if (tb[IFLA_ADDRESS]) { |
| 1566 | struct sockaddr *sa; |
| 1567 | int len; |
| 1568 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1569 | len = sizeof(sa_family_t) + dev->addr_len; |
| 1570 | sa = kmalloc(len, GFP_KERNEL); |
| 1571 | if (!sa) { |
| 1572 | err = -ENOMEM; |
| 1573 | goto errout; |
| 1574 | } |
| 1575 | sa->sa_family = dev->type; |
| 1576 | memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), |
| 1577 | dev->addr_len); |
Jiri Pirko | e7c3273 | 2013-01-01 03:30:13 +0000 | [diff] [blame] | 1578 | err = dev_set_mac_address(dev, sa); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1579 | kfree(sa); |
| 1580 | if (err) |
| 1581 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1582 | status |= DO_SETLINK_MODIFIED; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | if (tb[IFLA_MTU]) { |
| 1586 | err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); |
| 1587 | if (err < 0) |
| 1588 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1589 | status |= DO_SETLINK_MODIFIED; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1590 | } |
| 1591 | |
Vlad Dogaru | cbda10f | 2011-01-13 23:38:30 +0000 | [diff] [blame] | 1592 | if (tb[IFLA_GROUP]) { |
| 1593 | dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1594 | status |= DO_SETLINK_NOTIFY; |
Vlad Dogaru | cbda10f | 2011-01-13 23:38:30 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1597 | /* |
| 1598 | * Interface selected by interface index but interface |
| 1599 | * name provided implies that a name change has been |
| 1600 | * requested. |
| 1601 | */ |
| 1602 | if (ifm->ifi_index > 0 && ifname[0]) { |
| 1603 | err = dev_change_name(dev, ifname); |
| 1604 | if (err < 0) |
| 1605 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1606 | status |= DO_SETLINK_MODIFIED; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1607 | } |
| 1608 | |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 1609 | if (tb[IFLA_IFALIAS]) { |
| 1610 | err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), |
| 1611 | nla_len(tb[IFLA_IFALIAS])); |
| 1612 | if (err < 0) |
| 1613 | goto errout; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1614 | status |= DO_SETLINK_NOTIFY; |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 1615 | } |
| 1616 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1617 | if (tb[IFLA_BROADCAST]) { |
| 1618 | nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); |
Jiri Pirko | e7c3273 | 2013-01-01 03:30:13 +0000 | [diff] [blame] | 1619 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | if (ifm->ifi_flags || ifm->ifi_change) { |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 1623 | err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); |
Johannes Berg | 5f9021c | 2008-11-16 23:20:31 -0800 | [diff] [blame] | 1624 | if (err < 0) |
| 1625 | goto errout; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1626 | } |
| 1627 | |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1628 | if (tb[IFLA_MASTER]) { |
| 1629 | err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER])); |
| 1630 | if (err) |
| 1631 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1632 | status |= DO_SETLINK_MODIFIED; |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
Jiri Pirko | 9a57247 | 2012-12-27 23:49:39 +0000 | [diff] [blame] | 1635 | if (tb[IFLA_CARRIER]) { |
| 1636 | err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); |
| 1637 | if (err) |
| 1638 | goto errout; |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 1639 | status |= DO_SETLINK_MODIFIED; |
Jiri Pirko | 9a57247 | 2012-12-27 23:49:39 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
Nicolas Dichtel | 5d1180f | 2014-09-01 16:07:26 +0200 | [diff] [blame] | 1642 | if (tb[IFLA_TXQLEN]) { |
| 1643 | unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]); |
| 1644 | |
| 1645 | if (dev->tx_queue_len ^ value) |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1646 | status |= DO_SETLINK_NOTIFY; |
Nicolas Dichtel | 5d1180f | 2014-09-01 16:07:26 +0200 | [diff] [blame] | 1647 | |
| 1648 | dev->tx_queue_len = value; |
| 1649 | } |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1650 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1651 | if (tb[IFLA_OPERSTATE]) |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 1652 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1653 | |
| 1654 | if (tb[IFLA_LINKMODE]) { |
Nicolas Dichtel | 1889b0e | 2014-09-01 16:07:27 +0200 | [diff] [blame] | 1655 | unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); |
| 1656 | |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 1657 | write_lock_bh(&dev_base_lock); |
Nicolas Dichtel | 1889b0e | 2014-09-01 16:07:27 +0200 | [diff] [blame] | 1658 | if (dev->link_mode ^ value) |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1659 | status |= DO_SETLINK_NOTIFY; |
Nicolas Dichtel | 1889b0e | 2014-09-01 16:07:27 +0200 | [diff] [blame] | 1660 | dev->link_mode = value; |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 1661 | write_unlock_bh(&dev_base_lock); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1662 | } |
| 1663 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1664 | if (tb[IFLA_VFINFO_LIST]) { |
| 1665 | struct nlattr *attr; |
| 1666 | int rem; |
| 1667 | nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { |
David Howells | 253683b | 2010-05-21 02:25:27 +0000 | [diff] [blame] | 1668 | if (nla_type(attr) != IFLA_VF_INFO) { |
| 1669 | err = -EINVAL; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1670 | goto errout; |
David Howells | 253683b | 2010-05-21 02:25:27 +0000 | [diff] [blame] | 1671 | } |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1672 | err = do_setvfinfo(dev, attr); |
| 1673 | if (err < 0) |
| 1674 | goto errout; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1675 | status |= DO_SETLINK_NOTIFY; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1676 | } |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 1677 | } |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1678 | err = 0; |
| 1679 | |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1680 | if (tb[IFLA_VF_PORTS]) { |
| 1681 | struct nlattr *port[IFLA_PORT_MAX+1]; |
| 1682 | struct nlattr *attr; |
| 1683 | int vf; |
| 1684 | int rem; |
| 1685 | |
| 1686 | err = -EOPNOTSUPP; |
| 1687 | if (!ops->ndo_set_vf_port) |
| 1688 | goto errout; |
| 1689 | |
| 1690 | nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { |
| 1691 | if (nla_type(attr) != IFLA_VF_PORT) |
| 1692 | continue; |
| 1693 | err = nla_parse_nested(port, IFLA_PORT_MAX, |
| 1694 | attr, ifla_port_policy); |
| 1695 | if (err < 0) |
| 1696 | goto errout; |
| 1697 | if (!port[IFLA_PORT_VF]) { |
| 1698 | err = -EOPNOTSUPP; |
| 1699 | goto errout; |
| 1700 | } |
| 1701 | vf = nla_get_u32(port[IFLA_PORT_VF]); |
| 1702 | err = ops->ndo_set_vf_port(dev, vf, port); |
| 1703 | if (err < 0) |
| 1704 | goto errout; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1705 | status |= DO_SETLINK_NOTIFY; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1706 | } |
| 1707 | } |
| 1708 | err = 0; |
| 1709 | |
| 1710 | if (tb[IFLA_PORT_SELF]) { |
| 1711 | struct nlattr *port[IFLA_PORT_MAX+1]; |
| 1712 | |
| 1713 | err = nla_parse_nested(port, IFLA_PORT_MAX, |
| 1714 | tb[IFLA_PORT_SELF], ifla_port_policy); |
| 1715 | if (err < 0) |
| 1716 | goto errout; |
| 1717 | |
| 1718 | err = -EOPNOTSUPP; |
| 1719 | if (ops->ndo_set_vf_port) |
| 1720 | err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); |
| 1721 | if (err < 0) |
| 1722 | goto errout; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1723 | status |= DO_SETLINK_NOTIFY; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1724 | } |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 1725 | |
| 1726 | if (tb[IFLA_AF_SPEC]) { |
| 1727 | struct nlattr *af; |
| 1728 | int rem; |
| 1729 | |
| 1730 | nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { |
| 1731 | const struct rtnl_af_ops *af_ops; |
| 1732 | |
| 1733 | if (!(af_ops = rtnl_af_lookup(nla_type(af)))) |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 1734 | BUG(); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 1735 | |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 1736 | err = af_ops->set_link_af(dev, af); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 1737 | if (err < 0) |
| 1738 | goto errout; |
| 1739 | |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1740 | status |= DO_SETLINK_NOTIFY; |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 1741 | } |
| 1742 | } |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1743 | err = 0; |
| 1744 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1745 | errout: |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 1746 | if (status & DO_SETLINK_MODIFIED) { |
| 1747 | if (status & DO_SETLINK_NOTIFY) |
| 1748 | netdev_state_change(dev); |
| 1749 | |
| 1750 | if (err < 0) |
| 1751 | 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", |
| 1752 | dev->name); |
| 1753 | } |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1754 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1755 | return err; |
| 1756 | } |
| 1757 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 1758 | static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1760 | struct net *net = sock_net(skb->sk); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 1761 | struct ifinfomsg *ifm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | struct net_device *dev; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1763 | int err; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 1764 | struct nlattr *tb[IFLA_MAX+1]; |
| 1765 | char ifname[IFNAMSIZ]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 1767 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 1768 | if (err < 0) |
| 1769 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 1771 | if (tb[IFLA_IFNAME]) |
| 1772 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
Patrick McHardy | 78e5b891 | 2006-09-13 20:35:36 -0700 | [diff] [blame] | 1773 | else |
| 1774 | ifname[0] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | err = -EINVAL; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 1777 | ifm = nlmsg_data(nlh); |
Patrick McHardy | 51055be | 2007-06-05 12:40:01 -0700 | [diff] [blame] | 1778 | if (ifm->ifi_index > 0) |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 1779 | dev = __dev_get_by_index(net, ifm->ifi_index); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 1780 | else if (tb[IFLA_IFNAME]) |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 1781 | dev = __dev_get_by_name(net, ifname); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 1782 | else |
| 1783 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 1785 | if (dev == NULL) { |
| 1786 | err = -ENODEV; |
| 1787 | goto errout; |
| 1788 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 1790 | err = validate_linkmsg(dev, tb); |
| 1791 | if (err < 0) |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 1792 | goto errout; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 1793 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 1794 | err = do_setlink(skb, dev, ifm, tb, ifname, 0); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 1795 | errout: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | return err; |
| 1797 | } |
| 1798 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 1799 | static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1800 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1801 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1802 | const struct rtnl_link_ops *ops; |
| 1803 | struct net_device *dev; |
| 1804 | struct ifinfomsg *ifm; |
| 1805 | char ifname[IFNAMSIZ]; |
| 1806 | struct nlattr *tb[IFLA_MAX+1]; |
| 1807 | int err; |
Eric Dumazet | 226bd34 | 2011-05-08 23:17:57 +0000 | [diff] [blame] | 1808 | LIST_HEAD(list_kill); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1809 | |
| 1810 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 1811 | if (err < 0) |
| 1812 | return err; |
| 1813 | |
| 1814 | if (tb[IFLA_IFNAME]) |
| 1815 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
| 1816 | |
| 1817 | ifm = nlmsg_data(nlh); |
| 1818 | if (ifm->ifi_index > 0) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1819 | dev = __dev_get_by_index(net, ifm->ifi_index); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1820 | else if (tb[IFLA_IFNAME]) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1821 | dev = __dev_get_by_name(net, ifname); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1822 | else |
| 1823 | return -EINVAL; |
| 1824 | |
| 1825 | if (!dev) |
| 1826 | return -ENODEV; |
| 1827 | |
| 1828 | ops = dev->rtnl_link_ops; |
Jiri Pirko | b0ab2fa | 2014-06-26 09:58:25 +0200 | [diff] [blame] | 1829 | if (!ops || !ops->dellink) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1830 | return -EOPNOTSUPP; |
| 1831 | |
Eric Dumazet | 226bd34 | 2011-05-08 23:17:57 +0000 | [diff] [blame] | 1832 | ops->dellink(dev, &list_kill); |
| 1833 | unregister_netdevice_many(&list_kill); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1834 | return 0; |
| 1835 | } |
| 1836 | |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 1837 | int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) |
| 1838 | { |
| 1839 | unsigned int old_flags; |
| 1840 | int err; |
| 1841 | |
| 1842 | old_flags = dev->flags; |
| 1843 | if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { |
| 1844 | err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); |
| 1845 | if (err < 0) |
| 1846 | return err; |
| 1847 | } |
| 1848 | |
| 1849 | dev->rtnl_link_state = RTNL_LINK_INITIALIZED; |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 1850 | |
Nicolas Dichtel | a528c21 | 2013-09-25 12:02:44 +0200 | [diff] [blame] | 1851 | __dev_notify_flags(dev, old_flags, ~0U); |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 1852 | return 0; |
| 1853 | } |
| 1854 | EXPORT_SYMBOL(rtnl_configure_link); |
| 1855 | |
Rami Rosen | c071356 | 2012-11-30 01:08:47 +0000 | [diff] [blame] | 1856 | struct net_device *rtnl_create_link(struct net *net, |
Tom Gundersen | 5517750 | 2014-07-14 16:37:25 +0200 | [diff] [blame] | 1857 | char *ifname, unsigned char name_assign_type, |
| 1858 | const struct rtnl_link_ops *ops, struct nlattr *tb[]) |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1859 | { |
| 1860 | int err; |
| 1861 | struct net_device *dev; |
Jiri Pirko | d40156a | 2012-07-20 02:28:47 +0000 | [diff] [blame] | 1862 | unsigned int num_tx_queues = 1; |
| 1863 | unsigned int num_rx_queues = 1; |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1864 | |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 1865 | if (tb[IFLA_NUM_TX_QUEUES]) |
| 1866 | num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); |
| 1867 | else if (ops->get_num_tx_queues) |
Jiri Pirko | d40156a | 2012-07-20 02:28:47 +0000 | [diff] [blame] | 1868 | num_tx_queues = ops->get_num_tx_queues(); |
Jiri Pirko | 76ff5cc | 2012-07-20 02:28:48 +0000 | [diff] [blame] | 1869 | |
| 1870 | if (tb[IFLA_NUM_RX_QUEUES]) |
| 1871 | num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); |
| 1872 | else if (ops->get_num_rx_queues) |
Jiri Pirko | d40156a | 2012-07-20 02:28:47 +0000 | [diff] [blame] | 1873 | num_rx_queues = ops->get_num_rx_queues(); |
stephen hemminger | efacb30 | 2012-04-10 18:34:43 +0000 | [diff] [blame] | 1874 | |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1875 | err = -ENOMEM; |
Tom Gundersen | 5517750 | 2014-07-14 16:37:25 +0200 | [diff] [blame] | 1876 | dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type, |
Tom Gundersen | c835a67 | 2014-07-14 16:37:24 +0200 | [diff] [blame] | 1877 | ops->setup, num_tx_queues, num_rx_queues); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1878 | if (!dev) |
| 1879 | goto err; |
| 1880 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1881 | dev_net_set(dev, net); |
| 1882 | dev->rtnl_link_ops = ops; |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 1883 | dev->rtnl_link_state = RTNL_LINK_INITIALIZING; |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1884 | |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1885 | if (tb[IFLA_MTU]) |
| 1886 | dev->mtu = nla_get_u32(tb[IFLA_MTU]); |
Jiri Pirko | 2afb9b5 | 2013-01-06 12:41:57 +0000 | [diff] [blame] | 1887 | if (tb[IFLA_ADDRESS]) { |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1888 | memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), |
| 1889 | nla_len(tb[IFLA_ADDRESS])); |
Jiri Pirko | 2afb9b5 | 2013-01-06 12:41:57 +0000 | [diff] [blame] | 1890 | dev->addr_assign_type = NET_ADDR_SET; |
| 1891 | } |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1892 | if (tb[IFLA_BROADCAST]) |
| 1893 | memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), |
| 1894 | nla_len(tb[IFLA_BROADCAST])); |
| 1895 | if (tb[IFLA_TXQLEN]) |
| 1896 | dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); |
| 1897 | if (tb[IFLA_OPERSTATE]) |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 1898 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1899 | if (tb[IFLA_LINKMODE]) |
| 1900 | dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); |
Patrick McHardy | ffa934f | 2011-01-20 03:00:42 +0000 | [diff] [blame] | 1901 | if (tb[IFLA_GROUP]) |
| 1902 | dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1903 | |
| 1904 | return dev; |
| 1905 | |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1906 | err: |
| 1907 | return ERR_PTR(err); |
| 1908 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 1909 | EXPORT_SYMBOL(rtnl_create_link); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1910 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 1911 | static int rtnl_group_changelink(const struct sk_buff *skb, |
| 1912 | struct net *net, int group, |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 1913 | struct ifinfomsg *ifm, |
| 1914 | struct nlattr **tb) |
| 1915 | { |
| 1916 | struct net_device *dev; |
| 1917 | int err; |
| 1918 | |
| 1919 | for_each_netdev(net, dev) { |
| 1920 | if (dev->group == group) { |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 1921 | err = do_setlink(skb, dev, ifm, tb, NULL, 0); |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 1922 | if (err < 0) |
| 1923 | return err; |
| 1924 | } |
| 1925 | } |
| 1926 | |
| 1927 | return 0; |
| 1928 | } |
| 1929 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 1930 | static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1931 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1932 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1933 | const struct rtnl_link_ops *ops; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 1934 | const struct rtnl_link_ops *m_ops = NULL; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1935 | struct net_device *dev; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 1936 | struct net_device *master_dev = NULL; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1937 | struct ifinfomsg *ifm; |
| 1938 | char kind[MODULE_NAME_LEN]; |
| 1939 | char ifname[IFNAMSIZ]; |
| 1940 | struct nlattr *tb[IFLA_MAX+1]; |
| 1941 | struct nlattr *linkinfo[IFLA_INFO_MAX+1]; |
Tom Gundersen | 5517750 | 2014-07-14 16:37:25 +0200 | [diff] [blame] | 1942 | unsigned char name_assign_type = NET_NAME_USER; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1943 | int err; |
| 1944 | |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 1945 | #ifdef CONFIG_MODULES |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1946 | replay: |
Thomas Graf | 8072f08 | 2007-07-31 14:13:50 -0700 | [diff] [blame] | 1947 | #endif |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1948 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 1949 | if (err < 0) |
| 1950 | return err; |
| 1951 | |
| 1952 | if (tb[IFLA_IFNAME]) |
| 1953 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
| 1954 | else |
| 1955 | ifname[0] = '\0'; |
| 1956 | |
| 1957 | ifm = nlmsg_data(nlh); |
| 1958 | if (ifm->ifi_index > 0) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1959 | dev = __dev_get_by_index(net, ifm->ifi_index); |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 1960 | else { |
| 1961 | if (ifname[0]) |
| 1962 | dev = __dev_get_by_name(net, ifname); |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 1963 | else |
| 1964 | dev = NULL; |
| 1965 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1966 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 1967 | if (dev) { |
| 1968 | master_dev = netdev_master_upper_dev_get(dev); |
| 1969 | if (master_dev) |
| 1970 | m_ops = master_dev->rtnl_link_ops; |
| 1971 | } |
| 1972 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 1973 | err = validate_linkmsg(dev, tb); |
| 1974 | if (err < 0) |
Thomas Graf | 1840bb1 | 2008-02-23 19:54:36 -0800 | [diff] [blame] | 1975 | return err; |
| 1976 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1977 | if (tb[IFLA_LINKINFO]) { |
| 1978 | err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, |
| 1979 | tb[IFLA_LINKINFO], ifla_info_policy); |
| 1980 | if (err < 0) |
| 1981 | return err; |
| 1982 | } else |
| 1983 | memset(linkinfo, 0, sizeof(linkinfo)); |
| 1984 | |
| 1985 | if (linkinfo[IFLA_INFO_KIND]) { |
| 1986 | nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); |
| 1987 | ops = rtnl_link_ops_get(kind); |
| 1988 | } else { |
| 1989 | kind[0] = '\0'; |
| 1990 | ops = NULL; |
| 1991 | } |
| 1992 | |
| 1993 | if (1) { |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 1994 | struct nlattr *attr[ops ? ops->maxtype + 1 : 0]; |
| 1995 | struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 0]; |
| 1996 | struct nlattr **data = NULL; |
| 1997 | struct nlattr **slave_data = NULL; |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1998 | struct net *dest_net; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1999 | |
| 2000 | if (ops) { |
| 2001 | if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { |
| 2002 | err = nla_parse_nested(attr, ops->maxtype, |
| 2003 | linkinfo[IFLA_INFO_DATA], |
| 2004 | ops->policy); |
| 2005 | if (err < 0) |
| 2006 | return err; |
| 2007 | data = attr; |
| 2008 | } |
| 2009 | if (ops->validate) { |
| 2010 | err = ops->validate(tb, data); |
| 2011 | if (err < 0) |
| 2012 | return err; |
| 2013 | } |
| 2014 | } |
| 2015 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 2016 | if (m_ops) { |
| 2017 | if (m_ops->slave_maxtype && |
| 2018 | linkinfo[IFLA_INFO_SLAVE_DATA]) { |
| 2019 | err = nla_parse_nested(slave_attr, |
| 2020 | m_ops->slave_maxtype, |
| 2021 | linkinfo[IFLA_INFO_SLAVE_DATA], |
| 2022 | m_ops->slave_policy); |
| 2023 | if (err < 0) |
| 2024 | return err; |
| 2025 | slave_data = slave_attr; |
| 2026 | } |
| 2027 | if (m_ops->slave_validate) { |
| 2028 | err = m_ops->slave_validate(tb, slave_data); |
| 2029 | if (err < 0) |
| 2030 | return err; |
| 2031 | } |
| 2032 | } |
| 2033 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2034 | if (dev) { |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 2035 | int status = 0; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2036 | |
| 2037 | if (nlh->nlmsg_flags & NLM_F_EXCL) |
| 2038 | return -EEXIST; |
| 2039 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
| 2040 | return -EOPNOTSUPP; |
| 2041 | |
| 2042 | if (linkinfo[IFLA_INFO_DATA]) { |
| 2043 | if (!ops || ops != dev->rtnl_link_ops || |
| 2044 | !ops->changelink) |
| 2045 | return -EOPNOTSUPP; |
| 2046 | |
| 2047 | err = ops->changelink(dev, tb, data); |
| 2048 | if (err < 0) |
| 2049 | return err; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2050 | status |= DO_SETLINK_NOTIFY; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2051 | } |
| 2052 | |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 2053 | if (linkinfo[IFLA_INFO_SLAVE_DATA]) { |
| 2054 | if (!m_ops || !m_ops->slave_changelink) |
| 2055 | return -EOPNOTSUPP; |
| 2056 | |
| 2057 | err = m_ops->slave_changelink(master_dev, dev, |
| 2058 | tb, slave_data); |
| 2059 | if (err < 0) |
| 2060 | return err; |
Nicolas Dichtel | ba99890 | 2014-09-01 16:07:29 +0200 | [diff] [blame] | 2061 | status |= DO_SETLINK_NOTIFY; |
Jiri Pirko | ba7d49b | 2014-01-22 09:05:55 +0100 | [diff] [blame] | 2062 | } |
| 2063 | |
Nicolas Dichtel | 90c325e | 2014-09-01 16:07:28 +0200 | [diff] [blame] | 2064 | return do_setlink(skb, dev, ifm, tb, ifname, status); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2065 | } |
| 2066 | |
Patrick McHardy | ffa934f | 2011-01-20 03:00:42 +0000 | [diff] [blame] | 2067 | if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { |
| 2068 | if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 2069 | return rtnl_group_changelink(skb, net, |
Patrick McHardy | ffa934f | 2011-01-20 03:00:42 +0000 | [diff] [blame] | 2070 | nla_get_u32(tb[IFLA_GROUP]), |
| 2071 | ifm, tb); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2072 | return -ENODEV; |
Patrick McHardy | ffa934f | 2011-01-20 03:00:42 +0000 | [diff] [blame] | 2073 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2074 | |
Patrick McHardy | 0e06877 | 2007-07-11 19:42:31 -0700 | [diff] [blame] | 2075 | if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO]) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2076 | return -EOPNOTSUPP; |
| 2077 | |
| 2078 | if (!ops) { |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 2079 | #ifdef CONFIG_MODULES |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2080 | if (kind[0]) { |
| 2081 | __rtnl_unlock(); |
| 2082 | request_module("rtnl-link-%s", kind); |
| 2083 | rtnl_lock(); |
| 2084 | ops = rtnl_link_ops_get(kind); |
| 2085 | if (ops) |
| 2086 | goto replay; |
| 2087 | } |
| 2088 | #endif |
| 2089 | return -EOPNOTSUPP; |
| 2090 | } |
| 2091 | |
Jiri Pirko | b0ab2fa | 2014-06-26 09:58:25 +0200 | [diff] [blame] | 2092 | if (!ops->setup) |
| 2093 | return -EOPNOTSUPP; |
| 2094 | |
Tom Gundersen | 5517750 | 2014-07-14 16:37:25 +0200 | [diff] [blame] | 2095 | if (!ifname[0]) { |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2096 | snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); |
Tom Gundersen | 5517750 | 2014-07-14 16:37:25 +0200 | [diff] [blame] | 2097 | name_assign_type = NET_NAME_ENUM; |
| 2098 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2099 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 2100 | dest_net = rtnl_link_get_net(net, tb); |
Eric W. Biederman | 13ad177 | 2011-01-29 14:57:22 +0000 | [diff] [blame] | 2101 | if (IS_ERR(dest_net)) |
| 2102 | return PTR_ERR(dest_net); |
| 2103 | |
Tom Gundersen | 5517750 | 2014-07-14 16:37:25 +0200 | [diff] [blame] | 2104 | dev = rtnl_create_link(dest_net, ifname, name_assign_type, ops, tb); |
Pavel Emelyanov | 9c7dafb | 2012-08-08 21:52:46 +0000 | [diff] [blame] | 2105 | if (IS_ERR(dev)) { |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 2106 | err = PTR_ERR(dev); |
Pavel Emelyanov | 9c7dafb | 2012-08-08 21:52:46 +0000 | [diff] [blame] | 2107 | goto out; |
| 2108 | } |
| 2109 | |
| 2110 | dev->ifindex = ifm->ifi_index; |
| 2111 | |
Cong Wang | 0e0eee2 | 2014-02-11 15:51:30 -0800 | [diff] [blame] | 2112 | if (ops->newlink) { |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 2113 | err = ops->newlink(net, dev, tb, data); |
Cong Wang | 0e0eee2 | 2014-02-11 15:51:30 -0800 | [diff] [blame] | 2114 | /* Drivers should call free_netdev() in ->destructor |
Cong Wang | e51fb15 | 2014-06-03 16:40:47 -0700 | [diff] [blame] | 2115 | * and unregister it on failure after registration |
| 2116 | * so that device could be finally freed in rtnl_unlock. |
Cong Wang | 0e0eee2 | 2014-02-11 15:51:30 -0800 | [diff] [blame] | 2117 | */ |
Cong Wang | e51fb15 | 2014-06-03 16:40:47 -0700 | [diff] [blame] | 2118 | if (err < 0) { |
| 2119 | /* If device is not registered at all, free it now */ |
| 2120 | if (dev->reg_state == NETREG_UNINITIALIZED) |
| 2121 | free_netdev(dev); |
Cong Wang | 0e0eee2 | 2014-02-11 15:51:30 -0800 | [diff] [blame] | 2122 | goto out; |
Cong Wang | e51fb15 | 2014-06-03 16:40:47 -0700 | [diff] [blame] | 2123 | } |
Cong Wang | 0e0eee2 | 2014-02-11 15:51:30 -0800 | [diff] [blame] | 2124 | } else { |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 2125 | err = register_netdevice(dev); |
Cong Wang | 0e0eee2 | 2014-02-11 15:51:30 -0800 | [diff] [blame] | 2126 | if (err < 0) { |
| 2127 | free_netdev(dev); |
| 2128 | goto out; |
| 2129 | } |
Dan Carpenter | fce9b9b | 2013-08-14 12:35:42 +0300 | [diff] [blame] | 2130 | } |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 2131 | err = rtnl_configure_link(dev, ifm); |
| 2132 | if (err < 0) |
| 2133 | unregister_netdevice(dev); |
| 2134 | out: |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 2135 | put_net(dest_net); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 2136 | return err; |
| 2137 | } |
| 2138 | } |
| 2139 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 2140 | static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh) |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2141 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2142 | struct net *net = sock_net(skb->sk); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2143 | struct ifinfomsg *ifm; |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2144 | char ifname[IFNAMSIZ]; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2145 | struct nlattr *tb[IFLA_MAX+1]; |
| 2146 | struct net_device *dev = NULL; |
| 2147 | struct sk_buff *nskb; |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 2148 | int err; |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2149 | u32 ext_filter_mask = 0; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2150 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2151 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 2152 | if (err < 0) |
Eric Sesterhenn | 9918f23 | 2006-09-26 23:26:38 -0700 | [diff] [blame] | 2153 | return err; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2154 | |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2155 | if (tb[IFLA_IFNAME]) |
| 2156 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
| 2157 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2158 | if (tb[IFLA_EXT_MASK]) |
| 2159 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); |
| 2160 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2161 | ifm = nlmsg_data(nlh); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2162 | if (ifm->ifi_index > 0) |
| 2163 | dev = __dev_get_by_index(net, ifm->ifi_index); |
| 2164 | else if (tb[IFLA_IFNAME]) |
| 2165 | dev = __dev_get_by_name(net, ifname); |
| 2166 | else |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2167 | return -EINVAL; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2168 | |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2169 | if (dev == NULL) |
| 2170 | return -ENODEV; |
| 2171 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2172 | nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2173 | if (nskb == NULL) |
| 2174 | return -ENOBUFS; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2175 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2176 | err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid, |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2177 | nlh->nlmsg_seq, 0, 0, ext_filter_mask); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 2178 | if (err < 0) { |
| 2179 | /* -EMSGSIZE implies BUG in if_nlmsg_size */ |
| 2180 | WARN_ON(err == -EMSGSIZE); |
| 2181 | kfree_skb(nskb); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 2182 | } else |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2183 | err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 2184 | |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2185 | return err; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2186 | } |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 2187 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2188 | static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2189 | { |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2190 | struct net *net = sock_net(skb->sk); |
| 2191 | struct net_device *dev; |
| 2192 | struct nlattr *tb[IFLA_MAX+1]; |
| 2193 | u32 ext_filter_mask = 0; |
| 2194 | u16 min_ifinfo_dump_size = 0; |
Michal Schmidt | e5eca6d | 2014-05-28 14:15:19 +0200 | [diff] [blame] | 2195 | int hdrlen; |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2196 | |
Michal Schmidt | e5eca6d | 2014-05-28 14:15:19 +0200 | [diff] [blame] | 2197 | /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ |
| 2198 | hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? |
| 2199 | sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); |
| 2200 | |
| 2201 | if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) { |
Eric Dumazet | a4b64fb | 2012-03-04 12:32:10 +0000 | [diff] [blame] | 2202 | if (tb[IFLA_EXT_MASK]) |
| 2203 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); |
| 2204 | } |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2205 | |
| 2206 | if (!ext_filter_mask) |
| 2207 | return NLMSG_GOODSIZE; |
| 2208 | /* |
| 2209 | * traverse the list of net devices and compute the minimum |
| 2210 | * buffer size based upon the filter mask. |
| 2211 | */ |
| 2212 | list_for_each_entry(dev, &net->dev_base_head, dev_list) { |
| 2213 | min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size, |
| 2214 | if_nlmsg_size(dev, |
| 2215 | ext_filter_mask)); |
| 2216 | } |
| 2217 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2218 | return min_ifinfo_dump_size; |
| 2219 | } |
| 2220 | |
Adrian Bunk | 42bad1d | 2007-04-26 00:57:41 -0700 | [diff] [blame] | 2221 | 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] | 2222 | { |
| 2223 | int idx; |
| 2224 | int s_idx = cb->family; |
| 2225 | |
| 2226 | if (s_idx == 0) |
| 2227 | s_idx = 1; |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 2228 | for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2229 | int type = cb->nlh->nlmsg_type-RTM_BASE; |
| 2230 | if (idx < s_idx || idx == PF_PACKET) |
| 2231 | continue; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 2232 | if (rtnl_msg_handlers[idx] == NULL || |
| 2233 | rtnl_msg_handlers[idx][type].dumpit == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | continue; |
Nicolas Dichtel | 0465277 | 2013-03-22 06:28:42 +0000 | [diff] [blame] | 2235 | if (idx > s_idx) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | memset(&cb->args[0], 0, sizeof(cb->args)); |
Nicolas Dichtel | 0465277 | 2013-03-22 06:28:42 +0000 | [diff] [blame] | 2237 | cb->prev_seq = 0; |
| 2238 | cb->seq = 0; |
| 2239 | } |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 2240 | if (rtnl_msg_handlers[idx][type].dumpit(skb, cb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | break; |
| 2242 | } |
| 2243 | cb->family = idx; |
| 2244 | |
| 2245 | return skb->len; |
| 2246 | } |
| 2247 | |
Alexei Starovoitov | 7f29405 | 2013-10-23 16:02:42 -0700 | [diff] [blame] | 2248 | void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, |
| 2249 | gfp_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2250 | { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2251 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | struct sk_buff *skb; |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 2253 | int err = -ENOBUFS; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2254 | size_t if_info_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | |
Alexei Starovoitov | 7f29405 | 2013-10-23 16:02:42 -0700 | [diff] [blame] | 2256 | skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags); |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 2257 | if (skb == NULL) |
| 2258 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2260 | err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 2261 | if (err < 0) { |
| 2262 | /* -EMSGSIZE implies BUG in if_nlmsg_size() */ |
| 2263 | WARN_ON(err == -EMSGSIZE); |
| 2264 | kfree_skb(skb); |
| 2265 | goto errout; |
| 2266 | } |
Alexei Starovoitov | 7f29405 | 2013-10-23 16:02:42 -0700 | [diff] [blame] | 2267 | rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 2268 | return; |
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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | } |
Jiri Pirko | 471cb5a | 2013-01-03 22:49:01 +0000 | [diff] [blame] | 2273 | EXPORT_SYMBOL(rtmsg_ifinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2275 | static int nlmsg_populate_fdb_fill(struct sk_buff *skb, |
| 2276 | struct net_device *dev, |
| 2277 | u8 *addr, u32 pid, u32 seq, |
Nicolas Dichtel | 1c104a6 | 2014-03-19 17:47:49 +0100 | [diff] [blame] | 2278 | int type, unsigned int flags, |
| 2279 | int nlflags) |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2280 | { |
| 2281 | struct nlmsghdr *nlh; |
| 2282 | struct ndmsg *ndm; |
| 2283 | |
Nicolas Dichtel | 1c104a6 | 2014-03-19 17:47:49 +0100 | [diff] [blame] | 2284 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2285 | if (!nlh) |
| 2286 | return -EMSGSIZE; |
| 2287 | |
| 2288 | ndm = nlmsg_data(nlh); |
| 2289 | ndm->ndm_family = AF_BRIDGE; |
| 2290 | ndm->ndm_pad1 = 0; |
| 2291 | ndm->ndm_pad2 = 0; |
| 2292 | ndm->ndm_flags = flags; |
| 2293 | ndm->ndm_type = 0; |
| 2294 | ndm->ndm_ifindex = dev->ifindex; |
| 2295 | ndm->ndm_state = NUD_PERMANENT; |
| 2296 | |
| 2297 | if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) |
| 2298 | goto nla_put_failure; |
| 2299 | |
| 2300 | return nlmsg_end(skb, nlh); |
| 2301 | |
| 2302 | nla_put_failure: |
| 2303 | nlmsg_cancel(skb, nlh); |
| 2304 | return -EMSGSIZE; |
| 2305 | } |
| 2306 | |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2307 | static inline size_t rtnl_fdb_nlmsg_size(void) |
| 2308 | { |
| 2309 | return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN); |
| 2310 | } |
| 2311 | |
| 2312 | static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type) |
| 2313 | { |
| 2314 | struct net *net = dev_net(dev); |
| 2315 | struct sk_buff *skb; |
| 2316 | int err = -ENOBUFS; |
| 2317 | |
| 2318 | skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); |
| 2319 | if (!skb) |
| 2320 | goto errout; |
| 2321 | |
Nicolas Dichtel | 1c104a6 | 2014-03-19 17:47:49 +0100 | [diff] [blame] | 2322 | 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] | 2323 | if (err < 0) { |
| 2324 | kfree_skb(skb); |
| 2325 | goto errout; |
| 2326 | } |
| 2327 | |
| 2328 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); |
| 2329 | return; |
| 2330 | errout: |
| 2331 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); |
| 2332 | } |
| 2333 | |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2334 | /** |
| 2335 | * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry |
| 2336 | */ |
| 2337 | int ndo_dflt_fdb_add(struct ndmsg *ndm, |
| 2338 | struct nlattr *tb[], |
| 2339 | struct net_device *dev, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2340 | const unsigned char *addr, u16 vid, |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2341 | u16 flags) |
| 2342 | { |
| 2343 | int err = -EINVAL; |
| 2344 | |
| 2345 | /* If aging addresses are supported device will need to |
| 2346 | * implement its own handler for this. |
| 2347 | */ |
| 2348 | if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { |
| 2349 | pr_info("%s: FDB only supports static addresses\n", dev->name); |
| 2350 | return err; |
| 2351 | } |
| 2352 | |
| 2353 | if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) |
| 2354 | err = dev_uc_add_excl(dev, addr); |
| 2355 | else if (is_multicast_ether_addr(addr)) |
| 2356 | err = dev_mc_add_excl(dev, addr); |
| 2357 | |
| 2358 | /* Only return duplicate errors if NLM_F_EXCL is set */ |
| 2359 | if (err == -EEXIST && !(flags & NLM_F_EXCL)) |
| 2360 | err = 0; |
| 2361 | |
| 2362 | return err; |
| 2363 | } |
| 2364 | EXPORT_SYMBOL(ndo_dflt_fdb_add); |
| 2365 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2366 | static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid) |
| 2367 | { |
| 2368 | u16 vid = 0; |
| 2369 | |
| 2370 | if (vlan_attr) { |
| 2371 | if (nla_len(vlan_attr) != sizeof(u16)) { |
| 2372 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n"); |
| 2373 | return -EINVAL; |
| 2374 | } |
| 2375 | |
| 2376 | vid = nla_get_u16(vlan_attr); |
| 2377 | |
| 2378 | if (!vid || vid >= VLAN_VID_MASK) { |
| 2379 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n", |
| 2380 | vid); |
| 2381 | return -EINVAL; |
| 2382 | } |
| 2383 | } |
| 2384 | *p_vid = vid; |
| 2385 | return 0; |
| 2386 | } |
| 2387 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 2388 | static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh) |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2389 | { |
| 2390 | struct net *net = sock_net(skb->sk); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2391 | struct ndmsg *ndm; |
| 2392 | struct nlattr *tb[NDA_MAX+1]; |
| 2393 | struct net_device *dev; |
| 2394 | u8 *addr; |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2395 | u16 vid; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2396 | int err; |
| 2397 | |
| 2398 | err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL); |
| 2399 | if (err < 0) |
| 2400 | return err; |
| 2401 | |
| 2402 | ndm = nlmsg_data(nlh); |
| 2403 | if (ndm->ndm_ifindex == 0) { |
| 2404 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n"); |
| 2405 | return -EINVAL; |
| 2406 | } |
| 2407 | |
| 2408 | dev = __dev_get_by_index(net, ndm->ndm_ifindex); |
| 2409 | if (dev == NULL) { |
| 2410 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n"); |
| 2411 | return -ENODEV; |
| 2412 | } |
| 2413 | |
| 2414 | if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { |
| 2415 | pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n"); |
| 2416 | return -EINVAL; |
| 2417 | } |
| 2418 | |
| 2419 | addr = nla_data(tb[NDA_LLADDR]); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2420 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2421 | err = fdb_vid_parse(tb[NDA_VLAN], &vid); |
| 2422 | if (err) |
| 2423 | return err; |
| 2424 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2425 | err = -EOPNOTSUPP; |
| 2426 | |
| 2427 | /* Support fdb on master device the net/bridge default case */ |
| 2428 | if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && |
| 2429 | (dev->priv_flags & IFF_BRIDGE_PORT)) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2430 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
| 2431 | const struct net_device_ops *ops = br_dev->netdev_ops; |
| 2432 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2433 | err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, |
| 2434 | nlh->nlmsg_flags); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2435 | if (err) |
| 2436 | goto out; |
| 2437 | else |
| 2438 | ndm->ndm_flags &= ~NTF_MASTER; |
| 2439 | } |
| 2440 | |
| 2441 | /* Embedded bridge, macvlan, and any other device support */ |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2442 | if ((ndm->ndm_flags & NTF_SELF)) { |
| 2443 | if (dev->netdev_ops->ndo_fdb_add) |
| 2444 | err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2445 | vid, |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2446 | nlh->nlmsg_flags); |
| 2447 | else |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2448 | err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2449 | nlh->nlmsg_flags); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2450 | |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2451 | if (!err) { |
| 2452 | rtnl_fdb_notify(dev, addr, RTM_NEWNEIGH); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2453 | ndm->ndm_flags &= ~NTF_SELF; |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2454 | } |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2455 | } |
| 2456 | out: |
| 2457 | return err; |
| 2458 | } |
| 2459 | |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2460 | /** |
| 2461 | * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry |
| 2462 | */ |
| 2463 | int ndo_dflt_fdb_del(struct ndmsg *ndm, |
| 2464 | struct nlattr *tb[], |
| 2465 | struct net_device *dev, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2466 | const unsigned char *addr, u16 vid) |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2467 | { |
Alexander Duyck | c8a89c4 | 2014-07-15 15:15:20 -0700 | [diff] [blame] | 2468 | int err = -EINVAL; |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2469 | |
| 2470 | /* If aging addresses are supported device will need to |
| 2471 | * implement its own handler for this. |
| 2472 | */ |
Sridhar Samudrala | 6453599 | 2013-08-08 15:19:48 -0700 | [diff] [blame] | 2473 | if (!(ndm->ndm_state & NUD_PERMANENT)) { |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2474 | pr_info("%s: FDB only supports static addresses\n", dev->name); |
Alexander Duyck | c8a89c4 | 2014-07-15 15:15:20 -0700 | [diff] [blame] | 2475 | return err; |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
| 2478 | if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) |
| 2479 | err = dev_uc_del(dev, addr); |
| 2480 | else if (is_multicast_ether_addr(addr)) |
| 2481 | err = dev_mc_del(dev, addr); |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2482 | |
| 2483 | return err; |
| 2484 | } |
| 2485 | EXPORT_SYMBOL(ndo_dflt_fdb_del); |
| 2486 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 2487 | static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh) |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2488 | { |
| 2489 | struct net *net = sock_net(skb->sk); |
| 2490 | struct ndmsg *ndm; |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 2491 | struct nlattr *tb[NDA_MAX+1]; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2492 | struct net_device *dev; |
| 2493 | int err = -EINVAL; |
| 2494 | __u8 *addr; |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2495 | u16 vid; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2496 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 2497 | if (!netlink_capable(skb, CAP_NET_ADMIN)) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 2498 | return -EPERM; |
| 2499 | |
| 2500 | err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL); |
| 2501 | if (err < 0) |
| 2502 | return err; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2503 | |
| 2504 | ndm = nlmsg_data(nlh); |
| 2505 | if (ndm->ndm_ifindex == 0) { |
| 2506 | pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n"); |
| 2507 | return -EINVAL; |
| 2508 | } |
| 2509 | |
| 2510 | dev = __dev_get_by_index(net, ndm->ndm_ifindex); |
| 2511 | if (dev == NULL) { |
| 2512 | pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n"); |
| 2513 | return -ENODEV; |
| 2514 | } |
| 2515 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 2516 | if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { |
| 2517 | pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n"); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2518 | return -EINVAL; |
| 2519 | } |
| 2520 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 2521 | addr = nla_data(tb[NDA_LLADDR]); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 2522 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2523 | err = fdb_vid_parse(tb[NDA_VLAN], &vid); |
| 2524 | if (err) |
| 2525 | return err; |
| 2526 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2527 | err = -EOPNOTSUPP; |
| 2528 | |
| 2529 | /* Support fdb on master device the net/bridge default case */ |
| 2530 | if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && |
| 2531 | (dev->priv_flags & IFF_BRIDGE_PORT)) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2532 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
| 2533 | const struct net_device_ops *ops = br_dev->netdev_ops; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2534 | |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2535 | if (ops->ndo_fdb_del) |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2536 | err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2537 | |
| 2538 | if (err) |
| 2539 | goto out; |
| 2540 | else |
| 2541 | ndm->ndm_flags &= ~NTF_MASTER; |
| 2542 | } |
| 2543 | |
| 2544 | /* Embedded bridge, macvlan, and any other device support */ |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2545 | if (ndm->ndm_flags & NTF_SELF) { |
| 2546 | if (dev->netdev_ops->ndo_fdb_del) |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2547 | err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, |
| 2548 | vid); |
Vlad Yasevich | 090096b | 2013-03-06 15:39:42 +0000 | [diff] [blame] | 2549 | else |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 2550 | err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2551 | |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2552 | if (!err) { |
| 2553 | rtnl_fdb_notify(dev, addr, RTM_DELNEIGH); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2554 | ndm->ndm_flags &= ~NTF_SELF; |
John Fastabend | 3ff661c | 2012-04-15 06:44:14 +0000 | [diff] [blame] | 2555 | } |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2556 | } |
| 2557 | out: |
| 2558 | return err; |
| 2559 | } |
| 2560 | |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2561 | static int nlmsg_populate_fdb(struct sk_buff *skb, |
| 2562 | struct netlink_callback *cb, |
| 2563 | struct net_device *dev, |
| 2564 | int *idx, |
| 2565 | struct netdev_hw_addr_list *list) |
| 2566 | { |
| 2567 | struct netdev_hw_addr *ha; |
| 2568 | int err; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2569 | u32 portid, seq; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2570 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2571 | portid = NETLINK_CB(cb->skb).portid; |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2572 | seq = cb->nlh->nlmsg_seq; |
| 2573 | |
| 2574 | list_for_each_entry(ha, &list->list, list) { |
| 2575 | if (*idx < cb->args[0]) |
| 2576 | goto skip; |
| 2577 | |
| 2578 | err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, |
John Fastabend | a7a558f | 2012-11-01 16:23:10 +0000 | [diff] [blame] | 2579 | portid, seq, |
Nicolas Dichtel | 1c104a6 | 2014-03-19 17:47:49 +0100 | [diff] [blame] | 2580 | RTM_NEWNEIGH, NTF_SELF, |
| 2581 | NLM_F_MULTI); |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2582 | if (err < 0) |
| 2583 | return err; |
| 2584 | skip: |
| 2585 | *idx += 1; |
| 2586 | } |
| 2587 | return 0; |
| 2588 | } |
| 2589 | |
| 2590 | /** |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 2591 | * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2592 | * @nlh: netlink message header |
| 2593 | * @dev: netdevice |
| 2594 | * |
| 2595 | * Default netdevice operation to dump the existing unicast address list. |
John Fastabend | 91f3e7b | 2013-03-29 08:18:37 +0000 | [diff] [blame] | 2596 | * Returns number of addresses from list put in skb. |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2597 | */ |
| 2598 | int ndo_dflt_fdb_dump(struct sk_buff *skb, |
| 2599 | struct netlink_callback *cb, |
| 2600 | struct net_device *dev, |
Jamal Hadi Salim | 5d5eacb | 2014-07-10 07:01:58 -0400 | [diff] [blame] | 2601 | struct net_device *filter_dev, |
John Fastabend | d83b060 | 2012-04-15 06:44:08 +0000 | [diff] [blame] | 2602 | int idx) |
| 2603 | { |
| 2604 | int err; |
| 2605 | |
| 2606 | netif_addr_lock_bh(dev); |
| 2607 | err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc); |
| 2608 | if (err) |
| 2609 | goto out; |
| 2610 | nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc); |
| 2611 | out: |
| 2612 | netif_addr_unlock_bh(dev); |
| 2613 | return idx; |
| 2614 | } |
| 2615 | EXPORT_SYMBOL(ndo_dflt_fdb_dump); |
| 2616 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2617 | static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 2618 | { |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2619 | struct net_device *dev; |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 2620 | struct nlattr *tb[IFLA_MAX+1]; |
| 2621 | struct net_device *bdev = NULL; |
| 2622 | struct net_device *br_dev = NULL; |
| 2623 | const struct net_device_ops *ops = NULL; |
| 2624 | const struct net_device_ops *cops = NULL; |
| 2625 | struct ifinfomsg *ifm = nlmsg_data(cb->nlh); |
| 2626 | struct net *net = sock_net(skb->sk); |
| 2627 | int brport_idx = 0; |
| 2628 | int br_idx = 0; |
| 2629 | int idx = 0; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2630 | |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 2631 | if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX, |
| 2632 | ifla_policy) == 0) { |
| 2633 | if (tb[IFLA_MASTER]) |
| 2634 | br_idx = nla_get_u32(tb[IFLA_MASTER]); |
| 2635 | } |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2636 | |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 2637 | brport_idx = ifm->ifi_index; |
| 2638 | |
| 2639 | if (br_idx) { |
| 2640 | br_dev = __dev_get_by_index(net, br_idx); |
| 2641 | if (!br_dev) |
| 2642 | return -ENODEV; |
| 2643 | |
| 2644 | ops = br_dev->netdev_ops; |
| 2645 | bdev = br_dev; |
| 2646 | } |
| 2647 | |
| 2648 | for_each_netdev(net, dev) { |
| 2649 | if (brport_idx && (dev->ifindex != brport_idx)) |
| 2650 | continue; |
| 2651 | |
| 2652 | if (!br_idx) { /* user did not specify a specific bridge */ |
| 2653 | if (dev->priv_flags & IFF_BRIDGE_PORT) { |
| 2654 | br_dev = netdev_master_upper_dev_get(dev); |
| 2655 | cops = br_dev->netdev_ops; |
| 2656 | } |
| 2657 | |
| 2658 | bdev = dev; |
| 2659 | } else { |
| 2660 | if (dev != br_dev && |
| 2661 | !(dev->priv_flags & IFF_BRIDGE_PORT)) |
| 2662 | continue; |
| 2663 | |
| 2664 | if (br_dev != netdev_master_upper_dev_get(dev) && |
| 2665 | !(dev->priv_flags & IFF_EBRIDGE)) |
| 2666 | continue; |
| 2667 | |
| 2668 | bdev = br_dev; |
| 2669 | cops = ops; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2670 | } |
| 2671 | |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 2672 | if (dev->priv_flags & IFF_BRIDGE_PORT) { |
| 2673 | if (cops && cops->ndo_fdb_dump) |
| 2674 | idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev, |
| 2675 | idx); |
| 2676 | } |
| 2677 | |
| 2678 | idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2679 | if (dev->netdev_ops->ndo_fdb_dump) |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 2680 | idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, bdev, dev, |
Jamal Hadi Salim | 5d5eacb | 2014-07-10 07:01:58 -0400 | [diff] [blame] | 2681 | idx); |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 2682 | |
| 2683 | cops = NULL; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2684 | } |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 2685 | |
| 2686 | cb->args[0] = idx; |
| 2687 | return skb->len; |
| 2688 | } |
| 2689 | |
Scott Feldman | 2c3c031 | 2014-11-28 14:34:25 +0100 | [diff] [blame^] | 2690 | static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, |
| 2691 | unsigned int attrnum, unsigned int flag) |
| 2692 | { |
| 2693 | if (mask & flag) |
| 2694 | return nla_put_u8(skb, attrnum, !!(flags & flag)); |
| 2695 | return 0; |
| 2696 | } |
| 2697 | |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 2698 | 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^] | 2699 | struct net_device *dev, u16 mode, |
| 2700 | u32 flags, u32 mask) |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 2701 | { |
| 2702 | struct nlmsghdr *nlh; |
| 2703 | struct ifinfomsg *ifm; |
| 2704 | struct nlattr *br_afspec; |
Scott Feldman | 2c3c031 | 2014-11-28 14:34:25 +0100 | [diff] [blame^] | 2705 | struct nlattr *protinfo; |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 2706 | u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2707 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 2708 | |
| 2709 | nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), NLM_F_MULTI); |
| 2710 | if (nlh == NULL) |
| 2711 | return -EMSGSIZE; |
| 2712 | |
| 2713 | ifm = nlmsg_data(nlh); |
| 2714 | ifm->ifi_family = AF_BRIDGE; |
| 2715 | ifm->__ifi_pad = 0; |
| 2716 | ifm->ifi_type = dev->type; |
| 2717 | ifm->ifi_index = dev->ifindex; |
| 2718 | ifm->ifi_flags = dev_get_flags(dev); |
| 2719 | ifm->ifi_change = 0; |
| 2720 | |
| 2721 | |
| 2722 | if (nla_put_string(skb, IFLA_IFNAME, dev->name) || |
| 2723 | nla_put_u32(skb, IFLA_MTU, dev->mtu) || |
| 2724 | nla_put_u8(skb, IFLA_OPERSTATE, operstate) || |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2725 | (br_dev && |
| 2726 | nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 2727 | (dev->addr_len && |
| 2728 | nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || |
| 2729 | (dev->ifindex != dev->iflink && |
| 2730 | nla_put_u32(skb, IFLA_LINK, dev->iflink))) |
| 2731 | goto nla_put_failure; |
| 2732 | |
| 2733 | br_afspec = nla_nest_start(skb, IFLA_AF_SPEC); |
| 2734 | if (!br_afspec) |
| 2735 | goto nla_put_failure; |
| 2736 | |
| 2737 | if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF) || |
| 2738 | nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { |
| 2739 | nla_nest_cancel(skb, br_afspec); |
| 2740 | goto nla_put_failure; |
| 2741 | } |
| 2742 | nla_nest_end(skb, br_afspec); |
| 2743 | |
Scott Feldman | 2c3c031 | 2014-11-28 14:34:25 +0100 | [diff] [blame^] | 2744 | protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED); |
| 2745 | if (!protinfo) |
| 2746 | goto nla_put_failure; |
| 2747 | |
| 2748 | if (brport_nla_put_flag(skb, flags, mask, |
| 2749 | IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || |
| 2750 | brport_nla_put_flag(skb, flags, mask, |
| 2751 | IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || |
| 2752 | brport_nla_put_flag(skb, flags, mask, |
| 2753 | IFLA_BRPORT_FAST_LEAVE, |
| 2754 | BR_MULTICAST_FAST_LEAVE) || |
| 2755 | brport_nla_put_flag(skb, flags, mask, |
| 2756 | IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || |
| 2757 | brport_nla_put_flag(skb, flags, mask, |
| 2758 | IFLA_BRPORT_LEARNING, BR_LEARNING) || |
| 2759 | brport_nla_put_flag(skb, flags, mask, |
| 2760 | IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || |
| 2761 | brport_nla_put_flag(skb, flags, mask, |
| 2762 | IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || |
| 2763 | brport_nla_put_flag(skb, flags, mask, |
| 2764 | IFLA_BRPORT_PROXYARP, BR_PROXYARP)) { |
| 2765 | nla_nest_cancel(skb, protinfo); |
| 2766 | goto nla_put_failure; |
| 2767 | } |
| 2768 | |
| 2769 | nla_nest_end(skb, protinfo); |
| 2770 | |
John Fastabend | 815cccb | 2012-10-24 08:13:09 +0000 | [diff] [blame] | 2771 | return nlmsg_end(skb, nlh); |
| 2772 | nla_put_failure: |
| 2773 | nlmsg_cancel(skb, nlh); |
| 2774 | return -EMSGSIZE; |
| 2775 | } |
| 2776 | EXPORT_SYMBOL(ndo_dflt_bridge_getlink); |
| 2777 | |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 2778 | static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) |
| 2779 | { |
| 2780 | struct net *net = sock_net(skb->sk); |
| 2781 | struct net_device *dev; |
| 2782 | int idx = 0; |
| 2783 | u32 portid = NETLINK_CB(cb->skb).portid; |
| 2784 | u32 seq = cb->nlh->nlmsg_seq; |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 2785 | u32 filter_mask = 0; |
| 2786 | |
Thomas Graf | aa68c20 | 2014-11-26 13:42:20 +0100 | [diff] [blame] | 2787 | if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) { |
| 2788 | struct nlattr *extfilt; |
| 2789 | |
| 2790 | extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg), |
| 2791 | IFLA_EXT_MASK); |
| 2792 | if (extfilt) { |
| 2793 | if (nla_len(extfilt) < sizeof(filter_mask)) |
| 2794 | return -EINVAL; |
| 2795 | |
| 2796 | filter_mask = nla_get_u32(extfilt); |
| 2797 | } |
| 2798 | } |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 2799 | |
| 2800 | rcu_read_lock(); |
| 2801 | for_each_netdev_rcu(net, dev) { |
| 2802 | const struct net_device_ops *ops = dev->netdev_ops; |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2803 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 2804 | |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2805 | if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { |
Ben Hutchings | 25b1e67 | 2012-11-02 12:56:52 +0000 | [diff] [blame] | 2806 | if (idx >= cb->args[0] && |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2807 | br_dev->netdev_ops->ndo_bridge_getlink( |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 2808 | skb, portid, seq, dev, filter_mask) < 0) |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 2809 | break; |
Ben Hutchings | 25b1e67 | 2012-11-02 12:56:52 +0000 | [diff] [blame] | 2810 | idx++; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 2811 | } |
| 2812 | |
| 2813 | if (ops->ndo_bridge_getlink) { |
Ben Hutchings | 25b1e67 | 2012-11-02 12:56:52 +0000 | [diff] [blame] | 2814 | if (idx >= cb->args[0] && |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 2815 | ops->ndo_bridge_getlink(skb, portid, seq, dev, |
| 2816 | filter_mask) < 0) |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 2817 | break; |
Ben Hutchings | 25b1e67 | 2012-11-02 12:56:52 +0000 | [diff] [blame] | 2818 | idx++; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 2819 | } |
| 2820 | } |
| 2821 | rcu_read_unlock(); |
| 2822 | cb->args[0] = idx; |
| 2823 | |
| 2824 | return skb->len; |
| 2825 | } |
| 2826 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 2827 | static inline size_t bridge_nlmsg_size(void) |
| 2828 | { |
| 2829 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) |
| 2830 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ |
| 2831 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ |
| 2832 | + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ |
| 2833 | + nla_total_size(sizeof(u32)) /* IFLA_MTU */ |
| 2834 | + nla_total_size(sizeof(u32)) /* IFLA_LINK */ |
| 2835 | + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ |
| 2836 | + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ |
| 2837 | + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ |
| 2838 | + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ |
| 2839 | + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ |
| 2840 | } |
| 2841 | |
| 2842 | static int rtnl_bridge_notify(struct net_device *dev, u16 flags) |
| 2843 | { |
| 2844 | struct net *net = dev_net(dev); |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2845 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 2846 | struct sk_buff *skb; |
| 2847 | int err = -EOPNOTSUPP; |
| 2848 | |
| 2849 | skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); |
| 2850 | if (!skb) { |
| 2851 | err = -ENOMEM; |
| 2852 | goto errout; |
| 2853 | } |
| 2854 | |
John Fastabend | c38e01b | 2012-11-02 16:32:36 +0000 | [diff] [blame] | 2855 | if ((!flags || (flags & BRIDGE_FLAGS_MASTER)) && |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2856 | br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 2857 | 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] | 2858 | if (err < 0) |
| 2859 | goto errout; |
| 2860 | } |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 2861 | |
John Fastabend | c38e01b | 2012-11-02 16:32:36 +0000 | [diff] [blame] | 2862 | if ((flags & BRIDGE_FLAGS_SELF) && |
| 2863 | dev->netdev_ops->ndo_bridge_getlink) { |
Vlad Yasevich | 6cbdcee | 2013-02-13 12:00:13 +0000 | [diff] [blame] | 2864 | err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0); |
John Fastabend | c38e01b | 2012-11-02 16:32:36 +0000 | [diff] [blame] | 2865 | if (err < 0) |
| 2866 | goto errout; |
| 2867 | } |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 2868 | |
| 2869 | rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); |
| 2870 | return 0; |
| 2871 | errout: |
| 2872 | WARN_ON(err == -EMSGSIZE); |
| 2873 | kfree_skb(skb); |
| 2874 | rtnl_set_sk_err(net, RTNLGRP_LINK, err); |
| 2875 | return err; |
| 2876 | } |
| 2877 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 2878 | static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh) |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 2879 | { |
| 2880 | struct net *net = sock_net(skb->sk); |
| 2881 | struct ifinfomsg *ifm; |
| 2882 | struct net_device *dev; |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 2883 | struct nlattr *br_spec, *attr = NULL; |
| 2884 | int rem, err = -EOPNOTSUPP; |
John Fastabend | c38e01b | 2012-11-02 16:32:36 +0000 | [diff] [blame] | 2885 | u16 oflags, flags = 0; |
| 2886 | bool have_flags = false; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 2887 | |
| 2888 | if (nlmsg_len(nlh) < sizeof(*ifm)) |
| 2889 | return -EINVAL; |
| 2890 | |
| 2891 | ifm = nlmsg_data(nlh); |
| 2892 | if (ifm->ifi_family != AF_BRIDGE) |
| 2893 | return -EPFNOSUPPORT; |
| 2894 | |
| 2895 | dev = __dev_get_by_index(net, ifm->ifi_index); |
| 2896 | if (!dev) { |
| 2897 | pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n"); |
| 2898 | return -ENODEV; |
| 2899 | } |
| 2900 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 2901 | br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); |
| 2902 | if (br_spec) { |
| 2903 | nla_for_each_nested(attr, br_spec, rem) { |
| 2904 | if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { |
Thomas Graf | 6e8d1c5 | 2014-11-26 13:42:16 +0100 | [diff] [blame] | 2905 | if (nla_len(attr) < sizeof(flags)) |
| 2906 | return -EINVAL; |
| 2907 | |
John Fastabend | c38e01b | 2012-11-02 16:32:36 +0000 | [diff] [blame] | 2908 | have_flags = true; |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 2909 | flags = nla_get_u16(attr); |
| 2910 | break; |
| 2911 | } |
| 2912 | } |
| 2913 | } |
| 2914 | |
John Fastabend | c38e01b | 2012-11-02 16:32:36 +0000 | [diff] [blame] | 2915 | oflags = flags; |
| 2916 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 2917 | if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2918 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
| 2919 | |
| 2920 | if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 2921 | err = -EOPNOTSUPP; |
| 2922 | goto out; |
| 2923 | } |
| 2924 | |
Jiri Pirko | 898e506 | 2013-01-03 22:48:52 +0000 | [diff] [blame] | 2925 | err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 2926 | if (err) |
| 2927 | goto out; |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 2928 | |
| 2929 | flags &= ~BRIDGE_FLAGS_MASTER; |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 2930 | } |
| 2931 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 2932 | if ((flags & BRIDGE_FLAGS_SELF)) { |
| 2933 | if (!dev->netdev_ops->ndo_bridge_setlink) |
| 2934 | err = -EOPNOTSUPP; |
| 2935 | else |
| 2936 | err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 2937 | |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 2938 | if (!err) |
| 2939 | flags &= ~BRIDGE_FLAGS_SELF; |
| 2940 | } |
| 2941 | |
John Fastabend | c38e01b | 2012-11-02 16:32:36 +0000 | [diff] [blame] | 2942 | if (have_flags) |
John Fastabend | 2469ffd | 2012-10-24 08:13:03 +0000 | [diff] [blame] | 2943 | memcpy(nla_data(attr), &flags, sizeof(flags)); |
| 2944 | /* Generate event to notify upper layer of bridge change */ |
| 2945 | if (!err) |
John Fastabend | c38e01b | 2012-11-02 16:32:36 +0000 | [diff] [blame] | 2946 | err = rtnl_bridge_notify(dev, oflags); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 2947 | out: |
| 2948 | return err; |
| 2949 | } |
| 2950 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 2951 | static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh) |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 2952 | { |
| 2953 | struct net *net = sock_net(skb->sk); |
| 2954 | struct ifinfomsg *ifm; |
| 2955 | struct net_device *dev; |
| 2956 | struct nlattr *br_spec, *attr = NULL; |
| 2957 | int rem, err = -EOPNOTSUPP; |
| 2958 | u16 oflags, flags = 0; |
| 2959 | bool have_flags = false; |
| 2960 | |
| 2961 | if (nlmsg_len(nlh) < sizeof(*ifm)) |
| 2962 | return -EINVAL; |
| 2963 | |
| 2964 | ifm = nlmsg_data(nlh); |
| 2965 | if (ifm->ifi_family != AF_BRIDGE) |
| 2966 | return -EPFNOSUPPORT; |
| 2967 | |
| 2968 | dev = __dev_get_by_index(net, ifm->ifi_index); |
| 2969 | if (!dev) { |
| 2970 | pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n"); |
| 2971 | return -ENODEV; |
| 2972 | } |
| 2973 | |
| 2974 | br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); |
| 2975 | if (br_spec) { |
| 2976 | nla_for_each_nested(attr, br_spec, rem) { |
| 2977 | if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { |
Thomas Graf | 6e8d1c5 | 2014-11-26 13:42:16 +0100 | [diff] [blame] | 2978 | if (nla_len(attr) < sizeof(flags)) |
| 2979 | return -EINVAL; |
| 2980 | |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 2981 | have_flags = true; |
| 2982 | flags = nla_get_u16(attr); |
| 2983 | break; |
| 2984 | } |
| 2985 | } |
| 2986 | } |
| 2987 | |
| 2988 | oflags = flags; |
| 2989 | |
| 2990 | if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { |
| 2991 | struct net_device *br_dev = netdev_master_upper_dev_get(dev); |
| 2992 | |
| 2993 | if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { |
| 2994 | err = -EOPNOTSUPP; |
| 2995 | goto out; |
| 2996 | } |
| 2997 | |
| 2998 | err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh); |
| 2999 | if (err) |
| 3000 | goto out; |
| 3001 | |
| 3002 | flags &= ~BRIDGE_FLAGS_MASTER; |
| 3003 | } |
| 3004 | |
| 3005 | if ((flags & BRIDGE_FLAGS_SELF)) { |
| 3006 | if (!dev->netdev_ops->ndo_bridge_dellink) |
| 3007 | err = -EOPNOTSUPP; |
| 3008 | else |
| 3009 | err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh); |
| 3010 | |
| 3011 | if (!err) |
| 3012 | flags &= ~BRIDGE_FLAGS_SELF; |
| 3013 | } |
| 3014 | |
| 3015 | if (have_flags) |
| 3016 | memcpy(nla_data(attr), &flags, sizeof(flags)); |
| 3017 | /* Generate event to notify upper layer of bridge change */ |
| 3018 | if (!err) |
| 3019 | err = rtnl_bridge_notify(dev, oflags); |
| 3020 | out: |
| 3021 | return err; |
| 3022 | } |
| 3023 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3024 | /* Process one rtnetlink message. */ |
| 3025 | |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 3026 | static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3027 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 3028 | struct net *net = sock_net(skb->sk); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 3029 | rtnl_doit_func doit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3030 | int sz_idx, kind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3031 | int family; |
| 3032 | int type; |
Eric Dumazet | 2907c35 | 2011-05-25 07:34:04 +0000 | [diff] [blame] | 3033 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3034 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3035 | type = nlh->nlmsg_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3036 | if (type > RTM_MAX) |
Thomas Graf | 038890f | 2007-04-05 14:35:52 -0700 | [diff] [blame] | 3037 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3038 | |
| 3039 | type -= RTM_BASE; |
| 3040 | |
| 3041 | /* All the messages must have at least 1 byte length */ |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 3042 | if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3043 | return 0; |
| 3044 | |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 3045 | family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3046 | sz_idx = type>>2; |
| 3047 | kind = type&3; |
| 3048 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 3049 | if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 3050 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3051 | |
David S. Miller | b8f3ab4 | 2011-01-18 12:40:38 -0800 | [diff] [blame] | 3052 | if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3053 | struct sock *rtnl; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 3054 | rtnl_dumpit_func dumpit; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 3055 | rtnl_calcit_func calcit; |
| 3056 | u16 min_dump_alloc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 3058 | dumpit = rtnl_get_dumpit(family, type); |
| 3059 | if (dumpit == NULL) |
Thomas Graf | 038890f | 2007-04-05 14:35:52 -0700 | [diff] [blame] | 3060 | return -EOPNOTSUPP; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 3061 | calcit = rtnl_get_calcit(family, type); |
| 3062 | if (calcit) |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 3063 | min_dump_alloc = calcit(skb, nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3064 | |
Eric Dumazet | 2907c35 | 2011-05-25 07:34:04 +0000 | [diff] [blame] | 3065 | __rtnl_unlock(); |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3066 | rtnl = net->rtnl; |
Pablo Neira Ayuso | 80d326f | 2012-02-24 14:30:15 +0000 | [diff] [blame] | 3067 | { |
| 3068 | struct netlink_dump_control c = { |
| 3069 | .dump = dumpit, |
| 3070 | .min_dump_alloc = min_dump_alloc, |
| 3071 | }; |
| 3072 | err = netlink_dump_start(rtnl, skb, nlh, &c); |
| 3073 | } |
Eric Dumazet | 2907c35 | 2011-05-25 07:34:04 +0000 | [diff] [blame] | 3074 | rtnl_lock(); |
| 3075 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3076 | } |
| 3077 | |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 3078 | doit = rtnl_get_doit(family, type); |
| 3079 | if (doit == NULL) |
Thomas Graf | 038890f | 2007-04-05 14:35:52 -0700 | [diff] [blame] | 3080 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3081 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 3082 | return doit(skb, nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3083 | } |
| 3084 | |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 3085 | static void rtnetlink_rcv(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3086 | { |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 3087 | rtnl_lock(); |
| 3088 | netlink_rcv_skb(skb, &rtnetlink_rcv_msg); |
| 3089 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3090 | } |
| 3091 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3092 | static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 3093 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 3094 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 3095 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3096 | switch (event) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | case NETDEV_UP: |
| 3098 | case NETDEV_DOWN: |
Patrick McHardy | 10de05a | 2010-02-26 06:34:50 +0000 | [diff] [blame] | 3099 | case NETDEV_PRE_UP: |
Eric W. Biederman | d90a909 | 2009-12-12 22:11:15 +0000 | [diff] [blame] | 3100 | case NETDEV_POST_INIT: |
| 3101 | case NETDEV_REGISTER: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3102 | case NETDEV_CHANGE: |
Patrick McHardy | 755d0e7 | 2010-03-19 04:42:24 +0000 | [diff] [blame] | 3103 | case NETDEV_PRE_TYPE_CHANGE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3104 | case NETDEV_GOING_DOWN: |
Patrick McHardy | a283576 | 2010-02-26 06:34:51 +0000 | [diff] [blame] | 3105 | case NETDEV_UNREGISTER: |
Eric Dumazet | 0115e8e | 2012-08-22 17:19:46 +0000 | [diff] [blame] | 3106 | case NETDEV_UNREGISTER_FINAL: |
Amerigo Wang | ac3d3f8 | 2011-05-19 23:06:32 +0000 | [diff] [blame] | 3107 | case NETDEV_RELEASE: |
| 3108 | case NETDEV_JOIN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3109 | break; |
| 3110 | default: |
Alexei Starovoitov | 7f29405 | 2013-10-23 16:02:42 -0700 | [diff] [blame] | 3111 | rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3112 | break; |
| 3113 | } |
| 3114 | return NOTIFY_DONE; |
| 3115 | } |
| 3116 | |
| 3117 | static struct notifier_block rtnetlink_dev_notifier = { |
| 3118 | .notifier_call = rtnetlink_event, |
| 3119 | }; |
| 3120 | |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3121 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 3122 | static int __net_init rtnetlink_net_init(struct net *net) |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3123 | { |
| 3124 | struct sock *sk; |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 3125 | struct netlink_kernel_cfg cfg = { |
| 3126 | .groups = RTNLGRP_MAX, |
| 3127 | .input = rtnetlink_rcv, |
| 3128 | .cb_mutex = &rtnl_mutex, |
Pablo Neira Ayuso | 9785e10 | 2012-09-08 02:53:53 +0000 | [diff] [blame] | 3129 | .flags = NL_CFG_F_NONROOT_RECV, |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 3130 | }; |
| 3131 | |
Pablo Neira Ayuso | 9f00d97 | 2012-09-08 02:53:54 +0000 | [diff] [blame] | 3132 | sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3133 | if (!sk) |
| 3134 | return -ENOMEM; |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3135 | net->rtnl = sk; |
| 3136 | return 0; |
| 3137 | } |
| 3138 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 3139 | static void __net_exit rtnetlink_net_exit(struct net *net) |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3140 | { |
Denis V. Lunev | 775516b | 2008-01-18 23:55:19 -0800 | [diff] [blame] | 3141 | netlink_kernel_release(net->rtnl); |
| 3142 | net->rtnl = NULL; |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3143 | } |
| 3144 | |
| 3145 | static struct pernet_operations rtnetlink_net_ops = { |
| 3146 | .init = rtnetlink_net_init, |
| 3147 | .exit = rtnetlink_net_exit, |
| 3148 | }; |
| 3149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3150 | void __init rtnetlink_init(void) |
| 3151 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3152 | if (register_pernet_subsys(&rtnetlink_net_ops)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3153 | panic("rtnetlink_init: cannot initialize rtnetlink\n"); |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 3154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3155 | register_netdevice_notifier(&rtnetlink_dev_notifier); |
Thomas Graf | 340d17f | 2007-03-22 11:49:22 -0700 | [diff] [blame] | 3156 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 3157 | rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, |
| 3158 | rtnl_dump_ifinfo, rtnl_calcit); |
| 3159 | rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL); |
| 3160 | rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL); |
| 3161 | rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL); |
Thomas Graf | 687ad8c | 2007-03-22 11:59:42 -0700 | [diff] [blame] | 3162 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 3163 | rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL); |
| 3164 | rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL); |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 3165 | |
| 3166 | rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL); |
| 3167 | rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL); |
| 3168 | rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3169 | |
| 3170 | rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL); |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 3171 | rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL); |
John Fastabend | e5a55a8 | 2012-10-24 08:12:57 +0000 | [diff] [blame] | 3172 | rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3173 | } |
| 3174 | |