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> |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 38 | #include <linux/pci.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | #include <linux/inet.h> |
| 43 | #include <linux/netdevice.h> |
| 44 | #include <net/ip.h> |
| 45 | #include <net/protocol.h> |
| 46 | #include <net/arp.h> |
| 47 | #include <net/route.h> |
| 48 | #include <net/udp.h> |
| 49 | #include <net/sock.h> |
| 50 | #include <net/pkt_sched.h> |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 51 | #include <net/fib_rules.h> |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 52 | #include <net/rtnetlink.h> |
Johannes Berg | 30ffee8 | 2009-07-10 09:51:35 +0000 | [diff] [blame] | 53 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 55 | struct rtnl_link { |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 56 | rtnl_doit_func doit; |
| 57 | rtnl_dumpit_func dumpit; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 58 | rtnl_calcit_func calcit; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 61 | static DEFINE_MUTEX(rtnl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| 63 | void rtnl_lock(void) |
| 64 | { |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 65 | mutex_lock(&rtnl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 67 | EXPORT_SYMBOL(rtnl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 69 | void __rtnl_unlock(void) |
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 | mutex_unlock(&rtnl_mutex); |
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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | void rtnl_unlock(void) |
| 75 | { |
Herbert Xu | 58ec3b4 | 2008-10-07 15:50:03 -0700 | [diff] [blame] | 76 | /* This fellow will unlock it for us. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | netdev_run_todo(); |
| 78 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 79 | EXPORT_SYMBOL(rtnl_unlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 81 | int rtnl_trylock(void) |
| 82 | { |
| 83 | return mutex_trylock(&rtnl_mutex); |
| 84 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 85 | EXPORT_SYMBOL(rtnl_trylock); |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 86 | |
Patrick McHardy | c9c1014 | 2008-04-23 22:10:48 -0700 | [diff] [blame] | 87 | int rtnl_is_locked(void) |
| 88 | { |
| 89 | return mutex_is_locked(&rtnl_mutex); |
| 90 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 91 | EXPORT_SYMBOL(rtnl_is_locked); |
Patrick McHardy | c9c1014 | 2008-04-23 22:10:48 -0700 | [diff] [blame] | 92 | |
Paul E. McKenney | a898def | 2010-02-22 17:04:49 -0800 | [diff] [blame] | 93 | #ifdef CONFIG_PROVE_LOCKING |
| 94 | int lockdep_rtnl_is_held(void) |
| 95 | { |
| 96 | return lockdep_is_held(&rtnl_mutex); |
| 97 | } |
| 98 | EXPORT_SYMBOL(lockdep_rtnl_is_held); |
| 99 | #endif /* #ifdef CONFIG_PROVE_LOCKING */ |
| 100 | |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 101 | static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1]; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 102 | |
| 103 | static inline int rtm_msgindex(int msgtype) |
| 104 | { |
| 105 | int msgindex = msgtype - RTM_BASE; |
| 106 | |
| 107 | /* |
| 108 | * msgindex < 0 implies someone tried to register a netlink |
| 109 | * control code. msgindex >= RTM_NR_MSGTYPES may indicate that |
| 110 | * the message type has not been added to linux/rtnetlink.h |
| 111 | */ |
| 112 | BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); |
| 113 | |
| 114 | return msgindex; |
| 115 | } |
| 116 | |
| 117 | static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex) |
| 118 | { |
| 119 | struct rtnl_link *tab; |
| 120 | |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 121 | if (protocol <= RTNL_FAMILY_MAX) |
Patrick McHardy | 0f87b1d | 2010-04-13 05:03:17 +0000 | [diff] [blame] | 122 | tab = rtnl_msg_handlers[protocol]; |
| 123 | else |
| 124 | tab = NULL; |
| 125 | |
Thomas Graf | 51057f2 | 2007-03-22 21:41:06 -0700 | [diff] [blame] | 126 | if (tab == NULL || tab[msgindex].doit == NULL) |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 127 | tab = rtnl_msg_handlers[PF_UNSPEC]; |
| 128 | |
Thomas Graf | 51057f2 | 2007-03-22 21:41:06 -0700 | [diff] [blame] | 129 | return tab ? tab[msgindex].doit : NULL; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex) |
| 133 | { |
| 134 | struct rtnl_link *tab; |
| 135 | |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 136 | if (protocol <= RTNL_FAMILY_MAX) |
Patrick McHardy | 0f87b1d | 2010-04-13 05:03:17 +0000 | [diff] [blame] | 137 | tab = rtnl_msg_handlers[protocol]; |
| 138 | else |
| 139 | tab = NULL; |
| 140 | |
Thomas Graf | 51057f2 | 2007-03-22 21:41:06 -0700 | [diff] [blame] | 141 | if (tab == NULL || tab[msgindex].dumpit == NULL) |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 142 | tab = rtnl_msg_handlers[PF_UNSPEC]; |
| 143 | |
Thomas Graf | 51057f2 | 2007-03-22 21:41:06 -0700 | [diff] [blame] | 144 | return tab ? tab[msgindex].dumpit : NULL; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 147 | static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex) |
| 148 | { |
| 149 | struct rtnl_link *tab; |
| 150 | |
| 151 | if (protocol <= RTNL_FAMILY_MAX) |
| 152 | tab = rtnl_msg_handlers[protocol]; |
| 153 | else |
| 154 | tab = NULL; |
| 155 | |
| 156 | if (tab == NULL || tab[msgindex].calcit == NULL) |
| 157 | tab = rtnl_msg_handlers[PF_UNSPEC]; |
| 158 | |
| 159 | return tab ? tab[msgindex].calcit : NULL; |
| 160 | } |
| 161 | |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 162 | /** |
| 163 | * __rtnl_register - Register a rtnetlink message type |
| 164 | * @protocol: Protocol family or PF_UNSPEC |
| 165 | * @msgtype: rtnetlink message type |
| 166 | * @doit: Function pointer called for each request message |
| 167 | * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 168 | * @calcit: Function pointer to calc size of dump message |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 169 | * |
| 170 | * Registers the specified function pointers (at least one of them has |
| 171 | * to be non-NULL) to be called whenever a request message for the |
| 172 | * specified protocol family and message type is received. |
| 173 | * |
| 174 | * The special protocol family PF_UNSPEC may be used to define fallback |
| 175 | * function pointers for the case when no entry for the specific protocol |
| 176 | * family exists. |
| 177 | * |
| 178 | * Returns 0 on success or a negative error code. |
| 179 | */ |
| 180 | int __rtnl_register(int protocol, int msgtype, |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 181 | rtnl_doit_func doit, rtnl_dumpit_func dumpit, |
| 182 | rtnl_calcit_func calcit) |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 183 | { |
| 184 | struct rtnl_link *tab; |
| 185 | int msgindex; |
| 186 | |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 187 | BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 188 | msgindex = rtm_msgindex(msgtype); |
| 189 | |
| 190 | tab = rtnl_msg_handlers[protocol]; |
| 191 | if (tab == NULL) { |
| 192 | tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL); |
| 193 | if (tab == NULL) |
| 194 | return -ENOBUFS; |
| 195 | |
| 196 | rtnl_msg_handlers[protocol] = tab; |
| 197 | } |
| 198 | |
| 199 | if (doit) |
| 200 | tab[msgindex].doit = doit; |
| 201 | |
| 202 | if (dumpit) |
| 203 | tab[msgindex].dumpit = dumpit; |
| 204 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 205 | if (calcit) |
| 206 | tab[msgindex].calcit = calcit; |
| 207 | |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 208 | return 0; |
| 209 | } |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 210 | EXPORT_SYMBOL_GPL(__rtnl_register); |
| 211 | |
| 212 | /** |
| 213 | * rtnl_register - Register a rtnetlink message type |
| 214 | * |
| 215 | * Identical to __rtnl_register() but panics on failure. This is useful |
| 216 | * as failure of this function is very unlikely, it can only happen due |
| 217 | * to lack of memory when allocating the chain to store all message |
| 218 | * 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] | 219 | * of memory implies no sense in continuing. |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 220 | */ |
| 221 | void rtnl_register(int protocol, int msgtype, |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 222 | rtnl_doit_func doit, rtnl_dumpit_func dumpit, |
| 223 | rtnl_calcit_func calcit) |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 224 | { |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 225 | if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0) |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 226 | panic("Unable to register rtnetlink message handler, " |
| 227 | "protocol = %d, message type = %d\n", |
| 228 | protocol, msgtype); |
| 229 | } |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 230 | EXPORT_SYMBOL_GPL(rtnl_register); |
| 231 | |
| 232 | /** |
| 233 | * rtnl_unregister - Unregister a rtnetlink message type |
| 234 | * @protocol: Protocol family or PF_UNSPEC |
| 235 | * @msgtype: rtnetlink message type |
| 236 | * |
| 237 | * Returns 0 on success or a negative error code. |
| 238 | */ |
| 239 | int rtnl_unregister(int protocol, int msgtype) |
| 240 | { |
| 241 | int msgindex; |
| 242 | |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 243 | BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 244 | msgindex = rtm_msgindex(msgtype); |
| 245 | |
| 246 | if (rtnl_msg_handlers[protocol] == NULL) |
| 247 | return -ENOENT; |
| 248 | |
| 249 | rtnl_msg_handlers[protocol][msgindex].doit = NULL; |
| 250 | rtnl_msg_handlers[protocol][msgindex].dumpit = NULL; |
| 251 | |
| 252 | return 0; |
| 253 | } |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 254 | EXPORT_SYMBOL_GPL(rtnl_unregister); |
| 255 | |
| 256 | /** |
| 257 | * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol |
| 258 | * @protocol : Protocol family or PF_UNSPEC |
| 259 | * |
| 260 | * Identical to calling rtnl_unregster() for all registered message types |
| 261 | * of a certain protocol family. |
| 262 | */ |
| 263 | void rtnl_unregister_all(int protocol) |
| 264 | { |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 265 | BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 266 | |
| 267 | kfree(rtnl_msg_handlers[protocol]); |
| 268 | rtnl_msg_handlers[protocol] = NULL; |
| 269 | } |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 270 | EXPORT_SYMBOL_GPL(rtnl_unregister_all); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 272 | static LIST_HEAD(link_ops); |
| 273 | |
Eric Dumazet | c63044f | 2011-12-13 11:38:00 +0000 | [diff] [blame] | 274 | static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) |
| 275 | { |
| 276 | const struct rtnl_link_ops *ops; |
| 277 | |
| 278 | list_for_each_entry(ops, &link_ops, list) { |
| 279 | if (!strcmp(ops->kind, kind)) |
| 280 | return ops; |
| 281 | } |
| 282 | return NULL; |
| 283 | } |
| 284 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 285 | /** |
| 286 | * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. |
| 287 | * @ops: struct rtnl_link_ops * to register |
| 288 | * |
| 289 | * The caller must hold the rtnl_mutex. This function should be used |
| 290 | * by drivers that create devices during module initialization. It |
| 291 | * must be called before registering the devices. |
| 292 | * |
| 293 | * Returns 0 on success or a negative error code. |
| 294 | */ |
| 295 | int __rtnl_link_register(struct rtnl_link_ops *ops) |
| 296 | { |
Eric Dumazet | c63044f | 2011-12-13 11:38:00 +0000 | [diff] [blame] | 297 | if (rtnl_link_ops_get(ops->kind)) |
| 298 | return -EEXIST; |
| 299 | |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 300 | if (!ops->dellink) |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 301 | ops->dellink = unregister_netdevice_queue; |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 302 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 303 | list_add_tail(&ops->list, &link_ops); |
| 304 | return 0; |
| 305 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 306 | EXPORT_SYMBOL_GPL(__rtnl_link_register); |
| 307 | |
| 308 | /** |
| 309 | * rtnl_link_register - Register rtnl_link_ops with rtnetlink. |
| 310 | * @ops: struct rtnl_link_ops * to register |
| 311 | * |
| 312 | * Returns 0 on success or a negative error code. |
| 313 | */ |
| 314 | int rtnl_link_register(struct rtnl_link_ops *ops) |
| 315 | { |
| 316 | int err; |
| 317 | |
| 318 | rtnl_lock(); |
| 319 | err = __rtnl_link_register(ops); |
| 320 | rtnl_unlock(); |
| 321 | return err; |
| 322 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 323 | EXPORT_SYMBOL_GPL(rtnl_link_register); |
| 324 | |
Pavel Emelyanov | 669f87b | 2008-04-16 00:46:52 -0700 | [diff] [blame] | 325 | static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) |
| 326 | { |
| 327 | struct net_device *dev; |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 328 | LIST_HEAD(list_kill); |
| 329 | |
Pavel Emelyanov | 669f87b | 2008-04-16 00:46:52 -0700 | [diff] [blame] | 330 | for_each_netdev(net, dev) { |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 331 | if (dev->rtnl_link_ops == ops) |
| 332 | ops->dellink(dev, &list_kill); |
Pavel Emelyanov | 669f87b | 2008-04-16 00:46:52 -0700 | [diff] [blame] | 333 | } |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 334 | unregister_netdevice_many(&list_kill); |
Pavel Emelyanov | 669f87b | 2008-04-16 00:46:52 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 337 | /** |
| 338 | * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. |
| 339 | * @ops: struct rtnl_link_ops * to unregister |
| 340 | * |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 341 | * The caller must hold the rtnl_mutex. |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 342 | */ |
| 343 | void __rtnl_link_unregister(struct rtnl_link_ops *ops) |
| 344 | { |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 345 | struct net *net; |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 346 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 347 | for_each_net(net) { |
Pavel Emelyanov | 669f87b | 2008-04-16 00:46:52 -0700 | [diff] [blame] | 348 | __rtnl_kill_links(net, ops); |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 349 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 350 | list_del(&ops->list); |
| 351 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 352 | EXPORT_SYMBOL_GPL(__rtnl_link_unregister); |
| 353 | |
| 354 | /** |
| 355 | * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. |
| 356 | * @ops: struct rtnl_link_ops * to unregister |
| 357 | */ |
| 358 | void rtnl_link_unregister(struct rtnl_link_ops *ops) |
| 359 | { |
| 360 | rtnl_lock(); |
| 361 | __rtnl_link_unregister(ops); |
| 362 | rtnl_unlock(); |
| 363 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 364 | EXPORT_SYMBOL_GPL(rtnl_link_unregister); |
| 365 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 366 | static size_t rtnl_link_get_size(const struct net_device *dev) |
| 367 | { |
| 368 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; |
| 369 | size_t size; |
| 370 | |
| 371 | if (!ops) |
| 372 | return 0; |
| 373 | |
Thomas Graf | 369cf77 | 2010-11-11 15:47:59 +0000 | [diff] [blame] | 374 | size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ |
| 375 | nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 376 | |
| 377 | if (ops->get_size) |
| 378 | /* IFLA_INFO_DATA + nested data */ |
Thomas Graf | 369cf77 | 2010-11-11 15:47:59 +0000 | [diff] [blame] | 379 | size += nla_total_size(sizeof(struct nlattr)) + |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 380 | ops->get_size(dev); |
| 381 | |
| 382 | if (ops->get_xstats_size) |
Thomas Graf | 369cf77 | 2010-11-11 15:47:59 +0000 | [diff] [blame] | 383 | /* IFLA_INFO_XSTATS */ |
| 384 | size += nla_total_size(ops->get_xstats_size(dev)); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 385 | |
| 386 | return size; |
| 387 | } |
| 388 | |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 389 | static LIST_HEAD(rtnl_af_ops); |
| 390 | |
| 391 | static const struct rtnl_af_ops *rtnl_af_lookup(const int family) |
| 392 | { |
| 393 | const struct rtnl_af_ops *ops; |
| 394 | |
| 395 | list_for_each_entry(ops, &rtnl_af_ops, list) { |
| 396 | if (ops->family == family) |
| 397 | return ops; |
| 398 | } |
| 399 | |
| 400 | return NULL; |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * __rtnl_af_register - Register rtnl_af_ops with rtnetlink. |
| 405 | * @ops: struct rtnl_af_ops * to register |
| 406 | * |
| 407 | * The caller must hold the rtnl_mutex. |
| 408 | * |
| 409 | * Returns 0 on success or a negative error code. |
| 410 | */ |
| 411 | int __rtnl_af_register(struct rtnl_af_ops *ops) |
| 412 | { |
| 413 | list_add_tail(&ops->list, &rtnl_af_ops); |
| 414 | return 0; |
| 415 | } |
| 416 | EXPORT_SYMBOL_GPL(__rtnl_af_register); |
| 417 | |
| 418 | /** |
| 419 | * rtnl_af_register - Register rtnl_af_ops with rtnetlink. |
| 420 | * @ops: struct rtnl_af_ops * to register |
| 421 | * |
| 422 | * Returns 0 on success or a negative error code. |
| 423 | */ |
| 424 | int rtnl_af_register(struct rtnl_af_ops *ops) |
| 425 | { |
| 426 | int err; |
| 427 | |
| 428 | rtnl_lock(); |
| 429 | err = __rtnl_af_register(ops); |
| 430 | rtnl_unlock(); |
| 431 | return err; |
| 432 | } |
| 433 | EXPORT_SYMBOL_GPL(rtnl_af_register); |
| 434 | |
| 435 | /** |
| 436 | * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. |
| 437 | * @ops: struct rtnl_af_ops * to unregister |
| 438 | * |
| 439 | * The caller must hold the rtnl_mutex. |
| 440 | */ |
| 441 | void __rtnl_af_unregister(struct rtnl_af_ops *ops) |
| 442 | { |
| 443 | list_del(&ops->list); |
| 444 | } |
| 445 | EXPORT_SYMBOL_GPL(__rtnl_af_unregister); |
| 446 | |
| 447 | /** |
| 448 | * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. |
| 449 | * @ops: struct rtnl_af_ops * to unregister |
| 450 | */ |
| 451 | void rtnl_af_unregister(struct rtnl_af_ops *ops) |
| 452 | { |
| 453 | rtnl_lock(); |
| 454 | __rtnl_af_unregister(ops); |
| 455 | rtnl_unlock(); |
| 456 | } |
| 457 | EXPORT_SYMBOL_GPL(rtnl_af_unregister); |
| 458 | |
| 459 | static size_t rtnl_link_get_af_size(const struct net_device *dev) |
| 460 | { |
| 461 | struct rtnl_af_ops *af_ops; |
| 462 | size_t size; |
| 463 | |
| 464 | /* IFLA_AF_SPEC */ |
| 465 | size = nla_total_size(sizeof(struct nlattr)); |
| 466 | |
| 467 | list_for_each_entry(af_ops, &rtnl_af_ops, list) { |
| 468 | if (af_ops->get_link_af_size) { |
| 469 | /* AF_* + nested data */ |
| 470 | size += nla_total_size(sizeof(struct nlattr)) + |
| 471 | af_ops->get_link_af_size(dev); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | return size; |
| 476 | } |
| 477 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 478 | static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) |
| 479 | { |
| 480 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; |
| 481 | struct nlattr *linkinfo, *data; |
| 482 | int err = -EMSGSIZE; |
| 483 | |
| 484 | linkinfo = nla_nest_start(skb, IFLA_LINKINFO); |
| 485 | if (linkinfo == NULL) |
| 486 | goto out; |
| 487 | |
| 488 | if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) |
| 489 | goto err_cancel_link; |
| 490 | if (ops->fill_xstats) { |
| 491 | err = ops->fill_xstats(skb, dev); |
| 492 | if (err < 0) |
| 493 | goto err_cancel_link; |
| 494 | } |
| 495 | if (ops->fill_info) { |
| 496 | data = nla_nest_start(skb, IFLA_INFO_DATA); |
Wei Yongjun | a36da1e | 2013-03-27 03:22:45 +0000 | [diff] [blame] | 497 | if (data == NULL) { |
| 498 | err = -EMSGSIZE; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 499 | goto err_cancel_link; |
Wei Yongjun | a36da1e | 2013-03-27 03:22:45 +0000 | [diff] [blame] | 500 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 501 | err = ops->fill_info(skb, dev); |
| 502 | if (err < 0) |
| 503 | goto err_cancel_data; |
| 504 | nla_nest_end(skb, data); |
| 505 | } |
| 506 | |
| 507 | nla_nest_end(skb, linkinfo); |
| 508 | return 0; |
| 509 | |
| 510 | err_cancel_data: |
| 511 | nla_nest_cancel(skb, data); |
| 512 | err_cancel_link: |
| 513 | nla_nest_cancel(skb, linkinfo); |
| 514 | out: |
| 515 | return err; |
| 516 | } |
| 517 | |
Thomas Graf | db46edc | 2005-05-03 14:29:39 -0700 | [diff] [blame] | 518 | static const int rtm_min[RTM_NR_FAMILIES] = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | { |
Thomas Graf | f90a0a7 | 2005-05-03 14:29:00 -0700 | [diff] [blame] | 520 | [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)), |
| 521 | [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)), |
| 522 | [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)), |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 523 | [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)), |
Thomas Graf | f90a0a7 | 2005-05-03 14:29:00 -0700 | [diff] [blame] | 524 | [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)), |
| 525 | [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)), |
| 526 | [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)), |
| 527 | [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)), |
Thomas Graf | f90a0a7 | 2005-05-03 14:29:00 -0700 | [diff] [blame] | 528 | [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), |
| 529 | [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | }; |
| 531 | |
Thomas Graf | db46edc | 2005-05-03 14:29:39 -0700 | [diff] [blame] | 532 | static const int rta_max[RTM_NR_FAMILIES] = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | { |
Thomas Graf | f90a0a7 | 2005-05-03 14:29:00 -0700 | [diff] [blame] | 534 | [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX, |
| 535 | [RTM_FAM(RTM_NEWADDR)] = IFA_MAX, |
| 536 | [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX, |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 537 | [RTM_FAM(RTM_NEWRULE)] = FRA_MAX, |
Thomas Graf | f90a0a7 | 2005-05-03 14:29:00 -0700 | [diff] [blame] | 538 | [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX, |
| 539 | [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX, |
| 540 | [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX, |
| 541 | [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | }; |
| 543 | |
| 544 | void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data) |
| 545 | { |
| 546 | struct rtattr *rta; |
| 547 | int size = RTA_LENGTH(attrlen); |
| 548 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 549 | rta = (struct rtattr *)skb_put(skb, RTA_ALIGN(size)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | rta->rta_type = attrtype; |
| 551 | rta->rta_len = size; |
| 552 | memcpy(RTA_DATA(rta), data, attrlen); |
Patrick McHardy | b3563c4 | 2005-06-28 12:54:43 -0700 | [diff] [blame] | 553 | memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 555 | EXPORT_SYMBOL(__rta_fill); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 557 | int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned group, int echo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 559 | struct sock *rtnl = net->rtnl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | int err = 0; |
| 561 | |
Patrick McHardy | ac6d439 | 2005-08-14 19:29:52 -0700 | [diff] [blame] | 562 | NETLINK_CB(skb).dst_group = group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | if (echo) |
| 564 | atomic_inc(&skb->users); |
| 565 | netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); |
| 566 | if (echo) |
| 567 | err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); |
| 568 | return err; |
| 569 | } |
| 570 | |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 571 | int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) |
Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 572 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 573 | struct sock *rtnl = net->rtnl; |
| 574 | |
Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 575 | return nlmsg_unicast(rtnl, skb, pid); |
| 576 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 577 | EXPORT_SYMBOL(rtnl_unicast); |
Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 578 | |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 579 | void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, |
| 580 | struct nlmsghdr *nlh, gfp_t flags) |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 581 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 582 | struct sock *rtnl = net->rtnl; |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 583 | int report = 0; |
| 584 | |
| 585 | if (nlh) |
| 586 | report = nlmsg_report(nlh); |
| 587 | |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 588 | nlmsg_notify(rtnl, skb, pid, group, report, flags); |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 589 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 590 | EXPORT_SYMBOL(rtnl_notify); |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 591 | |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 592 | void rtnl_set_sk_err(struct net *net, u32 group, int error) |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 593 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 594 | struct sock *rtnl = net->rtnl; |
| 595 | |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 596 | netlink_set_err(rtnl, 0, group, error); |
| 597 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 598 | EXPORT_SYMBOL(rtnl_set_sk_err); |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 599 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) |
| 601 | { |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 602 | struct nlattr *mx; |
| 603 | int i, valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 605 | mx = nla_nest_start(skb, RTA_METRICS); |
| 606 | if (mx == NULL) |
| 607 | return -ENOBUFS; |
| 608 | |
| 609 | for (i = 0; i < RTAX_MAX; i++) { |
| 610 | if (metrics[i]) { |
| 611 | valid++; |
| 612 | NLA_PUT_U32(skb, i+1, metrics[i]); |
| 613 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | |
David S. Miller | a57d27f | 2006-08-22 22:20:14 -0700 | [diff] [blame] | 616 | if (!valid) { |
| 617 | nla_nest_cancel(skb, mx); |
| 618 | return 0; |
| 619 | } |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 620 | |
| 621 | return nla_nest_end(skb, mx); |
| 622 | |
| 623 | nla_put_failure: |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 624 | nla_nest_cancel(skb, mx); |
| 625 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 627 | EXPORT_SYMBOL(rtnetlink_put_metrics); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 629 | int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, |
| 630 | u32 ts, u32 tsage, long expires, u32 error) |
| 631 | { |
| 632 | struct rta_cacheinfo ci = { |
| 633 | .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse), |
| 634 | .rta_used = dst->__use, |
| 635 | .rta_clntref = atomic_read(&(dst->__refcnt)), |
| 636 | .rta_error = error, |
| 637 | .rta_id = id, |
| 638 | .rta_ts = ts, |
| 639 | .rta_tsage = tsage, |
| 640 | }; |
| 641 | |
| 642 | if (expires) |
| 643 | ci.rta_expires = jiffies_to_clock_t(expires); |
| 644 | |
| 645 | return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); |
| 646 | } |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 647 | EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 649 | static void set_operstate(struct net_device *dev, unsigned char transition) |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 650 | { |
| 651 | unsigned char operstate = dev->operstate; |
| 652 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 653 | switch (transition) { |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 654 | case IF_OPER_UP: |
| 655 | if ((operstate == IF_OPER_DORMANT || |
| 656 | operstate == IF_OPER_UNKNOWN) && |
| 657 | !netif_dormant(dev)) |
| 658 | operstate = IF_OPER_UP; |
| 659 | break; |
| 660 | |
| 661 | case IF_OPER_DORMANT: |
| 662 | if (operstate == IF_OPER_UP || |
| 663 | operstate == IF_OPER_UNKNOWN) |
| 664 | operstate = IF_OPER_DORMANT; |
| 665 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 666 | } |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 667 | |
| 668 | if (dev->operstate != operstate) { |
| 669 | write_lock_bh(&dev_base_lock); |
| 670 | dev->operstate = operstate; |
| 671 | write_unlock_bh(&dev_base_lock); |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 672 | netdev_state_change(dev); |
| 673 | } |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 674 | } |
| 675 | |
Jiri Benc | e7b539a | 2012-07-27 02:58:22 +0000 | [diff] [blame] | 676 | static unsigned int rtnl_dev_get_flags(const struct net_device *dev) |
| 677 | { |
| 678 | return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) | |
| 679 | (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI)); |
| 680 | } |
| 681 | |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 682 | static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, |
| 683 | const struct ifinfomsg *ifm) |
| 684 | { |
| 685 | unsigned int flags = ifm->ifi_flags; |
| 686 | |
| 687 | /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ |
| 688 | if (ifm->ifi_change) |
| 689 | flags = (flags & ifm->ifi_change) | |
Jiri Benc | e7b539a | 2012-07-27 02:58:22 +0000 | [diff] [blame] | 690 | (rtnl_dev_get_flags(dev) & ~ifm->ifi_change); |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 691 | |
| 692 | return flags; |
| 693 | } |
| 694 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 695 | static void copy_rtnl_link_stats(struct rtnl_link_stats *a, |
Ben Hutchings | be1f3c2 | 2010-06-08 07:19:54 +0000 | [diff] [blame] | 696 | const struct rtnl_link_stats64 *b) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | { |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 698 | a->rx_packets = b->rx_packets; |
| 699 | a->tx_packets = b->tx_packets; |
| 700 | a->rx_bytes = b->rx_bytes; |
| 701 | a->tx_bytes = b->tx_bytes; |
| 702 | a->rx_errors = b->rx_errors; |
| 703 | a->tx_errors = b->tx_errors; |
| 704 | a->rx_dropped = b->rx_dropped; |
| 705 | a->tx_dropped = b->tx_dropped; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 707 | a->multicast = b->multicast; |
| 708 | a->collisions = b->collisions; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 710 | a->rx_length_errors = b->rx_length_errors; |
| 711 | a->rx_over_errors = b->rx_over_errors; |
| 712 | a->rx_crc_errors = b->rx_crc_errors; |
| 713 | a->rx_frame_errors = b->rx_frame_errors; |
| 714 | a->rx_fifo_errors = b->rx_fifo_errors; |
| 715 | a->rx_missed_errors = b->rx_missed_errors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 717 | a->tx_aborted_errors = b->tx_aborted_errors; |
| 718 | a->tx_carrier_errors = b->tx_carrier_errors; |
| 719 | a->tx_fifo_errors = b->tx_fifo_errors; |
| 720 | a->tx_heartbeat_errors = b->tx_heartbeat_errors; |
| 721 | a->tx_window_errors = b->tx_window_errors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 723 | a->rx_compressed = b->rx_compressed; |
| 724 | a->tx_compressed = b->tx_compressed; |
Jan Engelhardt | 10708f3 | 2010-03-11 09:57:29 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Ben Hutchings | be1f3c2 | 2010-06-08 07:19:54 +0000 | [diff] [blame] | 727 | 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] | 728 | { |
Eric Dumazet | afdcba3 | 2010-08-23 07:14:36 +0000 | [diff] [blame] | 729 | memcpy(v, b, sizeof(*b)); |
Jan Engelhardt | 10708f3 | 2010-03-11 09:57:29 +0000 | [diff] [blame] | 730 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 732 | /* All VF info */ |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 733 | static inline int rtnl_vfinfo_size(const struct net_device *dev, |
| 734 | u32 ext_filter_mask) |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 735 | { |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 736 | if (dev->dev.parent && dev_is_pci(dev->dev.parent) && |
| 737 | (ext_filter_mask & RTEXT_FILTER_VF)) { |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 738 | int num_vfs = dev_num_vf(dev->dev.parent); |
Scott Feldman | 045de01 | 2010-05-28 03:42:43 -0700 | [diff] [blame] | 739 | size_t size = nla_total_size(sizeof(struct nlattr)); |
| 740 | size += nla_total_size(num_vfs * sizeof(struct nlattr)); |
| 741 | size += num_vfs * |
| 742 | (nla_total_size(sizeof(struct ifla_vf_mac)) + |
| 743 | nla_total_size(sizeof(struct ifla_vf_vlan)) + |
Greg Rose | 5f8444a | 2011-10-08 03:05:24 +0000 | [diff] [blame] | 744 | nla_total_size(sizeof(struct ifla_vf_tx_rate)) + |
| 745 | nla_total_size(sizeof(struct ifla_vf_spoofchk))); |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 746 | return size; |
| 747 | } else |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 748 | return 0; |
| 749 | } |
| 750 | |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 751 | static size_t rtnl_port_size(const struct net_device *dev) |
| 752 | { |
| 753 | size_t port_size = nla_total_size(4) /* PORT_VF */ |
| 754 | + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ |
| 755 | + nla_total_size(sizeof(struct ifla_port_vsi)) |
| 756 | /* PORT_VSI_TYPE */ |
| 757 | + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ |
| 758 | + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ |
| 759 | + nla_total_size(1) /* PROT_VDP_REQUEST */ |
| 760 | + nla_total_size(2); /* PORT_VDP_RESPONSE */ |
| 761 | size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); |
| 762 | size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) |
| 763 | + port_size; |
| 764 | size_t port_self_size = nla_total_size(sizeof(struct nlattr)) |
| 765 | + port_size; |
| 766 | |
| 767 | if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent) |
| 768 | return 0; |
| 769 | if (dev_num_vf(dev->dev.parent)) |
| 770 | return port_self_size + vf_ports_size + |
| 771 | vf_port_size * dev_num_vf(dev->dev.parent); |
| 772 | else |
| 773 | return port_self_size; |
| 774 | } |
| 775 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 776 | static noinline size_t if_nlmsg_size(const struct net_device *dev, |
| 777 | u32 ext_filter_mask) |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 778 | { |
| 779 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) |
| 780 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 781 | + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 782 | + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ |
| 783 | + nla_total_size(sizeof(struct rtnl_link_ifmap)) |
| 784 | + nla_total_size(sizeof(struct rtnl_link_stats)) |
Jan Engelhardt | adcfe19 | 2010-03-27 17:15:29 -0700 | [diff] [blame] | 785 | + nla_total_size(sizeof(struct rtnl_link_stats64)) |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 786 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ |
| 787 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ |
| 788 | + nla_total_size(4) /* IFLA_TXQLEN */ |
| 789 | + nla_total_size(4) /* IFLA_WEIGHT */ |
| 790 | + nla_total_size(4) /* IFLA_MTU */ |
| 791 | + nla_total_size(4) /* IFLA_LINK */ |
| 792 | + nla_total_size(4) /* IFLA_MASTER */ |
| 793 | + nla_total_size(1) /* IFLA_OPERSTATE */ |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 794 | + nla_total_size(1) /* IFLA_LINKMODE */ |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 795 | + nla_total_size(ext_filter_mask |
| 796 | & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ |
| 797 | + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 798 | + rtnl_port_size(dev) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 799 | + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ |
| 800 | + rtnl_link_get_af_size(dev); /* IFLA_AF_SPEC */ |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 801 | } |
| 802 | |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 803 | static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) |
| 804 | { |
| 805 | struct nlattr *vf_ports; |
| 806 | struct nlattr *vf_port; |
| 807 | int vf; |
| 808 | int err; |
| 809 | |
| 810 | vf_ports = nla_nest_start(skb, IFLA_VF_PORTS); |
| 811 | if (!vf_ports) |
| 812 | return -EMSGSIZE; |
| 813 | |
| 814 | for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { |
| 815 | vf_port = nla_nest_start(skb, IFLA_VF_PORT); |
Scott Feldman | 8ca9418 | 2010-05-28 03:42:18 -0700 | [diff] [blame] | 816 | if (!vf_port) |
| 817 | goto nla_put_failure; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 818 | NLA_PUT_U32(skb, IFLA_PORT_VF, vf); |
| 819 | err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); |
Scott Feldman | 8ca9418 | 2010-05-28 03:42:18 -0700 | [diff] [blame] | 820 | if (err == -EMSGSIZE) |
| 821 | goto nla_put_failure; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 822 | if (err) { |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 823 | nla_nest_cancel(skb, vf_port); |
| 824 | continue; |
| 825 | } |
| 826 | nla_nest_end(skb, vf_port); |
| 827 | } |
| 828 | |
| 829 | nla_nest_end(skb, vf_ports); |
| 830 | |
| 831 | return 0; |
Scott Feldman | 8ca9418 | 2010-05-28 03:42:18 -0700 | [diff] [blame] | 832 | |
| 833 | nla_put_failure: |
| 834 | nla_nest_cancel(skb, vf_ports); |
| 835 | return -EMSGSIZE; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) |
| 839 | { |
| 840 | struct nlattr *port_self; |
| 841 | int err; |
| 842 | |
| 843 | port_self = nla_nest_start(skb, IFLA_PORT_SELF); |
| 844 | if (!port_self) |
| 845 | return -EMSGSIZE; |
| 846 | |
| 847 | err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); |
| 848 | if (err) { |
| 849 | nla_nest_cancel(skb, port_self); |
Scott Feldman | 8ca9418 | 2010-05-28 03:42:18 -0700 | [diff] [blame] | 850 | return (err == -EMSGSIZE) ? err : 0; |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | nla_nest_end(skb, port_self); |
| 854 | |
| 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev) |
| 859 | { |
| 860 | int err; |
| 861 | |
| 862 | if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent) |
| 863 | return 0; |
| 864 | |
| 865 | err = rtnl_port_self_fill(skb, dev); |
| 866 | if (err) |
| 867 | return err; |
| 868 | |
| 869 | if (dev_num_vf(dev->dev.parent)) { |
| 870 | err = rtnl_vf_ports_fill(skb, dev); |
| 871 | if (err) |
| 872 | return err; |
| 873 | } |
| 874 | |
| 875 | return 0; |
| 876 | } |
| 877 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 878 | 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] | 879 | int type, u32 pid, u32 seq, u32 change, |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 880 | unsigned int flags, u32 ext_filter_mask) |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 881 | { |
| 882 | struct ifinfomsg *ifm; |
| 883 | struct nlmsghdr *nlh; |
Eric Dumazet | 2817273 | 2010-07-07 14:58:56 -0700 | [diff] [blame] | 884 | struct rtnl_link_stats64 temp; |
Ben Hutchings | be1f3c2 | 2010-06-08 07:19:54 +0000 | [diff] [blame] | 885 | const struct rtnl_link_stats64 *stats; |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 886 | struct nlattr *attr, *af_spec; |
| 887 | struct rtnl_af_ops *af_ops; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 888 | |
Eric Dumazet | 2907c35 | 2011-05-25 07:34:04 +0000 | [diff] [blame] | 889 | ASSERT_RTNL(); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 890 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); |
| 891 | if (nlh == NULL) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 892 | return -EMSGSIZE; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 893 | |
| 894 | ifm = nlmsg_data(nlh); |
| 895 | ifm->ifi_family = AF_UNSPEC; |
| 896 | ifm->__ifi_pad = 0; |
| 897 | ifm->ifi_type = dev->type; |
| 898 | ifm->ifi_index = dev->ifindex; |
| 899 | ifm->ifi_flags = dev_get_flags(dev); |
| 900 | ifm->ifi_change = change; |
| 901 | |
| 902 | NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name); |
| 903 | NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 904 | NLA_PUT_U8(skb, IFLA_OPERSTATE, |
| 905 | netif_running(dev) ? dev->operstate : IF_OPER_DOWN); |
| 906 | NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode); |
| 907 | NLA_PUT_U32(skb, IFLA_MTU, dev->mtu); |
Vlad Dogaru | cbda10f | 2011-01-13 23:38:30 +0000 | [diff] [blame] | 908 | NLA_PUT_U32(skb, IFLA_GROUP, dev->group); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 909 | |
| 910 | if (dev->ifindex != dev->iflink) |
| 911 | NLA_PUT_U32(skb, IFLA_LINK, dev->iflink); |
| 912 | |
| 913 | if (dev->master) |
| 914 | NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex); |
| 915 | |
Patrick McHardy | af356af | 2009-09-04 06:41:18 +0000 | [diff] [blame] | 916 | if (dev->qdisc) |
| 917 | NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc->ops->id); |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 918 | |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 919 | if (dev->ifalias) |
| 920 | NLA_PUT_STRING(skb, IFLA_IFALIAS, dev->ifalias); |
| 921 | |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 922 | if (1) { |
Teow Wan Yee | af9dd6d | 2016-07-26 16:53:39 +0800 | [diff] [blame] | 923 | struct rtnl_link_ifmap map; |
| 924 | |
| 925 | memset(&map, 0, sizeof(map)); |
| 926 | map.mem_start = dev->mem_start; |
| 927 | map.mem_end = dev->mem_end; |
| 928 | map.base_addr = dev->base_addr; |
| 929 | map.irq = dev->irq; |
| 930 | map.dma = dev->dma; |
| 931 | map.port = dev->if_port; |
| 932 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 933 | NLA_PUT(skb, IFLA_MAP, sizeof(map), &map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | if (dev->addr_len) { |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 937 | NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr); |
| 938 | NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Pavel Emelyanov | 96e7408 | 2008-05-21 14:12:46 -0700 | [diff] [blame] | 941 | attr = nla_reserve(skb, IFLA_STATS, |
| 942 | sizeof(struct rtnl_link_stats)); |
| 943 | if (attr == NULL) |
| 944 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
Eric Dumazet | 2817273 | 2010-07-07 14:58:56 -0700 | [diff] [blame] | 946 | stats = dev_get_stats(dev, &temp); |
Pavel Emelyanov | 96e7408 | 2008-05-21 14:12:46 -0700 | [diff] [blame] | 947 | copy_rtnl_link_stats(nla_data(attr), stats); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | |
Jan Engelhardt | 10708f3 | 2010-03-11 09:57:29 +0000 | [diff] [blame] | 949 | attr = nla_reserve(skb, IFLA_STATS64, |
| 950 | sizeof(struct rtnl_link_stats64)); |
| 951 | if (attr == NULL) |
| 952 | goto nla_put_failure; |
Jan Engelhardt | 10708f3 | 2010-03-11 09:57:29 +0000 | [diff] [blame] | 953 | copy_rtnl_link_stats64(nla_data(attr), stats); |
| 954 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 955 | if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 956 | NLA_PUT_U32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)); |
| 957 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 958 | if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent |
| 959 | && (ext_filter_mask & RTEXT_FILTER_VF)) { |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 960 | int i; |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 961 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 962 | struct nlattr *vfinfo, *vf; |
| 963 | int num_vfs = dev_num_vf(dev->dev.parent); |
| 964 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 965 | vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST); |
| 966 | if (!vfinfo) |
| 967 | goto nla_put_failure; |
| 968 | for (i = 0; i < num_vfs; i++) { |
| 969 | struct ifla_vf_info ivi; |
| 970 | struct ifla_vf_mac vf_mac; |
| 971 | struct ifla_vf_vlan vf_vlan; |
| 972 | struct ifla_vf_tx_rate vf_tx_rate; |
Greg Rose | 5f8444a | 2011-10-08 03:05:24 +0000 | [diff] [blame] | 973 | struct ifla_vf_spoofchk vf_spoofchk; |
| 974 | |
| 975 | /* |
| 976 | * Not all SR-IOV capable drivers support the |
| 977 | * spoofcheck query. Preset to -1 so the user |
| 978 | * space tool can detect that the driver didn't |
| 979 | * report anything. |
| 980 | */ |
| 981 | ivi.spoofchk = -1; |
Mathias Krause | 75deb32 | 2013-03-09 05:52:20 +0000 | [diff] [blame] | 982 | memset(ivi.mac, 0, sizeof(ivi.mac)); |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 983 | if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi)) |
| 984 | break; |
Greg Rose | 5f8444a | 2011-10-08 03:05:24 +0000 | [diff] [blame] | 985 | vf_mac.vf = |
| 986 | vf_vlan.vf = |
| 987 | vf_tx_rate.vf = |
| 988 | vf_spoofchk.vf = ivi.vf; |
| 989 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 990 | memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); |
| 991 | vf_vlan.vlan = ivi.vlan; |
| 992 | vf_vlan.qos = ivi.qos; |
| 993 | vf_tx_rate.rate = ivi.tx_rate; |
Greg Rose | 5f8444a | 2011-10-08 03:05:24 +0000 | [diff] [blame] | 994 | vf_spoofchk.setting = ivi.spoofchk; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 995 | vf = nla_nest_start(skb, IFLA_VF_INFO); |
| 996 | if (!vf) { |
| 997 | nla_nest_cancel(skb, vfinfo); |
| 998 | goto nla_put_failure; |
| 999 | } |
| 1000 | NLA_PUT(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac); |
| 1001 | NLA_PUT(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan); |
Greg Rose | 5f8444a | 2011-10-08 03:05:24 +0000 | [diff] [blame] | 1002 | NLA_PUT(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), |
| 1003 | &vf_tx_rate); |
| 1004 | NLA_PUT(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), |
| 1005 | &vf_spoofchk); |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1006 | nla_nest_end(skb, vf); |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 1007 | } |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1008 | nla_nest_end(skb, vfinfo); |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 1009 | } |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1010 | |
| 1011 | if (rtnl_port_fill(skb, dev)) |
| 1012 | goto nla_put_failure; |
| 1013 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1014 | if (dev->rtnl_link_ops) { |
| 1015 | if (rtnl_link_fill(skb, dev) < 0) |
| 1016 | goto nla_put_failure; |
| 1017 | } |
| 1018 | |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 1019 | if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC))) |
| 1020 | goto nla_put_failure; |
| 1021 | |
| 1022 | list_for_each_entry(af_ops, &rtnl_af_ops, list) { |
| 1023 | if (af_ops->fill_link_af) { |
| 1024 | struct nlattr *af; |
| 1025 | int err; |
| 1026 | |
| 1027 | if (!(af = nla_nest_start(skb, af_ops->family))) |
| 1028 | goto nla_put_failure; |
| 1029 | |
| 1030 | err = af_ops->fill_link_af(skb, dev); |
| 1031 | |
| 1032 | /* |
| 1033 | * Caller may return ENODATA to indicate that there |
| 1034 | * was no data to be dumped. This is not an error, it |
| 1035 | * means we should trim the attribute header and |
| 1036 | * continue. |
| 1037 | */ |
| 1038 | if (err == -ENODATA) |
| 1039 | nla_nest_cancel(skb, af); |
| 1040 | else if (err < 0) |
| 1041 | goto nla_put_failure; |
| 1042 | |
| 1043 | nla_nest_end(skb, af); |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | nla_nest_end(skb, af_spec); |
| 1048 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1049 | return nlmsg_end(skb, nlh); |
| 1050 | |
| 1051 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 1052 | nlmsg_cancel(skb, nlh); |
| 1053 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1056 | 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] | 1057 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1058 | struct net *net = sock_net(skb->sk); |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1059 | int h, s_h; |
| 1060 | int idx = 0, s_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | struct net_device *dev; |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1062 | struct hlist_head *head; |
| 1063 | struct hlist_node *node; |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1064 | struct nlattr *tb[IFLA_MAX+1]; |
| 1065 | u32 ext_filter_mask = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1067 | s_h = cb->args[0]; |
| 1068 | s_idx = cb->args[1]; |
| 1069 | |
Eric Dumazet | e67f88d | 2011-04-27 22:56:07 +0000 | [diff] [blame] | 1070 | rcu_read_lock(); |
Thomas Graf | 4e985ad | 2011-06-21 03:11:20 +0000 | [diff] [blame] | 1071 | cb->seq = net->dev_base_seq; |
| 1072 | |
Michael Riesch | b38a241 | 2013-04-08 05:45:26 +0000 | [diff] [blame] | 1073 | if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX, |
Eric Dumazet | a4b64fb | 2012-03-04 12:32:10 +0000 | [diff] [blame] | 1074 | ifla_policy) >= 0) { |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1075 | |
Eric Dumazet | a4b64fb | 2012-03-04 12:32:10 +0000 | [diff] [blame] | 1076 | if (tb[IFLA_EXT_MASK]) |
| 1077 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); |
| 1078 | } |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1079 | |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1080 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { |
| 1081 | idx = 0; |
| 1082 | head = &net->dev_index_head[h]; |
Eric Dumazet | e67f88d | 2011-04-27 22:56:07 +0000 | [diff] [blame] | 1083 | hlist_for_each_entry_rcu(dev, node, head, index_hlist) { |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1084 | if (idx < s_idx) |
| 1085 | goto cont; |
| 1086 | if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK, |
| 1087 | NETLINK_CB(cb->skb).pid, |
| 1088 | cb->nlh->nlmsg_seq, 0, |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1089 | NLM_F_MULTI, |
| 1090 | ext_filter_mask) <= 0) |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1091 | goto out; |
Thomas Graf | 4e985ad | 2011-06-21 03:11:20 +0000 | [diff] [blame] | 1092 | |
| 1093 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 1094 | cont: |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1095 | idx++; |
| 1096 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | } |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1098 | out: |
Eric Dumazet | e67f88d | 2011-04-27 22:56:07 +0000 | [diff] [blame] | 1099 | rcu_read_unlock(); |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 1100 | cb->args[1] = idx; |
| 1101 | cb->args[0] = h; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | |
| 1103 | return skb->len; |
| 1104 | } |
| 1105 | |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1106 | const struct nla_policy ifla_policy[IFLA_MAX+1] = { |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 1107 | [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1108 | [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, |
| 1109 | [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 1110 | [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 1111 | [IFLA_MTU] = { .type = NLA_U32 }, |
Thomas Graf | 76e8730 | 2008-02-19 16:12:08 -0800 | [diff] [blame] | 1112 | [IFLA_LINK] = { .type = NLA_U32 }, |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1113 | [IFLA_MASTER] = { .type = NLA_U32 }, |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 1114 | [IFLA_TXQLEN] = { .type = NLA_U32 }, |
| 1115 | [IFLA_WEIGHT] = { .type = NLA_U32 }, |
| 1116 | [IFLA_OPERSTATE] = { .type = NLA_U8 }, |
| 1117 | [IFLA_LINKMODE] = { .type = NLA_U8 }, |
Thomas Graf | 76e8730 | 2008-02-19 16:12:08 -0800 | [diff] [blame] | 1118 | [IFLA_LINKINFO] = { .type = NLA_NESTED }, |
Eric W. Biederman | d8a5ec6 | 2007-09-12 13:57:04 +0200 | [diff] [blame] | 1119 | [IFLA_NET_NS_PID] = { .type = NLA_U32 }, |
Eric W. Biederman | f063052 | 2011-05-04 17:51:50 -0700 | [diff] [blame] | 1120 | [IFLA_NET_NS_FD] = { .type = NLA_U32 }, |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 1121 | [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 }, |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1122 | [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1123 | [IFLA_VF_PORTS] = { .type = NLA_NESTED }, |
| 1124 | [IFLA_PORT_SELF] = { .type = NLA_NESTED }, |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 1125 | [IFLA_AF_SPEC] = { .type = NLA_NESTED }, |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1126 | [IFLA_EXT_MASK] = { .type = NLA_U32 }, |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 1127 | }; |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 1128 | EXPORT_SYMBOL(ifla_policy); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 1129 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1130 | static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { |
| 1131 | [IFLA_INFO_KIND] = { .type = NLA_STRING }, |
| 1132 | [IFLA_INFO_DATA] = { .type = NLA_NESTED }, |
| 1133 | }; |
| 1134 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1135 | static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = { |
| 1136 | [IFLA_VF_INFO] = { .type = NLA_NESTED }, |
| 1137 | }; |
| 1138 | |
| 1139 | static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { |
| 1140 | [IFLA_VF_MAC] = { .type = NLA_BINARY, |
| 1141 | .len = sizeof(struct ifla_vf_mac) }, |
| 1142 | [IFLA_VF_VLAN] = { .type = NLA_BINARY, |
| 1143 | .len = sizeof(struct ifla_vf_vlan) }, |
| 1144 | [IFLA_VF_TX_RATE] = { .type = NLA_BINARY, |
| 1145 | .len = sizeof(struct ifla_vf_tx_rate) }, |
Greg Rose | 48752f6 | 2012-02-08 00:45:00 +0000 | [diff] [blame] | 1146 | [IFLA_VF_SPOOFCHK] = { .type = NLA_BINARY, |
| 1147 | .len = sizeof(struct ifla_vf_spoofchk) }, |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1148 | }; |
| 1149 | |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1150 | static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { |
| 1151 | [IFLA_PORT_VF] = { .type = NLA_U32 }, |
| 1152 | [IFLA_PORT_PROFILE] = { .type = NLA_STRING, |
| 1153 | .len = PORT_PROFILE_MAX }, |
| 1154 | [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, |
| 1155 | .len = sizeof(struct ifla_port_vsi)}, |
| 1156 | [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, |
| 1157 | .len = PORT_UUID_MAX }, |
| 1158 | [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, |
| 1159 | .len = PORT_UUID_MAX }, |
| 1160 | [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, |
| 1161 | [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, |
| 1162 | }; |
| 1163 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1164 | struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) |
| 1165 | { |
| 1166 | struct net *net; |
| 1167 | /* Examine the link attributes and figure out which |
| 1168 | * network namespace we are talking about. |
| 1169 | */ |
| 1170 | if (tb[IFLA_NET_NS_PID]) |
| 1171 | 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] | 1172 | else if (tb[IFLA_NET_NS_FD]) |
| 1173 | 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] | 1174 | else |
| 1175 | net = get_net(src_net); |
| 1176 | return net; |
| 1177 | } |
| 1178 | EXPORT_SYMBOL(rtnl_link_get_net); |
| 1179 | |
Thomas Graf | 1840bb1 | 2008-02-23 19:54:36 -0800 | [diff] [blame] | 1180 | static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) |
| 1181 | { |
| 1182 | if (dev) { |
| 1183 | if (tb[IFLA_ADDRESS] && |
| 1184 | nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) |
| 1185 | return -EINVAL; |
| 1186 | |
| 1187 | if (tb[IFLA_BROADCAST] && |
| 1188 | nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) |
| 1189 | return -EINVAL; |
| 1190 | } |
| 1191 | |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 1192 | if (tb[IFLA_AF_SPEC]) { |
| 1193 | struct nlattr *af; |
| 1194 | int rem, err; |
| 1195 | |
| 1196 | nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { |
| 1197 | const struct rtnl_af_ops *af_ops; |
| 1198 | |
| 1199 | if (!(af_ops = rtnl_af_lookup(nla_type(af)))) |
| 1200 | return -EAFNOSUPPORT; |
| 1201 | |
| 1202 | if (!af_ops->set_link_af) |
| 1203 | return -EOPNOTSUPP; |
| 1204 | |
| 1205 | if (af_ops->validate_link_af) { |
Kurt Van Dijck | 6d3a9a6 | 2011-01-26 04:55:24 +0000 | [diff] [blame] | 1206 | err = af_ops->validate_link_af(dev, af); |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 1207 | if (err < 0) |
| 1208 | return err; |
| 1209 | } |
| 1210 | } |
| 1211 | } |
| 1212 | |
Thomas Graf | 1840bb1 | 2008-02-23 19:54:36 -0800 | [diff] [blame] | 1213 | return 0; |
| 1214 | } |
| 1215 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1216 | static int do_setvfinfo(struct net_device *dev, struct nlattr *attr) |
| 1217 | { |
| 1218 | int rem, err = -EINVAL; |
| 1219 | struct nlattr *vf; |
| 1220 | const struct net_device_ops *ops = dev->netdev_ops; |
| 1221 | |
| 1222 | nla_for_each_nested(vf, attr, rem) { |
| 1223 | switch (nla_type(vf)) { |
| 1224 | case IFLA_VF_MAC: { |
| 1225 | struct ifla_vf_mac *ivm; |
| 1226 | ivm = nla_data(vf); |
| 1227 | err = -EOPNOTSUPP; |
| 1228 | if (ops->ndo_set_vf_mac) |
| 1229 | err = ops->ndo_set_vf_mac(dev, ivm->vf, |
| 1230 | ivm->mac); |
| 1231 | break; |
| 1232 | } |
| 1233 | case IFLA_VF_VLAN: { |
| 1234 | struct ifla_vf_vlan *ivv; |
| 1235 | ivv = nla_data(vf); |
| 1236 | err = -EOPNOTSUPP; |
| 1237 | if (ops->ndo_set_vf_vlan) |
| 1238 | err = ops->ndo_set_vf_vlan(dev, ivv->vf, |
| 1239 | ivv->vlan, |
| 1240 | ivv->qos); |
| 1241 | break; |
| 1242 | } |
| 1243 | case IFLA_VF_TX_RATE: { |
| 1244 | struct ifla_vf_tx_rate *ivt; |
| 1245 | ivt = nla_data(vf); |
| 1246 | err = -EOPNOTSUPP; |
| 1247 | if (ops->ndo_set_vf_tx_rate) |
| 1248 | err = ops->ndo_set_vf_tx_rate(dev, ivt->vf, |
| 1249 | ivt->rate); |
| 1250 | break; |
| 1251 | } |
Greg Rose | 5f8444a | 2011-10-08 03:05:24 +0000 | [diff] [blame] | 1252 | case IFLA_VF_SPOOFCHK: { |
| 1253 | struct ifla_vf_spoofchk *ivs; |
| 1254 | ivs = nla_data(vf); |
| 1255 | err = -EOPNOTSUPP; |
| 1256 | if (ops->ndo_set_vf_spoofchk) |
| 1257 | err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, |
| 1258 | ivs->setting); |
| 1259 | break; |
| 1260 | } |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1261 | default: |
| 1262 | err = -EINVAL; |
| 1263 | break; |
| 1264 | } |
| 1265 | if (err) |
| 1266 | break; |
| 1267 | } |
| 1268 | return err; |
| 1269 | } |
| 1270 | |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1271 | static int do_set_master(struct net_device *dev, int ifindex) |
| 1272 | { |
| 1273 | struct net_device *master_dev; |
| 1274 | const struct net_device_ops *ops; |
| 1275 | int err; |
| 1276 | |
| 1277 | if (dev->master) { |
| 1278 | if (dev->master->ifindex == ifindex) |
| 1279 | return 0; |
| 1280 | ops = dev->master->netdev_ops; |
| 1281 | if (ops->ndo_del_slave) { |
| 1282 | err = ops->ndo_del_slave(dev->master, dev); |
| 1283 | if (err) |
| 1284 | return err; |
| 1285 | } else { |
| 1286 | return -EOPNOTSUPP; |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | if (ifindex) { |
| 1291 | master_dev = __dev_get_by_index(dev_net(dev), ifindex); |
| 1292 | if (!master_dev) |
| 1293 | return -EINVAL; |
| 1294 | ops = master_dev->netdev_ops; |
| 1295 | if (ops->ndo_add_slave) { |
| 1296 | err = ops->ndo_add_slave(master_dev, dev); |
| 1297 | if (err) |
| 1298 | return err; |
| 1299 | } else { |
| 1300 | return -EOPNOTSUPP; |
| 1301 | } |
| 1302 | } |
| 1303 | return 0; |
| 1304 | } |
| 1305 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1306 | static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm, |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1307 | struct nlattr **tb, char *ifname, int modified) |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1308 | { |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 1309 | const struct net_device_ops *ops = dev->netdev_ops; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1310 | int send_addr_notify = 0; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1311 | int err; |
| 1312 | |
Eric W. Biederman | f063052 | 2011-05-04 17:51:50 -0700 | [diff] [blame] | 1313 | if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) { |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1314 | struct net *net = rtnl_link_get_net(dev_net(dev), tb); |
Eric W. Biederman | d8a5ec6 | 2007-09-12 13:57:04 +0200 | [diff] [blame] | 1315 | if (IS_ERR(net)) { |
| 1316 | err = PTR_ERR(net); |
| 1317 | goto errout; |
| 1318 | } |
| 1319 | err = dev_change_net_namespace(dev, net, ifname); |
| 1320 | put_net(net); |
| 1321 | if (err) |
| 1322 | goto errout; |
| 1323 | modified = 1; |
| 1324 | } |
| 1325 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1326 | if (tb[IFLA_MAP]) { |
| 1327 | struct rtnl_link_ifmap *u_map; |
| 1328 | struct ifmap k_map; |
| 1329 | |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 1330 | if (!ops->ndo_set_config) { |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1331 | err = -EOPNOTSUPP; |
| 1332 | goto errout; |
| 1333 | } |
| 1334 | |
| 1335 | if (!netif_device_present(dev)) { |
| 1336 | err = -ENODEV; |
| 1337 | goto errout; |
| 1338 | } |
| 1339 | |
| 1340 | u_map = nla_data(tb[IFLA_MAP]); |
| 1341 | k_map.mem_start = (unsigned long) u_map->mem_start; |
| 1342 | k_map.mem_end = (unsigned long) u_map->mem_end; |
| 1343 | k_map.base_addr = (unsigned short) u_map->base_addr; |
| 1344 | k_map.irq = (unsigned char) u_map->irq; |
| 1345 | k_map.dma = (unsigned char) u_map->dma; |
| 1346 | k_map.port = (unsigned char) u_map->port; |
| 1347 | |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 1348 | err = ops->ndo_set_config(dev, &k_map); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1349 | if (err < 0) |
| 1350 | goto errout; |
| 1351 | |
| 1352 | modified = 1; |
| 1353 | } |
| 1354 | |
| 1355 | if (tb[IFLA_ADDRESS]) { |
| 1356 | struct sockaddr *sa; |
| 1357 | int len; |
| 1358 | |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 1359 | if (!ops->ndo_set_mac_address) { |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1360 | err = -EOPNOTSUPP; |
| 1361 | goto errout; |
| 1362 | } |
| 1363 | |
| 1364 | if (!netif_device_present(dev)) { |
| 1365 | err = -ENODEV; |
| 1366 | goto errout; |
| 1367 | } |
| 1368 | |
| 1369 | len = sizeof(sa_family_t) + dev->addr_len; |
| 1370 | sa = kmalloc(len, GFP_KERNEL); |
| 1371 | if (!sa) { |
| 1372 | err = -ENOMEM; |
| 1373 | goto errout; |
| 1374 | } |
| 1375 | sa->sa_family = dev->type; |
| 1376 | memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), |
| 1377 | dev->addr_len); |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 1378 | err = ops->ndo_set_mac_address(dev, sa); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1379 | kfree(sa); |
| 1380 | if (err) |
| 1381 | goto errout; |
| 1382 | send_addr_notify = 1; |
| 1383 | modified = 1; |
Theodore Ts'o | 7044edd | 2012-07-04 21:23:25 -0400 | [diff] [blame] | 1384 | add_device_randomness(dev->dev_addr, dev->addr_len); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | if (tb[IFLA_MTU]) { |
| 1388 | err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); |
| 1389 | if (err < 0) |
| 1390 | goto errout; |
| 1391 | modified = 1; |
| 1392 | } |
| 1393 | |
Vlad Dogaru | cbda10f | 2011-01-13 23:38:30 +0000 | [diff] [blame] | 1394 | if (tb[IFLA_GROUP]) { |
| 1395 | dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); |
| 1396 | modified = 1; |
| 1397 | } |
| 1398 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1399 | /* |
| 1400 | * Interface selected by interface index but interface |
| 1401 | * name provided implies that a name change has been |
| 1402 | * requested. |
| 1403 | */ |
| 1404 | if (ifm->ifi_index > 0 && ifname[0]) { |
| 1405 | err = dev_change_name(dev, ifname); |
| 1406 | if (err < 0) |
| 1407 | goto errout; |
| 1408 | modified = 1; |
| 1409 | } |
| 1410 | |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 1411 | if (tb[IFLA_IFALIAS]) { |
| 1412 | err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), |
| 1413 | nla_len(tb[IFLA_IFALIAS])); |
| 1414 | if (err < 0) |
| 1415 | goto errout; |
| 1416 | modified = 1; |
| 1417 | } |
| 1418 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1419 | if (tb[IFLA_BROADCAST]) { |
| 1420 | nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); |
| 1421 | send_addr_notify = 1; |
| 1422 | } |
| 1423 | |
| 1424 | if (ifm->ifi_flags || ifm->ifi_change) { |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 1425 | err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); |
Johannes Berg | 5f9021c | 2008-11-16 23:20:31 -0800 | [diff] [blame] | 1426 | if (err < 0) |
| 1427 | goto errout; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1428 | } |
| 1429 | |
Jiri Pirko | fbaec0e | 2011-02-13 10:15:37 +0000 | [diff] [blame] | 1430 | if (tb[IFLA_MASTER]) { |
| 1431 | err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER])); |
| 1432 | if (err) |
| 1433 | goto errout; |
| 1434 | modified = 1; |
| 1435 | } |
| 1436 | |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 1437 | if (tb[IFLA_TXQLEN]) |
| 1438 | dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1439 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1440 | if (tb[IFLA_OPERSTATE]) |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 1441 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1442 | |
| 1443 | if (tb[IFLA_LINKMODE]) { |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 1444 | write_lock_bh(&dev_base_lock); |
| 1445 | dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); |
| 1446 | write_unlock_bh(&dev_base_lock); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1447 | } |
| 1448 | |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1449 | if (tb[IFLA_VFINFO_LIST]) { |
| 1450 | struct nlattr *attr; |
| 1451 | int rem; |
| 1452 | nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { |
David Howells | 253683b | 2010-05-21 02:25:27 +0000 | [diff] [blame] | 1453 | if (nla_type(attr) != IFLA_VF_INFO) { |
| 1454 | err = -EINVAL; |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1455 | goto errout; |
David Howells | 253683b | 2010-05-21 02:25:27 +0000 | [diff] [blame] | 1456 | } |
Chris Wright | c02db8c | 2010-05-16 01:05:45 -0700 | [diff] [blame] | 1457 | err = do_setvfinfo(dev, attr); |
| 1458 | if (err < 0) |
| 1459 | goto errout; |
| 1460 | modified = 1; |
| 1461 | } |
Williams, Mitch A | ebc08a6 | 2010-02-10 01:44:05 +0000 | [diff] [blame] | 1462 | } |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1463 | err = 0; |
| 1464 | |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1465 | if (tb[IFLA_VF_PORTS]) { |
| 1466 | struct nlattr *port[IFLA_PORT_MAX+1]; |
| 1467 | struct nlattr *attr; |
| 1468 | int vf; |
| 1469 | int rem; |
| 1470 | |
| 1471 | err = -EOPNOTSUPP; |
| 1472 | if (!ops->ndo_set_vf_port) |
| 1473 | goto errout; |
| 1474 | |
| 1475 | nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { |
| 1476 | if (nla_type(attr) != IFLA_VF_PORT) |
| 1477 | continue; |
| 1478 | err = nla_parse_nested(port, IFLA_PORT_MAX, |
| 1479 | attr, ifla_port_policy); |
| 1480 | if (err < 0) |
| 1481 | goto errout; |
| 1482 | if (!port[IFLA_PORT_VF]) { |
| 1483 | err = -EOPNOTSUPP; |
| 1484 | goto errout; |
| 1485 | } |
| 1486 | vf = nla_get_u32(port[IFLA_PORT_VF]); |
| 1487 | err = ops->ndo_set_vf_port(dev, vf, port); |
| 1488 | if (err < 0) |
| 1489 | goto errout; |
| 1490 | modified = 1; |
| 1491 | } |
| 1492 | } |
| 1493 | err = 0; |
| 1494 | |
| 1495 | if (tb[IFLA_PORT_SELF]) { |
| 1496 | struct nlattr *port[IFLA_PORT_MAX+1]; |
| 1497 | |
| 1498 | err = nla_parse_nested(port, IFLA_PORT_MAX, |
| 1499 | tb[IFLA_PORT_SELF], ifla_port_policy); |
| 1500 | if (err < 0) |
| 1501 | goto errout; |
| 1502 | |
| 1503 | err = -EOPNOTSUPP; |
| 1504 | if (ops->ndo_set_vf_port) |
| 1505 | err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); |
| 1506 | if (err < 0) |
| 1507 | goto errout; |
| 1508 | modified = 1; |
| 1509 | } |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 1510 | |
| 1511 | if (tb[IFLA_AF_SPEC]) { |
| 1512 | struct nlattr *af; |
| 1513 | int rem; |
| 1514 | |
| 1515 | nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { |
| 1516 | const struct rtnl_af_ops *af_ops; |
| 1517 | |
| 1518 | if (!(af_ops = rtnl_af_lookup(nla_type(af)))) |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 1519 | BUG(); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 1520 | |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 1521 | err = af_ops->set_link_af(dev, af); |
Thomas Graf | f8ff182 | 2010-11-16 04:30:14 +0000 | [diff] [blame] | 1522 | if (err < 0) |
| 1523 | goto errout; |
| 1524 | |
| 1525 | modified = 1; |
| 1526 | } |
| 1527 | } |
Scott Feldman | 57b6108 | 2010-05-17 22:49:55 -0700 | [diff] [blame] | 1528 | err = 0; |
| 1529 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1530 | errout: |
| 1531 | if (err < 0 && modified && net_ratelimit()) |
| 1532 | printk(KERN_WARNING "A link change request failed with " |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1533 | "some changes committed already. Interface %s may " |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1534 | "have been left with an inconsistent configuration, " |
| 1535 | "please check.\n", dev->name); |
| 1536 | |
| 1537 | if (send_addr_notify) |
| 1538 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); |
Stefan Gula | f18da14 | 2012-01-26 11:01:06 +0000 | [diff] [blame] | 1539 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1540 | return err; |
| 1541 | } |
| 1542 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 1543 | static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1545 | struct net *net = sock_net(skb->sk); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 1546 | struct ifinfomsg *ifm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | struct net_device *dev; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 1548 | int err; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 1549 | struct nlattr *tb[IFLA_MAX+1]; |
| 1550 | char ifname[IFNAMSIZ]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 1552 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 1553 | if (err < 0) |
| 1554 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 1556 | if (tb[IFLA_IFNAME]) |
| 1557 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
Patrick McHardy | 78e5b891 | 2006-09-13 20:35:36 -0700 | [diff] [blame] | 1558 | else |
| 1559 | ifname[0] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | err = -EINVAL; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 1562 | ifm = nlmsg_data(nlh); |
Patrick McHardy | 51055be | 2007-06-05 12:40:01 -0700 | [diff] [blame] | 1563 | if (ifm->ifi_index > 0) |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 1564 | dev = __dev_get_by_index(net, ifm->ifi_index); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 1565 | else if (tb[IFLA_IFNAME]) |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 1566 | dev = __dev_get_by_name(net, ifname); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 1567 | else |
| 1568 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 1570 | if (dev == NULL) { |
| 1571 | err = -ENODEV; |
| 1572 | goto errout; |
| 1573 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 1575 | err = validate_linkmsg(dev, tb); |
| 1576 | if (err < 0) |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 1577 | goto errout; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 1578 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1579 | err = do_setlink(dev, ifm, tb, ifname, 0); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 1580 | errout: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | return err; |
| 1582 | } |
| 1583 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1584 | static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) |
| 1585 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1586 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1587 | const struct rtnl_link_ops *ops; |
| 1588 | struct net_device *dev; |
| 1589 | struct ifinfomsg *ifm; |
| 1590 | char ifname[IFNAMSIZ]; |
| 1591 | struct nlattr *tb[IFLA_MAX+1]; |
| 1592 | int err; |
Eric Dumazet | 226bd34 | 2011-05-08 23:17:57 +0000 | [diff] [blame] | 1593 | LIST_HEAD(list_kill); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1594 | |
| 1595 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 1596 | if (err < 0) |
| 1597 | return err; |
| 1598 | |
| 1599 | if (tb[IFLA_IFNAME]) |
| 1600 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
| 1601 | |
| 1602 | ifm = nlmsg_data(nlh); |
| 1603 | if (ifm->ifi_index > 0) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1604 | dev = __dev_get_by_index(net, ifm->ifi_index); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1605 | else if (tb[IFLA_IFNAME]) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1606 | dev = __dev_get_by_name(net, ifname); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1607 | else |
| 1608 | return -EINVAL; |
| 1609 | |
| 1610 | if (!dev) |
| 1611 | return -ENODEV; |
| 1612 | |
| 1613 | ops = dev->rtnl_link_ops; |
| 1614 | if (!ops) |
| 1615 | return -EOPNOTSUPP; |
| 1616 | |
Eric Dumazet | 226bd34 | 2011-05-08 23:17:57 +0000 | [diff] [blame] | 1617 | ops->dellink(dev, &list_kill); |
| 1618 | unregister_netdevice_many(&list_kill); |
| 1619 | list_del(&list_kill); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1620 | return 0; |
| 1621 | } |
| 1622 | |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 1623 | int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) |
| 1624 | { |
| 1625 | unsigned int old_flags; |
| 1626 | int err; |
| 1627 | |
| 1628 | old_flags = dev->flags; |
| 1629 | if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { |
| 1630 | err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); |
| 1631 | if (err < 0) |
| 1632 | return err; |
| 1633 | } |
| 1634 | |
| 1635 | dev->rtnl_link_state = RTNL_LINK_INITIALIZED; |
| 1636 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); |
| 1637 | |
| 1638 | __dev_notify_flags(dev, old_flags); |
| 1639 | return 0; |
| 1640 | } |
| 1641 | EXPORT_SYMBOL(rtnl_configure_link); |
| 1642 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1643 | struct net_device *rtnl_create_link(struct net *src_net, struct net *net, |
| 1644 | char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[]) |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1645 | { |
| 1646 | int err; |
| 1647 | struct net_device *dev; |
Eric Dumazet | 2e59af3 | 2009-09-02 18:03:00 -0700 | [diff] [blame] | 1648 | unsigned int num_queues = 1; |
| 1649 | unsigned int real_num_queues = 1; |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1650 | |
Eric Dumazet | 2e59af3 | 2009-09-02 18:03:00 -0700 | [diff] [blame] | 1651 | if (ops->get_tx_queues) { |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1652 | err = ops->get_tx_queues(src_net, tb, &num_queues, |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 1653 | &real_num_queues); |
Eric Dumazet | 2e59af3 | 2009-09-02 18:03:00 -0700 | [diff] [blame] | 1654 | if (err) |
| 1655 | goto err; |
| 1656 | } |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1657 | err = -ENOMEM; |
Eric Dumazet | 2e59af3 | 2009-09-02 18:03:00 -0700 | [diff] [blame] | 1658 | dev = alloc_netdev_mq(ops->priv_size, ifname, ops->setup, num_queues); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1659 | if (!dev) |
| 1660 | goto err; |
| 1661 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1662 | dev_net_set(dev, net); |
| 1663 | dev->rtnl_link_ops = ops; |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 1664 | dev->rtnl_link_state = RTNL_LINK_INITIALIZING; |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1665 | |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1666 | if (tb[IFLA_MTU]) |
| 1667 | dev->mtu = nla_get_u32(tb[IFLA_MTU]); |
| 1668 | if (tb[IFLA_ADDRESS]) |
| 1669 | memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), |
| 1670 | nla_len(tb[IFLA_ADDRESS])); |
| 1671 | if (tb[IFLA_BROADCAST]) |
| 1672 | memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), |
| 1673 | nla_len(tb[IFLA_BROADCAST])); |
| 1674 | if (tb[IFLA_TXQLEN]) |
| 1675 | dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); |
| 1676 | if (tb[IFLA_OPERSTATE]) |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 1677 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1678 | if (tb[IFLA_LINKMODE]) |
| 1679 | dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); |
Patrick McHardy | ffa934f | 2011-01-20 03:00:42 +0000 | [diff] [blame] | 1680 | if (tb[IFLA_GROUP]) |
| 1681 | dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1682 | |
| 1683 | return dev; |
| 1684 | |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1685 | err: |
| 1686 | return ERR_PTR(err); |
| 1687 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 1688 | EXPORT_SYMBOL(rtnl_create_link); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1689 | |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 1690 | static int rtnl_group_changelink(struct net *net, int group, |
| 1691 | struct ifinfomsg *ifm, |
| 1692 | struct nlattr **tb) |
| 1693 | { |
| 1694 | struct net_device *dev; |
| 1695 | int err; |
| 1696 | |
| 1697 | for_each_netdev(net, dev) { |
| 1698 | if (dev->group == group) { |
| 1699 | err = do_setlink(dev, ifm, tb, NULL, 0); |
| 1700 | if (err < 0) |
| 1701 | return err; |
| 1702 | } |
| 1703 | } |
| 1704 | |
| 1705 | return 0; |
| 1706 | } |
| 1707 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1708 | static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) |
| 1709 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1710 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1711 | const struct rtnl_link_ops *ops; |
| 1712 | struct net_device *dev; |
| 1713 | struct ifinfomsg *ifm; |
| 1714 | char kind[MODULE_NAME_LEN]; |
| 1715 | char ifname[IFNAMSIZ]; |
| 1716 | struct nlattr *tb[IFLA_MAX+1]; |
| 1717 | struct nlattr *linkinfo[IFLA_INFO_MAX+1]; |
| 1718 | int err; |
| 1719 | |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 1720 | #ifdef CONFIG_MODULES |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1721 | replay: |
Thomas Graf | 8072f08 | 2007-07-31 14:13:50 -0700 | [diff] [blame] | 1722 | #endif |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1723 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 1724 | if (err < 0) |
| 1725 | return err; |
| 1726 | |
| 1727 | if (tb[IFLA_IFNAME]) |
| 1728 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
| 1729 | else |
| 1730 | ifname[0] = '\0'; |
| 1731 | |
| 1732 | ifm = nlmsg_data(nlh); |
| 1733 | if (ifm->ifi_index > 0) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1734 | dev = __dev_get_by_index(net, ifm->ifi_index); |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 1735 | else { |
| 1736 | if (ifname[0]) |
| 1737 | dev = __dev_get_by_name(net, ifname); |
Vlad Dogaru | e7ed828 | 2011-01-13 23:38:31 +0000 | [diff] [blame] | 1738 | else |
| 1739 | dev = NULL; |
| 1740 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1741 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 1742 | err = validate_linkmsg(dev, tb); |
| 1743 | if (err < 0) |
Thomas Graf | 1840bb1 | 2008-02-23 19:54:36 -0800 | [diff] [blame] | 1744 | return err; |
| 1745 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1746 | if (tb[IFLA_LINKINFO]) { |
| 1747 | err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, |
| 1748 | tb[IFLA_LINKINFO], ifla_info_policy); |
| 1749 | if (err < 0) |
| 1750 | return err; |
| 1751 | } else |
| 1752 | memset(linkinfo, 0, sizeof(linkinfo)); |
| 1753 | |
| 1754 | if (linkinfo[IFLA_INFO_KIND]) { |
| 1755 | nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); |
| 1756 | ops = rtnl_link_ops_get(kind); |
| 1757 | } else { |
| 1758 | kind[0] = '\0'; |
| 1759 | ops = NULL; |
| 1760 | } |
| 1761 | |
| 1762 | if (1) { |
| 1763 | struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL; |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1764 | struct net *dest_net; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1765 | |
| 1766 | if (ops) { |
| 1767 | if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { |
| 1768 | err = nla_parse_nested(attr, ops->maxtype, |
| 1769 | linkinfo[IFLA_INFO_DATA], |
| 1770 | ops->policy); |
| 1771 | if (err < 0) |
| 1772 | return err; |
| 1773 | data = attr; |
| 1774 | } |
| 1775 | if (ops->validate) { |
| 1776 | err = ops->validate(tb, data); |
| 1777 | if (err < 0) |
| 1778 | return err; |
| 1779 | } |
| 1780 | } |
| 1781 | |
| 1782 | if (dev) { |
| 1783 | int modified = 0; |
| 1784 | |
| 1785 | if (nlh->nlmsg_flags & NLM_F_EXCL) |
| 1786 | return -EEXIST; |
| 1787 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
| 1788 | return -EOPNOTSUPP; |
| 1789 | |
| 1790 | if (linkinfo[IFLA_INFO_DATA]) { |
| 1791 | if (!ops || ops != dev->rtnl_link_ops || |
| 1792 | !ops->changelink) |
| 1793 | return -EOPNOTSUPP; |
| 1794 | |
| 1795 | err = ops->changelink(dev, tb, data); |
| 1796 | if (err < 0) |
| 1797 | return err; |
| 1798 | modified = 1; |
| 1799 | } |
| 1800 | |
| 1801 | return do_setlink(dev, ifm, tb, ifname, modified); |
| 1802 | } |
| 1803 | |
Patrick McHardy | ffa934f | 2011-01-20 03:00:42 +0000 | [diff] [blame] | 1804 | if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { |
| 1805 | if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) |
| 1806 | return rtnl_group_changelink(net, |
| 1807 | nla_get_u32(tb[IFLA_GROUP]), |
| 1808 | ifm, tb); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1809 | return -ENODEV; |
Patrick McHardy | ffa934f | 2011-01-20 03:00:42 +0000 | [diff] [blame] | 1810 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1811 | |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 1812 | if (ifm->ifi_index) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1813 | return -EOPNOTSUPP; |
Patrick McHardy | 0e06877 | 2007-07-11 19:42:31 -0700 | [diff] [blame] | 1814 | if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO]) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1815 | return -EOPNOTSUPP; |
| 1816 | |
| 1817 | if (!ops) { |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 1818 | #ifdef CONFIG_MODULES |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1819 | if (kind[0]) { |
| 1820 | __rtnl_unlock(); |
| 1821 | request_module("rtnl-link-%s", kind); |
| 1822 | rtnl_lock(); |
| 1823 | ops = rtnl_link_ops_get(kind); |
| 1824 | if (ops) |
| 1825 | goto replay; |
| 1826 | } |
| 1827 | #endif |
| 1828 | return -EOPNOTSUPP; |
| 1829 | } |
| 1830 | |
| 1831 | if (!ifname[0]) |
| 1832 | snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1833 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1834 | dest_net = rtnl_link_get_net(net, tb); |
Eric W. Biederman | 13ad177 | 2011-01-29 14:57:22 +0000 | [diff] [blame] | 1835 | if (IS_ERR(dest_net)) |
| 1836 | return PTR_ERR(dest_net); |
| 1837 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1838 | dev = rtnl_create_link(net, dest_net, ifname, ops, tb); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1839 | |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1840 | if (IS_ERR(dev)) |
| 1841 | err = PTR_ERR(dev); |
| 1842 | else if (ops->newlink) |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1843 | err = ops->newlink(net, dev, tb, data); |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 1844 | else |
| 1845 | err = register_netdevice(dev); |
Dan Carpenter | 80032cf | 2010-04-21 23:53:27 +0000 | [diff] [blame] | 1846 | |
| 1847 | if (err < 0 && !IS_ERR(dev)) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1848 | free_netdev(dev); |
Dan Carpenter | 80032cf | 2010-04-21 23:53:27 +0000 | [diff] [blame] | 1849 | if (err < 0) |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 1850 | goto out; |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1851 | |
Patrick McHardy | 3729d50 | 2010-02-26 06:34:54 +0000 | [diff] [blame] | 1852 | err = rtnl_configure_link(dev, ifm); |
| 1853 | if (err < 0) |
| 1854 | unregister_netdevice(dev); |
| 1855 | out: |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1856 | put_net(dest_net); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1857 | return err; |
| 1858 | } |
| 1859 | } |
| 1860 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1861 | static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 1862 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1863 | struct net *net = sock_net(skb->sk); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1864 | struct ifinfomsg *ifm; |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 1865 | char ifname[IFNAMSIZ]; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1866 | struct nlattr *tb[IFLA_MAX+1]; |
| 1867 | struct net_device *dev = NULL; |
| 1868 | struct sk_buff *nskb; |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 1869 | int err; |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1870 | u32 ext_filter_mask = 0; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 1871 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1872 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 1873 | if (err < 0) |
Eric Sesterhenn | 9918f23 | 2006-09-26 23:26:38 -0700 | [diff] [blame] | 1874 | return err; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1875 | |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 1876 | if (tb[IFLA_IFNAME]) |
| 1877 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
| 1878 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1879 | if (tb[IFLA_EXT_MASK]) |
| 1880 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); |
| 1881 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1882 | ifm = nlmsg_data(nlh); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 1883 | if (ifm->ifi_index > 0) |
| 1884 | dev = __dev_get_by_index(net, ifm->ifi_index); |
| 1885 | else if (tb[IFLA_IFNAME]) |
| 1886 | dev = __dev_get_by_name(net, ifname); |
| 1887 | else |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 1888 | return -EINVAL; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1889 | |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 1890 | if (dev == NULL) |
| 1891 | return -ENODEV; |
| 1892 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1893 | nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 1894 | if (nskb == NULL) |
| 1895 | return -ENOBUFS; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 1896 | |
Patrick McHardy | 575c3e2 | 2007-05-22 17:00:49 -0700 | [diff] [blame] | 1897 | err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid, |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1898 | nlh->nlmsg_seq, 0, 0, ext_filter_mask); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 1899 | if (err < 0) { |
| 1900 | /* -EMSGSIZE implies BUG in if_nlmsg_size */ |
| 1901 | WARN_ON(err == -EMSGSIZE); |
| 1902 | kfree_skb(nskb); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 1903 | } else |
| 1904 | err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1905 | |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 1906 | return err; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 1907 | } |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 1908 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1909 | static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 1910 | { |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1911 | struct net *net = sock_net(skb->sk); |
| 1912 | struct net_device *dev; |
| 1913 | struct nlattr *tb[IFLA_MAX+1]; |
| 1914 | u32 ext_filter_mask = 0; |
| 1915 | u16 min_ifinfo_dump_size = 0; |
| 1916 | |
Michael Riesch | b38a241 | 2013-04-08 05:45:26 +0000 | [diff] [blame] | 1917 | if (nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX, |
Eric Dumazet | a4b64fb | 2012-03-04 12:32:10 +0000 | [diff] [blame] | 1918 | ifla_policy) >= 0) { |
| 1919 | if (tb[IFLA_EXT_MASK]) |
| 1920 | ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); |
| 1921 | } |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1922 | |
| 1923 | if (!ext_filter_mask) |
| 1924 | return NLMSG_GOODSIZE; |
| 1925 | /* |
| 1926 | * traverse the list of net devices and compute the minimum |
| 1927 | * buffer size based upon the filter mask. |
| 1928 | */ |
| 1929 | list_for_each_entry(dev, &net->dev_base_head, dev_list) { |
| 1930 | min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size, |
| 1931 | if_nlmsg_size(dev, |
| 1932 | ext_filter_mask)); |
| 1933 | } |
| 1934 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 1935 | return min_ifinfo_dump_size; |
| 1936 | } |
| 1937 | |
Adrian Bunk | 42bad1d | 2007-04-26 00:57:41 -0700 | [diff] [blame] | 1938 | 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] | 1939 | { |
| 1940 | int idx; |
| 1941 | int s_idx = cb->family; |
| 1942 | |
| 1943 | if (s_idx == 0) |
| 1944 | s_idx = 1; |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 1945 | for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1946 | int type = cb->nlh->nlmsg_type-RTM_BASE; |
| 1947 | if (idx < s_idx || idx == PF_PACKET) |
| 1948 | continue; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 1949 | if (rtnl_msg_handlers[idx] == NULL || |
| 1950 | rtnl_msg_handlers[idx][type].dumpit == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1951 | continue; |
| 1952 | if (idx > s_idx) |
| 1953 | memset(&cb->args[0], 0, sizeof(cb->args)); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 1954 | if (rtnl_msg_handlers[idx][type].dumpit(skb, cb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | break; |
| 1956 | } |
| 1957 | cb->family = idx; |
| 1958 | |
| 1959 | return skb->len; |
| 1960 | } |
| 1961 | |
| 1962 | void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change) |
| 1963 | { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1964 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | struct sk_buff *skb; |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 1966 | int err = -ENOBUFS; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 1967 | size_t if_info_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1969 | skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), GFP_KERNEL); |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 1970 | if (skb == NULL) |
| 1971 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 1973 | err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 1974 | if (err < 0) { |
| 1975 | /* -EMSGSIZE implies BUG in if_nlmsg_size() */ |
| 1976 | WARN_ON(err == -EMSGSIZE); |
| 1977 | kfree_skb(skb); |
| 1978 | goto errout; |
| 1979 | } |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 1980 | rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL); |
| 1981 | return; |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 1982 | errout: |
| 1983 | if (err < 0) |
Eric W. Biederman | 4b3da70 | 2007-11-19 22:27:40 -0800 | [diff] [blame] | 1984 | rtnl_set_sk_err(net, RTNLGRP_LINK, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | } |
| 1986 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | /* Protected by RTNL sempahore. */ |
| 1988 | static struct rtattr **rta_buf; |
| 1989 | static int rtattr_max; |
| 1990 | |
| 1991 | /* Process one rtnetlink message. */ |
| 1992 | |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 1993 | static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1995 | struct net *net = sock_net(skb->sk); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 1996 | rtnl_doit_func doit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | int sz_idx, kind; |
| 1998 | int min_len; |
| 1999 | int family; |
| 2000 | int type; |
Eric Dumazet | 2907c35 | 2011-05-25 07:34:04 +0000 | [diff] [blame] | 2001 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | type = nlh->nlmsg_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | if (type > RTM_MAX) |
Thomas Graf | 038890f | 2007-04-05 14:35:52 -0700 | [diff] [blame] | 2005 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | |
| 2007 | type -= RTM_BASE; |
| 2008 | |
| 2009 | /* All the messages must have at least 1 byte length */ |
| 2010 | if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg))) |
| 2011 | return 0; |
| 2012 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 2013 | family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | sz_idx = type>>2; |
| 2015 | kind = type&3; |
| 2016 | |
Eric Paris | fd77846 | 2012-01-03 12:25:16 -0500 | [diff] [blame] | 2017 | if (kind != 2 && !capable(CAP_NET_ADMIN)) |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 2018 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | |
David S. Miller | b8f3ab4 | 2011-01-18 12:40:38 -0800 | [diff] [blame] | 2020 | if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 2021 | struct sock *rtnl; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 2022 | rtnl_dumpit_func dumpit; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2023 | rtnl_calcit_func calcit; |
| 2024 | u16 min_dump_alloc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 2026 | dumpit = rtnl_get_dumpit(family, type); |
| 2027 | if (dumpit == NULL) |
Thomas Graf | 038890f | 2007-04-05 14:35:52 -0700 | [diff] [blame] | 2028 | return -EOPNOTSUPP; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2029 | calcit = rtnl_get_calcit(family, type); |
| 2030 | if (calcit) |
Greg Rose | 115c9b8 | 2012-02-21 16:54:48 -0500 | [diff] [blame] | 2031 | min_dump_alloc = calcit(skb, nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | |
Eric Dumazet | 2907c35 | 2011-05-25 07:34:04 +0000 | [diff] [blame] | 2033 | __rtnl_unlock(); |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 2034 | rtnl = net->rtnl; |
Pablo Neira Ayuso | 80d326f | 2012-02-24 14:30:15 +0000 | [diff] [blame] | 2035 | { |
| 2036 | struct netlink_dump_control c = { |
| 2037 | .dump = dumpit, |
| 2038 | .min_dump_alloc = min_dump_alloc, |
| 2039 | }; |
| 2040 | err = netlink_dump_start(rtnl, skb, nlh, &c); |
| 2041 | } |
Eric Dumazet | 2907c35 | 2011-05-25 07:34:04 +0000 | [diff] [blame] | 2042 | rtnl_lock(); |
| 2043 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | } |
| 2045 | |
| 2046 | memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *))); |
| 2047 | |
| 2048 | min_len = rtm_min[sz_idx]; |
| 2049 | if (nlh->nlmsg_len < min_len) |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 2050 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | |
| 2052 | if (nlh->nlmsg_len > min_len) { |
| 2053 | int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len); |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 2054 | struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | |
| 2056 | while (RTA_OK(attr, attrlen)) { |
Vlad Yasevich | 40abddfd | 2013-03-13 04:18:58 +0000 | [diff] [blame] | 2057 | unsigned int flavor = attr->rta_type & NLA_TYPE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | if (flavor) { |
| 2059 | if (flavor > rta_max[sz_idx]) |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 2060 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | rta_buf[flavor-1] = attr; |
| 2062 | } |
| 2063 | attr = RTA_NEXT(attr, attrlen); |
| 2064 | } |
| 2065 | } |
| 2066 | |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 2067 | doit = rtnl_get_doit(family, type); |
| 2068 | if (doit == NULL) |
Thomas Graf | 038890f | 2007-04-05 14:35:52 -0700 | [diff] [blame] | 2069 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 2071 | return doit(skb, nlh, (void *)&rta_buf[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | } |
| 2073 | |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 2074 | static void rtnetlink_rcv(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | { |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 2076 | rtnl_lock(); |
| 2077 | netlink_rcv_skb(skb, &rtnetlink_rcv_msg); |
| 2078 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | } |
| 2080 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 2082 | { |
| 2083 | struct net_device *dev = ptr; |
Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 2084 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | switch (event) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | case NETDEV_UP: |
| 2087 | case NETDEV_DOWN: |
Patrick McHardy | 10de05a | 2010-02-26 06:34:50 +0000 | [diff] [blame] | 2088 | case NETDEV_PRE_UP: |
Eric W. Biederman | d90a909 | 2009-12-12 22:11:15 +0000 | [diff] [blame] | 2089 | case NETDEV_POST_INIT: |
| 2090 | case NETDEV_REGISTER: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | case NETDEV_CHANGE: |
Patrick McHardy | 755d0e7 | 2010-03-19 04:42:24 +0000 | [diff] [blame] | 2092 | case NETDEV_PRE_TYPE_CHANGE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | case NETDEV_GOING_DOWN: |
Patrick McHardy | a283576 | 2010-02-26 06:34:51 +0000 | [diff] [blame] | 2094 | case NETDEV_UNREGISTER: |
Eric W. Biederman | d90a909 | 2009-12-12 22:11:15 +0000 | [diff] [blame] | 2095 | case NETDEV_UNREGISTER_BATCH: |
Amerigo Wang | ac3d3f8 | 2011-05-19 23:06:32 +0000 | [diff] [blame] | 2096 | case NETDEV_RELEASE: |
| 2097 | case NETDEV_JOIN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | break; |
| 2099 | default: |
| 2100 | rtmsg_ifinfo(RTM_NEWLINK, dev, 0); |
| 2101 | break; |
| 2102 | } |
| 2103 | return NOTIFY_DONE; |
| 2104 | } |
| 2105 | |
| 2106 | static struct notifier_block rtnetlink_dev_notifier = { |
| 2107 | .notifier_call = rtnetlink_event, |
| 2108 | }; |
| 2109 | |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 2110 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 2111 | static int __net_init rtnetlink_net_init(struct net *net) |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 2112 | { |
| 2113 | struct sock *sk; |
| 2114 | sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX, |
Eric Dumazet | 2907c35 | 2011-05-25 07:34:04 +0000 | [diff] [blame] | 2115 | rtnetlink_rcv, &rtnl_mutex, THIS_MODULE); |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 2116 | if (!sk) |
| 2117 | return -ENOMEM; |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 2118 | net->rtnl = sk; |
| 2119 | return 0; |
| 2120 | } |
| 2121 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 2122 | static void __net_exit rtnetlink_net_exit(struct net *net) |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 2123 | { |
Denis V. Lunev | 775516b | 2008-01-18 23:55:19 -0800 | [diff] [blame] | 2124 | netlink_kernel_release(net->rtnl); |
| 2125 | net->rtnl = NULL; |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 2126 | } |
| 2127 | |
| 2128 | static struct pernet_operations rtnetlink_net_ops = { |
| 2129 | .init = rtnetlink_net_init, |
| 2130 | .exit = rtnetlink_net_exit, |
| 2131 | }; |
| 2132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | void __init rtnetlink_init(void) |
| 2134 | { |
| 2135 | int i; |
| 2136 | |
| 2137 | rtattr_max = 0; |
| 2138 | for (i = 0; i < ARRAY_SIZE(rta_max); i++) |
| 2139 | if (rta_max[i] > rtattr_max) |
| 2140 | rtattr_max = rta_max[i]; |
| 2141 | rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL); |
| 2142 | if (!rta_buf) |
| 2143 | panic("rtnetlink_init: cannot allocate rta_buf\n"); |
| 2144 | |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 2145 | if (register_pernet_subsys(&rtnetlink_net_ops)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | panic("rtnetlink_init: cannot initialize rtnetlink\n"); |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 2147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV); |
| 2149 | register_netdevice_notifier(&rtnetlink_dev_notifier); |
Thomas Graf | 340d17f | 2007-03-22 11:49:22 -0700 | [diff] [blame] | 2150 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2151 | rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, |
| 2152 | rtnl_dump_ifinfo, rtnl_calcit); |
| 2153 | rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL); |
| 2154 | rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL); |
| 2155 | rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL); |
Thomas Graf | 687ad8c | 2007-03-22 11:59:42 -0700 | [diff] [blame] | 2156 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2157 | rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL); |
| 2158 | rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | } |
| 2160 | |