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 | |
Thomas Graf | db46edc | 2005-05-03 14:29:39 -0700 | [diff] [blame] | 246 | static const int rtm_min[RTM_NR_FAMILIES] = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | { |
Thomas Graf | f90a0a7 | 2005-05-03 14:29:00 -0700 | [diff] [blame] | 248 | [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)), |
| 249 | [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)), |
| 250 | [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)), |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 251 | [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)), |
Thomas Graf | f90a0a7 | 2005-05-03 14:29:00 -0700 | [diff] [blame] | 252 | [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)), |
| 253 | [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)), |
| 254 | [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)), |
| 255 | [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)), |
Thomas Graf | f90a0a7 | 2005-05-03 14:29:00 -0700 | [diff] [blame] | 256 | [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), |
| 257 | [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | }; |
| 259 | |
Thomas Graf | db46edc | 2005-05-03 14:29:39 -0700 | [diff] [blame] | 260 | static const int rta_max[RTM_NR_FAMILIES] = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | { |
Thomas Graf | f90a0a7 | 2005-05-03 14:29:00 -0700 | [diff] [blame] | 262 | [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX, |
| 263 | [RTM_FAM(RTM_NEWADDR)] = IFA_MAX, |
| 264 | [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX, |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 265 | [RTM_FAM(RTM_NEWRULE)] = FRA_MAX, |
Thomas Graf | f90a0a7 | 2005-05-03 14:29:00 -0700 | [diff] [blame] | 266 | [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX, |
| 267 | [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX, |
| 268 | [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX, |
| 269 | [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | }; |
| 271 | |
| 272 | void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data) |
| 273 | { |
| 274 | struct rtattr *rta; |
| 275 | int size = RTA_LENGTH(attrlen); |
| 276 | |
| 277 | rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size)); |
| 278 | rta->rta_type = attrtype; |
| 279 | rta->rta_len = size; |
| 280 | memcpy(RTA_DATA(rta), data, attrlen); |
Patrick McHardy | b3563c4 | 2005-06-28 12:54:43 -0700 | [diff] [blame] | 281 | memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size) |
| 285 | { |
| 286 | size_t ret = RTA_PAYLOAD(rta); |
| 287 | char *src = RTA_DATA(rta); |
| 288 | |
| 289 | if (ret > 0 && src[ret - 1] == '\0') |
| 290 | ret--; |
| 291 | if (size > 0) { |
| 292 | size_t len = (ret >= size) ? size - 1 : ret; |
| 293 | memset(dest, 0, size); |
| 294 | memcpy(dest, src, len); |
| 295 | } |
| 296 | return ret; |
| 297 | } |
| 298 | |
| 299 | int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo) |
| 300 | { |
| 301 | int err = 0; |
| 302 | |
Patrick McHardy | ac6d439 | 2005-08-14 19:29:52 -0700 | [diff] [blame] | 303 | NETLINK_CB(skb).dst_group = group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | if (echo) |
| 305 | atomic_inc(&skb->users); |
| 306 | netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); |
| 307 | if (echo) |
| 308 | err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); |
| 309 | return err; |
| 310 | } |
| 311 | |
Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 312 | int rtnl_unicast(struct sk_buff *skb, u32 pid) |
| 313 | { |
| 314 | return nlmsg_unicast(rtnl, skb, pid); |
| 315 | } |
| 316 | |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 317 | int rtnl_notify(struct sk_buff *skb, u32 pid, u32 group, |
| 318 | struct nlmsghdr *nlh, gfp_t flags) |
| 319 | { |
| 320 | int report = 0; |
| 321 | |
| 322 | if (nlh) |
| 323 | report = nlmsg_report(nlh); |
| 324 | |
| 325 | return nlmsg_notify(rtnl, skb, pid, group, report, flags); |
| 326 | } |
| 327 | |
| 328 | void rtnl_set_sk_err(u32 group, int error) |
| 329 | { |
| 330 | netlink_set_err(rtnl, 0, group, error); |
| 331 | } |
| 332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) |
| 334 | { |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 335 | struct nlattr *mx; |
| 336 | int i, valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 338 | mx = nla_nest_start(skb, RTA_METRICS); |
| 339 | if (mx == NULL) |
| 340 | return -ENOBUFS; |
| 341 | |
| 342 | for (i = 0; i < RTAX_MAX; i++) { |
| 343 | if (metrics[i]) { |
| 344 | valid++; |
| 345 | NLA_PUT_U32(skb, i+1, metrics[i]); |
| 346 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
David S. Miller | a57d27f | 2006-08-22 22:20:14 -0700 | [diff] [blame] | 349 | if (!valid) { |
| 350 | nla_nest_cancel(skb, mx); |
| 351 | return 0; |
| 352 | } |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 353 | |
| 354 | return nla_nest_end(skb, mx); |
| 355 | |
| 356 | nla_put_failure: |
| 357 | return nla_nest_cancel(skb, mx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 360 | int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, |
| 361 | u32 ts, u32 tsage, long expires, u32 error) |
| 362 | { |
| 363 | struct rta_cacheinfo ci = { |
| 364 | .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse), |
| 365 | .rta_used = dst->__use, |
| 366 | .rta_clntref = atomic_read(&(dst->__refcnt)), |
| 367 | .rta_error = error, |
| 368 | .rta_id = id, |
| 369 | .rta_ts = ts, |
| 370 | .rta_tsage = tsage, |
| 371 | }; |
| 372 | |
| 373 | if (expires) |
| 374 | ci.rta_expires = jiffies_to_clock_t(expires); |
| 375 | |
| 376 | return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); |
| 377 | } |
| 378 | |
| 379 | EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 381 | static void set_operstate(struct net_device *dev, unsigned char transition) |
| 382 | { |
| 383 | unsigned char operstate = dev->operstate; |
| 384 | |
| 385 | switch(transition) { |
| 386 | case IF_OPER_UP: |
| 387 | if ((operstate == IF_OPER_DORMANT || |
| 388 | operstate == IF_OPER_UNKNOWN) && |
| 389 | !netif_dormant(dev)) |
| 390 | operstate = IF_OPER_UP; |
| 391 | break; |
| 392 | |
| 393 | case IF_OPER_DORMANT: |
| 394 | if (operstate == IF_OPER_UP || |
| 395 | operstate == IF_OPER_UNKNOWN) |
| 396 | operstate = IF_OPER_DORMANT; |
| 397 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 398 | } |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 399 | |
| 400 | if (dev->operstate != operstate) { |
| 401 | write_lock_bh(&dev_base_lock); |
| 402 | dev->operstate = operstate; |
| 403 | write_unlock_bh(&dev_base_lock); |
| 404 | netdev_state_change(dev); |
| 405 | } |
| 406 | } |
| 407 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 408 | static void copy_rtnl_link_stats(struct rtnl_link_stats *a, |
| 409 | struct net_device_stats *b) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | { |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 411 | a->rx_packets = b->rx_packets; |
| 412 | a->tx_packets = b->tx_packets; |
| 413 | a->rx_bytes = b->rx_bytes; |
| 414 | a->tx_bytes = b->tx_bytes; |
| 415 | a->rx_errors = b->rx_errors; |
| 416 | a->tx_errors = b->tx_errors; |
| 417 | a->rx_dropped = b->rx_dropped; |
| 418 | a->tx_dropped = b->tx_dropped; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 420 | a->multicast = b->multicast; |
| 421 | a->collisions = b->collisions; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 423 | a->rx_length_errors = b->rx_length_errors; |
| 424 | a->rx_over_errors = b->rx_over_errors; |
| 425 | a->rx_crc_errors = b->rx_crc_errors; |
| 426 | a->rx_frame_errors = b->rx_frame_errors; |
| 427 | a->rx_fifo_errors = b->rx_fifo_errors; |
| 428 | a->rx_missed_errors = b->rx_missed_errors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 430 | a->tx_aborted_errors = b->tx_aborted_errors; |
| 431 | a->tx_carrier_errors = b->tx_carrier_errors; |
| 432 | a->tx_fifo_errors = b->tx_fifo_errors; |
| 433 | a->tx_heartbeat_errors = b->tx_heartbeat_errors; |
| 434 | a->tx_window_errors = b->tx_window_errors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 436 | a->rx_compressed = b->rx_compressed; |
| 437 | a->tx_compressed = b->tx_compressed; |
| 438 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 440 | static inline size_t if_nlmsg_size(int iwbuflen) |
| 441 | { |
| 442 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) |
| 443 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ |
| 444 | + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ |
| 445 | + nla_total_size(sizeof(struct rtnl_link_ifmap)) |
| 446 | + nla_total_size(sizeof(struct rtnl_link_stats)) |
| 447 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ |
| 448 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ |
| 449 | + nla_total_size(4) /* IFLA_TXQLEN */ |
| 450 | + nla_total_size(4) /* IFLA_WEIGHT */ |
| 451 | + nla_total_size(4) /* IFLA_MTU */ |
| 452 | + nla_total_size(4) /* IFLA_LINK */ |
| 453 | + nla_total_size(4) /* IFLA_MASTER */ |
| 454 | + nla_total_size(1) /* IFLA_OPERSTATE */ |
| 455 | + nla_total_size(1) /* IFLA_LINKMODE */ |
| 456 | + nla_total_size(iwbuflen); |
| 457 | } |
| 458 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 459 | static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev, |
| 460 | void *iwbuf, int iwbuflen, int type, u32 pid, |
| 461 | u32 seq, u32 change, unsigned int flags) |
| 462 | { |
| 463 | struct ifinfomsg *ifm; |
| 464 | struct nlmsghdr *nlh; |
| 465 | |
| 466 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); |
| 467 | if (nlh == NULL) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 468 | return -EMSGSIZE; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 469 | |
| 470 | ifm = nlmsg_data(nlh); |
| 471 | ifm->ifi_family = AF_UNSPEC; |
| 472 | ifm->__ifi_pad = 0; |
| 473 | ifm->ifi_type = dev->type; |
| 474 | ifm->ifi_index = dev->ifindex; |
| 475 | ifm->ifi_flags = dev_get_flags(dev); |
| 476 | ifm->ifi_change = change; |
| 477 | |
| 478 | NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name); |
| 479 | NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len); |
| 480 | NLA_PUT_U32(skb, IFLA_WEIGHT, dev->weight); |
| 481 | NLA_PUT_U8(skb, IFLA_OPERSTATE, |
| 482 | netif_running(dev) ? dev->operstate : IF_OPER_DOWN); |
| 483 | NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode); |
| 484 | NLA_PUT_U32(skb, IFLA_MTU, dev->mtu); |
| 485 | |
| 486 | if (dev->ifindex != dev->iflink) |
| 487 | NLA_PUT_U32(skb, IFLA_LINK, dev->iflink); |
| 488 | |
| 489 | if (dev->master) |
| 490 | NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex); |
| 491 | |
| 492 | if (dev->qdisc_sleeping) |
| 493 | NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc_sleeping->ops->id); |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 494 | |
| 495 | if (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | struct rtnl_link_ifmap map = { |
| 497 | .mem_start = dev->mem_start, |
| 498 | .mem_end = dev->mem_end, |
| 499 | .base_addr = dev->base_addr, |
| 500 | .irq = dev->irq, |
| 501 | .dma = dev->dma, |
| 502 | .port = dev->if_port, |
| 503 | }; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 504 | NLA_PUT(skb, IFLA_MAP, sizeof(map), &map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | if (dev->addr_len) { |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 508 | NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr); |
| 509 | NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | if (dev->get_stats) { |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 513 | struct net_device_stats *stats = dev->get_stats(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | if (stats) { |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 515 | struct nlattr *attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 517 | attr = nla_reserve(skb, IFLA_STATS, |
| 518 | sizeof(struct rtnl_link_stats)); |
| 519 | if (attr == NULL) |
| 520 | goto nla_put_failure; |
| 521 | |
| 522 | copy_rtnl_link_stats(nla_data(attr), stats); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | } |
| 524 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 526 | if (iwbuf) |
| 527 | NLA_PUT(skb, IFLA_WIRELESS, iwbuflen, iwbuf); |
| 528 | |
| 529 | return nlmsg_end(skb, nlh); |
| 530 | |
| 531 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 532 | nlmsg_cancel(skb, nlh); |
| 533 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 536 | 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] | 537 | { |
| 538 | int idx; |
| 539 | int s_idx = cb->args[0]; |
| 540 | struct net_device *dev; |
| 541 | |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 542 | idx = 0; |
| 543 | for_each_netdev(dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | if (idx < s_idx) |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 545 | goto cont; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 546 | if (rtnl_fill_ifinfo(skb, dev, NULL, 0, RTM_NEWLINK, |
| 547 | NETLINK_CB(cb->skb).pid, |
| 548 | cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | break; |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 550 | cont: |
| 551 | idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | cb->args[0] = idx; |
| 554 | |
| 555 | return skb->len; |
| 556 | } |
| 557 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 558 | static struct nla_policy ifla_policy[IFLA_MAX+1] __read_mostly = { |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 559 | [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, |
| 560 | [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 561 | [IFLA_MTU] = { .type = NLA_U32 }, |
| 562 | [IFLA_TXQLEN] = { .type = NLA_U32 }, |
| 563 | [IFLA_WEIGHT] = { .type = NLA_U32 }, |
| 564 | [IFLA_OPERSTATE] = { .type = NLA_U8 }, |
| 565 | [IFLA_LINKMODE] = { .type = NLA_U8 }, |
| 566 | }; |
| 567 | |
| 568 | 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] | 569 | { |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 570 | struct ifinfomsg *ifm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | struct net_device *dev; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 572 | int err, send_addr_notify = 0, modified = 0; |
| 573 | struct nlattr *tb[IFLA_MAX+1]; |
| 574 | char ifname[IFNAMSIZ]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 576 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 577 | if (err < 0) |
| 578 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 580 | if (tb[IFLA_IFNAME]) |
| 581 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); |
Patrick McHardy | 78e5b891 | 2006-09-13 20:35:36 -0700 | [diff] [blame] | 582 | else |
| 583 | ifname[0] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | err = -EINVAL; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 586 | ifm = nlmsg_data(nlh); |
| 587 | if (ifm->ifi_index >= 0) |
| 588 | dev = dev_get_by_index(ifm->ifi_index); |
| 589 | else if (tb[IFLA_IFNAME]) |
| 590 | dev = dev_get_by_name(ifname); |
| 591 | else |
| 592 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 594 | if (dev == NULL) { |
| 595 | err = -ENODEV; |
| 596 | goto errout; |
| 597 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 599 | if (tb[IFLA_ADDRESS] && |
| 600 | nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) |
| 601 | goto errout_dev; |
| 602 | |
| 603 | if (tb[IFLA_BROADCAST] && |
| 604 | nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) |
| 605 | goto errout_dev; |
| 606 | |
| 607 | if (tb[IFLA_MAP]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | struct rtnl_link_ifmap *u_map; |
| 609 | struct ifmap k_map; |
| 610 | |
| 611 | if (!dev->set_config) { |
| 612 | err = -EOPNOTSUPP; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 613 | goto errout_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | if (!netif_device_present(dev)) { |
| 617 | err = -ENODEV; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 618 | goto errout_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 621 | u_map = nla_data(tb[IFLA_MAP]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | k_map.mem_start = (unsigned long) u_map->mem_start; |
| 623 | k_map.mem_end = (unsigned long) u_map->mem_end; |
| 624 | k_map.base_addr = (unsigned short) u_map->base_addr; |
| 625 | k_map.irq = (unsigned char) u_map->irq; |
| 626 | k_map.dma = (unsigned char) u_map->dma; |
| 627 | k_map.port = (unsigned char) u_map->port; |
| 628 | |
| 629 | err = dev->set_config(dev, &k_map); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 630 | if (err < 0) |
| 631 | goto errout_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 633 | modified = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 636 | if (tb[IFLA_ADDRESS]) { |
David S. Miller | 70f8e78 | 2006-08-08 16:47:37 -0700 | [diff] [blame] | 637 | struct sockaddr *sa; |
| 638 | int len; |
| 639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | if (!dev->set_mac_address) { |
| 641 | err = -EOPNOTSUPP; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 642 | goto errout_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | } |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 644 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | if (!netif_device_present(dev)) { |
| 646 | err = -ENODEV; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 647 | goto errout_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | |
David S. Miller | 70f8e78 | 2006-08-08 16:47:37 -0700 | [diff] [blame] | 650 | len = sizeof(sa_family_t) + dev->addr_len; |
| 651 | sa = kmalloc(len, GFP_KERNEL); |
| 652 | if (!sa) { |
| 653 | err = -ENOMEM; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 654 | goto errout_dev; |
David S. Miller | 70f8e78 | 2006-08-08 16:47:37 -0700 | [diff] [blame] | 655 | } |
| 656 | sa->sa_family = dev->type; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 657 | memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), |
David S. Miller | 70f8e78 | 2006-08-08 16:47:37 -0700 | [diff] [blame] | 658 | dev->addr_len); |
| 659 | err = dev->set_mac_address(dev, sa); |
| 660 | kfree(sa); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | if (err) |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 662 | goto errout_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | send_addr_notify = 1; |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 664 | modified = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 667 | if (tb[IFLA_MTU]) { |
| 668 | err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); |
| 669 | if (err < 0) |
| 670 | goto errout_dev; |
| 671 | modified = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 674 | /* |
| 675 | * Interface selected by interface index but interface |
| 676 | * name provided implies that a name change has been |
| 677 | * requested. |
| 678 | */ |
| 679 | if (ifm->ifi_index >= 0 && ifname[0]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | err = dev_change_name(dev, ifname); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 681 | if (err < 0) |
| 682 | goto errout_dev; |
| 683 | modified = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 686 | if (tb[IFLA_BROADCAST]) { |
| 687 | nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); |
| 688 | send_addr_notify = 1; |
| 689 | } |
| 690 | |
| 691 | |
| 692 | if (ifm->ifi_flags) |
| 693 | dev_change_flags(dev, ifm->ifi_flags); |
| 694 | |
| 695 | if (tb[IFLA_TXQLEN]) |
| 696 | dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); |
| 697 | |
| 698 | if (tb[IFLA_WEIGHT]) |
| 699 | dev->weight = nla_get_u32(tb[IFLA_WEIGHT]); |
| 700 | |
| 701 | if (tb[IFLA_OPERSTATE]) |
| 702 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); |
| 703 | |
| 704 | if (tb[IFLA_LINKMODE]) { |
| 705 | write_lock_bh(&dev_base_lock); |
| 706 | dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); |
| 707 | write_unlock_bh(&dev_base_lock); |
| 708 | } |
| 709 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | err = 0; |
| 711 | |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 712 | errout_dev: |
| 713 | if (err < 0 && modified && net_ratelimit()) |
| 714 | printk(KERN_WARNING "A link change request failed with " |
| 715 | "some changes comitted already. Interface %s may " |
| 716 | "have been left with an inconsistent configuration, " |
| 717 | "please check.\n", dev->name); |
| 718 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | if (send_addr_notify) |
| 720 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); |
| 721 | |
| 722 | dev_put(dev); |
Thomas Graf | da5e049 | 2006-08-10 21:17:37 -0700 | [diff] [blame] | 723 | errout: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | return err; |
| 725 | } |
| 726 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 727 | 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] | 728 | { |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 729 | struct ifinfomsg *ifm; |
| 730 | struct nlattr *tb[IFLA_MAX+1]; |
| 731 | struct net_device *dev = NULL; |
| 732 | struct sk_buff *nskb; |
| 733 | char *iw_buf = NULL, *iw = NULL; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 734 | int iw_buf_len = 0; |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 735 | int err; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 736 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 737 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
| 738 | if (err < 0) |
Eric Sesterhenn | 9918f23 | 2006-09-26 23:26:38 -0700 | [diff] [blame] | 739 | return err; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 740 | |
| 741 | ifm = nlmsg_data(nlh); |
| 742 | if (ifm->ifi_index >= 0) { |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 743 | dev = dev_get_by_index(ifm->ifi_index); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 744 | if (dev == NULL) |
| 745 | return -ENODEV; |
| 746 | } else |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 747 | return -EINVAL; |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 748 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 749 | nskb = nlmsg_new(if_nlmsg_size(iw_buf_len), GFP_KERNEL); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 750 | if (nskb == NULL) { |
| 751 | err = -ENOBUFS; |
| 752 | goto errout; |
| 753 | } |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 754 | |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 755 | err = rtnl_fill_ifinfo(nskb, dev, iw, iw_buf_len, RTM_NEWLINK, |
| 756 | NETLINK_CB(skb).pid, nlh->nlmsg_seq, 0, 0); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 757 | if (err < 0) { |
| 758 | /* -EMSGSIZE implies BUG in if_nlmsg_size */ |
| 759 | WARN_ON(err == -EMSGSIZE); |
| 760 | kfree_skb(nskb); |
| 761 | goto errout; |
| 762 | } |
Patrick McHardy | b974179 | 2006-10-12 01:50:30 -0700 | [diff] [blame] | 763 | err = rtnl_unicast(nskb, NETLINK_CB(skb).pid); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 764 | errout: |
| 765 | kfree(iw_buf); |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 766 | dev_put(dev); |
Thomas Graf | b60c511 | 2006-08-04 23:05:34 -0700 | [diff] [blame] | 767 | |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 768 | return err; |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 769 | } |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 770 | |
Adrian Bunk | 42bad1d | 2007-04-26 00:57:41 -0700 | [diff] [blame] | 771 | 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] | 772 | { |
| 773 | int idx; |
| 774 | int s_idx = cb->family; |
| 775 | |
| 776 | if (s_idx == 0) |
| 777 | s_idx = 1; |
| 778 | for (idx=1; idx<NPROTO; idx++) { |
| 779 | int type = cb->nlh->nlmsg_type-RTM_BASE; |
| 780 | if (idx < s_idx || idx == PF_PACKET) |
| 781 | continue; |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 782 | if (rtnl_msg_handlers[idx] == NULL || |
| 783 | rtnl_msg_handlers[idx][type].dumpit == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | continue; |
| 785 | if (idx > s_idx) |
| 786 | memset(&cb->args[0], 0, sizeof(cb->args)); |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 787 | if (rtnl_msg_handlers[idx][type].dumpit(skb, cb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | break; |
| 789 | } |
| 790 | cb->family = idx; |
| 791 | |
| 792 | return skb->len; |
| 793 | } |
| 794 | |
| 795 | void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change) |
| 796 | { |
| 797 | struct sk_buff *skb; |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 798 | int err = -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 800 | skb = nlmsg_new(if_nlmsg_size(0), GFP_KERNEL); |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 801 | if (skb == NULL) |
| 802 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 804 | err = rtnl_fill_ifinfo(skb, dev, NULL, 0, type, 0, 0, change, 0); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 805 | if (err < 0) { |
| 806 | /* -EMSGSIZE implies BUG in if_nlmsg_size() */ |
| 807 | WARN_ON(err == -EMSGSIZE); |
| 808 | kfree_skb(skb); |
| 809 | goto errout; |
| 810 | } |
Thomas Graf | 0ec6d3f | 2006-08-15 00:37:09 -0700 | [diff] [blame] | 811 | err = rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_KERNEL); |
| 812 | errout: |
| 813 | if (err < 0) |
| 814 | rtnl_set_sk_err(RTNLGRP_LINK, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | /* Protected by RTNL sempahore. */ |
| 818 | static struct rtattr **rta_buf; |
| 819 | static int rtattr_max; |
| 820 | |
| 821 | /* Process one rtnetlink message. */ |
| 822 | |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 823 | static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | { |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 825 | rtnl_doit_func doit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | int sz_idx, kind; |
| 827 | int min_len; |
| 828 | int family; |
| 829 | int type; |
Patrick McHardy | 1c2d670 | 2007-04-16 16:59:10 -0700 | [diff] [blame] | 830 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | type = nlh->nlmsg_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | if (type > RTM_MAX) |
Thomas Graf | 038890f | 2007-04-05 14:35:52 -0700 | [diff] [blame] | 834 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | |
| 836 | type -= RTM_BASE; |
| 837 | |
| 838 | /* All the messages must have at least 1 byte length */ |
| 839 | if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg))) |
| 840 | return 0; |
| 841 | |
| 842 | family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family; |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 843 | if (family >= NPROTO) |
| 844 | return -EAFNOSUPPORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | sz_idx = type>>2; |
| 847 | kind = type&3; |
| 848 | |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 849 | if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN)) |
| 850 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | |
| 852 | if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 853 | rtnl_dumpit_func dumpit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 855 | dumpit = rtnl_get_dumpit(family, type); |
| 856 | if (dumpit == NULL) |
Thomas Graf | 038890f | 2007-04-05 14:35:52 -0700 | [diff] [blame] | 857 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | |
Patrick McHardy | 1c2d670 | 2007-04-16 16:59:10 -0700 | [diff] [blame] | 859 | __rtnl_unlock(); |
| 860 | err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL); |
| 861 | rtnl_lock(); |
| 862 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *))); |
| 866 | |
| 867 | min_len = rtm_min[sz_idx]; |
| 868 | if (nlh->nlmsg_len < min_len) |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 869 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | |
| 871 | if (nlh->nlmsg_len > min_len) { |
| 872 | int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len); |
| 873 | struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len); |
| 874 | |
| 875 | while (RTA_OK(attr, attrlen)) { |
| 876 | unsigned flavor = attr->rta_type; |
| 877 | if (flavor) { |
| 878 | if (flavor > rta_max[sz_idx]) |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 879 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | rta_buf[flavor-1] = attr; |
| 881 | } |
| 882 | attr = RTA_NEXT(attr, attrlen); |
| 883 | } |
| 884 | } |
| 885 | |
Thomas Graf | e284986 | 2007-03-22 11:48:11 -0700 | [diff] [blame] | 886 | doit = rtnl_get_doit(family, type); |
| 887 | if (doit == NULL) |
Thomas Graf | 038890f | 2007-04-05 14:35:52 -0700 | [diff] [blame] | 888 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 890 | return doit(skb, nlh, (void *)&rta_buf[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | } |
| 892 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | static void rtnetlink_rcv(struct sock *sk, int len) |
| 894 | { |
Thomas Graf | 9ac4a16 | 2005-11-10 02:25:55 +0100 | [diff] [blame] | 895 | unsigned int qlen = 0; |
Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 896 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | do { |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 898 | mutex_lock(&rtnl_mutex); |
Thomas Graf | 9ac4a16 | 2005-11-10 02:25:55 +0100 | [diff] [blame] | 899 | netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg); |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 900 | mutex_unlock(&rtnl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | |
| 902 | netdev_run_todo(); |
Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 903 | } while (qlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | } |
| 905 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 907 | { |
| 908 | struct net_device *dev = ptr; |
| 909 | switch (event) { |
| 910 | case NETDEV_UNREGISTER: |
| 911 | rtmsg_ifinfo(RTM_DELLINK, dev, ~0U); |
| 912 | break; |
| 913 | case NETDEV_REGISTER: |
| 914 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); |
| 915 | break; |
| 916 | case NETDEV_UP: |
| 917 | case NETDEV_DOWN: |
| 918 | rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING); |
| 919 | break; |
| 920 | case NETDEV_CHANGE: |
| 921 | case NETDEV_GOING_DOWN: |
| 922 | break; |
| 923 | default: |
| 924 | rtmsg_ifinfo(RTM_NEWLINK, dev, 0); |
| 925 | break; |
| 926 | } |
| 927 | return NOTIFY_DONE; |
| 928 | } |
| 929 | |
| 930 | static struct notifier_block rtnetlink_dev_notifier = { |
| 931 | .notifier_call = rtnetlink_event, |
| 932 | }; |
| 933 | |
| 934 | void __init rtnetlink_init(void) |
| 935 | { |
| 936 | int i; |
| 937 | |
| 938 | rtattr_max = 0; |
| 939 | for (i = 0; i < ARRAY_SIZE(rta_max); i++) |
| 940 | if (rta_max[i] > rtattr_max) |
| 941 | rtattr_max = rta_max[i]; |
| 942 | rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL); |
| 943 | if (!rta_buf) |
| 944 | panic("rtnetlink_init: cannot allocate rta_buf\n"); |
| 945 | |
Patrick McHardy | 0662860 | 2005-08-15 12:33:26 -0700 | [diff] [blame] | 946 | rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv, |
Patrick McHardy | 1c2d670 | 2007-04-16 16:59:10 -0700 | [diff] [blame] | 947 | &rtnl_mutex, THIS_MODULE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | if (rtnl == NULL) |
| 949 | panic("rtnetlink_init: cannot initialize rtnetlink\n"); |
| 950 | netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV); |
| 951 | register_netdevice_notifier(&rtnetlink_dev_notifier); |
Thomas Graf | 340d17f | 2007-03-22 11:49:22 -0700 | [diff] [blame] | 952 | |
| 953 | rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo); |
| 954 | rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL); |
Thomas Graf | 687ad8c | 2007-03-22 11:59:42 -0700 | [diff] [blame] | 955 | |
| 956 | rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all); |
| 957 | rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | EXPORT_SYMBOL(__rta_fill); |
| 961 | EXPORT_SYMBOL(rtattr_strlcpy); |
| 962 | EXPORT_SYMBOL(rtattr_parse); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | EXPORT_SYMBOL(rtnetlink_put_metrics); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | EXPORT_SYMBOL(rtnl_lock); |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 965 | EXPORT_SYMBOL(rtnl_trylock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | EXPORT_SYMBOL(rtnl_unlock); |
Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 967 | EXPORT_SYMBOL(rtnl_unicast); |
Thomas Graf | 97676b6 | 2006-08-15 00:31:41 -0700 | [diff] [blame] | 968 | EXPORT_SYMBOL(rtnl_notify); |
| 969 | EXPORT_SYMBOL(rtnl_set_sk_err); |