blob: 25ca219154e062e82c71856f1f9c80fdaf87efd3 [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
Thomas Grafdb46edc2005-05-03 14:29:39 -0700246static const int rtm_min[RTM_NR_FAMILIES] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Thomas Graff90a0a72005-05-03 14:29:00 -0700248 [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 Graf14c0b972006-08-04 03:38:38 -0700251 [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
Thomas Graff90a0a72005-05-03 14:29:00 -0700252 [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 Graff90a0a72005-05-03 14:29:00 -0700256 [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
257 [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258};
259
Thomas Grafdb46edc2005-05-03 14:29:39 -0700260static const int rta_max[RTM_NR_FAMILIES] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Thomas Graff90a0a72005-05-03 14:29:00 -0700262 [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX,
263 [RTM_FAM(RTM_NEWADDR)] = IFA_MAX,
264 [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX,
Thomas Graf14c0b972006-08-04 03:38:38 -0700265 [RTM_FAM(RTM_NEWRULE)] = FRA_MAX,
Thomas Graff90a0a72005-05-03 14:29:00 -0700266 [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 Torvalds1da177e2005-04-16 15:20:36 -0700270};
271
272void __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 McHardyb3563c42005-06-28 12:54:43 -0700281 memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284size_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
299int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
300{
301 int err = 0;
302
Patrick McHardyac6d4392005-08-14 19:29:52 -0700303 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 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 Graf2942e902006-08-15 00:30:25 -0700312int rtnl_unicast(struct sk_buff *skb, u32 pid)
313{
314 return nlmsg_unicast(rtnl, skb, pid);
315}
316
Thomas Graf97676b62006-08-15 00:31:41 -0700317int 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
328void rtnl_set_sk_err(u32 group, int error)
329{
330 netlink_set_err(rtnl, 0, group, error);
331}
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
334{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700335 struct nlattr *mx;
336 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Thomas Graf2d7202b2006-08-22 00:01:27 -0700338 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 Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
David S. Millera57d27f2006-08-22 22:20:14 -0700349 if (!valid) {
350 nla_nest_cancel(skb, mx);
351 return 0;
352 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700353
354 return nla_nest_end(skb, mx);
355
356nla_put_failure:
357 return nla_nest_cancel(skb, mx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Thomas Grafe3703b32006-11-27 09:27:07 -0800360int 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
379EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Stefan Rompfb00055a2006-03-20 17:09:11 -0800381static 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 Hemminger3ff50b72007-04-20 17:09:22 -0700398 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800399
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 Grafb60c5112006-08-04 23:05:34 -0700408static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
409 struct net_device_stats *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Thomas Grafb60c5112006-08-04 23:05:34 -0700411 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 Torvalds1da177e2005-04-16 15:20:36 -0700419
Thomas Grafb60c5112006-08-04 23:05:34 -0700420 a->multicast = b->multicast;
421 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Thomas Grafb60c5112006-08-04 23:05:34 -0700423 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 Torvalds1da177e2005-04-16 15:20:36 -0700429
Thomas Grafb60c5112006-08-04 23:05:34 -0700430 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 Torvalds1da177e2005-04-16 15:20:36 -0700435
Thomas Grafb60c5112006-08-04 23:05:34 -0700436 a->rx_compressed = b->rx_compressed;
437 a->tx_compressed = b->tx_compressed;
438};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Patrick McHardy575c3e22007-05-22 17:00:49 -0700440static inline size_t if_nlmsg_size(void)
Thomas Graf339bf982006-11-10 14:10:15 -0800441{
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 */
Patrick McHardy575c3e22007-05-22 17:00:49 -0700455 + nla_total_size(1); /* IFLA_LINKMODE */
Thomas Graf339bf982006-11-10 14:10:15 -0800456}
457
Thomas Grafb60c5112006-08-04 23:05:34 -0700458static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -0700459 int type, u32 pid, u32 seq, u32 change,
460 unsigned int flags)
Thomas Grafb60c5112006-08-04 23:05:34 -0700461{
462 struct ifinfomsg *ifm;
463 struct nlmsghdr *nlh;
464
465 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
466 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800467 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -0700468
469 ifm = nlmsg_data(nlh);
470 ifm->ifi_family = AF_UNSPEC;
471 ifm->__ifi_pad = 0;
472 ifm->ifi_type = dev->type;
473 ifm->ifi_index = dev->ifindex;
474 ifm->ifi_flags = dev_get_flags(dev);
475 ifm->ifi_change = change;
476
477 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
478 NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
479 NLA_PUT_U32(skb, IFLA_WEIGHT, dev->weight);
480 NLA_PUT_U8(skb, IFLA_OPERSTATE,
481 netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
482 NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
483 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
484
485 if (dev->ifindex != dev->iflink)
486 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
487
488 if (dev->master)
489 NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
490
491 if (dev->qdisc_sleeping)
492 NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc_sleeping->ops->id);
Stefan Rompfb00055a2006-03-20 17:09:11 -0800493
494 if (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 struct rtnl_link_ifmap map = {
496 .mem_start = dev->mem_start,
497 .mem_end = dev->mem_end,
498 .base_addr = dev->base_addr,
499 .irq = dev->irq,
500 .dma = dev->dma,
501 .port = dev->if_port,
502 };
Thomas Grafb60c5112006-08-04 23:05:34 -0700503 NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505
506 if (dev->addr_len) {
Thomas Grafb60c5112006-08-04 23:05:34 -0700507 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
508 NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510
511 if (dev->get_stats) {
Thomas Grafb60c5112006-08-04 23:05:34 -0700512 struct net_device_stats *stats = dev->get_stats(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (stats) {
Thomas Grafb60c5112006-08-04 23:05:34 -0700514 struct nlattr *attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Thomas Grafb60c5112006-08-04 23:05:34 -0700516 attr = nla_reserve(skb, IFLA_STATS,
517 sizeof(struct rtnl_link_stats));
518 if (attr == NULL)
519 goto nla_put_failure;
520
521 copy_rtnl_link_stats(nla_data(attr), stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Thomas Grafb60c5112006-08-04 23:05:34 -0700525 return nlmsg_end(skb, nlh);
526
527nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800528 nlmsg_cancel(skb, nlh);
529 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
Thomas Grafb60c5112006-08-04 23:05:34 -0700532static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 int idx;
535 int s_idx = cb->args[0];
536 struct net_device *dev;
537
Pavel Emelianov7562f872007-05-03 15:13:45 -0700538 idx = 0;
539 for_each_netdev(dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (idx < s_idx)
Pavel Emelianov7562f872007-05-03 15:13:45 -0700541 goto cont;
Patrick McHardy575c3e22007-05-22 17:00:49 -0700542 if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
Thomas Grafb60c5112006-08-04 23:05:34 -0700543 NETLINK_CB(cb->skb).pid,
544 cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 break;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700546cont:
547 idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 cb->args[0] = idx;
550
551 return skb->len;
552}
553
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700554static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -0700555 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
556 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
Thomas Grafda5e0492006-08-10 21:17:37 -0700557 [IFLA_MTU] = { .type = NLA_U32 },
558 [IFLA_TXQLEN] = { .type = NLA_U32 },
559 [IFLA_WEIGHT] = { .type = NLA_U32 },
560 [IFLA_OPERSTATE] = { .type = NLA_U8 },
561 [IFLA_LINKMODE] = { .type = NLA_U8 },
562};
563
Patrick McHardy0157f602007-06-13 12:03:36 -0700564static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
565 struct nlattr **tb, char *ifname)
566{
567 int modified = 0, send_addr_notify = 0;
568 int err;
569
570 if (tb[IFLA_MAP]) {
571 struct rtnl_link_ifmap *u_map;
572 struct ifmap k_map;
573
574 if (!dev->set_config) {
575 err = -EOPNOTSUPP;
576 goto errout;
577 }
578
579 if (!netif_device_present(dev)) {
580 err = -ENODEV;
581 goto errout;
582 }
583
584 u_map = nla_data(tb[IFLA_MAP]);
585 k_map.mem_start = (unsigned long) u_map->mem_start;
586 k_map.mem_end = (unsigned long) u_map->mem_end;
587 k_map.base_addr = (unsigned short) u_map->base_addr;
588 k_map.irq = (unsigned char) u_map->irq;
589 k_map.dma = (unsigned char) u_map->dma;
590 k_map.port = (unsigned char) u_map->port;
591
592 err = dev->set_config(dev, &k_map);
593 if (err < 0)
594 goto errout;
595
596 modified = 1;
597 }
598
599 if (tb[IFLA_ADDRESS]) {
600 struct sockaddr *sa;
601 int len;
602
603 if (!dev->set_mac_address) {
604 err = -EOPNOTSUPP;
605 goto errout;
606 }
607
608 if (!netif_device_present(dev)) {
609 err = -ENODEV;
610 goto errout;
611 }
612
613 len = sizeof(sa_family_t) + dev->addr_len;
614 sa = kmalloc(len, GFP_KERNEL);
615 if (!sa) {
616 err = -ENOMEM;
617 goto errout;
618 }
619 sa->sa_family = dev->type;
620 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
621 dev->addr_len);
622 err = dev->set_mac_address(dev, sa);
623 kfree(sa);
624 if (err)
625 goto errout;
626 send_addr_notify = 1;
627 modified = 1;
628 }
629
630 if (tb[IFLA_MTU]) {
631 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
632 if (err < 0)
633 goto errout;
634 modified = 1;
635 }
636
637 /*
638 * Interface selected by interface index but interface
639 * name provided implies that a name change has been
640 * requested.
641 */
642 if (ifm->ifi_index > 0 && ifname[0]) {
643 err = dev_change_name(dev, ifname);
644 if (err < 0)
645 goto errout;
646 modified = 1;
647 }
648
649 if (tb[IFLA_BROADCAST]) {
650 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
651 send_addr_notify = 1;
652 }
653
654 if (ifm->ifi_flags || ifm->ifi_change) {
655 unsigned int flags = ifm->ifi_flags;
656
657 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
658 if (ifm->ifi_change)
659 flags = (flags & ifm->ifi_change) |
660 (dev->flags & ~ifm->ifi_change);
661 dev_change_flags(dev, flags);
662 }
663
664 if (tb[IFLA_TXQLEN])
665 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
666
667 if (tb[IFLA_WEIGHT])
668 dev->weight = nla_get_u32(tb[IFLA_WEIGHT]);
669
670 if (tb[IFLA_OPERSTATE])
671 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
672
673 if (tb[IFLA_LINKMODE]) {
674 write_lock_bh(&dev_base_lock);
675 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
676 write_unlock_bh(&dev_base_lock);
677 }
678
679 err = 0;
680
681errout:
682 if (err < 0 && modified && net_ratelimit())
683 printk(KERN_WARNING "A link change request failed with "
684 "some changes comitted already. Interface %s may "
685 "have been left with an inconsistent configuration, "
686 "please check.\n", dev->name);
687
688 if (send_addr_notify)
689 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
690 return err;
691}
692
Thomas Grafda5e0492006-08-10 21:17:37 -0700693static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Thomas Grafda5e0492006-08-10 21:17:37 -0700695 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -0700697 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -0700698 struct nlattr *tb[IFLA_MAX+1];
699 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Thomas Grafda5e0492006-08-10 21:17:37 -0700701 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
702 if (err < 0)
703 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Thomas Graf5176f912006-08-26 20:13:18 -0700705 if (tb[IFLA_IFNAME])
706 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -0700707 else
708 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -0700711 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -0700712 if (ifm->ifi_index > 0)
Thomas Grafda5e0492006-08-10 21:17:37 -0700713 dev = dev_get_by_index(ifm->ifi_index);
714 else if (tb[IFLA_IFNAME])
715 dev = dev_get_by_name(ifname);
716 else
717 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Thomas Grafda5e0492006-08-10 21:17:37 -0700719 if (dev == NULL) {
720 err = -ENODEV;
721 goto errout;
722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Thomas Grafda5e0492006-08-10 21:17:37 -0700724 if (tb[IFLA_ADDRESS] &&
725 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
726 goto errout_dev;
727
728 if (tb[IFLA_BROADCAST] &&
729 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
730 goto errout_dev;
731
Patrick McHardy0157f602007-06-13 12:03:36 -0700732 err = do_setlink(dev, ifm, tb, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -0700733errout_dev:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 dev_put(dev);
Thomas Grafda5e0492006-08-10 21:17:37 -0700735errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return err;
737}
738
Thomas Grafb60c5112006-08-04 23:05:34 -0700739static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800740{
Thomas Grafb60c5112006-08-04 23:05:34 -0700741 struct ifinfomsg *ifm;
742 struct nlattr *tb[IFLA_MAX+1];
743 struct net_device *dev = NULL;
744 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -0800745 int err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800746
Thomas Grafb60c5112006-08-04 23:05:34 -0700747 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
748 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -0700749 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -0700750
751 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -0700752 if (ifm->ifi_index > 0) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800753 dev = dev_get_by_index(ifm->ifi_index);
Thomas Grafb60c5112006-08-04 23:05:34 -0700754 if (dev == NULL)
755 return -ENODEV;
756 } else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800757 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -0700758
Patrick McHardy575c3e22007-05-22 17:00:49 -0700759 nskb = nlmsg_new(if_nlmsg_size(), GFP_KERNEL);
Thomas Grafb60c5112006-08-04 23:05:34 -0700760 if (nskb == NULL) {
761 err = -ENOBUFS;
762 goto errout;
763 }
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800764
Patrick McHardy575c3e22007-05-22 17:00:49 -0700765 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
766 nlh->nlmsg_seq, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -0800767 if (err < 0) {
768 /* -EMSGSIZE implies BUG in if_nlmsg_size */
769 WARN_ON(err == -EMSGSIZE);
770 kfree_skb(nskb);
771 goto errout;
772 }
Patrick McHardyb9741792006-10-12 01:50:30 -0700773 err = rtnl_unicast(nskb, NETLINK_CB(skb).pid);
Thomas Grafb60c5112006-08-04 23:05:34 -0700774errout:
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800775 dev_put(dev);
Thomas Grafb60c5112006-08-04 23:05:34 -0700776
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800777 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800778}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800779
Adrian Bunk42bad1d2007-04-26 00:57:41 -0700780static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 int idx;
783 int s_idx = cb->family;
784
785 if (s_idx == 0)
786 s_idx = 1;
787 for (idx=1; idx<NPROTO; idx++) {
788 int type = cb->nlh->nlmsg_type-RTM_BASE;
789 if (idx < s_idx || idx == PF_PACKET)
790 continue;
Thomas Grafe2849862007-03-22 11:48:11 -0700791 if (rtnl_msg_handlers[idx] == NULL ||
792 rtnl_msg_handlers[idx][type].dumpit == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 continue;
794 if (idx > s_idx)
795 memset(&cb->args[0], 0, sizeof(cb->args));
Thomas Grafe2849862007-03-22 11:48:11 -0700796 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 break;
798 }
799 cb->family = idx;
800
801 return skb->len;
802}
803
804void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
805{
806 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -0700807 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Patrick McHardy575c3e22007-05-22 17:00:49 -0700809 skb = nlmsg_new(if_nlmsg_size(), GFP_KERNEL);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -0700810 if (skb == NULL)
811 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Patrick McHardy575c3e22007-05-22 17:00:49 -0700813 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0);
Patrick McHardy26932562007-01-31 23:16:40 -0800814 if (err < 0) {
815 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
816 WARN_ON(err == -EMSGSIZE);
817 kfree_skb(skb);
818 goto errout;
819 }
Thomas Graf0ec6d3f2006-08-15 00:37:09 -0700820 err = rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
821errout:
822 if (err < 0)
823 rtnl_set_sk_err(RTNLGRP_LINK, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826/* Protected by RTNL sempahore. */
827static struct rtattr **rta_buf;
828static int rtattr_max;
829
830/* Process one rtnetlink message. */
831
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700832static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Thomas Grafe2849862007-03-22 11:48:11 -0700834 rtnl_doit_func doit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 int sz_idx, kind;
836 int min_len;
837 int family;
838 int type;
Patrick McHardy1c2d6702007-04-16 16:59:10 -0700839 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -0700843 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 type -= RTM_BASE;
846
847 /* All the messages must have at least 1 byte length */
848 if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
849 return 0;
850
851 family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700852 if (family >= NPROTO)
853 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 sz_idx = type>>2;
856 kind = type&3;
857
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700858 if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN))
859 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Thomas Grafe2849862007-03-22 11:48:11 -0700862 rtnl_dumpit_func dumpit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Thomas Grafe2849862007-03-22 11:48:11 -0700864 dumpit = rtnl_get_dumpit(family, type);
865 if (dumpit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -0700866 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Patrick McHardy1c2d6702007-04-16 16:59:10 -0700868 __rtnl_unlock();
869 err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL);
870 rtnl_lock();
871 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873
874 memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
875
876 min_len = rtm_min[sz_idx];
877 if (nlh->nlmsg_len < min_len)
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700878 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 if (nlh->nlmsg_len > min_len) {
881 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
882 struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
883
884 while (RTA_OK(attr, attrlen)) {
885 unsigned flavor = attr->rta_type;
886 if (flavor) {
887 if (flavor > rta_max[sz_idx])
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700888 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 rta_buf[flavor-1] = attr;
890 }
891 attr = RTA_NEXT(attr, attrlen);
892 }
893 }
894
Thomas Grafe2849862007-03-22 11:48:11 -0700895 doit = rtnl_get_doit(family, type);
896 if (doit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -0700897 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700899 return doit(skb, nlh, (void *)&rta_buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902static void rtnetlink_rcv(struct sock *sk, int len)
903{
Thomas Graf9ac4a162005-11-10 02:25:55 +0100904 unsigned int qlen = 0;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 do {
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800907 mutex_lock(&rtnl_mutex);
Thomas Graf9ac4a162005-11-10 02:25:55 +0100908 netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg);
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800909 mutex_unlock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 netdev_run_todo();
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700912 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
916{
917 struct net_device *dev = ptr;
918 switch (event) {
919 case NETDEV_UNREGISTER:
920 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
921 break;
922 case NETDEV_REGISTER:
923 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
924 break;
925 case NETDEV_UP:
926 case NETDEV_DOWN:
927 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
928 break;
929 case NETDEV_CHANGE:
930 case NETDEV_GOING_DOWN:
931 break;
932 default:
933 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
934 break;
935 }
936 return NOTIFY_DONE;
937}
938
939static struct notifier_block rtnetlink_dev_notifier = {
940 .notifier_call = rtnetlink_event,
941};
942
943void __init rtnetlink_init(void)
944{
945 int i;
946
947 rtattr_max = 0;
948 for (i = 0; i < ARRAY_SIZE(rta_max); i++)
949 if (rta_max[i] > rtattr_max)
950 rtattr_max = rta_max[i];
951 rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
952 if (!rta_buf)
953 panic("rtnetlink_init: cannot allocate rta_buf\n");
954
Patrick McHardy06628602005-08-15 12:33:26 -0700955 rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv,
Patrick McHardy1c2d6702007-04-16 16:59:10 -0700956 &rtnl_mutex, THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (rtnl == NULL)
958 panic("rtnetlink_init: cannot initialize rtnetlink\n");
959 netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
960 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -0700961
962 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo);
963 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL);
Thomas Graf687ad8c2007-03-22 11:59:42 -0700964
965 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all);
966 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
969EXPORT_SYMBOL(__rta_fill);
970EXPORT_SYMBOL(rtattr_strlcpy);
971EXPORT_SYMBOL(rtattr_parse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973EXPORT_SYMBOL(rtnl_lock);
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800974EXPORT_SYMBOL(rtnl_trylock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975EXPORT_SYMBOL(rtnl_unlock);
Thomas Graf2942e902006-08-15 00:30:25 -0700976EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf97676b62006-08-15 00:31:41 -0700977EXPORT_SYMBOL(rtnl_notify);
978EXPORT_SYMBOL(rtnl_set_sk_err);