KVM: use gfn_to_pfn_memslot in kvm_iommu_map_pages
[linux/fpc-iii.git] / net / core / rtnetlink.c
blob4c7d3f635ba7ab97dbc76b55f000ccb398bd2dbc
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.
6 * Routing netlink socket interface: protocol independent part.
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
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.
15 * Fixes:
16 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
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>
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>
36 #include <linux/mutex.h>
37 #include <linux/if_addr.h>
39 #include <asm/uaccess.h>
40 #include <asm/system.h>
42 #include <linux/inet.h>
43 #include <linux/netdevice.h>
44 #include <net/ip.h>
45 #include <net/protocol.h>
46 #include <net/arp.h>
47 #include <net/route.h>
48 #include <net/udp.h>
49 #include <net/sock.h>
50 #include <net/pkt_sched.h>
51 #include <net/fib_rules.h>
52 #include <net/rtnetlink.h>
53 #include <net/net_namespace.h>
55 struct rtnl_link {
56 rtnl_doit_func doit;
57 rtnl_dumpit_func dumpit;
60 static DEFINE_MUTEX(rtnl_mutex);
62 void rtnl_lock(void)
64 mutex_lock(&rtnl_mutex);
66 EXPORT_SYMBOL(rtnl_lock);
68 void __rtnl_unlock(void)
70 mutex_unlock(&rtnl_mutex);
73 void rtnl_unlock(void)
75 /* This fellow will unlock it for us. */
76 netdev_run_todo();
78 EXPORT_SYMBOL(rtnl_unlock);
80 int rtnl_trylock(void)
82 return mutex_trylock(&rtnl_mutex);
84 EXPORT_SYMBOL(rtnl_trylock);
86 int rtnl_is_locked(void)
88 return mutex_is_locked(&rtnl_mutex);
90 EXPORT_SYMBOL(rtnl_is_locked);
92 #ifdef CONFIG_PROVE_LOCKING
93 int lockdep_rtnl_is_held(void)
95 return lockdep_is_held(&rtnl_mutex);
97 EXPORT_SYMBOL(lockdep_rtnl_is_held);
98 #endif /* #ifdef CONFIG_PROVE_LOCKING */
100 static struct rtnl_link *rtnl_msg_handlers[NPROTO];
102 static inline int rtm_msgindex(int msgtype)
104 int msgindex = msgtype - RTM_BASE;
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
111 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
113 return msgindex;
116 static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
118 struct rtnl_link *tab;
120 tab = rtnl_msg_handlers[protocol];
121 if (tab == NULL || tab[msgindex].doit == NULL)
122 tab = rtnl_msg_handlers[PF_UNSPEC];
124 return tab ? tab[msgindex].doit : NULL;
127 static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
129 struct rtnl_link *tab;
131 tab = rtnl_msg_handlers[protocol];
132 if (tab == NULL || tab[msgindex].dumpit == NULL)
133 tab = rtnl_msg_handlers[PF_UNSPEC];
135 return tab ? tab[msgindex].dumpit : NULL;
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
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.
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.
153 * Returns 0 on success or a negative error code.
155 int __rtnl_register(int protocol, int msgtype,
156 rtnl_doit_func doit, rtnl_dumpit_func dumpit)
158 struct rtnl_link *tab;
159 int msgindex;
161 BUG_ON(protocol < 0 || protocol >= NPROTO);
162 msgindex = rtm_msgindex(msgtype);
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;
170 rtnl_msg_handlers[protocol] = tab;
173 if (doit)
174 tab[msgindex].doit = doit;
176 if (dumpit)
177 tab[msgindex].dumpit = dumpit;
179 return 0;
181 EXPORT_SYMBOL_GPL(__rtnl_register);
184 * rtnl_register - Register a rtnetlink message type
186 * Identical to __rtnl_register() but panics on failure. This is useful
187 * as failure of this function is very unlikely, it can only happen due
188 * to lack of memory when allocating the chain to store all message
189 * handlers for a protocol. Meant for use in init functions where lack
190 * of memory implies no sense in continueing.
192 void rtnl_register(int protocol, int msgtype,
193 rtnl_doit_func doit, rtnl_dumpit_func dumpit)
195 if (__rtnl_register(protocol, msgtype, doit, dumpit) < 0)
196 panic("Unable to register rtnetlink message handler, "
197 "protocol = %d, message type = %d\n",
198 protocol, msgtype);
200 EXPORT_SYMBOL_GPL(rtnl_register);
203 * rtnl_unregister - Unregister a rtnetlink message type
204 * @protocol: Protocol family or PF_UNSPEC
205 * @msgtype: rtnetlink message type
207 * Returns 0 on success or a negative error code.
209 int rtnl_unregister(int protocol, int msgtype)
211 int msgindex;
213 BUG_ON(protocol < 0 || protocol >= NPROTO);
214 msgindex = rtm_msgindex(msgtype);
216 if (rtnl_msg_handlers[protocol] == NULL)
217 return -ENOENT;
219 rtnl_msg_handlers[protocol][msgindex].doit = NULL;
220 rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
222 return 0;
224 EXPORT_SYMBOL_GPL(rtnl_unregister);
227 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
228 * @protocol : Protocol family or PF_UNSPEC
230 * Identical to calling rtnl_unregster() for all registered message types
231 * of a certain protocol family.
233 void rtnl_unregister_all(int protocol)
235 BUG_ON(protocol < 0 || protocol >= NPROTO);
237 kfree(rtnl_msg_handlers[protocol]);
238 rtnl_msg_handlers[protocol] = NULL;
240 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
242 static LIST_HEAD(link_ops);
245 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
246 * @ops: struct rtnl_link_ops * to register
248 * The caller must hold the rtnl_mutex. This function should be used
249 * by drivers that create devices during module initialization. It
250 * must be called before registering the devices.
252 * Returns 0 on success or a negative error code.
254 int __rtnl_link_register(struct rtnl_link_ops *ops)
256 if (!ops->dellink)
257 ops->dellink = unregister_netdevice_queue;
259 list_add_tail(&ops->list, &link_ops);
260 return 0;
262 EXPORT_SYMBOL_GPL(__rtnl_link_register);
265 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
266 * @ops: struct rtnl_link_ops * to register
268 * Returns 0 on success or a negative error code.
270 int rtnl_link_register(struct rtnl_link_ops *ops)
272 int err;
274 rtnl_lock();
275 err = __rtnl_link_register(ops);
276 rtnl_unlock();
277 return err;
279 EXPORT_SYMBOL_GPL(rtnl_link_register);
281 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
283 struct net_device *dev;
284 LIST_HEAD(list_kill);
286 for_each_netdev(net, dev) {
287 if (dev->rtnl_link_ops == ops)
288 ops->dellink(dev, &list_kill);
290 unregister_netdevice_many(&list_kill);
293 void rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
295 rtnl_lock();
296 __rtnl_kill_links(net, ops);
297 rtnl_unlock();
299 EXPORT_SYMBOL_GPL(rtnl_kill_links);
302 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
303 * @ops: struct rtnl_link_ops * to unregister
305 * The caller must hold the rtnl_mutex.
307 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
309 struct net *net;
311 for_each_net(net) {
312 __rtnl_kill_links(net, ops);
314 list_del(&ops->list);
316 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
319 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
320 * @ops: struct rtnl_link_ops * to unregister
322 void rtnl_link_unregister(struct rtnl_link_ops *ops)
324 rtnl_lock();
325 __rtnl_link_unregister(ops);
326 rtnl_unlock();
328 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
330 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
332 const struct rtnl_link_ops *ops;
334 list_for_each_entry(ops, &link_ops, list) {
335 if (!strcmp(ops->kind, kind))
336 return ops;
338 return NULL;
341 static size_t rtnl_link_get_size(const struct net_device *dev)
343 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
344 size_t size;
346 if (!ops)
347 return 0;
349 size = nlmsg_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
350 nlmsg_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
352 if (ops->get_size)
353 /* IFLA_INFO_DATA + nested data */
354 size += nlmsg_total_size(sizeof(struct nlattr)) +
355 ops->get_size(dev);
357 if (ops->get_xstats_size)
358 size += ops->get_xstats_size(dev); /* IFLA_INFO_XSTATS */
360 return size;
363 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
365 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
366 struct nlattr *linkinfo, *data;
367 int err = -EMSGSIZE;
369 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
370 if (linkinfo == NULL)
371 goto out;
373 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
374 goto err_cancel_link;
375 if (ops->fill_xstats) {
376 err = ops->fill_xstats(skb, dev);
377 if (err < 0)
378 goto err_cancel_link;
380 if (ops->fill_info) {
381 data = nla_nest_start(skb, IFLA_INFO_DATA);
382 if (data == NULL)
383 goto err_cancel_link;
384 err = ops->fill_info(skb, dev);
385 if (err < 0)
386 goto err_cancel_data;
387 nla_nest_end(skb, data);
390 nla_nest_end(skb, linkinfo);
391 return 0;
393 err_cancel_data:
394 nla_nest_cancel(skb, data);
395 err_cancel_link:
396 nla_nest_cancel(skb, linkinfo);
397 out:
398 return err;
401 static const int rtm_min[RTM_NR_FAMILIES] =
403 [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
404 [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
405 [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)),
406 [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
407 [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
408 [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
409 [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
410 [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)),
411 [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
412 [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
415 static const int rta_max[RTM_NR_FAMILIES] =
417 [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX,
418 [RTM_FAM(RTM_NEWADDR)] = IFA_MAX,
419 [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX,
420 [RTM_FAM(RTM_NEWRULE)] = FRA_MAX,
421 [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX,
422 [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX,
423 [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX,
424 [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX,
427 void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
429 struct rtattr *rta;
430 int size = RTA_LENGTH(attrlen);
432 rta = (struct rtattr *)skb_put(skb, RTA_ALIGN(size));
433 rta->rta_type = attrtype;
434 rta->rta_len = size;
435 memcpy(RTA_DATA(rta), data, attrlen);
436 memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
438 EXPORT_SYMBOL(__rta_fill);
440 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned group, int echo)
442 struct sock *rtnl = net->rtnl;
443 int err = 0;
445 NETLINK_CB(skb).dst_group = group;
446 if (echo)
447 atomic_inc(&skb->users);
448 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
449 if (echo)
450 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
451 return err;
454 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
456 struct sock *rtnl = net->rtnl;
458 return nlmsg_unicast(rtnl, skb, pid);
460 EXPORT_SYMBOL(rtnl_unicast);
462 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
463 struct nlmsghdr *nlh, gfp_t flags)
465 struct sock *rtnl = net->rtnl;
466 int report = 0;
468 if (nlh)
469 report = nlmsg_report(nlh);
471 nlmsg_notify(rtnl, skb, pid, group, report, flags);
473 EXPORT_SYMBOL(rtnl_notify);
475 void rtnl_set_sk_err(struct net *net, u32 group, int error)
477 struct sock *rtnl = net->rtnl;
479 netlink_set_err(rtnl, 0, group, error);
481 EXPORT_SYMBOL(rtnl_set_sk_err);
483 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
485 struct nlattr *mx;
486 int i, valid = 0;
488 mx = nla_nest_start(skb, RTA_METRICS);
489 if (mx == NULL)
490 return -ENOBUFS;
492 for (i = 0; i < RTAX_MAX; i++) {
493 if (metrics[i]) {
494 valid++;
495 NLA_PUT_U32(skb, i+1, metrics[i]);
499 if (!valid) {
500 nla_nest_cancel(skb, mx);
501 return 0;
504 return nla_nest_end(skb, mx);
506 nla_put_failure:
507 nla_nest_cancel(skb, mx);
508 return -EMSGSIZE;
510 EXPORT_SYMBOL(rtnetlink_put_metrics);
512 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
513 u32 ts, u32 tsage, long expires, u32 error)
515 struct rta_cacheinfo ci = {
516 .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
517 .rta_used = dst->__use,
518 .rta_clntref = atomic_read(&(dst->__refcnt)),
519 .rta_error = error,
520 .rta_id = id,
521 .rta_ts = ts,
522 .rta_tsage = tsage,
525 if (expires)
526 ci.rta_expires = jiffies_to_clock_t(expires);
528 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
530 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
532 static void set_operstate(struct net_device *dev, unsigned char transition)
534 unsigned char operstate = dev->operstate;
536 switch (transition) {
537 case IF_OPER_UP:
538 if ((operstate == IF_OPER_DORMANT ||
539 operstate == IF_OPER_UNKNOWN) &&
540 !netif_dormant(dev))
541 operstate = IF_OPER_UP;
542 break;
544 case IF_OPER_DORMANT:
545 if (operstate == IF_OPER_UP ||
546 operstate == IF_OPER_UNKNOWN)
547 operstate = IF_OPER_DORMANT;
548 break;
551 if (dev->operstate != operstate) {
552 write_lock_bh(&dev_base_lock);
553 dev->operstate = operstate;
554 write_unlock_bh(&dev_base_lock);
555 netdev_state_change(dev);
559 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
560 const struct net_device_stats *b)
562 a->rx_packets = b->rx_packets;
563 a->tx_packets = b->tx_packets;
564 a->rx_bytes = b->rx_bytes;
565 a->tx_bytes = b->tx_bytes;
566 a->rx_errors = b->rx_errors;
567 a->tx_errors = b->tx_errors;
568 a->rx_dropped = b->rx_dropped;
569 a->tx_dropped = b->tx_dropped;
571 a->multicast = b->multicast;
572 a->collisions = b->collisions;
574 a->rx_length_errors = b->rx_length_errors;
575 a->rx_over_errors = b->rx_over_errors;
576 a->rx_crc_errors = b->rx_crc_errors;
577 a->rx_frame_errors = b->rx_frame_errors;
578 a->rx_fifo_errors = b->rx_fifo_errors;
579 a->rx_missed_errors = b->rx_missed_errors;
581 a->tx_aborted_errors = b->tx_aborted_errors;
582 a->tx_carrier_errors = b->tx_carrier_errors;
583 a->tx_fifo_errors = b->tx_fifo_errors;
584 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
585 a->tx_window_errors = b->tx_window_errors;
587 a->rx_compressed = b->rx_compressed;
588 a->tx_compressed = b->tx_compressed;
591 static inline size_t if_nlmsg_size(const struct net_device *dev)
593 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
594 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
595 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
596 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
597 + nla_total_size(sizeof(struct rtnl_link_ifmap))
598 + nla_total_size(sizeof(struct rtnl_link_stats))
599 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
600 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
601 + nla_total_size(4) /* IFLA_TXQLEN */
602 + nla_total_size(4) /* IFLA_WEIGHT */
603 + nla_total_size(4) /* IFLA_MTU */
604 + nla_total_size(4) /* IFLA_LINK */
605 + nla_total_size(4) /* IFLA_MASTER */
606 + nla_total_size(1) /* IFLA_OPERSTATE */
607 + nla_total_size(1) /* IFLA_LINKMODE */
608 + rtnl_link_get_size(dev); /* IFLA_LINKINFO */
611 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
612 int type, u32 pid, u32 seq, u32 change,
613 unsigned int flags)
615 struct ifinfomsg *ifm;
616 struct nlmsghdr *nlh;
617 const struct net_device_stats *stats;
618 struct nlattr *attr;
620 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
621 if (nlh == NULL)
622 return -EMSGSIZE;
624 ifm = nlmsg_data(nlh);
625 ifm->ifi_family = AF_UNSPEC;
626 ifm->__ifi_pad = 0;
627 ifm->ifi_type = dev->type;
628 ifm->ifi_index = dev->ifindex;
629 ifm->ifi_flags = dev_get_flags(dev);
630 ifm->ifi_change = change;
632 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
633 NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
634 NLA_PUT_U8(skb, IFLA_OPERSTATE,
635 netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
636 NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
637 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
639 if (dev->ifindex != dev->iflink)
640 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
642 if (dev->master)
643 NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
645 if (dev->qdisc)
646 NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc->ops->id);
648 if (dev->ifalias)
649 NLA_PUT_STRING(skb, IFLA_IFALIAS, dev->ifalias);
651 if (1) {
652 struct rtnl_link_ifmap map = {
653 .mem_start = dev->mem_start,
654 .mem_end = dev->mem_end,
655 .base_addr = dev->base_addr,
656 .irq = dev->irq,
657 .dma = dev->dma,
658 .port = dev->if_port,
660 NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
663 if (dev->addr_len) {
664 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
665 NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
668 attr = nla_reserve(skb, IFLA_STATS,
669 sizeof(struct rtnl_link_stats));
670 if (attr == NULL)
671 goto nla_put_failure;
673 stats = dev_get_stats(dev);
674 copy_rtnl_link_stats(nla_data(attr), stats);
676 if (dev->rtnl_link_ops) {
677 if (rtnl_link_fill(skb, dev) < 0)
678 goto nla_put_failure;
681 return nlmsg_end(skb, nlh);
683 nla_put_failure:
684 nlmsg_cancel(skb, nlh);
685 return -EMSGSIZE;
688 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
690 struct net *net = sock_net(skb->sk);
691 int h, s_h;
692 int idx = 0, s_idx;
693 struct net_device *dev;
694 struct hlist_head *head;
695 struct hlist_node *node;
697 s_h = cb->args[0];
698 s_idx = cb->args[1];
700 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
701 idx = 0;
702 head = &net->dev_index_head[h];
703 hlist_for_each_entry(dev, node, head, index_hlist) {
704 if (idx < s_idx)
705 goto cont;
706 if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
707 NETLINK_CB(cb->skb).pid,
708 cb->nlh->nlmsg_seq, 0,
709 NLM_F_MULTI) <= 0)
710 goto out;
711 cont:
712 idx++;
715 out:
716 cb->args[1] = idx;
717 cb->args[0] = h;
719 return skb->len;
722 const struct nla_policy ifla_policy[IFLA_MAX+1] = {
723 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
724 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
725 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
726 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
727 [IFLA_MTU] = { .type = NLA_U32 },
728 [IFLA_LINK] = { .type = NLA_U32 },
729 [IFLA_TXQLEN] = { .type = NLA_U32 },
730 [IFLA_WEIGHT] = { .type = NLA_U32 },
731 [IFLA_OPERSTATE] = { .type = NLA_U8 },
732 [IFLA_LINKMODE] = { .type = NLA_U8 },
733 [IFLA_LINKINFO] = { .type = NLA_NESTED },
734 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
735 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
737 EXPORT_SYMBOL(ifla_policy);
739 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
740 [IFLA_INFO_KIND] = { .type = NLA_STRING },
741 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
744 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
746 struct net *net;
747 /* Examine the link attributes and figure out which
748 * network namespace we are talking about.
750 if (tb[IFLA_NET_NS_PID])
751 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
752 else
753 net = get_net(src_net);
754 return net;
756 EXPORT_SYMBOL(rtnl_link_get_net);
758 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
760 if (dev) {
761 if (tb[IFLA_ADDRESS] &&
762 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
763 return -EINVAL;
765 if (tb[IFLA_BROADCAST] &&
766 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
767 return -EINVAL;
770 return 0;
773 static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
774 struct nlattr **tb, char *ifname, int modified)
776 const struct net_device_ops *ops = dev->netdev_ops;
777 int send_addr_notify = 0;
778 int err;
780 if (tb[IFLA_NET_NS_PID]) {
781 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
782 if (IS_ERR(net)) {
783 err = PTR_ERR(net);
784 goto errout;
786 err = dev_change_net_namespace(dev, net, ifname);
787 put_net(net);
788 if (err)
789 goto errout;
790 modified = 1;
793 if (tb[IFLA_MAP]) {
794 struct rtnl_link_ifmap *u_map;
795 struct ifmap k_map;
797 if (!ops->ndo_set_config) {
798 err = -EOPNOTSUPP;
799 goto errout;
802 if (!netif_device_present(dev)) {
803 err = -ENODEV;
804 goto errout;
807 u_map = nla_data(tb[IFLA_MAP]);
808 k_map.mem_start = (unsigned long) u_map->mem_start;
809 k_map.mem_end = (unsigned long) u_map->mem_end;
810 k_map.base_addr = (unsigned short) u_map->base_addr;
811 k_map.irq = (unsigned char) u_map->irq;
812 k_map.dma = (unsigned char) u_map->dma;
813 k_map.port = (unsigned char) u_map->port;
815 err = ops->ndo_set_config(dev, &k_map);
816 if (err < 0)
817 goto errout;
819 modified = 1;
822 if (tb[IFLA_ADDRESS]) {
823 struct sockaddr *sa;
824 int len;
826 if (!ops->ndo_set_mac_address) {
827 err = -EOPNOTSUPP;
828 goto errout;
831 if (!netif_device_present(dev)) {
832 err = -ENODEV;
833 goto errout;
836 len = sizeof(sa_family_t) + dev->addr_len;
837 sa = kmalloc(len, GFP_KERNEL);
838 if (!sa) {
839 err = -ENOMEM;
840 goto errout;
842 sa->sa_family = dev->type;
843 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
844 dev->addr_len);
845 err = ops->ndo_set_mac_address(dev, sa);
846 kfree(sa);
847 if (err)
848 goto errout;
849 send_addr_notify = 1;
850 modified = 1;
853 if (tb[IFLA_MTU]) {
854 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
855 if (err < 0)
856 goto errout;
857 modified = 1;
861 * Interface selected by interface index but interface
862 * name provided implies that a name change has been
863 * requested.
865 if (ifm->ifi_index > 0 && ifname[0]) {
866 err = dev_change_name(dev, ifname);
867 if (err < 0)
868 goto errout;
869 modified = 1;
872 if (tb[IFLA_IFALIAS]) {
873 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
874 nla_len(tb[IFLA_IFALIAS]));
875 if (err < 0)
876 goto errout;
877 modified = 1;
880 if (tb[IFLA_BROADCAST]) {
881 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
882 send_addr_notify = 1;
885 if (ifm->ifi_flags || ifm->ifi_change) {
886 unsigned int flags = ifm->ifi_flags;
888 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
889 if (ifm->ifi_change)
890 flags = (flags & ifm->ifi_change) |
891 (dev->flags & ~ifm->ifi_change);
892 err = dev_change_flags(dev, flags);
893 if (err < 0)
894 goto errout;
897 if (tb[IFLA_TXQLEN])
898 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
900 if (tb[IFLA_OPERSTATE])
901 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
903 if (tb[IFLA_LINKMODE]) {
904 write_lock_bh(&dev_base_lock);
905 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
906 write_unlock_bh(&dev_base_lock);
909 err = 0;
911 errout:
912 if (err < 0 && modified && net_ratelimit())
913 printk(KERN_WARNING "A link change request failed with "
914 "some changes comitted already. Interface %s may "
915 "have been left with an inconsistent configuration, "
916 "please check.\n", dev->name);
918 if (send_addr_notify)
919 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
920 return err;
923 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
925 struct net *net = sock_net(skb->sk);
926 struct ifinfomsg *ifm;
927 struct net_device *dev;
928 int err;
929 struct nlattr *tb[IFLA_MAX+1];
930 char ifname[IFNAMSIZ];
932 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
933 if (err < 0)
934 goto errout;
936 if (tb[IFLA_IFNAME])
937 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
938 else
939 ifname[0] = '\0';
941 err = -EINVAL;
942 ifm = nlmsg_data(nlh);
943 if (ifm->ifi_index > 0)
944 dev = __dev_get_by_index(net, ifm->ifi_index);
945 else if (tb[IFLA_IFNAME])
946 dev = __dev_get_by_name(net, ifname);
947 else
948 goto errout;
950 if (dev == NULL) {
951 err = -ENODEV;
952 goto errout;
955 err = validate_linkmsg(dev, tb);
956 if (err < 0)
957 goto errout;
959 err = do_setlink(dev, ifm, tb, ifname, 0);
960 errout:
961 return err;
964 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
966 struct net *net = sock_net(skb->sk);
967 const struct rtnl_link_ops *ops;
968 struct net_device *dev;
969 struct ifinfomsg *ifm;
970 char ifname[IFNAMSIZ];
971 struct nlattr *tb[IFLA_MAX+1];
972 int err;
974 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
975 if (err < 0)
976 return err;
978 if (tb[IFLA_IFNAME])
979 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
981 ifm = nlmsg_data(nlh);
982 if (ifm->ifi_index > 0)
983 dev = __dev_get_by_index(net, ifm->ifi_index);
984 else if (tb[IFLA_IFNAME])
985 dev = __dev_get_by_name(net, ifname);
986 else
987 return -EINVAL;
989 if (!dev)
990 return -ENODEV;
992 ops = dev->rtnl_link_ops;
993 if (!ops)
994 return -EOPNOTSUPP;
996 ops->dellink(dev, NULL);
997 return 0;
1000 struct net_device *rtnl_create_link(struct net *src_net, struct net *net,
1001 char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
1003 int err;
1004 struct net_device *dev;
1005 unsigned int num_queues = 1;
1006 unsigned int real_num_queues = 1;
1008 if (ops->get_tx_queues) {
1009 err = ops->get_tx_queues(src_net, tb, &num_queues,
1010 &real_num_queues);
1011 if (err)
1012 goto err;
1014 err = -ENOMEM;
1015 dev = alloc_netdev_mq(ops->priv_size, ifname, ops->setup, num_queues);
1016 if (!dev)
1017 goto err;
1019 dev_net_set(dev, net);
1020 dev->rtnl_link_ops = ops;
1021 dev->real_num_tx_queues = real_num_queues;
1023 if (strchr(dev->name, '%')) {
1024 err = dev_alloc_name(dev, dev->name);
1025 if (err < 0)
1026 goto err_free;
1029 if (tb[IFLA_MTU])
1030 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
1031 if (tb[IFLA_ADDRESS])
1032 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1033 nla_len(tb[IFLA_ADDRESS]));
1034 if (tb[IFLA_BROADCAST])
1035 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1036 nla_len(tb[IFLA_BROADCAST]));
1037 if (tb[IFLA_TXQLEN])
1038 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1039 if (tb[IFLA_OPERSTATE])
1040 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1041 if (tb[IFLA_LINKMODE])
1042 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1044 return dev;
1046 err_free:
1047 free_netdev(dev);
1048 err:
1049 return ERR_PTR(err);
1051 EXPORT_SYMBOL(rtnl_create_link);
1053 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1055 struct net *net = sock_net(skb->sk);
1056 const struct rtnl_link_ops *ops;
1057 struct net_device *dev;
1058 struct ifinfomsg *ifm;
1059 char kind[MODULE_NAME_LEN];
1060 char ifname[IFNAMSIZ];
1061 struct nlattr *tb[IFLA_MAX+1];
1062 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
1063 int err;
1065 #ifdef CONFIG_MODULES
1066 replay:
1067 #endif
1068 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1069 if (err < 0)
1070 return err;
1072 if (tb[IFLA_IFNAME])
1073 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1074 else
1075 ifname[0] = '\0';
1077 ifm = nlmsg_data(nlh);
1078 if (ifm->ifi_index > 0)
1079 dev = __dev_get_by_index(net, ifm->ifi_index);
1080 else if (ifname[0])
1081 dev = __dev_get_by_name(net, ifname);
1082 else
1083 dev = NULL;
1085 err = validate_linkmsg(dev, tb);
1086 if (err < 0)
1087 return err;
1089 if (tb[IFLA_LINKINFO]) {
1090 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
1091 tb[IFLA_LINKINFO], ifla_info_policy);
1092 if (err < 0)
1093 return err;
1094 } else
1095 memset(linkinfo, 0, sizeof(linkinfo));
1097 if (linkinfo[IFLA_INFO_KIND]) {
1098 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
1099 ops = rtnl_link_ops_get(kind);
1100 } else {
1101 kind[0] = '\0';
1102 ops = NULL;
1105 if (1) {
1106 struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
1107 struct net *dest_net;
1109 if (ops) {
1110 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
1111 err = nla_parse_nested(attr, ops->maxtype,
1112 linkinfo[IFLA_INFO_DATA],
1113 ops->policy);
1114 if (err < 0)
1115 return err;
1116 data = attr;
1118 if (ops->validate) {
1119 err = ops->validate(tb, data);
1120 if (err < 0)
1121 return err;
1125 if (dev) {
1126 int modified = 0;
1128 if (nlh->nlmsg_flags & NLM_F_EXCL)
1129 return -EEXIST;
1130 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1131 return -EOPNOTSUPP;
1133 if (linkinfo[IFLA_INFO_DATA]) {
1134 if (!ops || ops != dev->rtnl_link_ops ||
1135 !ops->changelink)
1136 return -EOPNOTSUPP;
1138 err = ops->changelink(dev, tb, data);
1139 if (err < 0)
1140 return err;
1141 modified = 1;
1144 return do_setlink(dev, ifm, tb, ifname, modified);
1147 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1148 return -ENODEV;
1150 if (ifm->ifi_index || ifm->ifi_flags || ifm->ifi_change)
1151 return -EOPNOTSUPP;
1152 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
1153 return -EOPNOTSUPP;
1155 if (!ops) {
1156 #ifdef CONFIG_MODULES
1157 if (kind[0]) {
1158 __rtnl_unlock();
1159 request_module("rtnl-link-%s", kind);
1160 rtnl_lock();
1161 ops = rtnl_link_ops_get(kind);
1162 if (ops)
1163 goto replay;
1165 #endif
1166 return -EOPNOTSUPP;
1169 if (!ifname[0])
1170 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
1172 dest_net = rtnl_link_get_net(net, tb);
1173 dev = rtnl_create_link(net, dest_net, ifname, ops, tb);
1175 if (IS_ERR(dev))
1176 err = PTR_ERR(dev);
1177 else if (ops->newlink)
1178 err = ops->newlink(net, dev, tb, data);
1179 else
1180 err = register_netdevice(dev);
1181 if (err < 0 && !IS_ERR(dev))
1182 free_netdev(dev);
1184 put_net(dest_net);
1185 return err;
1189 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
1191 struct net *net = sock_net(skb->sk);
1192 struct ifinfomsg *ifm;
1193 char ifname[IFNAMSIZ];
1194 struct nlattr *tb[IFLA_MAX+1];
1195 struct net_device *dev = NULL;
1196 struct sk_buff *nskb;
1197 int err;
1199 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1200 if (err < 0)
1201 return err;
1203 if (tb[IFLA_IFNAME])
1204 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1206 ifm = nlmsg_data(nlh);
1207 if (ifm->ifi_index > 0)
1208 dev = __dev_get_by_index(net, ifm->ifi_index);
1209 else if (tb[IFLA_IFNAME])
1210 dev = __dev_get_by_name(net, ifname);
1211 else
1212 return -EINVAL;
1214 if (dev == NULL)
1215 return -ENODEV;
1217 nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
1218 if (nskb == NULL)
1219 return -ENOBUFS;
1221 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
1222 nlh->nlmsg_seq, 0, 0);
1223 if (err < 0) {
1224 /* -EMSGSIZE implies BUG in if_nlmsg_size */
1225 WARN_ON(err == -EMSGSIZE);
1226 kfree_skb(nskb);
1227 } else
1228 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid);
1230 return err;
1233 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
1235 int idx;
1236 int s_idx = cb->family;
1238 if (s_idx == 0)
1239 s_idx = 1;
1240 for (idx = 1; idx < NPROTO; idx++) {
1241 int type = cb->nlh->nlmsg_type-RTM_BASE;
1242 if (idx < s_idx || idx == PF_PACKET)
1243 continue;
1244 if (rtnl_msg_handlers[idx] == NULL ||
1245 rtnl_msg_handlers[idx][type].dumpit == NULL)
1246 continue;
1247 if (idx > s_idx)
1248 memset(&cb->args[0], 0, sizeof(cb->args));
1249 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
1250 break;
1252 cb->family = idx;
1254 return skb->len;
1257 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
1259 struct net *net = dev_net(dev);
1260 struct sk_buff *skb;
1261 int err = -ENOBUFS;
1263 skb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
1264 if (skb == NULL)
1265 goto errout;
1267 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0);
1268 if (err < 0) {
1269 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
1270 WARN_ON(err == -EMSGSIZE);
1271 kfree_skb(skb);
1272 goto errout;
1274 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
1275 return;
1276 errout:
1277 if (err < 0)
1278 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
1281 /* Protected by RTNL sempahore. */
1282 static struct rtattr **rta_buf;
1283 static int rtattr_max;
1285 /* Process one rtnetlink message. */
1287 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1289 struct net *net = sock_net(skb->sk);
1290 rtnl_doit_func doit;
1291 int sz_idx, kind;
1292 int min_len;
1293 int family;
1294 int type;
1295 int err;
1297 type = nlh->nlmsg_type;
1298 if (type > RTM_MAX)
1299 return -EOPNOTSUPP;
1301 type -= RTM_BASE;
1303 /* All the messages must have at least 1 byte length */
1304 if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
1305 return 0;
1307 family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family;
1308 if (family >= NPROTO)
1309 return -EAFNOSUPPORT;
1311 sz_idx = type>>2;
1312 kind = type&3;
1314 if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN))
1315 return -EPERM;
1317 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
1318 struct sock *rtnl;
1319 rtnl_dumpit_func dumpit;
1321 dumpit = rtnl_get_dumpit(family, type);
1322 if (dumpit == NULL)
1323 return -EOPNOTSUPP;
1325 __rtnl_unlock();
1326 rtnl = net->rtnl;
1327 err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL);
1328 rtnl_lock();
1329 return err;
1332 memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
1334 min_len = rtm_min[sz_idx];
1335 if (nlh->nlmsg_len < min_len)
1336 return -EINVAL;
1338 if (nlh->nlmsg_len > min_len) {
1339 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1340 struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len);
1342 while (RTA_OK(attr, attrlen)) {
1343 unsigned flavor = attr->rta_type;
1344 if (flavor) {
1345 if (flavor > rta_max[sz_idx])
1346 return -EINVAL;
1347 rta_buf[flavor-1] = attr;
1349 attr = RTA_NEXT(attr, attrlen);
1353 doit = rtnl_get_doit(family, type);
1354 if (doit == NULL)
1355 return -EOPNOTSUPP;
1357 return doit(skb, nlh, (void *)&rta_buf[0]);
1360 static void rtnetlink_rcv(struct sk_buff *skb)
1362 rtnl_lock();
1363 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
1364 rtnl_unlock();
1367 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
1369 struct net_device *dev = ptr;
1371 switch (event) {
1372 case NETDEV_UNREGISTER:
1373 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
1374 break;
1375 case NETDEV_UP:
1376 case NETDEV_DOWN:
1377 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
1378 break;
1379 case NETDEV_POST_INIT:
1380 case NETDEV_REGISTER:
1381 case NETDEV_CHANGE:
1382 case NETDEV_GOING_DOWN:
1383 case NETDEV_UNREGISTER_BATCH:
1384 break;
1385 default:
1386 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
1387 break;
1389 return NOTIFY_DONE;
1392 static struct notifier_block rtnetlink_dev_notifier = {
1393 .notifier_call = rtnetlink_event,
1397 static int rtnetlink_net_init(struct net *net)
1399 struct sock *sk;
1400 sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX,
1401 rtnetlink_rcv, &rtnl_mutex, THIS_MODULE);
1402 if (!sk)
1403 return -ENOMEM;
1404 net->rtnl = sk;
1405 return 0;
1408 static void rtnetlink_net_exit(struct net *net)
1410 netlink_kernel_release(net->rtnl);
1411 net->rtnl = NULL;
1414 static struct pernet_operations rtnetlink_net_ops = {
1415 .init = rtnetlink_net_init,
1416 .exit = rtnetlink_net_exit,
1419 void __init rtnetlink_init(void)
1421 int i;
1423 rtattr_max = 0;
1424 for (i = 0; i < ARRAY_SIZE(rta_max); i++)
1425 if (rta_max[i] > rtattr_max)
1426 rtattr_max = rta_max[i];
1427 rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
1428 if (!rta_buf)
1429 panic("rtnetlink_init: cannot allocate rta_buf\n");
1431 if (register_pernet_subsys(&rtnetlink_net_ops))
1432 panic("rtnetlink_init: cannot initialize rtnetlink\n");
1434 netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
1435 register_netdevice_notifier(&rtnetlink_dev_notifier);
1437 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo);
1438 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL);
1439 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL);
1440 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL);
1442 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all);
1443 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all);