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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | #include <asm/uaccess.h> |
| 40 | #include <asm/system.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | #include <linux/inet.h> |
| 43 | #include <linux/netdevice.h> |
| 44 | #include <net/ip.h> |
| 45 | #include <net/protocol.h> |
| 46 | #include <net/arp.h> |
| 47 | #include <net/route.h> |
| 48 | #include <net/udp.h> |
| 49 | #include <net/sock.h> |
| 50 | #include <net/pkt_sched.h> |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 51 | #include <net/fib_rules.h> |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 52 | #include <net/rtnetlink.h> |
Johannes Berg | 30ffee8 | 2009-07-10 09:51:35 +0000 | [diff] [blame] | 53 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 55 | struct rtnl_link { |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 56 | rtnl_doit_func doit; |
| 57 | rtnl_dumpit_func dumpit; |
| 58 | }; |
| 59 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 60 | static DEFINE_MUTEX(rtnl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | void rtnl_lock(void) |
| 63 | { |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 64 | mutex_lock(&rtnl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 66 | EXPORT_SYMBOL(rtnl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 68 | void __rtnl_unlock(void) |
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 | mutex_unlock(&rtnl_mutex); |
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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | void rtnl_unlock(void) |
| 74 | { |
Herbert Xu | 58ec3b4 | 2008-10-07 15:50:03 -0700 | [diff] [blame] | 75 | /* This fellow will unlock it for us. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | netdev_run_todo(); |
| 77 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 78 | EXPORT_SYMBOL(rtnl_unlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 80 | int rtnl_trylock(void) |
| 81 | { |
| 82 | return mutex_trylock(&rtnl_mutex); |
| 83 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 84 | EXPORT_SYMBOL(rtnl_trylock); |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 85 | |
Patrick McHardy | c9c1014 | 2008-04-23 22:10:48 -0700 | [diff] [blame] | 86 | int rtnl_is_locked(void) |
| 87 | { |
| 88 | return mutex_is_locked(&rtnl_mutex); |
| 89 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 90 | EXPORT_SYMBOL(rtnl_is_locked); |
Patrick McHardy | c9c1014 | 2008-04-23 22:10:48 -0700 | [diff] [blame] | 91 | |
Paul E. McKenney | a898def | 2010-02-22 17:04:49 -0800 | [diff] [blame^] | 92 | #ifdef CONFIG_PROVE_LOCKING |
| 93 | int lockdep_rtnl_is_held(void) |
| 94 | { |
| 95 | return lockdep_is_held(&rtnl_mutex); |
| 96 | } |
| 97 | EXPORT_SYMBOL(lockdep_rtnl_is_held); |
| 98 | #endif /* #ifdef CONFIG_PROVE_LOCKING */ |
| 99 | |
Adrian Bunk | 42bad1d | 2007-04-26 00:57:41 -0700 | [diff] [blame] | 100 | static struct rtnl_link *rtnl_msg_handlers[NPROTO]; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 101 | |
| 102 | static inline int rtm_msgindex(int msgtype) |
| 103 | { |
| 104 | int msgindex = msgtype - RTM_BASE; |
| 105 | |
| 106 | /* |
| 107 | * msgindex < 0 implies someone tried to register a netlink |
| 108 | * control code. msgindex >= RTM_NR_MSGTYPES may indicate that |
| 109 | * the message type has not been added to linux/rtnetlink.h |
| 110 | */ |
| 111 | BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); |
| 112 | |
| 113 | return msgindex; |
| 114 | } |
| 115 | |
| 116 | static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex) |
| 117 | { |
| 118 | struct rtnl_link *tab; |
| 119 | |
| 120 | tab = rtnl_msg_handlers[protocol]; |
Thomas Graf | 51057f2 | 2007-03-22 21:41:06 -0700 | [diff] [blame] | 121 | if (tab == NULL || tab[msgindex].doit == NULL) |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 122 | tab = rtnl_msg_handlers[PF_UNSPEC]; |
| 123 | |
Thomas Graf | 51057f2 | 2007-03-22 21:41:06 -0700 | [diff] [blame] | 124 | return tab ? tab[msgindex].doit : NULL; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex) |
| 128 | { |
| 129 | struct rtnl_link *tab; |
| 130 | |
| 131 | tab = rtnl_msg_handlers[protocol]; |
Thomas Graf | 51057f2 | 2007-03-22 21:41:06 -0700 | [diff] [blame] | 132 | if (tab == NULL || tab[msgindex].dumpit == NULL) |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 133 | tab = rtnl_msg_handlers[PF_UNSPEC]; |
| 134 | |
Thomas Graf | 51057f2 | 2007-03-22 21:41:06 -0700 | [diff] [blame] | 135 | return tab ? tab[msgindex].dumpit : NULL; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** |
| 139 | * __rtnl_register - Register a rtnetlink message type |
| 140 | * @protocol: Protocol family or PF_UNSPEC |
| 141 | * @msgtype: rtnetlink message type |
| 142 | * @doit: Function pointer called for each request message |
| 143 | * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message |
| 144 | * |
| 145 | * Registers the specified function pointers (at least one of them has |
| 146 | * to be non-NULL) to be called whenever a request message for the |
| 147 | * specified protocol family and message type is received. |
| 148 | * |
| 149 | * The special protocol family PF_UNSPEC may be used to define fallback |
| 150 | * function pointers for the case when no entry for the specific protocol |
| 151 | * family exists. |
| 152 | * |
| 153 | * Returns 0 on success or a negative error code. |
| 154 | */ |
| 155 | int __rtnl_register(int protocol, int msgtype, |
| 156 | rtnl_doit_func doit, rtnl_dumpit_func dumpit) |
| 157 | { |
| 158 | struct rtnl_link *tab; |
| 159 | int msgindex; |
| 160 | |
| 161 | BUG_ON(protocol < 0 || protocol >= NPROTO); |
| 162 | msgindex = rtm_msgindex(msgtype); |
| 163 | |
| 164 | tab = rtnl_msg_handlers[protocol]; |
| 165 | if (tab == NULL) { |
| 166 | tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL); |
| 167 | if (tab == NULL) |
| 168 | return -ENOBUFS; |
| 169 | |
| 170 | rtnl_msg_handlers[protocol] = tab; |
| 171 | } |
| 172 | |
| 173 | if (doit) |
| 174 | tab[msgindex].doit = doit; |
| 175 | |
| 176 | if (dumpit) |
| 177 | tab[msgindex].dumpit = dumpit; |
| 178 | |
| 179 | return 0; |
| 180 | } |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 181 | EXPORT_SYMBOL_GPL(__rtnl_register); |
| 182 | |
| 183 | /** |
| 184 | * rtnl_register - Register a rtnetlink message type |
| 185 | * |
| 186 | * Identical to __rtnl_register() but panics on failure. This is useful |
| 187 | * as failure of this function is very unlikely, it can only happen due |
| 188 | * to lack of memory when allocating the chain to store all message |
| 189 | * handlers for a protocol. Meant for use in init functions where lack |
| 190 | * of memory implies no sense in continueing. |
| 191 | */ |
| 192 | void rtnl_register(int protocol, int msgtype, |
| 193 | rtnl_doit_func doit, rtnl_dumpit_func dumpit) |
| 194 | { |
| 195 | if (__rtnl_register(protocol, msgtype, doit, dumpit) < 0) |
| 196 | panic("Unable to register rtnetlink message handler, " |
| 197 | "protocol = %d, message type = %d\n", |
| 198 | protocol, msgtype); |
| 199 | } |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 200 | EXPORT_SYMBOL_GPL(rtnl_register); |
| 201 | |
| 202 | /** |
| 203 | * rtnl_unregister - Unregister a rtnetlink message type |
| 204 | * @protocol: Protocol family or PF_UNSPEC |
| 205 | * @msgtype: rtnetlink message type |
| 206 | * |
| 207 | * Returns 0 on success or a negative error code. |
| 208 | */ |
| 209 | int rtnl_unregister(int protocol, int msgtype) |
| 210 | { |
| 211 | int msgindex; |
| 212 | |
| 213 | BUG_ON(protocol < 0 || protocol >= NPROTO); |
| 214 | msgindex = rtm_msgindex(msgtype); |
| 215 | |
| 216 | if (rtnl_msg_handlers[protocol] == NULL) |
| 217 | return -ENOENT; |
| 218 | |
| 219 | rtnl_msg_handlers[protocol][msgindex].doit = NULL; |
| 220 | rtnl_msg_handlers[protocol][msgindex].dumpit = NULL; |
| 221 | |
| 222 | return 0; |
| 223 | } |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 224 | EXPORT_SYMBOL_GPL(rtnl_unregister); |
| 225 | |
| 226 | /** |
| 227 | * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol |
| 228 | * @protocol : Protocol family or PF_UNSPEC |
| 229 | * |
| 230 | * Identical to calling rtnl_unregster() for all registered message types |
| 231 | * of a certain protocol family. |
| 232 | */ |
| 233 | void rtnl_unregister_all(int protocol) |
| 234 | { |
| 235 | BUG_ON(protocol < 0 || protocol >= NPROTO); |
| 236 | |
| 237 | kfree(rtnl_msg_handlers[protocol]); |
| 238 | rtnl_msg_handlers[protocol] = NULL; |
| 239 | } |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 240 | EXPORT_SYMBOL_GPL(rtnl_unregister_all); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 242 | static LIST_HEAD(link_ops); |
| 243 | |
| 244 | /** |
| 245 | * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. |
| 246 | * @ops: struct rtnl_link_ops * to register |
| 247 | * |
| 248 | * The caller must hold the rtnl_mutex. This function should be used |
| 249 | * by drivers that create devices during module initialization. It |
| 250 | * must be called before registering the devices. |
| 251 | * |
| 252 | * Returns 0 on success or a negative error code. |
| 253 | */ |
| 254 | int __rtnl_link_register(struct rtnl_link_ops *ops) |
| 255 | { |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 256 | if (!ops->dellink) |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 257 | ops->dellink = unregister_netdevice_queue; |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 258 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 259 | list_add_tail(&ops->list, &link_ops); |
| 260 | return 0; |
| 261 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 262 | EXPORT_SYMBOL_GPL(__rtnl_link_register); |
| 263 | |
| 264 | /** |
| 265 | * rtnl_link_register - Register rtnl_link_ops with rtnetlink. |
| 266 | * @ops: struct rtnl_link_ops * to register |
| 267 | * |
| 268 | * Returns 0 on success or a negative error code. |
| 269 | */ |
| 270 | int rtnl_link_register(struct rtnl_link_ops *ops) |
| 271 | { |
| 272 | int err; |
| 273 | |
| 274 | rtnl_lock(); |
| 275 | err = __rtnl_link_register(ops); |
| 276 | rtnl_unlock(); |
| 277 | return err; |
| 278 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 279 | EXPORT_SYMBOL_GPL(rtnl_link_register); |
| 280 | |
Pavel Emelyanov | 669f87b | 2008-04-16 00:46:52 -0700 | [diff] [blame] | 281 | static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) |
| 282 | { |
| 283 | struct net_device *dev; |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 284 | LIST_HEAD(list_kill); |
| 285 | |
Pavel Emelyanov | 669f87b | 2008-04-16 00:46:52 -0700 | [diff] [blame] | 286 | for_each_netdev(net, dev) { |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 287 | if (dev->rtnl_link_ops == ops) |
| 288 | ops->dellink(dev, &list_kill); |
Pavel Emelyanov | 669f87b | 2008-04-16 00:46:52 -0700 | [diff] [blame] | 289 | } |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 290 | unregister_netdevice_many(&list_kill); |
Pavel Emelyanov | 669f87b | 2008-04-16 00:46:52 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | void rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) |
| 294 | { |
| 295 | rtnl_lock(); |
| 296 | __rtnl_kill_links(net, ops); |
| 297 | rtnl_unlock(); |
| 298 | } |
| 299 | EXPORT_SYMBOL_GPL(rtnl_kill_links); |
| 300 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 301 | /** |
| 302 | * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. |
| 303 | * @ops: struct rtnl_link_ops * to unregister |
| 304 | * |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 305 | * The caller must hold the rtnl_mutex. |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 306 | */ |
| 307 | void __rtnl_link_unregister(struct rtnl_link_ops *ops) |
| 308 | { |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 309 | struct net *net; |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 310 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 311 | for_each_net(net) { |
Pavel Emelyanov | 669f87b | 2008-04-16 00:46:52 -0700 | [diff] [blame] | 312 | __rtnl_kill_links(net, ops); |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 313 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 314 | list_del(&ops->list); |
| 315 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 316 | EXPORT_SYMBOL_GPL(__rtnl_link_unregister); |
| 317 | |
| 318 | /** |
| 319 | * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. |
| 320 | * @ops: struct rtnl_link_ops * to unregister |
| 321 | */ |
| 322 | void rtnl_link_unregister(struct rtnl_link_ops *ops) |
| 323 | { |
| 324 | rtnl_lock(); |
| 325 | __rtnl_link_unregister(ops); |
| 326 | rtnl_unlock(); |
| 327 | } |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 328 | EXPORT_SYMBOL_GPL(rtnl_link_unregister); |
| 329 | |
| 330 | static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) |
| 331 | { |
| 332 | const struct rtnl_link_ops *ops; |
| 333 | |
| 334 | list_for_each_entry(ops, &link_ops, list) { |
| 335 | if (!strcmp(ops->kind, kind)) |
| 336 | return ops; |
| 337 | } |
| 338 | return NULL; |
| 339 | } |
| 340 | |
| 341 | static size_t rtnl_link_get_size(const struct net_device *dev) |
| 342 | { |
| 343 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; |
| 344 | size_t size; |
| 345 | |
| 346 | if (!ops) |
| 347 | return 0; |
| 348 | |
| 349 | size = nlmsg_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ |
| 350 | nlmsg_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ |
| 351 | |
| 352 | if (ops->get_size) |
| 353 | /* IFLA_INFO_DATA + nested data */ |
| 354 | size += nlmsg_total_size(sizeof(struct nlattr)) + |
| 355 | ops->get_size(dev); |
| 356 | |
| 357 | if (ops->get_xstats_size) |
| 358 | size += ops->get_xstats_size(dev); /* IFLA_INFO_XSTATS */ |
| 359 | |
| 360 | return size; |
| 361 | } |
| 362 | |
| 363 | static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) |
| 364 | { |
| 365 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; |
| 366 | struct nlattr *linkinfo, *data; |
| 367 | int err = -EMSGSIZE; |
| 368 | |
| 369 | linkinfo = nla_nest_start(skb, IFLA_LINKINFO); |
| 370 | if (linkinfo == NULL) |
| 371 | goto out; |
| 372 | |
| 373 | if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) |
| 374 | goto err_cancel_link; |
| 375 | if (ops->fill_xstats) { |
| 376 | err = ops->fill_xstats(skb, dev); |
| 377 | if (err < 0) |
| 378 | goto err_cancel_link; |
| 379 | } |
| 380 | if (ops->fill_info) { |
| 381 | data = nla_nest_start(skb, IFLA_INFO_DATA); |
| 382 | if (data == NULL) |
| 383 | goto err_cancel_link; |
| 384 | err = ops->fill_info(skb, dev); |
| 385 | if (err < 0) |
| 386 | goto err_cancel_data; |
| 387 | nla_nest_end(skb, data); |
| 388 | } |
| 389 | |
| 390 | nla_nest_end(skb, linkinfo); |
| 391 | return 0; |
| 392 | |
| 393 | err_cancel_data: |
| 394 | nla_nest_cancel(skb, data); |
| 395 | err_cancel_link: |
| 396 | nla_nest_cancel(skb, linkinfo); |
| 397 | out: |
| 398 | return err; |
| 399 | } |
| 400 | |
Thomas Graf | db46edc | 2005-05-03 14:29:39 -0700 | [diff] [blame] | 401 | static const int rtm_min[RTM_NR_FAMILIES] = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | { |
Thomas Graf | f90a0a7 | 2005-05-03 14:29:00 -0700 | [diff] [blame] | 403 | [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)), |
| 404 | [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)), |
| 405 | [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)), |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 406 | [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)), |
Thomas Graf | f90a0a7 | 2005-05-03 14:29:00 -0700 | [diff] [blame] | 407 | [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)), |
| 408 | [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)), |
| 409 | [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)), |
| 410 | [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)), |
Thomas Graf | f90a0a7 | 2005-05-03 14:29:00 -0700 | [diff] [blame] | 411 | [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), |
| 412 | [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | }; |
| 414 | |
Thomas Graf | db46edc | 2005-05-03 14:29:39 -0700 | [diff] [blame] | 415 | static const int rta_max[RTM_NR_FAMILIES] = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | { |
Thomas Graf | f90a0a7 | 2005-05-03 14:29:00 -0700 | [diff] [blame] | 417 | [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX, |
| 418 | [RTM_FAM(RTM_NEWADDR)] = IFA_MAX, |
| 419 | [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX, |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 420 | [RTM_FAM(RTM_NEWRULE)] = FRA_MAX, |
Thomas Graf | f90a0a7 | 2005-05-03 14:29:00 -0700 | [diff] [blame] | 421 | [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX, |
| 422 | [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX, |
| 423 | [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX, |
| 424 | [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | }; |
| 426 | |
| 427 | void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data) |
| 428 | { |
| 429 | struct rtattr *rta; |
| 430 | int size = RTA_LENGTH(attrlen); |
| 431 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 432 | rta = (struct rtattr *)skb_put(skb, RTA_ALIGN(size)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | rta->rta_type = attrtype; |
| 434 | rta->rta_len = size; |
| 435 | memcpy(RTA_DATA(rta), data, attrlen); |
Patrick McHardy | b3563c4 | 2005-06-28 12:54:43 -0700 | [diff] [blame] | 436 | memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 438 | EXPORT_SYMBOL(__rta_fill); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 440 | 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] | 441 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 442 | struct sock *rtnl = net->rtnl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | int err = 0; |
| 444 | |
Patrick McHardy | ac6d439 | 2005-08-14 19:29:52 -0700 | [diff] [blame] | 445 | NETLINK_CB(skb).dst_group = group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | if (echo) |
| 447 | atomic_inc(&skb->users); |
| 448 | netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); |
| 449 | if (echo) |
| 450 | err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); |
| 451 | return err; |
| 452 | } |
| 453 | |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 454 | int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) |
Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 455 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 456 | struct sock *rtnl = net->rtnl; |
| 457 | |
Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 458 | return nlmsg_unicast(rtnl, skb, pid); |
| 459 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 460 | EXPORT_SYMBOL(rtnl_unicast); |
Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 461 | |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 462 | void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, |
| 463 | struct nlmsghdr *nlh, gfp_t flags) |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 464 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 465 | struct sock *rtnl = net->rtnl; |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 466 | int report = 0; |
| 467 | |
| 468 | if (nlh) |
| 469 | report = nlmsg_report(nlh); |
| 470 | |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 471 | nlmsg_notify(rtnl, skb, pid, group, report, flags); |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 472 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 473 | EXPORT_SYMBOL(rtnl_notify); |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 474 | |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 475 | void rtnl_set_sk_err(struct net *net, u32 group, int error) |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 476 | { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 477 | struct sock *rtnl = net->rtnl; |
| 478 | |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 479 | netlink_set_err(rtnl, 0, group, error); |
| 480 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 481 | EXPORT_SYMBOL(rtnl_set_sk_err); |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) |
| 484 | { |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 485 | struct nlattr *mx; |
| 486 | int i, valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 488 | mx = nla_nest_start(skb, RTA_METRICS); |
| 489 | if (mx == NULL) |
| 490 | return -ENOBUFS; |
| 491 | |
| 492 | for (i = 0; i < RTAX_MAX; i++) { |
| 493 | if (metrics[i]) { |
| 494 | valid++; |
| 495 | NLA_PUT_U32(skb, i+1, metrics[i]); |
| 496 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | |
David S. Miller | a57d27f | 2006-08-22 22:20:14 -0700 | [diff] [blame] | 499 | if (!valid) { |
| 500 | nla_nest_cancel(skb, mx); |
| 501 | return 0; |
| 502 | } |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 503 | |
| 504 | return nla_nest_end(skb, mx); |
| 505 | |
| 506 | nla_put_failure: |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 507 | nla_nest_cancel(skb, mx); |
| 508 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 510 | EXPORT_SYMBOL(rtnetlink_put_metrics); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 512 | int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, |
| 513 | u32 ts, u32 tsage, long expires, u32 error) |
| 514 | { |
| 515 | struct rta_cacheinfo ci = { |
| 516 | .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse), |
| 517 | .rta_used = dst->__use, |
| 518 | .rta_clntref = atomic_read(&(dst->__refcnt)), |
| 519 | .rta_error = error, |
| 520 | .rta_id = id, |
| 521 | .rta_ts = ts, |
| 522 | .rta_tsage = tsage, |
| 523 | }; |
| 524 | |
| 525 | if (expires) |
| 526 | ci.rta_expires = jiffies_to_clock_t(expires); |
| 527 | |
| 528 | return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); |
| 529 | } |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 530 | EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 532 | static void set_operstate(struct net_device *dev, unsigned char transition) |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 533 | { |
| 534 | unsigned char operstate = dev->operstate; |
| 535 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 536 | switch (transition) { |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 537 | case IF_OPER_UP: |
| 538 | if ((operstate == IF_OPER_DORMANT || |
| 539 | operstate == IF_OPER_UNKNOWN) && |
| 540 | !netif_dormant(dev)) |
| 541 | operstate = IF_OPER_UP; |
| 542 | break; |
| 543 | |
| 544 | case IF_OPER_DORMANT: |
| 545 | if (operstate == IF_OPER_UP || |
| 546 | operstate == IF_OPER_UNKNOWN) |
| 547 | operstate = IF_OPER_DORMANT; |
| 548 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 549 | } |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 550 | |
| 551 | if (dev->operstate != operstate) { |
| 552 | write_lock_bh(&dev_base_lock); |
| 553 | dev->operstate = operstate; |
| 554 | write_unlock_bh(&dev_base_lock); |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 555 | netdev_state_change(dev); |
| 556 | } |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 557 | } |
| 558 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 559 | static void copy_rtnl_link_stats(struct rtnl_link_stats *a, |
Stephen Hemminger | eeda3fd | 2008-11-19 21:40:23 -0800 | [diff] [blame] | 560 | const struct net_device_stats *b) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | { |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 562 | a->rx_packets = b->rx_packets; |
| 563 | a->tx_packets = b->tx_packets; |
| 564 | a->rx_bytes = b->rx_bytes; |
| 565 | a->tx_bytes = b->tx_bytes; |
| 566 | a->rx_errors = b->rx_errors; |
| 567 | a->tx_errors = b->tx_errors; |
| 568 | a->rx_dropped = b->rx_dropped; |
| 569 | a->tx_dropped = b->tx_dropped; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 571 | a->multicast = b->multicast; |
| 572 | a->collisions = b->collisions; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 574 | a->rx_length_errors = b->rx_length_errors; |
| 575 | a->rx_over_errors = b->rx_over_errors; |
| 576 | a->rx_crc_errors = b->rx_crc_errors; |
| 577 | a->rx_frame_errors = b->rx_frame_errors; |
| 578 | a->rx_fifo_errors = b->rx_fifo_errors; |
| 579 | a->rx_missed_errors = b->rx_missed_errors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 581 | a->tx_aborted_errors = b->tx_aborted_errors; |
| 582 | a->tx_carrier_errors = b->tx_carrier_errors; |
| 583 | a->tx_fifo_errors = b->tx_fifo_errors; |
| 584 | a->tx_heartbeat_errors = b->tx_heartbeat_errors; |
| 585 | a->tx_window_errors = b->tx_window_errors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 587 | a->rx_compressed = b->rx_compressed; |
| 588 | a->tx_compressed = b->tx_compressed; |
| 589 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 591 | static inline size_t if_nlmsg_size(const struct net_device *dev) |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 592 | { |
| 593 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) |
| 594 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 595 | + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 596 | + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ |
| 597 | + nla_total_size(sizeof(struct rtnl_link_ifmap)) |
| 598 | + nla_total_size(sizeof(struct rtnl_link_stats)) |
| 599 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ |
| 600 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ |
| 601 | + nla_total_size(4) /* IFLA_TXQLEN */ |
| 602 | + nla_total_size(4) /* IFLA_WEIGHT */ |
| 603 | + nla_total_size(4) /* IFLA_MTU */ |
| 604 | + nla_total_size(4) /* IFLA_LINK */ |
| 605 | + nla_total_size(4) /* IFLA_MASTER */ |
| 606 | + nla_total_size(1) /* IFLA_OPERSTATE */ |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 607 | + nla_total_size(1) /* IFLA_LINKMODE */ |
| 608 | + rtnl_link_get_size(dev); /* IFLA_LINKINFO */ |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 609 | } |
| 610 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 611 | 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] | 612 | int type, u32 pid, u32 seq, u32 change, |
| 613 | unsigned int flags) |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 614 | { |
| 615 | struct ifinfomsg *ifm; |
| 616 | struct nlmsghdr *nlh; |
Stephen Hemminger | eeda3fd | 2008-11-19 21:40:23 -0800 | [diff] [blame] | 617 | const struct net_device_stats *stats; |
Pavel Emelyanov | 96e7408 | 2008-05-21 14:12:46 -0700 | [diff] [blame] | 618 | struct nlattr *attr; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 619 | |
| 620 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); |
| 621 | if (nlh == NULL) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 622 | return -EMSGSIZE; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 623 | |
| 624 | ifm = nlmsg_data(nlh); |
| 625 | ifm->ifi_family = AF_UNSPEC; |
| 626 | ifm->__ifi_pad = 0; |
| 627 | ifm->ifi_type = dev->type; |
| 628 | ifm->ifi_index = dev->ifindex; |
| 629 | ifm->ifi_flags = dev_get_flags(dev); |
| 630 | ifm->ifi_change = change; |
| 631 | |
| 632 | NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name); |
| 633 | NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 634 | NLA_PUT_U8(skb, IFLA_OPERSTATE, |
| 635 | netif_running(dev) ? dev->operstate : IF_OPER_DOWN); |
| 636 | NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode); |
| 637 | NLA_PUT_U32(skb, IFLA_MTU, dev->mtu); |
| 638 | |
| 639 | if (dev->ifindex != dev->iflink) |
| 640 | NLA_PUT_U32(skb, IFLA_LINK, dev->iflink); |
| 641 | |
| 642 | if (dev->master) |
| 643 | NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex); |
| 644 | |
Patrick McHardy | af356af | 2009-09-04 06:41:18 +0000 | [diff] [blame] | 645 | if (dev->qdisc) |
| 646 | NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc->ops->id); |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 647 | |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 648 | if (dev->ifalias) |
| 649 | NLA_PUT_STRING(skb, IFLA_IFALIAS, dev->ifalias); |
| 650 | |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 651 | if (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | struct rtnl_link_ifmap map = { |
| 653 | .mem_start = dev->mem_start, |
| 654 | .mem_end = dev->mem_end, |
| 655 | .base_addr = dev->base_addr, |
| 656 | .irq = dev->irq, |
| 657 | .dma = dev->dma, |
| 658 | .port = dev->if_port, |
| 659 | }; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 660 | NLA_PUT(skb, IFLA_MAP, sizeof(map), &map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | if (dev->addr_len) { |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 664 | NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr); |
| 665 | NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Pavel Emelyanov | 96e7408 | 2008-05-21 14:12:46 -0700 | [diff] [blame] | 668 | attr = nla_reserve(skb, IFLA_STATS, |
| 669 | sizeof(struct rtnl_link_stats)); |
| 670 | if (attr == NULL) |
| 671 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | |
Stephen Hemminger | eeda3fd | 2008-11-19 21:40:23 -0800 | [diff] [blame] | 673 | stats = dev_get_stats(dev); |
Pavel Emelyanov | 96e7408 | 2008-05-21 14:12:46 -0700 | [diff] [blame] | 674 | copy_rtnl_link_stats(nla_data(attr), stats); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 676 | if (dev->rtnl_link_ops) { |
| 677 | if (rtnl_link_fill(skb, dev) < 0) |
| 678 | goto nla_put_failure; |
| 679 | } |
| 680 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 681 | return nlmsg_end(skb, nlh); |
| 682 | |
| 683 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 684 | nlmsg_cancel(skb, nlh); |
| 685 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 688 | 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] | 689 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 690 | struct net *net = sock_net(skb->sk); |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 691 | int h, s_h; |
| 692 | int idx = 0, s_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | struct net_device *dev; |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 694 | struct hlist_head *head; |
| 695 | struct hlist_node *node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 697 | s_h = cb->args[0]; |
| 698 | s_idx = cb->args[1]; |
| 699 | |
| 700 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { |
| 701 | idx = 0; |
| 702 | head = &net->dev_index_head[h]; |
| 703 | hlist_for_each_entry(dev, node, head, index_hlist) { |
| 704 | if (idx < s_idx) |
| 705 | goto cont; |
| 706 | if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK, |
| 707 | NETLINK_CB(cb->skb).pid, |
| 708 | cb->nlh->nlmsg_seq, 0, |
| 709 | NLM_F_MULTI) <= 0) |
| 710 | goto out; |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 711 | cont: |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 712 | idx++; |
| 713 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | } |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 715 | out: |
| 716 | cb->args[1] = idx; |
| 717 | cb->args[0] = h; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | |
| 719 | return skb->len; |
| 720 | } |
| 721 | |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 722 | const struct nla_policy ifla_policy[IFLA_MAX+1] = { |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 723 | [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 724 | [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, |
| 725 | [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 726 | [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 727 | [IFLA_MTU] = { .type = NLA_U32 }, |
Thomas Graf | 76e8730 | 2008-02-19 16:12:08 -0800 | [diff] [blame] | 728 | [IFLA_LINK] = { .type = NLA_U32 }, |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 729 | [IFLA_TXQLEN] = { .type = NLA_U32 }, |
| 730 | [IFLA_WEIGHT] = { .type = NLA_U32 }, |
| 731 | [IFLA_OPERSTATE] = { .type = NLA_U8 }, |
| 732 | [IFLA_LINKMODE] = { .type = NLA_U8 }, |
Thomas Graf | 76e8730 | 2008-02-19 16:12:08 -0800 | [diff] [blame] | 733 | [IFLA_LINKINFO] = { .type = NLA_NESTED }, |
Eric W. Biederman | d8a5ec6 | 2007-09-12 13:57:04 +0200 | [diff] [blame] | 734 | [IFLA_NET_NS_PID] = { .type = NLA_U32 }, |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 735 | [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 }, |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 736 | }; |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 737 | EXPORT_SYMBOL(ifla_policy); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 738 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 739 | static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { |
| 740 | [IFLA_INFO_KIND] = { .type = NLA_STRING }, |
| 741 | [IFLA_INFO_DATA] = { .type = NLA_NESTED }, |
| 742 | }; |
| 743 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 744 | struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) |
| 745 | { |
| 746 | struct net *net; |
| 747 | /* Examine the link attributes and figure out which |
| 748 | * network namespace we are talking about. |
| 749 | */ |
| 750 | if (tb[IFLA_NET_NS_PID]) |
| 751 | net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); |
| 752 | else |
| 753 | net = get_net(src_net); |
| 754 | return net; |
| 755 | } |
| 756 | EXPORT_SYMBOL(rtnl_link_get_net); |
| 757 | |
Thomas Graf | 1840bb1 | 2008-02-23 19:54:36 -0800 | [diff] [blame] | 758 | static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) |
| 759 | { |
| 760 | if (dev) { |
| 761 | if (tb[IFLA_ADDRESS] && |
| 762 | nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) |
| 763 | return -EINVAL; |
| 764 | |
| 765 | if (tb[IFLA_BROADCAST] && |
| 766 | nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) |
| 767 | return -EINVAL; |
| 768 | } |
| 769 | |
| 770 | return 0; |
| 771 | } |
| 772 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 773 | static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm, |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 774 | struct nlattr **tb, char *ifname, int modified) |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 775 | { |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 776 | const struct net_device_ops *ops = dev->netdev_ops; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 777 | int send_addr_notify = 0; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 778 | int err; |
| 779 | |
Eric W. Biederman | d8a5ec6 | 2007-09-12 13:57:04 +0200 | [diff] [blame] | 780 | if (tb[IFLA_NET_NS_PID]) { |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 781 | struct net *net = rtnl_link_get_net(dev_net(dev), tb); |
Eric W. Biederman | d8a5ec6 | 2007-09-12 13:57:04 +0200 | [diff] [blame] | 782 | if (IS_ERR(net)) { |
| 783 | err = PTR_ERR(net); |
| 784 | goto errout; |
| 785 | } |
| 786 | err = dev_change_net_namespace(dev, net, ifname); |
| 787 | put_net(net); |
| 788 | if (err) |
| 789 | goto errout; |
| 790 | modified = 1; |
| 791 | } |
| 792 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 793 | if (tb[IFLA_MAP]) { |
| 794 | struct rtnl_link_ifmap *u_map; |
| 795 | struct ifmap k_map; |
| 796 | |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 797 | if (!ops->ndo_set_config) { |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 798 | err = -EOPNOTSUPP; |
| 799 | goto errout; |
| 800 | } |
| 801 | |
| 802 | if (!netif_device_present(dev)) { |
| 803 | err = -ENODEV; |
| 804 | goto errout; |
| 805 | } |
| 806 | |
| 807 | u_map = nla_data(tb[IFLA_MAP]); |
| 808 | k_map.mem_start = (unsigned long) u_map->mem_start; |
| 809 | k_map.mem_end = (unsigned long) u_map->mem_end; |
| 810 | k_map.base_addr = (unsigned short) u_map->base_addr; |
| 811 | k_map.irq = (unsigned char) u_map->irq; |
| 812 | k_map.dma = (unsigned char) u_map->dma; |
| 813 | k_map.port = (unsigned char) u_map->port; |
| 814 | |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 815 | err = ops->ndo_set_config(dev, &k_map); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 816 | if (err < 0) |
| 817 | goto errout; |
| 818 | |
| 819 | modified = 1; |
| 820 | } |
| 821 | |
| 822 | if (tb[IFLA_ADDRESS]) { |
| 823 | struct sockaddr *sa; |
| 824 | int len; |
| 825 | |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 826 | if (!ops->ndo_set_mac_address) { |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 827 | err = -EOPNOTSUPP; |
| 828 | goto errout; |
| 829 | } |
| 830 | |
| 831 | if (!netif_device_present(dev)) { |
| 832 | err = -ENODEV; |
| 833 | goto errout; |
| 834 | } |
| 835 | |
| 836 | len = sizeof(sa_family_t) + dev->addr_len; |
| 837 | sa = kmalloc(len, GFP_KERNEL); |
| 838 | if (!sa) { |
| 839 | err = -ENOMEM; |
| 840 | goto errout; |
| 841 | } |
| 842 | sa->sa_family = dev->type; |
| 843 | memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), |
| 844 | dev->addr_len); |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 845 | err = ops->ndo_set_mac_address(dev, sa); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 846 | kfree(sa); |
| 847 | if (err) |
| 848 | goto errout; |
| 849 | send_addr_notify = 1; |
| 850 | modified = 1; |
| 851 | } |
| 852 | |
| 853 | if (tb[IFLA_MTU]) { |
| 854 | err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); |
| 855 | if (err < 0) |
| 856 | goto errout; |
| 857 | modified = 1; |
| 858 | } |
| 859 | |
| 860 | /* |
| 861 | * Interface selected by interface index but interface |
| 862 | * name provided implies that a name change has been |
| 863 | * requested. |
| 864 | */ |
| 865 | if (ifm->ifi_index > 0 && ifname[0]) { |
| 866 | err = dev_change_name(dev, ifname); |
| 867 | if (err < 0) |
| 868 | goto errout; |
| 869 | modified = 1; |
| 870 | } |
| 871 | |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 872 | if (tb[IFLA_IFALIAS]) { |
| 873 | err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), |
| 874 | nla_len(tb[IFLA_IFALIAS])); |
| 875 | if (err < 0) |
| 876 | goto errout; |
| 877 | modified = 1; |
| 878 | } |
| 879 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 880 | if (tb[IFLA_BROADCAST]) { |
| 881 | nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); |
| 882 | send_addr_notify = 1; |
| 883 | } |
| 884 | |
| 885 | if (ifm->ifi_flags || ifm->ifi_change) { |
| 886 | unsigned int flags = ifm->ifi_flags; |
| 887 | |
| 888 | /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ |
| 889 | if (ifm->ifi_change) |
| 890 | flags = (flags & ifm->ifi_change) | |
| 891 | (dev->flags & ~ifm->ifi_change); |
Johannes Berg | 5f9021c | 2008-11-16 23:20:31 -0800 | [diff] [blame] | 892 | err = dev_change_flags(dev, flags); |
| 893 | if (err < 0) |
| 894 | goto errout; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 895 | } |
| 896 | |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 897 | if (tb[IFLA_TXQLEN]) |
| 898 | dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 899 | |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 900 | if (tb[IFLA_OPERSTATE]) |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 901 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 902 | |
| 903 | if (tb[IFLA_LINKMODE]) { |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 904 | write_lock_bh(&dev_base_lock); |
| 905 | dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); |
| 906 | write_unlock_bh(&dev_base_lock); |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | err = 0; |
| 910 | |
| 911 | errout: |
| 912 | if (err < 0 && modified && net_ratelimit()) |
| 913 | printk(KERN_WARNING "A link change request failed with " |
| 914 | "some changes comitted already. Interface %s may " |
| 915 | "have been left with an inconsistent configuration, " |
| 916 | "please check.\n", dev->name); |
| 917 | |
| 918 | if (send_addr_notify) |
| 919 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); |
| 920 | return err; |
| 921 | } |
| 922 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 923 | 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] | 924 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 925 | struct net *net = sock_net(skb->sk); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 926 | struct ifinfomsg *ifm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | struct net_device *dev; |
Patrick McHardy | 0157f60 | 2007-06-13 12:03:36 -0700 | [diff] [blame] | 928 | int err; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 929 | struct nlattr *tb[IFLA_MAX+1]; |
| 930 | char ifname[IFNAMSIZ]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 932 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 933 | if (err < 0) |
| 934 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 936 | if (tb[IFLA_IFNAME]) |
| 937 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
Patrick McHardy | 78e5b891 | 2006-09-13 20:35:36 -0700 | [diff] [blame] | 938 | else |
| 939 | ifname[0] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | err = -EINVAL; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 942 | ifm = nlmsg_data(nlh); |
Patrick McHardy | 51055be | 2007-06-05 12:40:01 -0700 | [diff] [blame] | 943 | if (ifm->ifi_index > 0) |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 944 | dev = __dev_get_by_index(net, ifm->ifi_index); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 945 | else if (tb[IFLA_IFNAME]) |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 946 | dev = __dev_get_by_name(net, ifname); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 947 | else |
| 948 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 950 | if (dev == NULL) { |
| 951 | err = -ENODEV; |
| 952 | goto errout; |
| 953 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 955 | err = validate_linkmsg(dev, tb); |
| 956 | if (err < 0) |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 957 | goto errout; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 958 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 959 | err = do_setlink(dev, ifm, tb, ifname, 0); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 960 | errout: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | return err; |
| 962 | } |
| 963 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 964 | static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) |
| 965 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 966 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 967 | const struct rtnl_link_ops *ops; |
| 968 | struct net_device *dev; |
| 969 | struct ifinfomsg *ifm; |
| 970 | char ifname[IFNAMSIZ]; |
| 971 | struct nlattr *tb[IFLA_MAX+1]; |
| 972 | int err; |
| 973 | |
| 974 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 975 | if (err < 0) |
| 976 | return err; |
| 977 | |
| 978 | if (tb[IFLA_IFNAME]) |
| 979 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
| 980 | |
| 981 | ifm = nlmsg_data(nlh); |
| 982 | if (ifm->ifi_index > 0) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 983 | dev = __dev_get_by_index(net, ifm->ifi_index); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 984 | else if (tb[IFLA_IFNAME]) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 985 | dev = __dev_get_by_name(net, ifname); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 986 | else |
| 987 | return -EINVAL; |
| 988 | |
| 989 | if (!dev) |
| 990 | return -ENODEV; |
| 991 | |
| 992 | ops = dev->rtnl_link_ops; |
| 993 | if (!ops) |
| 994 | return -EOPNOTSUPP; |
| 995 | |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 996 | ops->dellink(dev, NULL); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 997 | return 0; |
| 998 | } |
| 999 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1000 | struct net_device *rtnl_create_link(struct net *src_net, struct net *net, |
| 1001 | char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[]) |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1002 | { |
| 1003 | int err; |
| 1004 | struct net_device *dev; |
Eric Dumazet | 2e59af3 | 2009-09-02 18:03:00 -0700 | [diff] [blame] | 1005 | unsigned int num_queues = 1; |
| 1006 | unsigned int real_num_queues = 1; |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1007 | |
Eric Dumazet | 2e59af3 | 2009-09-02 18:03:00 -0700 | [diff] [blame] | 1008 | if (ops->get_tx_queues) { |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1009 | err = ops->get_tx_queues(src_net, tb, &num_queues, |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 1010 | &real_num_queues); |
Eric Dumazet | 2e59af3 | 2009-09-02 18:03:00 -0700 | [diff] [blame] | 1011 | if (err) |
| 1012 | goto err; |
| 1013 | } |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1014 | err = -ENOMEM; |
Eric Dumazet | 2e59af3 | 2009-09-02 18:03:00 -0700 | [diff] [blame] | 1015 | dev = alloc_netdev_mq(ops->priv_size, ifname, ops->setup, num_queues); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1016 | if (!dev) |
| 1017 | goto err; |
| 1018 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1019 | dev_net_set(dev, net); |
| 1020 | dev->rtnl_link_ops = ops; |
Eric Dumazet | 2e59af3 | 2009-09-02 18:03:00 -0700 | [diff] [blame] | 1021 | dev->real_num_tx_queues = real_num_queues; |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1022 | |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1023 | if (strchr(dev->name, '%')) { |
| 1024 | err = dev_alloc_name(dev, dev->name); |
| 1025 | if (err < 0) |
| 1026 | goto err_free; |
| 1027 | } |
| 1028 | |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1029 | if (tb[IFLA_MTU]) |
| 1030 | dev->mtu = nla_get_u32(tb[IFLA_MTU]); |
| 1031 | if (tb[IFLA_ADDRESS]) |
| 1032 | memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), |
| 1033 | nla_len(tb[IFLA_ADDRESS])); |
| 1034 | if (tb[IFLA_BROADCAST]) |
| 1035 | memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), |
| 1036 | nla_len(tb[IFLA_BROADCAST])); |
| 1037 | if (tb[IFLA_TXQLEN]) |
| 1038 | dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); |
| 1039 | if (tb[IFLA_OPERSTATE]) |
David S. Miller | 93b2d4a | 2008-02-17 18:35:07 -0800 | [diff] [blame] | 1040 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1041 | if (tb[IFLA_LINKMODE]) |
| 1042 | dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); |
| 1043 | |
| 1044 | return dev; |
| 1045 | |
| 1046 | err_free: |
| 1047 | free_netdev(dev); |
| 1048 | err: |
| 1049 | return ERR_PTR(err); |
| 1050 | } |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 1051 | EXPORT_SYMBOL(rtnl_create_link); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1052 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1053 | static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) |
| 1054 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1055 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1056 | const struct rtnl_link_ops *ops; |
| 1057 | struct net_device *dev; |
| 1058 | struct ifinfomsg *ifm; |
| 1059 | char kind[MODULE_NAME_LEN]; |
| 1060 | char ifname[IFNAMSIZ]; |
| 1061 | struct nlattr *tb[IFLA_MAX+1]; |
| 1062 | struct nlattr *linkinfo[IFLA_INFO_MAX+1]; |
| 1063 | int err; |
| 1064 | |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 1065 | #ifdef CONFIG_MODULES |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1066 | replay: |
Thomas Graf | 8072f08 | 2007-07-31 14:13:50 -0700 | [diff] [blame] | 1067 | #endif |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1068 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 1069 | if (err < 0) |
| 1070 | return err; |
| 1071 | |
| 1072 | if (tb[IFLA_IFNAME]) |
| 1073 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
| 1074 | else |
| 1075 | ifname[0] = '\0'; |
| 1076 | |
| 1077 | ifm = nlmsg_data(nlh); |
| 1078 | if (ifm->ifi_index > 0) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1079 | dev = __dev_get_by_index(net, ifm->ifi_index); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1080 | else if (ifname[0]) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1081 | dev = __dev_get_by_name(net, ifname); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1082 | else |
| 1083 | dev = NULL; |
| 1084 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 1085 | err = validate_linkmsg(dev, tb); |
| 1086 | if (err < 0) |
Thomas Graf | 1840bb1 | 2008-02-23 19:54:36 -0800 | [diff] [blame] | 1087 | return err; |
| 1088 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1089 | if (tb[IFLA_LINKINFO]) { |
| 1090 | err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, |
| 1091 | tb[IFLA_LINKINFO], ifla_info_policy); |
| 1092 | if (err < 0) |
| 1093 | return err; |
| 1094 | } else |
| 1095 | memset(linkinfo, 0, sizeof(linkinfo)); |
| 1096 | |
| 1097 | if (linkinfo[IFLA_INFO_KIND]) { |
| 1098 | nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); |
| 1099 | ops = rtnl_link_ops_get(kind); |
| 1100 | } else { |
| 1101 | kind[0] = '\0'; |
| 1102 | ops = NULL; |
| 1103 | } |
| 1104 | |
| 1105 | if (1) { |
| 1106 | struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL; |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1107 | struct net *dest_net; |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1108 | |
| 1109 | if (ops) { |
| 1110 | if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { |
| 1111 | err = nla_parse_nested(attr, ops->maxtype, |
| 1112 | linkinfo[IFLA_INFO_DATA], |
| 1113 | ops->policy); |
| 1114 | if (err < 0) |
| 1115 | return err; |
| 1116 | data = attr; |
| 1117 | } |
| 1118 | if (ops->validate) { |
| 1119 | err = ops->validate(tb, data); |
| 1120 | if (err < 0) |
| 1121 | return err; |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | if (dev) { |
| 1126 | int modified = 0; |
| 1127 | |
| 1128 | if (nlh->nlmsg_flags & NLM_F_EXCL) |
| 1129 | return -EEXIST; |
| 1130 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
| 1131 | return -EOPNOTSUPP; |
| 1132 | |
| 1133 | if (linkinfo[IFLA_INFO_DATA]) { |
| 1134 | if (!ops || ops != dev->rtnl_link_ops || |
| 1135 | !ops->changelink) |
| 1136 | return -EOPNOTSUPP; |
| 1137 | |
| 1138 | err = ops->changelink(dev, tb, data); |
| 1139 | if (err < 0) |
| 1140 | return err; |
| 1141 | modified = 1; |
| 1142 | } |
| 1143 | |
| 1144 | return do_setlink(dev, ifm, tb, ifname, modified); |
| 1145 | } |
| 1146 | |
| 1147 | if (!(nlh->nlmsg_flags & NLM_F_CREATE)) |
| 1148 | return -ENODEV; |
| 1149 | |
| 1150 | if (ifm->ifi_index || ifm->ifi_flags || ifm->ifi_change) |
| 1151 | return -EOPNOTSUPP; |
Patrick McHardy | 0e06877 | 2007-07-11 19:42:31 -0700 | [diff] [blame] | 1152 | if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO]) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1153 | return -EOPNOTSUPP; |
| 1154 | |
| 1155 | if (!ops) { |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 1156 | #ifdef CONFIG_MODULES |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1157 | if (kind[0]) { |
| 1158 | __rtnl_unlock(); |
| 1159 | request_module("rtnl-link-%s", kind); |
| 1160 | rtnl_lock(); |
| 1161 | ops = rtnl_link_ops_get(kind); |
| 1162 | if (ops) |
| 1163 | goto replay; |
| 1164 | } |
| 1165 | #endif |
| 1166 | return -EOPNOTSUPP; |
| 1167 | } |
| 1168 | |
| 1169 | if (!ifname[0]) |
| 1170 | snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1171 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1172 | dest_net = rtnl_link_get_net(net, tb); |
| 1173 | dev = rtnl_create_link(net, dest_net, ifname, ops, tb); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1174 | |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1175 | if (IS_ERR(dev)) |
| 1176 | err = PTR_ERR(dev); |
| 1177 | else if (ops->newlink) |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1178 | err = ops->newlink(net, dev, tb, data); |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 1179 | else |
| 1180 | err = register_netdevice(dev); |
Pavel Emelianov | e719928 | 2007-08-08 22:16:38 -0700 | [diff] [blame] | 1181 | if (err < 0 && !IS_ERR(dev)) |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1182 | free_netdev(dev); |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 1183 | |
| 1184 | put_net(dest_net); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1185 | return err; |
| 1186 | } |
| 1187 | } |
| 1188 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1189 | 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] | 1190 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1191 | struct net *net = sock_net(skb->sk); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1192 | struct ifinfomsg *ifm; |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 1193 | char ifname[IFNAMSIZ]; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1194 | struct nlattr *tb[IFLA_MAX+1]; |
| 1195 | struct net_device *dev = NULL; |
| 1196 | struct sk_buff *nskb; |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 1197 | int err; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 1198 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1199 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 1200 | if (err < 0) |
Eric Sesterhenn | 9918f23 | 2006-09-26 23:26:38 -0700 | [diff] [blame] | 1201 | return err; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1202 | |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 1203 | if (tb[IFLA_IFNAME]) |
| 1204 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
| 1205 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1206 | ifm = nlmsg_data(nlh); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 1207 | if (ifm->ifi_index > 0) |
| 1208 | dev = __dev_get_by_index(net, ifm->ifi_index); |
| 1209 | else if (tb[IFLA_IFNAME]) |
| 1210 | dev = __dev_get_by_name(net, ifname); |
| 1211 | else |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 1212 | return -EINVAL; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1213 | |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 1214 | if (dev == NULL) |
| 1215 | return -ENODEV; |
| 1216 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1217 | nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 1218 | if (nskb == NULL) |
| 1219 | return -ENOBUFS; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 1220 | |
Patrick McHardy | 575c3e2 | 2007-05-22 17:00:49 -0700 | [diff] [blame] | 1221 | err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid, |
| 1222 | nlh->nlmsg_seq, 0, 0); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 1223 | if (err < 0) { |
| 1224 | /* -EMSGSIZE implies BUG in if_nlmsg_size */ |
| 1225 | WARN_ON(err == -EMSGSIZE); |
| 1226 | kfree_skb(nskb); |
Eric Dumazet | a3d1289 | 2009-10-21 10:59:31 +0000 | [diff] [blame] | 1227 | } else |
| 1228 | err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 1229 | |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 1230 | return err; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 1231 | } |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 1232 | |
Adrian Bunk | 42bad1d | 2007-04-26 00:57:41 -0700 | [diff] [blame] | 1233 | 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] | 1234 | { |
| 1235 | int idx; |
| 1236 | int s_idx = cb->family; |
| 1237 | |
| 1238 | if (s_idx == 0) |
| 1239 | s_idx = 1; |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 1240 | for (idx = 1; idx < NPROTO; idx++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | int type = cb->nlh->nlmsg_type-RTM_BASE; |
| 1242 | if (idx < s_idx || idx == PF_PACKET) |
| 1243 | continue; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 1244 | if (rtnl_msg_handlers[idx] == NULL || |
| 1245 | rtnl_msg_handlers[idx][type].dumpit == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | continue; |
| 1247 | if (idx > s_idx) |
| 1248 | memset(&cb->args[0], 0, sizeof(cb->args)); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 1249 | if (rtnl_msg_handlers[idx][type].dumpit(skb, cb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | break; |
| 1251 | } |
| 1252 | cb->family = idx; |
| 1253 | |
| 1254 | return skb->len; |
| 1255 | } |
| 1256 | |
| 1257 | void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change) |
| 1258 | { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1259 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | struct sk_buff *skb; |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 1261 | int err = -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1263 | skb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL); |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 1264 | if (skb == NULL) |
| 1265 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | |
Patrick McHardy | 575c3e2 | 2007-05-22 17:00:49 -0700 | [diff] [blame] | 1267 | err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 1268 | if (err < 0) { |
| 1269 | /* -EMSGSIZE implies BUG in if_nlmsg_size() */ |
| 1270 | WARN_ON(err == -EMSGSIZE); |
| 1271 | kfree_skb(skb); |
| 1272 | goto errout; |
| 1273 | } |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 1274 | rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL); |
| 1275 | return; |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 1276 | errout: |
| 1277 | if (err < 0) |
Eric W. Biederman | 4b3da70 | 2007-11-19 22:27:40 -0800 | [diff] [blame] | 1278 | rtnl_set_sk_err(net, RTNLGRP_LINK, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | /* Protected by RTNL sempahore. */ |
| 1282 | static struct rtattr **rta_buf; |
| 1283 | static int rtattr_max; |
| 1284 | |
| 1285 | /* Process one rtnetlink message. */ |
| 1286 | |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 1287 | static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1289 | struct net *net = sock_net(skb->sk); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 1290 | rtnl_doit_func doit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | int sz_idx, kind; |
| 1292 | int min_len; |
| 1293 | int family; |
| 1294 | int type; |
Patrick McHardy | 1c2d670 | 2007-04-16 16:59:10 -0700 | [diff] [blame] | 1295 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | type = nlh->nlmsg_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | if (type > RTM_MAX) |
Thomas Graf | 038890f | 2007-04-05 14:35:52 -0700 | [diff] [blame] | 1299 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | |
| 1301 | type -= RTM_BASE; |
| 1302 | |
| 1303 | /* All the messages must have at least 1 byte length */ |
| 1304 | if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg))) |
| 1305 | return 0; |
| 1306 | |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 1307 | family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family; |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 1308 | if (family >= NPROTO) |
| 1309 | return -EAFNOSUPPORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | sz_idx = type>>2; |
| 1312 | kind = type&3; |
| 1313 | |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 1314 | if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN)) |
| 1315 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | |
| 1317 | if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 1318 | struct sock *rtnl; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 1319 | rtnl_dumpit_func dumpit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 1321 | dumpit = rtnl_get_dumpit(family, type); |
| 1322 | if (dumpit == NULL) |
Thomas Graf | 038890f | 2007-04-05 14:35:52 -0700 | [diff] [blame] | 1323 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | |
Patrick McHardy | 1c2d670 | 2007-04-16 16:59:10 -0700 | [diff] [blame] | 1325 | __rtnl_unlock(); |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 1326 | rtnl = net->rtnl; |
Patrick McHardy | 1c2d670 | 2007-04-16 16:59:10 -0700 | [diff] [blame] | 1327 | err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL); |
| 1328 | rtnl_lock(); |
| 1329 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *))); |
| 1333 | |
| 1334 | min_len = rtm_min[sz_idx]; |
| 1335 | if (nlh->nlmsg_len < min_len) |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 1336 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | |
| 1338 | if (nlh->nlmsg_len > min_len) { |
| 1339 | int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len); |
Eric Dumazet | e0d087a | 2009-11-07 01:26:17 -0800 | [diff] [blame] | 1340 | struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | |
| 1342 | while (RTA_OK(attr, attrlen)) { |
| 1343 | unsigned flavor = attr->rta_type; |
| 1344 | if (flavor) { |
| 1345 | if (flavor > rta_max[sz_idx]) |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 1346 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | rta_buf[flavor-1] = attr; |
| 1348 | } |
| 1349 | attr = RTA_NEXT(attr, attrlen); |
| 1350 | } |
| 1351 | } |
| 1352 | |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 1353 | doit = rtnl_get_doit(family, type); |
| 1354 | if (doit == NULL) |
Thomas Graf | 038890f | 2007-04-05 14:35:52 -0700 | [diff] [blame] | 1355 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 1357 | return doit(skb, nlh, (void *)&rta_buf[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | } |
| 1359 | |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 1360 | static void rtnetlink_rcv(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | { |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 1362 | rtnl_lock(); |
| 1363 | netlink_rcv_skb(skb, &rtnetlink_rcv_msg); |
| 1364 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 1368 | { |
| 1369 | struct net_device *dev = ptr; |
Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 1370 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | switch (event) { |
| 1372 | case NETDEV_UNREGISTER: |
| 1373 | rtmsg_ifinfo(RTM_DELLINK, dev, ~0U); |
| 1374 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | case NETDEV_UP: |
| 1376 | case NETDEV_DOWN: |
| 1377 | rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING); |
| 1378 | break; |
Eric W. Biederman | d90a909 | 2009-12-12 22:11:15 +0000 | [diff] [blame] | 1379 | case NETDEV_POST_INIT: |
| 1380 | case NETDEV_REGISTER: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | case NETDEV_CHANGE: |
| 1382 | case NETDEV_GOING_DOWN: |
Eric W. Biederman | d90a909 | 2009-12-12 22:11:15 +0000 | [diff] [blame] | 1383 | case NETDEV_UNREGISTER_BATCH: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | break; |
| 1385 | default: |
| 1386 | rtmsg_ifinfo(RTM_NEWLINK, dev, 0); |
| 1387 | break; |
| 1388 | } |
| 1389 | return NOTIFY_DONE; |
| 1390 | } |
| 1391 | |
| 1392 | static struct notifier_block rtnetlink_dev_notifier = { |
| 1393 | .notifier_call = rtnetlink_event, |
| 1394 | }; |
| 1395 | |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 1396 | |
| 1397 | static int rtnetlink_net_init(struct net *net) |
| 1398 | { |
| 1399 | struct sock *sk; |
| 1400 | sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX, |
| 1401 | rtnetlink_rcv, &rtnl_mutex, THIS_MODULE); |
| 1402 | if (!sk) |
| 1403 | return -ENOMEM; |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 1404 | net->rtnl = sk; |
| 1405 | return 0; |
| 1406 | } |
| 1407 | |
| 1408 | static void rtnetlink_net_exit(struct net *net) |
| 1409 | { |
Denis V. Lunev | 775516b | 2008-01-18 23:55:19 -0800 | [diff] [blame] | 1410 | netlink_kernel_release(net->rtnl); |
| 1411 | net->rtnl = NULL; |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 1412 | } |
| 1413 | |
| 1414 | static struct pernet_operations rtnetlink_net_ops = { |
| 1415 | .init = rtnetlink_net_init, |
| 1416 | .exit = rtnetlink_net_exit, |
| 1417 | }; |
| 1418 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | void __init rtnetlink_init(void) |
| 1420 | { |
| 1421 | int i; |
| 1422 | |
| 1423 | rtattr_max = 0; |
| 1424 | for (i = 0; i < ARRAY_SIZE(rta_max); i++) |
| 1425 | if (rta_max[i] > rtattr_max) |
| 1426 | rtattr_max = rta_max[i]; |
| 1427 | rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL); |
| 1428 | if (!rta_buf) |
| 1429 | panic("rtnetlink_init: cannot allocate rta_buf\n"); |
| 1430 | |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 1431 | if (register_pernet_subsys(&rtnetlink_net_ops)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | panic("rtnetlink_init: cannot initialize rtnetlink\n"); |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 1433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV); |
| 1435 | register_netdevice_notifier(&rtnetlink_dev_notifier); |
Thomas Graf | 340d17f | 2007-03-22 11:49:22 -0700 | [diff] [blame] | 1436 | |
| 1437 | rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo); |
| 1438 | rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL); |
Patrick McHardy | 38f7b87 | 2007-06-13 12:03:51 -0700 | [diff] [blame] | 1439 | rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL); |
| 1440 | rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL); |
Thomas Graf | 687ad8c | 2007-03-22 11:59:42 -0700 | [diff] [blame] | 1441 | |
| 1442 | rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all); |
| 1443 | rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | } |
| 1445 | |