blob: 06c0c5afabf0a141239929ec9a35e8dba4a27dc2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070019#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 Torvalds1da177e2005-04-16 15:20:36 -070024#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 Hemminger6756ae42006-03-20 22:23:58 -080036#include <linux/mutex.h>
Thomas Graf18237302006-08-04 23:04:54 -070037#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
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 Graf14c0b972006-08-04 03:38:38 -070052#include <net/fib_rules.h>
Thomas Grafe2849862007-03-22 11:48:11 -070053#include <net/rtnetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Thomas Grafe2849862007-03-22 11:48:11 -070055struct rtnl_link
56{
57 rtnl_doit_func doit;
58 rtnl_dumpit_func dumpit;
59};
60
Stephen Hemminger6756ae42006-03-20 22:23:58 -080061static DEFINE_MUTEX(rtnl_mutex);
Thomas Graf56fc85a2006-08-15 00:37:29 -070062static struct sock *rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64void rtnl_lock(void)
65{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080066 mutex_lock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
Stephen Hemminger6756ae42006-03-20 22:23:58 -080069void __rtnl_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080071 mutex_unlock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
Stephen Hemminger6756ae42006-03-20 22:23:58 -080073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074void rtnl_unlock(void)
75{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080076 mutex_unlock(&rtnl_mutex);
77 if (rtnl && rtnl->sk_receive_queue.qlen)
78 rtnl->sk_data_ready(rtnl, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 netdev_run_todo();
80}
81
Stephen Hemminger6756ae42006-03-20 22:23:58 -080082int rtnl_trylock(void)
83{
84 return mutex_trylock(&rtnl_mutex);
85}
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087int 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 Bunk42bad1d2007-04-26 00:57:41 -0700100static struct rtnl_link *rtnl_msg_handlers[NPROTO];
Thomas Grafe2849862007-03-22 11:48:11 -0700101
102static 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
116static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
117{
118 struct rtnl_link *tab;
119
120 tab = rtnl_msg_handlers[protocol];
Thomas Graf51057f22007-03-22 21:41:06 -0700121 if (tab == NULL || tab[msgindex].doit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700122 tab = rtnl_msg_handlers[PF_UNSPEC];
123
Thomas Graf51057f22007-03-22 21:41:06 -0700124 return tab ? tab[msgindex].doit : NULL;
Thomas Grafe2849862007-03-22 11:48:11 -0700125}
126
127static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
128{
129 struct rtnl_link *tab;
130
131 tab = rtnl_msg_handlers[protocol];
Thomas Graf51057f22007-03-22 21:41:06 -0700132 if (tab == NULL || tab[msgindex].dumpit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700133 tab = rtnl_msg_handlers[PF_UNSPEC];
134
Thomas Graf51057f22007-03-22 21:41:06 -0700135 return tab ? tab[msgindex].dumpit : NULL;
Thomas Grafe2849862007-03-22 11:48:11 -0700136}
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 */
155int __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
182EXPORT_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 */
193void 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
202EXPORT_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 */
211int 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
227EXPORT_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 */
236void 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
244EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Patrick McHardy38f7b872007-06-13 12:03:51 -0700246static 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 */
258int __rtnl_link_register(struct rtnl_link_ops *ops)
259{
260 list_add_tail(&ops->list, &link_ops);
261 return 0;
262}
263
264EXPORT_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 */
272int 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
282EXPORT_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 */
292void __rtnl_link_unregister(struct rtnl_link_ops *ops)
293{
294 list_del(&ops->list);
295}
296
297EXPORT_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 */
303void rtnl_link_unregister(struct rtnl_link_ops *ops)
304{
305 rtnl_lock();
306 __rtnl_link_unregister(ops);
307 rtnl_unlock();
308}
309
310EXPORT_SYMBOL_GPL(rtnl_link_unregister);
311
312static 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
323static 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
345static 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
375err_cancel_data:
376 nla_nest_cancel(skb, data);
377err_cancel_link:
378 nla_nest_cancel(skb, linkinfo);
379out:
380 return err;
381}
382
Thomas Grafdb46edc2005-05-03 14:29:39 -0700383static const int rtm_min[RTM_NR_FAMILIES] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Thomas Graff90a0a72005-05-03 14:29:00 -0700385 [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 Graf14c0b972006-08-04 03:38:38 -0700388 [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
Thomas Graff90a0a72005-05-03 14:29:00 -0700389 [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 Graff90a0a72005-05-03 14:29:00 -0700393 [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
394 [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395};
396
Thomas Grafdb46edc2005-05-03 14:29:39 -0700397static const int rta_max[RTM_NR_FAMILIES] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Thomas Graff90a0a72005-05-03 14:29:00 -0700399 [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX,
400 [RTM_FAM(RTM_NEWADDR)] = IFA_MAX,
401 [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX,
Thomas Graf14c0b972006-08-04 03:38:38 -0700402 [RTM_FAM(RTM_NEWRULE)] = FRA_MAX,
Thomas Graff90a0a72005-05-03 14:29:00 -0700403 [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 Torvalds1da177e2005-04-16 15:20:36 -0700407};
408
409void __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 McHardyb3563c42005-06-28 12:54:43 -0700418 memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
421size_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
436int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
437{
438 int err = 0;
439
Patrick McHardyac6d4392005-08-14 19:29:52 -0700440 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 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 Graf2942e902006-08-15 00:30:25 -0700449int rtnl_unicast(struct sk_buff *skb, u32 pid)
450{
451 return nlmsg_unicast(rtnl, skb, pid);
452}
453
Thomas Graf97676b62006-08-15 00:31:41 -0700454int 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
465void rtnl_set_sk_err(u32 group, int error)
466{
467 netlink_set_err(rtnl, 0, group, error);
468}
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
471{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700472 struct nlattr *mx;
473 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Thomas Graf2d7202b2006-08-22 00:01:27 -0700475 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 Torvalds1da177e2005-04-16 15:20:36 -0700484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
David S. Millera57d27f2006-08-22 22:20:14 -0700486 if (!valid) {
487 nla_nest_cancel(skb, mx);
488 return 0;
489 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700490
491 return nla_nest_end(skb, mx);
492
493nla_put_failure:
494 return nla_nest_cancel(skb, mx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
Thomas Grafe3703b32006-11-27 09:27:07 -0800497int 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
516EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Stefan Rompfb00055a2006-03-20 17:09:11 -0800518static 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 Hemminger3ff50b72007-04-20 17:09:22 -0700535 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800536
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 Grafb60c5112006-08-04 23:05:34 -0700545static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
546 struct net_device_stats *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Thomas Grafb60c5112006-08-04 23:05:34 -0700548 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 Torvalds1da177e2005-04-16 15:20:36 -0700556
Thomas Grafb60c5112006-08-04 23:05:34 -0700557 a->multicast = b->multicast;
558 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Thomas Grafb60c5112006-08-04 23:05:34 -0700560 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 Torvalds1da177e2005-04-16 15:20:36 -0700566
Thomas Grafb60c5112006-08-04 23:05:34 -0700567 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 Torvalds1da177e2005-04-16 15:20:36 -0700572
Thomas Grafb60c5112006-08-04 23:05:34 -0700573 a->rx_compressed = b->rx_compressed;
574 a->tx_compressed = b->tx_compressed;
575};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Patrick McHardy38f7b872007-06-13 12:03:51 -0700577static inline size_t if_nlmsg_size(const struct net_device *dev)
Thomas Graf339bf982006-11-10 14:10:15 -0800578{
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 McHardy38f7b872007-06-13 12:03:51 -0700592 + nla_total_size(1) /* IFLA_LINKMODE */
593 + rtnl_link_get_size(dev); /* IFLA_LINKINFO */
Thomas Graf339bf982006-11-10 14:10:15 -0800594}
595
Thomas Grafb60c5112006-08-04 23:05:34 -0700596static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -0700597 int type, u32 pid, u32 seq, u32 change,
598 unsigned int flags)
Thomas Grafb60c5112006-08-04 23:05:34 -0700599{
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 McHardy26932562007-01-31 23:16:40 -0800605 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -0700606
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 Rompfb00055a2006-03-20 17:09:11 -0800631
632 if (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 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 Grafb60c5112006-08-04 23:05:34 -0700641 NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643
644 if (dev->addr_len) {
Thomas Grafb60c5112006-08-04 23:05:34 -0700645 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
646 NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648
649 if (dev->get_stats) {
Thomas Grafb60c5112006-08-04 23:05:34 -0700650 struct net_device_stats *stats = dev->get_stats(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (stats) {
Thomas Grafb60c5112006-08-04 23:05:34 -0700652 struct nlattr *attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Thomas Grafb60c5112006-08-04 23:05:34 -0700654 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 Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Patrick McHardy38f7b872007-06-13 12:03:51 -0700663 if (dev->rtnl_link_ops) {
664 if (rtnl_link_fill(skb, dev) < 0)
665 goto nla_put_failure;
666 }
667
Thomas Grafb60c5112006-08-04 23:05:34 -0700668 return nlmsg_end(skb, nlh);
669
670nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800671 nlmsg_cancel(skb, nlh);
672 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
Thomas Grafb60c5112006-08-04 23:05:34 -0700675static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 int idx;
678 int s_idx = cb->args[0];
679 struct net_device *dev;
680
Pavel Emelianov7562f872007-05-03 15:13:45 -0700681 idx = 0;
682 for_each_netdev(dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (idx < s_idx)
Pavel Emelianov7562f872007-05-03 15:13:45 -0700684 goto cont;
Patrick McHardy575c3e22007-05-22 17:00:49 -0700685 if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
Thomas Grafb60c5112006-08-04 23:05:34 -0700686 NETLINK_CB(cb->skb).pid,
687 cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 break;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700689cont:
690 idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 cb->args[0] = idx;
693
694 return skb->len;
695}
696
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700697static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -0700698 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
Patrick McHardy38f7b872007-06-13 12:03:51 -0700699 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
700 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
Thomas Graf5176f912006-08-26 20:13:18 -0700701 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
Thomas Grafda5e0492006-08-10 21:17:37 -0700702 [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 McHardy38f7b872007-06-13 12:03:51 -0700709static 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 McHardy0157f602007-06-13 12:03:36 -0700714static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
Patrick McHardy38f7b872007-06-13 12:03:51 -0700715 struct nlattr **tb, char *ifname, int modified)
Patrick McHardy0157f602007-06-13 12:03:36 -0700716{
Patrick McHardy38f7b872007-06-13 12:03:51 -0700717 int send_addr_notify = 0;
Patrick McHardy0157f602007-06-13 12:03:36 -0700718 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
831errout:
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 Grafda5e0492006-08-10 21:17:37 -0700843static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Thomas Grafda5e0492006-08-10 21:17:37 -0700845 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -0700847 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -0700848 struct nlattr *tb[IFLA_MAX+1];
849 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Thomas Grafda5e0492006-08-10 21:17:37 -0700851 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
852 if (err < 0)
853 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Thomas Graf5176f912006-08-26 20:13:18 -0700855 if (tb[IFLA_IFNAME])
856 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -0700857 else
858 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -0700861 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -0700862 if (ifm->ifi_index > 0)
Thomas Grafda5e0492006-08-10 21:17:37 -0700863 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 Torvalds1da177e2005-04-16 15:20:36 -0700868
Thomas Grafda5e0492006-08-10 21:17:37 -0700869 if (dev == NULL) {
870 err = -ENODEV;
871 goto errout;
872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Thomas Grafda5e0492006-08-10 21:17:37 -0700874 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 McHardy38f7b872007-06-13 12:03:51 -0700882 err = do_setlink(dev, ifm, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -0700883errout_dev:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 dev_put(dev);
Thomas Grafda5e0492006-08-10 21:17:37 -0700885errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return err;
887}
888
Patrick McHardy38f7b872007-06-13 12:03:51 -0700889static 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
924static 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
935replay:
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);
1058err_free:
1059 if (err < 0)
1060 free_netdev(dev);
1061 return err;
1062 }
1063}
1064
Thomas Grafb60c5112006-08-04 23:05:34 -07001065static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001066{
Thomas Grafb60c5112006-08-04 23:05:34 -07001067 struct ifinfomsg *ifm;
1068 struct nlattr *tb[IFLA_MAX+1];
1069 struct net_device *dev = NULL;
1070 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08001071 int err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001072
Thomas Grafb60c5112006-08-04 23:05:34 -07001073 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1074 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07001075 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07001076
1077 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07001078 if (ifm->ifi_index > 0) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001079 dev = dev_get_by_index(ifm->ifi_index);
Thomas Grafb60c5112006-08-04 23:05:34 -07001080 if (dev == NULL)
1081 return -ENODEV;
1082 } else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001083 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07001084
Patrick McHardy38f7b872007-06-13 12:03:51 -07001085 nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
Thomas Grafb60c5112006-08-04 23:05:34 -07001086 if (nskb == NULL) {
1087 err = -ENOBUFS;
1088 goto errout;
1089 }
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001090
Patrick McHardy575c3e22007-05-22 17:00:49 -07001091 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
1092 nlh->nlmsg_seq, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08001093 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 McHardyb9741792006-10-12 01:50:30 -07001099 err = rtnl_unicast(nskb, NETLINK_CB(skb).pid);
Thomas Grafb60c5112006-08-04 23:05:34 -07001100errout:
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001101 dev_put(dev);
Thomas Grafb60c5112006-08-04 23:05:34 -07001102
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001103 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001104}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001105
Adrian Bunk42bad1d2007-04-26 00:57:41 -07001106static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
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 Grafe2849862007-03-22 11:48:11 -07001117 if (rtnl_msg_handlers[idx] == NULL ||
1118 rtnl_msg_handlers[idx][type].dumpit == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 continue;
1120 if (idx > s_idx)
1121 memset(&cb->args[0], 0, sizeof(cb->args));
Thomas Grafe2849862007-03-22 11:48:11 -07001122 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 break;
1124 }
1125 cb->family = idx;
1126
1127 return skb->len;
1128}
1129
1130void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
1131{
1132 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07001133 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Patrick McHardy38f7b872007-06-13 12:03:51 -07001135 skb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07001136 if (skb == NULL)
1137 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Patrick McHardy575c3e22007-05-22 17:00:49 -07001139 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08001140 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 Graf0ec6d3f2006-08-15 00:37:09 -07001146 err = rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
1147errout:
1148 if (err < 0)
1149 rtnl_set_sk_err(RTNLGRP_LINK, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152/* Protected by RTNL sempahore. */
1153static struct rtattr **rta_buf;
1154static int rtattr_max;
1155
1156/* Process one rtnetlink message. */
1157
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001158static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Thomas Grafe2849862007-03-22 11:48:11 -07001160 rtnl_doit_func doit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 int sz_idx, kind;
1162 int min_len;
1163 int family;
1164 int type;
Patrick McHardy1c2d6702007-04-16 16:59:10 -07001165 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07001169 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
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 Graf1d00a4e2007-03-22 23:30:12 -07001178 if (family >= NPROTO)
1179 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 sz_idx = type>>2;
1182 kind = type&3;
1183
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001184 if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN))
1185 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Thomas Grafe2849862007-03-22 11:48:11 -07001188 rtnl_dumpit_func dumpit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Thomas Grafe2849862007-03-22 11:48:11 -07001190 dumpit = rtnl_get_dumpit(family, type);
1191 if (dumpit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07001192 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Patrick McHardy1c2d6702007-04-16 16:59:10 -07001194 __rtnl_unlock();
1195 err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL);
1196 rtnl_lock();
1197 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
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 Graf1d00a4e2007-03-22 23:30:12 -07001204 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
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 Graf1d00a4e2007-03-22 23:30:12 -07001214 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 rta_buf[flavor-1] = attr;
1216 }
1217 attr = RTA_NEXT(attr, attrlen);
1218 }
1219 }
1220
Thomas Grafe2849862007-03-22 11:48:11 -07001221 doit = rtnl_get_doit(family, type);
1222 if (doit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07001223 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001225 return doit(skb, nlh, (void *)&rta_buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228static void rtnetlink_rcv(struct sock *sk, int len)
1229{
Thomas Graf9ac4a162005-11-10 02:25:55 +01001230 unsigned int qlen = 0;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 do {
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001233 mutex_lock(&rtnl_mutex);
Thomas Graf9ac4a162005-11-10 02:25:55 +01001234 netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001235 mutex_unlock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 netdev_run_todo();
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001238 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241static 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
1265static struct notifier_block rtnetlink_dev_notifier = {
1266 .notifier_call = rtnetlink_event,
1267};
1268
1269void __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 McHardy06628602005-08-15 12:33:26 -07001281 rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv,
Patrick McHardy1c2d6702007-04-16 16:59:10 -07001282 &rtnl_mutex, THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 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 Graf340d17f2007-03-22 11:49:22 -07001287
1288 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo);
1289 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001290 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL);
1291 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL);
Thomas Graf687ad8c2007-03-22 11:59:42 -07001292
1293 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all);
1294 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
1296
1297EXPORT_SYMBOL(__rta_fill);
1298EXPORT_SYMBOL(rtattr_strlcpy);
1299EXPORT_SYMBOL(rtattr_parse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301EXPORT_SYMBOL(rtnl_lock);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001302EXPORT_SYMBOL(rtnl_trylock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303EXPORT_SYMBOL(rtnl_unlock);
Thomas Graf2942e902006-08-15 00:30:25 -07001304EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf97676b62006-08-15 00:31:41 -07001305EXPORT_SYMBOL(rtnl_notify);
1306EXPORT_SYMBOL(rtnl_set_sk_err);