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