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