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