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