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