]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/ipv6/addrconf.c
Linux-2.6.12-rc2
[net-next-2.6.git] / net / ipv6 / addrconf.c
CommitLineData
1da177e4
LT
1/*
2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
4 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 *
9 * $Id: addrconf.c,v 1.69 2001/10/31 21:55:54 davem Exp $
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 */
16
17/*
18 * Changes:
19 *
20 * Janos Farkas : delete timer on ifdown
21 * <chexum@bankinf.banki.hu>
22 * Andi Kleen : kill double kfree on module
23 * unload.
24 * Maciej W. Rozycki : FDDI support
25 * sekiya@USAGI : Don't send too many RS
26 * packets.
27 * yoshfuji@USAGI : Fixed interval between DAD
28 * packets.
29 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
30 * address validation timer.
31 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
32 * support.
33 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
34 * address on a same interface.
35 * YOSHIFUJI Hideaki @USAGI : ARCnet support
36 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
37 * seq_file.
38 */
39
40#include <linux/config.h>
41#include <linux/errno.h>
42#include <linux/types.h>
43#include <linux/socket.h>
44#include <linux/sockios.h>
45#include <linux/sched.h>
46#include <linux/net.h>
47#include <linux/in6.h>
48#include <linux/netdevice.h>
49#include <linux/if_arp.h>
50#include <linux/if_arcnet.h>
51#include <linux/if_infiniband.h>
52#include <linux/route.h>
53#include <linux/inetdevice.h>
54#include <linux/init.h>
55#ifdef CONFIG_SYSCTL
56#include <linux/sysctl.h>
57#endif
58#include <linux/delay.h>
59#include <linux/notifier.h>
60
61#include <net/sock.h>
62#include <net/snmp.h>
63
64#include <net/ipv6.h>
65#include <net/protocol.h>
66#include <net/ndisc.h>
67#include <net/ip6_route.h>
68#include <net/addrconf.h>
69#include <net/tcp.h>
70#include <net/ip.h>
71#include <linux/if_tunnel.h>
72#include <linux/rtnetlink.h>
73
74#ifdef CONFIG_IPV6_PRIVACY
75#include <linux/random.h>
76#include <linux/crypto.h>
77#include <asm/scatterlist.h>
78#endif
79
80#include <asm/uaccess.h>
81
82#include <linux/proc_fs.h>
83#include <linux/seq_file.h>
84
85/* Set to 3 to get tracing... */
86#define ACONF_DEBUG 2
87
88#if ACONF_DEBUG >= 3
89#define ADBG(x) printk x
90#else
91#define ADBG(x)
92#endif
93
94#define INFINITY_LIFE_TIME 0xFFFFFFFF
95#define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
96
97#ifdef CONFIG_SYSCTL
98static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
99static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
100#endif
101
102#ifdef CONFIG_IPV6_PRIVACY
103static int __ipv6_regen_rndid(struct inet6_dev *idev);
104static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
105static void ipv6_regen_rndid(unsigned long data);
106
107static int desync_factor = MAX_DESYNC_FACTOR * HZ;
108static struct crypto_tfm *md5_tfm;
109static DEFINE_SPINLOCK(md5_tfm_lock);
110#endif
111
112static int ipv6_count_addresses(struct inet6_dev *idev);
113
114/*
115 * Configured unicast address hash table
116 */
117static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];
118static DEFINE_RWLOCK(addrconf_hash_lock);
119
120/* Protects inet6 devices */
121DEFINE_RWLOCK(addrconf_lock);
122
123static void addrconf_verify(unsigned long);
124
125static struct timer_list addr_chk_timer =
126 TIMER_INITIALIZER(addrconf_verify, 0, 0);
127static DEFINE_SPINLOCK(addrconf_verify_lock);
128
129static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
130static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
131
132static int addrconf_ifdown(struct net_device *dev, int how);
133
134static void addrconf_dad_start(struct inet6_ifaddr *ifp, int flags);
135static void addrconf_dad_timer(unsigned long data);
136static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
137static void addrconf_rs_timer(unsigned long data);
138static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
139static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
140
141static void inet6_prefix_notify(int event, struct inet6_dev *idev,
142 struct prefix_info *pinfo);
143static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
144
145static struct notifier_block *inet6addr_chain;
146
147struct ipv6_devconf ipv6_devconf = {
148 .forwarding = 0,
149 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
150 .mtu6 = IPV6_MIN_MTU,
151 .accept_ra = 1,
152 .accept_redirects = 1,
153 .autoconf = 1,
154 .force_mld_version = 0,
155 .dad_transmits = 1,
156 .rtr_solicits = MAX_RTR_SOLICITATIONS,
157 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
158 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
159#ifdef CONFIG_IPV6_PRIVACY
160 .use_tempaddr = 0,
161 .temp_valid_lft = TEMP_VALID_LIFETIME,
162 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
163 .regen_max_retry = REGEN_MAX_RETRY,
164 .max_desync_factor = MAX_DESYNC_FACTOR,
165#endif
166 .max_addresses = IPV6_MAX_ADDRESSES,
167};
168
169static struct ipv6_devconf ipv6_devconf_dflt = {
170 .forwarding = 0,
171 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
172 .mtu6 = IPV6_MIN_MTU,
173 .accept_ra = 1,
174 .accept_redirects = 1,
175 .autoconf = 1,
176 .dad_transmits = 1,
177 .rtr_solicits = MAX_RTR_SOLICITATIONS,
178 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
179 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
180#ifdef CONFIG_IPV6_PRIVACY
181 .use_tempaddr = 0,
182 .temp_valid_lft = TEMP_VALID_LIFETIME,
183 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
184 .regen_max_retry = REGEN_MAX_RETRY,
185 .max_desync_factor = MAX_DESYNC_FACTOR,
186#endif
187 .max_addresses = IPV6_MAX_ADDRESSES,
188};
189
190/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
191#if 0
192const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
193#endif
194const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
195
196int ipv6_addr_type(const struct in6_addr *addr)
197{
198 int type;
199 u32 st;
200
201 st = addr->s6_addr32[0];
202
203 if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
204 type = IPV6_ADDR_MULTICAST;
205
206 switch((st & htonl(0x00FF0000))) {
207 case __constant_htonl(0x00010000):
208 type |= IPV6_ADDR_LOOPBACK;
209 break;
210
211 case __constant_htonl(0x00020000):
212 type |= IPV6_ADDR_LINKLOCAL;
213 break;
214
215 case __constant_htonl(0x00050000):
216 type |= IPV6_ADDR_SITELOCAL;
217 break;
218 };
219 return type;
220 }
221
222 type = IPV6_ADDR_UNICAST;
223
224 /* Consider all addresses with the first three bits different of
225 000 and 111 as finished.
226 */
227 if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
228 (st & htonl(0xE0000000)) != htonl(0xE0000000))
229 return type;
230
231 if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
232 return (IPV6_ADDR_LINKLOCAL | type);
233
234 if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
235 return (IPV6_ADDR_SITELOCAL | type);
236
237 if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
238 if (addr->s6_addr32[2] == 0) {
239 if (addr->s6_addr32[3] == 0)
240 return IPV6_ADDR_ANY;
241
242 if (addr->s6_addr32[3] == htonl(0x00000001))
243 return (IPV6_ADDR_LOOPBACK | type);
244
245 return (IPV6_ADDR_COMPATv4 | type);
246 }
247
248 if (addr->s6_addr32[2] == htonl(0x0000ffff))
249 return IPV6_ADDR_MAPPED;
250 }
251
252 st &= htonl(0xFF000000);
253 if (st == 0)
254 return IPV6_ADDR_RESERVED;
255 st &= htonl(0xFE000000);
256 if (st == htonl(0x02000000))
257 return IPV6_ADDR_RESERVED; /* for NSAP */
258 if (st == htonl(0x04000000))
259 return IPV6_ADDR_RESERVED; /* for IPX */
260 return type;
261}
262
263static void addrconf_del_timer(struct inet6_ifaddr *ifp)
264{
265 if (del_timer(&ifp->timer))
266 __in6_ifa_put(ifp);
267}
268
269enum addrconf_timer_t
270{
271 AC_NONE,
272 AC_DAD,
273 AC_RS,
274};
275
276static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
277 enum addrconf_timer_t what,
278 unsigned long when)
279{
280 if (!del_timer(&ifp->timer))
281 in6_ifa_hold(ifp);
282
283 switch (what) {
284 case AC_DAD:
285 ifp->timer.function = addrconf_dad_timer;
286 break;
287 case AC_RS:
288 ifp->timer.function = addrconf_rs_timer;
289 break;
290 default:;
291 }
292 ifp->timer.expires = jiffies + when;
293 add_timer(&ifp->timer);
294}
295
296/* Nobody refers to this device, we may destroy it. */
297
298void in6_dev_finish_destroy(struct inet6_dev *idev)
299{
300 struct net_device *dev = idev->dev;
301 BUG_TRAP(idev->addr_list==NULL);
302 BUG_TRAP(idev->mc_list==NULL);
303#ifdef NET_REFCNT_DEBUG
304 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
305#endif
306 dev_put(dev);
307 if (!idev->dead) {
308 printk("Freeing alive inet6 device %p\n", idev);
309 return;
310 }
311 snmp6_free_dev(idev);
312 kfree(idev);
313}
314
315static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
316{
317 struct inet6_dev *ndev;
318
319 ASSERT_RTNL();
320
321 if (dev->mtu < IPV6_MIN_MTU)
322 return NULL;
323
324 ndev = kmalloc(sizeof(struct inet6_dev), GFP_KERNEL);
325
326 if (ndev) {
327 memset(ndev, 0, sizeof(struct inet6_dev));
328
329 rwlock_init(&ndev->lock);
330 ndev->dev = dev;
331 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
332 ndev->cnf.mtu6 = dev->mtu;
333 ndev->cnf.sysctl = NULL;
334 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
335 if (ndev->nd_parms == NULL) {
336 kfree(ndev);
337 return NULL;
338 }
339 /* We refer to the device */
340 dev_hold(dev);
341
342 if (snmp6_alloc_dev(ndev) < 0) {
343 ADBG((KERN_WARNING
344 "%s(): cannot allocate memory for statistics; dev=%s.\n",
345 __FUNCTION__, dev->name));
346 neigh_parms_release(&nd_tbl, ndev->nd_parms);
347 ndev->dead = 1;
348 in6_dev_finish_destroy(ndev);
349 return NULL;
350 }
351
352 if (snmp6_register_dev(ndev) < 0) {
353 ADBG((KERN_WARNING
354 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
355 __FUNCTION__, dev->name));
356 neigh_parms_release(&nd_tbl, ndev->nd_parms);
357 ndev->dead = 1;
358 in6_dev_finish_destroy(ndev);
359 return NULL;
360 }
361
362 /* One reference from device. We must do this before
363 * we invoke __ipv6_regen_rndid().
364 */
365 in6_dev_hold(ndev);
366
367#ifdef CONFIG_IPV6_PRIVACY
368 get_random_bytes(ndev->rndid, sizeof(ndev->rndid));
369 get_random_bytes(ndev->entropy, sizeof(ndev->entropy));
370 init_timer(&ndev->regen_timer);
371 ndev->regen_timer.function = ipv6_regen_rndid;
372 ndev->regen_timer.data = (unsigned long) ndev;
373 if ((dev->flags&IFF_LOOPBACK) ||
374 dev->type == ARPHRD_TUNNEL ||
375 dev->type == ARPHRD_SIT) {
376 printk(KERN_INFO
377 "Disabled Privacy Extensions on device %p(%s)\n",
378 dev, dev->name);
379 ndev->cnf.use_tempaddr = -1;
380 } else {
381 in6_dev_hold(ndev);
382 ipv6_regen_rndid((unsigned long) ndev);
383 }
384#endif
385
386 write_lock_bh(&addrconf_lock);
387 dev->ip6_ptr = ndev;
388 write_unlock_bh(&addrconf_lock);
389
390 ipv6_mc_init_dev(ndev);
391 ndev->tstamp = jiffies;
392#ifdef CONFIG_SYSCTL
393 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
394 NET_IPV6_NEIGH, "ipv6",
395 &ndisc_ifinfo_sysctl_change,
396 NULL);
397 addrconf_sysctl_register(ndev, &ndev->cnf);
398#endif
399 }
400 return ndev;
401}
402
403static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
404{
405 struct inet6_dev *idev;
406
407 ASSERT_RTNL();
408
409 if ((idev = __in6_dev_get(dev)) == NULL) {
410 if ((idev = ipv6_add_dev(dev)) == NULL)
411 return NULL;
412 }
413 if (dev->flags&IFF_UP)
414 ipv6_mc_up(idev);
415 return idev;
416}
417
418#ifdef CONFIG_SYSCTL
419static void dev_forward_change(struct inet6_dev *idev)
420{
421 struct net_device *dev;
422 struct inet6_ifaddr *ifa;
423 struct in6_addr addr;
424
425 if (!idev)
426 return;
427 dev = idev->dev;
428 if (dev && (dev->flags & IFF_MULTICAST)) {
429 ipv6_addr_all_routers(&addr);
430
431 if (idev->cnf.forwarding)
432 ipv6_dev_mc_inc(dev, &addr);
433 else
434 ipv6_dev_mc_dec(dev, &addr);
435 }
436 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
437 if (idev->cnf.forwarding)
438 addrconf_join_anycast(ifa);
439 else
440 addrconf_leave_anycast(ifa);
441 }
442}
443
444
445static void addrconf_forward_change(void)
446{
447 struct net_device *dev;
448 struct inet6_dev *idev;
449
450 read_lock(&dev_base_lock);
451 for (dev=dev_base; dev; dev=dev->next) {
452 read_lock(&addrconf_lock);
453 idev = __in6_dev_get(dev);
454 if (idev) {
455 int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
456 idev->cnf.forwarding = ipv6_devconf.forwarding;
457 if (changed)
458 dev_forward_change(idev);
459 }
460 read_unlock(&addrconf_lock);
461 }
462 read_unlock(&dev_base_lock);
463}
464#endif
465
466/* Nobody refers to this ifaddr, destroy it */
467
468void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
469{
470 BUG_TRAP(ifp->if_next==NULL);
471 BUG_TRAP(ifp->lst_next==NULL);
472#ifdef NET_REFCNT_DEBUG
473 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
474#endif
475
476 in6_dev_put(ifp->idev);
477
478 if (del_timer(&ifp->timer))
479 printk("Timer is still running, when freeing ifa=%p\n", ifp);
480
481 if (!ifp->dead) {
482 printk("Freeing alive inet6 address %p\n", ifp);
483 return;
484 }
485 dst_release(&ifp->rt->u.dst);
486
487 kfree(ifp);
488}
489
490/* On success it returns ifp with increased reference count */
491
492static struct inet6_ifaddr *
493ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
494 int scope, unsigned flags)
495{
496 struct inet6_ifaddr *ifa = NULL;
497 struct rt6_info *rt;
498 int hash;
499 int err = 0;
500
501 read_lock_bh(&addrconf_lock);
502 if (idev->dead) {
503 err = -ENODEV; /*XXX*/
504 goto out2;
505 }
506
507 write_lock(&addrconf_hash_lock);
508
509 /* Ignore adding duplicate addresses on an interface */
510 if (ipv6_chk_same_addr(addr, idev->dev)) {
511 ADBG(("ipv6_add_addr: already assigned\n"));
512 err = -EEXIST;
513 goto out;
514 }
515
516 ifa = kmalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
517
518 if (ifa == NULL) {
519 ADBG(("ipv6_add_addr: malloc failed\n"));
520 err = -ENOBUFS;
521 goto out;
522 }
523
524 rt = addrconf_dst_alloc(idev, addr, 0);
525 if (IS_ERR(rt)) {
526 err = PTR_ERR(rt);
527 goto out;
528 }
529
530 memset(ifa, 0, sizeof(struct inet6_ifaddr));
531 ipv6_addr_copy(&ifa->addr, addr);
532
533 spin_lock_init(&ifa->lock);
534 init_timer(&ifa->timer);
535 ifa->timer.data = (unsigned long) ifa;
536 ifa->scope = scope;
537 ifa->prefix_len = pfxlen;
538 ifa->flags = flags | IFA_F_TENTATIVE;
539 ifa->cstamp = ifa->tstamp = jiffies;
540
541 ifa->idev = idev;
542 in6_dev_hold(idev);
543 /* For caller */
544 in6_ifa_hold(ifa);
545
546 /* Add to big hash table */
547 hash = ipv6_addr_hash(addr);
548
549 ifa->lst_next = inet6_addr_lst[hash];
550 inet6_addr_lst[hash] = ifa;
551 in6_ifa_hold(ifa);
552 write_unlock(&addrconf_hash_lock);
553
554 write_lock(&idev->lock);
555 /* Add to inet6_dev unicast addr list. */
556 ifa->if_next = idev->addr_list;
557 idev->addr_list = ifa;
558
559#ifdef CONFIG_IPV6_PRIVACY
560 if (ifa->flags&IFA_F_TEMPORARY) {
561 ifa->tmp_next = idev->tempaddr_list;
562 idev->tempaddr_list = ifa;
563 in6_ifa_hold(ifa);
564 }
565#endif
566
567 ifa->rt = rt;
568
569 in6_ifa_hold(ifa);
570 write_unlock(&idev->lock);
571out2:
572 read_unlock_bh(&addrconf_lock);
573
574 if (unlikely(err == 0))
575 notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
576 else {
577 kfree(ifa);
578 ifa = ERR_PTR(err);
579 }
580
581 return ifa;
582out:
583 write_unlock(&addrconf_hash_lock);
584 goto out2;
585}
586
587/* This function wants to get referenced ifp and releases it before return */
588
589static void ipv6_del_addr(struct inet6_ifaddr *ifp)
590{
591 struct inet6_ifaddr *ifa, **ifap;
592 struct inet6_dev *idev = ifp->idev;
593 int hash;
594 int deleted = 0, onlink = 0;
595 unsigned long expires = jiffies;
596
597 hash = ipv6_addr_hash(&ifp->addr);
598
599 ifp->dead = 1;
600
601 write_lock_bh(&addrconf_hash_lock);
602 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
603 ifap = &ifa->lst_next) {
604 if (ifa == ifp) {
605 *ifap = ifa->lst_next;
606 __in6_ifa_put(ifp);
607 ifa->lst_next = NULL;
608 break;
609 }
610 }
611 write_unlock_bh(&addrconf_hash_lock);
612
613 write_lock_bh(&idev->lock);
614#ifdef CONFIG_IPV6_PRIVACY
615 if (ifp->flags&IFA_F_TEMPORARY) {
616 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
617 ifap = &ifa->tmp_next) {
618 if (ifa == ifp) {
619 *ifap = ifa->tmp_next;
620 if (ifp->ifpub) {
621 in6_ifa_put(ifp->ifpub);
622 ifp->ifpub = NULL;
623 }
624 __in6_ifa_put(ifp);
625 ifa->tmp_next = NULL;
626 break;
627 }
628 }
629 }
630#endif
631
632 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;
633 ifap = &ifa->if_next) {
634 if (ifa == ifp) {
635 *ifap = ifa->if_next;
636 __in6_ifa_put(ifp);
637 ifa->if_next = NULL;
638 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
639 break;
640 deleted = 1;
641 } else if (ifp->flags & IFA_F_PERMANENT) {
642 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
643 ifp->prefix_len)) {
644 if (ifa->flags & IFA_F_PERMANENT) {
645 onlink = 1;
646 if (deleted)
647 break;
648 } else {
649 unsigned long lifetime;
650
651 if (!onlink)
652 onlink = -1;
653
654 spin_lock(&ifa->lock);
655 lifetime = min_t(unsigned long,
656 ifa->valid_lft, 0x7fffffffUL/HZ);
657 if (time_before(expires,
658 ifa->tstamp + lifetime * HZ))
659 expires = ifa->tstamp + lifetime * HZ;
660 spin_unlock(&ifa->lock);
661 }
662 }
663 }
664 }
665 write_unlock_bh(&idev->lock);
666
667 ipv6_ifa_notify(RTM_DELADDR, ifp);
668
669 notifier_call_chain(&inet6addr_chain,NETDEV_DOWN,ifp);
670
671 addrconf_del_timer(ifp);
672
673 /*
674 * Purge or update corresponding prefix
675 *
676 * 1) we don't purge prefix here if address was not permanent.
677 * prefix is managed by its own lifetime.
678 * 2) if there're no addresses, delete prefix.
679 * 3) if there're still other permanent address(es),
680 * corresponding prefix is still permanent.
681 * 4) otherwise, update prefix lifetime to the
682 * longest valid lifetime among the corresponding
683 * addresses on the device.
684 * Note: subsequent RA will update lifetime.
685 *
686 * --yoshfuji
687 */
688 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
689 struct in6_addr prefix;
690 struct rt6_info *rt;
691
692 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
693 rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
694
695 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
696 if (onlink == 0) {
697 ip6_del_rt(rt, NULL, NULL);
698 rt = NULL;
699 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
700 rt->rt6i_expires = expires;
701 rt->rt6i_flags |= RTF_EXPIRES;
702 }
703 }
704 dst_release(&rt->u.dst);
705 }
706
707 in6_ifa_put(ifp);
708}
709
710#ifdef CONFIG_IPV6_PRIVACY
711static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
712{
713 struct inet6_dev *idev = ifp->idev;
714 struct in6_addr addr, *tmpaddr;
715 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
716 int tmp_plen;
717 int ret = 0;
718 int max_addresses;
719
720 write_lock(&idev->lock);
721 if (ift) {
722 spin_lock_bh(&ift->lock);
723 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
724 spin_unlock_bh(&ift->lock);
725 tmpaddr = &addr;
726 } else {
727 tmpaddr = NULL;
728 }
729retry:
730 in6_dev_hold(idev);
731 if (idev->cnf.use_tempaddr <= 0) {
732 write_unlock(&idev->lock);
733 printk(KERN_INFO
734 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
735 in6_dev_put(idev);
736 ret = -1;
737 goto out;
738 }
739 spin_lock_bh(&ifp->lock);
740 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
741 idev->cnf.use_tempaddr = -1; /*XXX*/
742 spin_unlock_bh(&ifp->lock);
743 write_unlock(&idev->lock);
744 printk(KERN_WARNING
745 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
746 in6_dev_put(idev);
747 ret = -1;
748 goto out;
749 }
750 in6_ifa_hold(ifp);
751 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
752 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
753 spin_unlock_bh(&ifp->lock);
754 write_unlock(&idev->lock);
755 printk(KERN_WARNING
756 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
757 in6_ifa_put(ifp);
758 in6_dev_put(idev);
759 ret = -1;
760 goto out;
761 }
762 memcpy(&addr.s6_addr[8], idev->rndid, 8);
763 tmp_valid_lft = min_t(__u32,
764 ifp->valid_lft,
765 idev->cnf.temp_valid_lft);
766 tmp_prefered_lft = min_t(__u32,
767 ifp->prefered_lft,
768 idev->cnf.temp_prefered_lft - desync_factor / HZ);
769 tmp_plen = ifp->prefix_len;
770 max_addresses = idev->cnf.max_addresses;
771 tmp_cstamp = ifp->cstamp;
772 tmp_tstamp = ifp->tstamp;
773 spin_unlock_bh(&ifp->lock);
774
775 write_unlock(&idev->lock);
776 ift = !max_addresses ||
777 ipv6_count_addresses(idev) < max_addresses ?
778 ipv6_add_addr(idev, &addr, tmp_plen,
779 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : NULL;
780 if (!ift || IS_ERR(ift)) {
781 in6_ifa_put(ifp);
782 in6_dev_put(idev);
783 printk(KERN_INFO
784 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
785 tmpaddr = &addr;
786 write_lock(&idev->lock);
787 goto retry;
788 }
789
790 spin_lock_bh(&ift->lock);
791 ift->ifpub = ifp;
792 ift->valid_lft = tmp_valid_lft;
793 ift->prefered_lft = tmp_prefered_lft;
794 ift->cstamp = tmp_cstamp;
795 ift->tstamp = tmp_tstamp;
796 spin_unlock_bh(&ift->lock);
797
798 addrconf_dad_start(ift, 0);
799 in6_ifa_put(ift);
800 in6_dev_put(idev);
801out:
802 return ret;
803}
804#endif
805
806/*
807 * Choose an appropriate source address
808 * should do:
809 * i) get an address with an appropriate scope
810 * ii) see if there is a specific route for the destination and use
811 * an address of the attached interface
812 * iii) don't use deprecated addresses
813 */
814static int inline ipv6_saddr_pref(const struct inet6_ifaddr *ifp, u8 invpref)
815{
816 int pref;
817 pref = ifp->flags&IFA_F_DEPRECATED ? 0 : 2;
818#ifdef CONFIG_IPV6_PRIVACY
819 pref |= (ifp->flags^invpref)&IFA_F_TEMPORARY ? 0 : 1;
820#endif
821 return pref;
822}
823
824#ifdef CONFIG_IPV6_PRIVACY
825#define IPV6_GET_SADDR_MAXSCORE(score) ((score) == 3)
826#else
827#define IPV6_GET_SADDR_MAXSCORE(score) (score)
828#endif
829
830int ipv6_dev_get_saddr(struct net_device *dev,
831 struct in6_addr *daddr, struct in6_addr *saddr)
832{
833 struct inet6_ifaddr *ifp = NULL;
834 struct inet6_ifaddr *match = NULL;
835 struct inet6_dev *idev;
836 int scope;
837 int err;
838 int hiscore = -1, score;
839
840 scope = ipv6_addr_scope(daddr);
841
842 /*
843 * known dev
844 * search dev and walk through dev addresses
845 */
846
847 if (dev) {
848 if (dev->flags & IFF_LOOPBACK)
849 scope = IFA_HOST;
850
851 read_lock(&addrconf_lock);
852 idev = __in6_dev_get(dev);
853 if (idev) {
854 read_lock_bh(&idev->lock);
855 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
856 if (ifp->scope == scope) {
857 if (ifp->flags&IFA_F_TENTATIVE)
858 continue;
859#ifdef CONFIG_IPV6_PRIVACY
860 score = ipv6_saddr_pref(ifp, idev->cnf.use_tempaddr > 1 ? IFA_F_TEMPORARY : 0);
861#else
862 score = ipv6_saddr_pref(ifp, 0);
863#endif
864 if (score <= hiscore)
865 continue;
866
867 if (match)
868 in6_ifa_put(match);
869 match = ifp;
870 hiscore = score;
871 in6_ifa_hold(ifp);
872
873 if (IPV6_GET_SADDR_MAXSCORE(score)) {
874 read_unlock_bh(&idev->lock);
875 read_unlock(&addrconf_lock);
876 goto out;
877 }
878 }
879 }
880 read_unlock_bh(&idev->lock);
881 }
882 read_unlock(&addrconf_lock);
883 }
884
885 if (scope == IFA_LINK)
886 goto out;
887
888 /*
889 * dev == NULL or search failed for specified dev
890 */
891
892 read_lock(&dev_base_lock);
893 read_lock(&addrconf_lock);
894 for (dev = dev_base; dev; dev=dev->next) {
895 idev = __in6_dev_get(dev);
896 if (idev) {
897 read_lock_bh(&idev->lock);
898 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
899 if (ifp->scope == scope) {
900 if (ifp->flags&IFA_F_TENTATIVE)
901 continue;
902#ifdef CONFIG_IPV6_PRIVACY
903 score = ipv6_saddr_pref(ifp, idev->cnf.use_tempaddr > 1 ? IFA_F_TEMPORARY : 0);
904#else
905 score = ipv6_saddr_pref(ifp, 0);
906#endif
907 if (score <= hiscore)
908 continue;
909
910 if (match)
911 in6_ifa_put(match);
912 match = ifp;
913 hiscore = score;
914 in6_ifa_hold(ifp);
915
916 if (IPV6_GET_SADDR_MAXSCORE(score)) {
917 read_unlock_bh(&idev->lock);
918 goto out_unlock_base;
919 }
920 }
921 }
922 read_unlock_bh(&idev->lock);
923 }
924 }
925
926out_unlock_base:
927 read_unlock(&addrconf_lock);
928 read_unlock(&dev_base_lock);
929
930out:
931 err = -EADDRNOTAVAIL;
932 if (match) {
933 ipv6_addr_copy(saddr, &match->addr);
934 err = 0;
935 in6_ifa_put(match);
936 }
937
938 return err;
939}
940
941
942int ipv6_get_saddr(struct dst_entry *dst,
943 struct in6_addr *daddr, struct in6_addr *saddr)
944{
945 return ipv6_dev_get_saddr(dst ? ((struct rt6_info *)dst)->rt6i_idev->dev : NULL, daddr, saddr);
946}
947
948
949int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
950{
951 struct inet6_dev *idev;
952 int err = -EADDRNOTAVAIL;
953
954 read_lock(&addrconf_lock);
955 if ((idev = __in6_dev_get(dev)) != NULL) {
956 struct inet6_ifaddr *ifp;
957
958 read_lock_bh(&idev->lock);
959 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
960 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
961 ipv6_addr_copy(addr, &ifp->addr);
962 err = 0;
963 break;
964 }
965 }
966 read_unlock_bh(&idev->lock);
967 }
968 read_unlock(&addrconf_lock);
969 return err;
970}
971
972static int ipv6_count_addresses(struct inet6_dev *idev)
973{
974 int cnt = 0;
975 struct inet6_ifaddr *ifp;
976
977 read_lock_bh(&idev->lock);
978 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
979 cnt++;
980 read_unlock_bh(&idev->lock);
981 return cnt;
982}
983
984int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
985{
986 struct inet6_ifaddr * ifp;
987 u8 hash = ipv6_addr_hash(addr);
988
989 read_lock_bh(&addrconf_hash_lock);
990 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
991 if (ipv6_addr_equal(&ifp->addr, addr) &&
992 !(ifp->flags&IFA_F_TENTATIVE)) {
993 if (dev == NULL || ifp->idev->dev == dev ||
994 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
995 break;
996 }
997 }
998 read_unlock_bh(&addrconf_hash_lock);
999 return ifp != NULL;
1000}
1001
1002static
1003int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
1004{
1005 struct inet6_ifaddr * ifp;
1006 u8 hash = ipv6_addr_hash(addr);
1007
1008 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1009 if (ipv6_addr_equal(&ifp->addr, addr)) {
1010 if (dev == NULL || ifp->idev->dev == dev)
1011 break;
1012 }
1013 }
1014 return ifp != NULL;
1015}
1016
1017struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
1018{
1019 struct inet6_ifaddr * ifp;
1020 u8 hash = ipv6_addr_hash(addr);
1021
1022 read_lock_bh(&addrconf_hash_lock);
1023 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1024 if (ipv6_addr_equal(&ifp->addr, addr)) {
1025 if (dev == NULL || ifp->idev->dev == dev ||
1026 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1027 in6_ifa_hold(ifp);
1028 break;
1029 }
1030 }
1031 }
1032 read_unlock_bh(&addrconf_hash_lock);
1033
1034 return ifp;
1035}
1036
1037int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1038{
1039 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
1040 const struct in6_addr *sk2_rcv_saddr6 = tcp_v6_rcv_saddr(sk2);
1041 u32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1042 u32 sk2_rcv_saddr = tcp_v4_rcv_saddr(sk2);
1043 int sk_ipv6only = ipv6_only_sock(sk);
1044 int sk2_ipv6only = tcp_v6_ipv6only(sk2);
1045 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1046 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1047
1048 if (!sk2_rcv_saddr && !sk_ipv6only)
1049 return 1;
1050
1051 if (addr_type2 == IPV6_ADDR_ANY &&
1052 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1053 return 1;
1054
1055 if (addr_type == IPV6_ADDR_ANY &&
1056 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1057 return 1;
1058
1059 if (sk2_rcv_saddr6 &&
1060 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1061 return 1;
1062
1063 if (addr_type == IPV6_ADDR_MAPPED &&
1064 !sk2_ipv6only &&
1065 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1066 return 1;
1067
1068 return 0;
1069}
1070
1071/* Gets referenced address, destroys ifaddr */
1072
1073void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1074{
1075 if (net_ratelimit())
1076 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1077 if (ifp->flags&IFA_F_PERMANENT) {
1078 spin_lock_bh(&ifp->lock);
1079 addrconf_del_timer(ifp);
1080 ifp->flags |= IFA_F_TENTATIVE;
1081 spin_unlock_bh(&ifp->lock);
1082 in6_ifa_put(ifp);
1083#ifdef CONFIG_IPV6_PRIVACY
1084 } else if (ifp->flags&IFA_F_TEMPORARY) {
1085 struct inet6_ifaddr *ifpub;
1086 spin_lock_bh(&ifp->lock);
1087 ifpub = ifp->ifpub;
1088 if (ifpub) {
1089 in6_ifa_hold(ifpub);
1090 spin_unlock_bh(&ifp->lock);
1091 ipv6_create_tempaddr(ifpub, ifp);
1092 in6_ifa_put(ifpub);
1093 } else {
1094 spin_unlock_bh(&ifp->lock);
1095 }
1096 ipv6_del_addr(ifp);
1097#endif
1098 } else
1099 ipv6_del_addr(ifp);
1100}
1101
1102
1103/* Join to solicited addr multicast group. */
1104
1105void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1106{
1107 struct in6_addr maddr;
1108
1109 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1110 return;
1111
1112 addrconf_addr_solict_mult(addr, &maddr);
1113 ipv6_dev_mc_inc(dev, &maddr);
1114}
1115
1116void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1117{
1118 struct in6_addr maddr;
1119
1120 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1121 return;
1122
1123 addrconf_addr_solict_mult(addr, &maddr);
1124 __ipv6_dev_mc_dec(idev, &maddr);
1125}
1126
1127void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1128{
1129 struct in6_addr addr;
1130 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1131 if (ipv6_addr_any(&addr))
1132 return;
1133 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1134}
1135
1136void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1137{
1138 struct in6_addr addr;
1139 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1140 if (ipv6_addr_any(&addr))
1141 return;
1142 __ipv6_dev_ac_dec(ifp->idev, &addr);
1143}
1144
1145static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1146{
1147 switch (dev->type) {
1148 case ARPHRD_ETHER:
1149 case ARPHRD_FDDI:
1150 case ARPHRD_IEEE802_TR:
1151 if (dev->addr_len != ETH_ALEN)
1152 return -1;
1153 memcpy(eui, dev->dev_addr, 3);
1154 memcpy(eui + 5, dev->dev_addr + 3, 3);
1155
1156 /*
1157 * The zSeries OSA network cards can be shared among various
1158 * OS instances, but the OSA cards have only one MAC address.
1159 * This leads to duplicate address conflicts in conjunction
1160 * with IPv6 if more than one instance uses the same card.
1161 *
1162 * The driver for these cards can deliver a unique 16-bit
1163 * identifier for each instance sharing the same card. It is
1164 * placed instead of 0xFFFE in the interface identifier. The
1165 * "u" bit of the interface identifier is not inverted in this
1166 * case. Hence the resulting interface identifier has local
1167 * scope according to RFC2373.
1168 */
1169 if (dev->dev_id) {
1170 eui[3] = (dev->dev_id >> 8) & 0xFF;
1171 eui[4] = dev->dev_id & 0xFF;
1172 } else {
1173 eui[3] = 0xFF;
1174 eui[4] = 0xFE;
1175 eui[0] ^= 2;
1176 }
1177 return 0;
1178 case ARPHRD_ARCNET:
1179 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1180 if (dev->addr_len != ARCNET_ALEN)
1181 return -1;
1182 memset(eui, 0, 7);
1183 eui[7] = *(u8*)dev->dev_addr;
1184 return 0;
1185 case ARPHRD_INFINIBAND:
1186 if (dev->addr_len != INFINIBAND_ALEN)
1187 return -1;
1188 memcpy(eui, dev->dev_addr + 12, 8);
1189 eui[0] |= 2;
1190 return 0;
1191 }
1192 return -1;
1193}
1194
1195static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1196{
1197 int err = -1;
1198 struct inet6_ifaddr *ifp;
1199
1200 read_lock_bh(&idev->lock);
1201 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1202 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1203 memcpy(eui, ifp->addr.s6_addr+8, 8);
1204 err = 0;
1205 break;
1206 }
1207 }
1208 read_unlock_bh(&idev->lock);
1209 return err;
1210}
1211
1212#ifdef CONFIG_IPV6_PRIVACY
1213/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1214static int __ipv6_regen_rndid(struct inet6_dev *idev)
1215{
1216 struct net_device *dev;
1217 struct scatterlist sg[2];
1218
1219 sg[0].page = virt_to_page(idev->entropy);
1220 sg[0].offset = offset_in_page(idev->entropy);
1221 sg[0].length = 8;
1222 sg[1].page = virt_to_page(idev->work_eui64);
1223 sg[1].offset = offset_in_page(idev->work_eui64);
1224 sg[1].length = 8;
1225
1226 dev = idev->dev;
1227
1228 if (ipv6_generate_eui64(idev->work_eui64, dev)) {
1229 printk(KERN_INFO
1230 "__ipv6_regen_rndid(idev=%p): cannot get EUI64 identifier; use random bytes.\n",
1231 idev);
1232 get_random_bytes(idev->work_eui64, sizeof(idev->work_eui64));
1233 }
1234regen:
1235 spin_lock(&md5_tfm_lock);
1236 if (unlikely(md5_tfm == NULL)) {
1237 spin_unlock(&md5_tfm_lock);
1238 return -1;
1239 }
1240 crypto_digest_init(md5_tfm);
1241 crypto_digest_update(md5_tfm, sg, 2);
1242 crypto_digest_final(md5_tfm, idev->work_digest);
1243 spin_unlock(&md5_tfm_lock);
1244
1245 memcpy(idev->rndid, &idev->work_digest[0], 8);
1246 idev->rndid[0] &= ~0x02;
1247 memcpy(idev->entropy, &idev->work_digest[8], 8);
1248
1249 /*
1250 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1251 * check if generated address is not inappropriate
1252 *
1253 * - Reserved subnet anycast (RFC 2526)
1254 * 11111101 11....11 1xxxxxxx
1255 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1256 * 00-00-5E-FE-xx-xx-xx-xx
1257 * - value 0
1258 * - XXX: already assigned to an address on the device
1259 */
1260 if (idev->rndid[0] == 0xfd &&
1261 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1262 (idev->rndid[7]&0x80))
1263 goto regen;
1264 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1265 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1266 goto regen;
1267 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1268 goto regen;
1269 }
1270
1271 return 0;
1272}
1273
1274static void ipv6_regen_rndid(unsigned long data)
1275{
1276 struct inet6_dev *idev = (struct inet6_dev *) data;
1277 unsigned long expires;
1278
1279 read_lock_bh(&addrconf_lock);
1280 write_lock_bh(&idev->lock);
1281
1282 if (idev->dead)
1283 goto out;
1284
1285 if (__ipv6_regen_rndid(idev) < 0)
1286 goto out;
1287
1288 expires = jiffies +
1289 idev->cnf.temp_prefered_lft * HZ -
1290 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1291 if (time_before(expires, jiffies)) {
1292 printk(KERN_WARNING
1293 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1294 idev->dev->name);
1295 goto out;
1296 }
1297
1298 if (!mod_timer(&idev->regen_timer, expires))
1299 in6_dev_hold(idev);
1300
1301out:
1302 write_unlock_bh(&idev->lock);
1303 read_unlock_bh(&addrconf_lock);
1304 in6_dev_put(idev);
1305}
1306
1307static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1308 int ret = 0;
1309
1310 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1311 ret = __ipv6_regen_rndid(idev);
1312 return ret;
1313}
1314#endif
1315
1316/*
1317 * Add prefix route.
1318 */
1319
1320static void
1321addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1322 unsigned long expires, unsigned flags)
1323{
1324 struct in6_rtmsg rtmsg;
1325
1326 memset(&rtmsg, 0, sizeof(rtmsg));
1327 ipv6_addr_copy(&rtmsg.rtmsg_dst, pfx);
1328 rtmsg.rtmsg_dst_len = plen;
1329 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1330 rtmsg.rtmsg_ifindex = dev->ifindex;
1331 rtmsg.rtmsg_info = expires;
1332 rtmsg.rtmsg_flags = RTF_UP|flags;
1333 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1334
1335 /* Prevent useless cloning on PtP SIT.
1336 This thing is done here expecting that the whole
1337 class of non-broadcast devices need not cloning.
1338 */
1339 if (dev->type == ARPHRD_SIT && (dev->flags&IFF_POINTOPOINT))
1340 rtmsg.rtmsg_flags |= RTF_NONEXTHOP;
1341
1342 ip6_route_add(&rtmsg, NULL, NULL);
1343}
1344
1345/* Create "default" multicast route to the interface */
1346
1347static void addrconf_add_mroute(struct net_device *dev)
1348{
1349 struct in6_rtmsg rtmsg;
1350
1351 memset(&rtmsg, 0, sizeof(rtmsg));
1352 ipv6_addr_set(&rtmsg.rtmsg_dst,
1353 htonl(0xFF000000), 0, 0, 0);
1354 rtmsg.rtmsg_dst_len = 8;
1355 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1356 rtmsg.rtmsg_ifindex = dev->ifindex;
1357 rtmsg.rtmsg_flags = RTF_UP;
1358 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1359 ip6_route_add(&rtmsg, NULL, NULL);
1360}
1361
1362static void sit_route_add(struct net_device *dev)
1363{
1364 struct in6_rtmsg rtmsg;
1365
1366 memset(&rtmsg, 0, sizeof(rtmsg));
1367
1368 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1369 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1370
1371 /* prefix length - 96 bits "::d.d.d.d" */
1372 rtmsg.rtmsg_dst_len = 96;
1373 rtmsg.rtmsg_flags = RTF_UP|RTF_NONEXTHOP;
1374 rtmsg.rtmsg_ifindex = dev->ifindex;
1375
1376 ip6_route_add(&rtmsg, NULL, NULL);
1377}
1378
1379static void addrconf_add_lroute(struct net_device *dev)
1380{
1381 struct in6_addr addr;
1382
1383 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1384 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1385}
1386
1387static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1388{
1389 struct inet6_dev *idev;
1390
1391 ASSERT_RTNL();
1392
1393 if ((idev = ipv6_find_idev(dev)) == NULL)
1394 return NULL;
1395
1396 /* Add default multicast route */
1397 addrconf_add_mroute(dev);
1398
1399 /* Add link local route */
1400 addrconf_add_lroute(dev);
1401 return idev;
1402}
1403
1404void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1405{
1406 struct prefix_info *pinfo;
1407 __u32 valid_lft;
1408 __u32 prefered_lft;
1409 int addr_type;
1410 unsigned long rt_expires;
1411 struct inet6_dev *in6_dev;
1412
1413 pinfo = (struct prefix_info *) opt;
1414
1415 if (len < sizeof(struct prefix_info)) {
1416 ADBG(("addrconf: prefix option too short\n"));
1417 return;
1418 }
1419
1420 /*
1421 * Validation checks ([ADDRCONF], page 19)
1422 */
1423
1424 addr_type = ipv6_addr_type(&pinfo->prefix);
1425
1426 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1427 return;
1428
1429 valid_lft = ntohl(pinfo->valid);
1430 prefered_lft = ntohl(pinfo->prefered);
1431
1432 if (prefered_lft > valid_lft) {
1433 if (net_ratelimit())
1434 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1435 return;
1436 }
1437
1438 in6_dev = in6_dev_get(dev);
1439
1440 if (in6_dev == NULL) {
1441 if (net_ratelimit())
1442 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1443 return;
1444 }
1445
1446 /*
1447 * Two things going on here:
1448 * 1) Add routes for on-link prefixes
1449 * 2) Configure prefixes with the auto flag set
1450 */
1451
1452 /* Avoid arithmetic overflow. Really, we could
1453 save rt_expires in seconds, likely valid_lft,
1454 but it would require division in fib gc, that it
1455 not good.
1456 */
1457 if (valid_lft >= 0x7FFFFFFF/HZ)
1458 rt_expires = 0;
1459 else
1460 rt_expires = jiffies + valid_lft * HZ;
1461
1462 if (pinfo->onlink) {
1463 struct rt6_info *rt;
1464 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1465
1466 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1467 if (rt->rt6i_flags&RTF_EXPIRES) {
1468 if (valid_lft == 0) {
1469 ip6_del_rt(rt, NULL, NULL);
1470 rt = NULL;
1471 } else {
1472 rt->rt6i_expires = rt_expires;
1473 }
1474 }
1475 } else if (valid_lft) {
1476 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1477 dev, rt_expires, RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
1478 }
1479 if (rt)
1480 dst_release(&rt->u.dst);
1481 }
1482
1483 /* Try to figure out our local address for this prefix */
1484
1485 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1486 struct inet6_ifaddr * ifp;
1487 struct in6_addr addr;
1488 int create = 0, update_lft = 0;
1489
1490 if (pinfo->prefix_len == 64) {
1491 memcpy(&addr, &pinfo->prefix, 8);
1492 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1493 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1494 in6_dev_put(in6_dev);
1495 return;
1496 }
1497 goto ok;
1498 }
1499 if (net_ratelimit())
1500 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1501 pinfo->prefix_len);
1502 in6_dev_put(in6_dev);
1503 return;
1504
1505ok:
1506
1507 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1508
1509 if (ifp == NULL && valid_lft) {
1510 int max_addresses = in6_dev->cnf.max_addresses;
1511
1512 /* Do not allow to create too much of autoconfigured
1513 * addresses; this would be too easy way to crash kernel.
1514 */
1515 if (!max_addresses ||
1516 ipv6_count_addresses(in6_dev) < max_addresses)
1517 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1518 addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1519
1520 if (!ifp || IS_ERR(ifp)) {
1521 in6_dev_put(in6_dev);
1522 return;
1523 }
1524
1525 update_lft = create = 1;
1526 ifp->cstamp = jiffies;
1527 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1528 }
1529
1530 if (ifp) {
1531 int flags;
1532 unsigned long now;
1533#ifdef CONFIG_IPV6_PRIVACY
1534 struct inet6_ifaddr *ift;
1535#endif
1536 u32 stored_lft;
1537
1538 /* update lifetime (RFC2462 5.5.3 e) */
1539 spin_lock(&ifp->lock);
1540 now = jiffies;
1541 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1542 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1543 else
1544 stored_lft = 0;
1545 if (!update_lft && stored_lft) {
1546 if (valid_lft > MIN_VALID_LIFETIME ||
1547 valid_lft > stored_lft)
1548 update_lft = 1;
1549 else if (stored_lft <= MIN_VALID_LIFETIME) {
1550 /* valid_lft <= stored_lft is always true */
1551 /* XXX: IPsec */
1552 update_lft = 0;
1553 } else {
1554 valid_lft = MIN_VALID_LIFETIME;
1555 if (valid_lft < prefered_lft)
1556 prefered_lft = valid_lft;
1557 update_lft = 1;
1558 }
1559 }
1560
1561 if (update_lft) {
1562 ifp->valid_lft = valid_lft;
1563 ifp->prefered_lft = prefered_lft;
1564 ifp->tstamp = now;
1565 flags = ifp->flags;
1566 ifp->flags &= ~IFA_F_DEPRECATED;
1567 spin_unlock(&ifp->lock);
1568
1569 if (!(flags&IFA_F_TENTATIVE))
1570 ipv6_ifa_notify(0, ifp);
1571 } else
1572 spin_unlock(&ifp->lock);
1573
1574#ifdef CONFIG_IPV6_PRIVACY
1575 read_lock_bh(&in6_dev->lock);
1576 /* update all temporary addresses in the list */
1577 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1578 /*
1579 * When adjusting the lifetimes of an existing
1580 * temporary address, only lower the lifetimes.
1581 * Implementations must not increase the
1582 * lifetimes of an existing temporary address
1583 * when processing a Prefix Information Option.
1584 */
1585 spin_lock(&ift->lock);
1586 flags = ift->flags;
1587 if (ift->valid_lft > valid_lft &&
1588 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1589 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1590 if (ift->prefered_lft > prefered_lft &&
1591 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1592 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1593 spin_unlock(&ift->lock);
1594 if (!(flags&IFA_F_TENTATIVE))
1595 ipv6_ifa_notify(0, ift);
1596 }
1597
1598 if (create && in6_dev->cnf.use_tempaddr > 0) {
1599 /*
1600 * When a new public address is created as described in [ADDRCONF],
1601 * also create a new temporary address.
1602 */
1603 read_unlock_bh(&in6_dev->lock);
1604 ipv6_create_tempaddr(ifp, NULL);
1605 } else {
1606 read_unlock_bh(&in6_dev->lock);
1607 }
1608#endif
1609 in6_ifa_put(ifp);
1610 addrconf_verify(0);
1611 }
1612 }
1613 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1614 in6_dev_put(in6_dev);
1615}
1616
1617/*
1618 * Set destination address.
1619 * Special case for SIT interfaces where we create a new "virtual"
1620 * device.
1621 */
1622int addrconf_set_dstaddr(void __user *arg)
1623{
1624 struct in6_ifreq ireq;
1625 struct net_device *dev;
1626 int err = -EINVAL;
1627
1628 rtnl_lock();
1629
1630 err = -EFAULT;
1631 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1632 goto err_exit;
1633
1634 dev = __dev_get_by_index(ireq.ifr6_ifindex);
1635
1636 err = -ENODEV;
1637 if (dev == NULL)
1638 goto err_exit;
1639
1640 if (dev->type == ARPHRD_SIT) {
1641 struct ifreq ifr;
1642 mm_segment_t oldfs;
1643 struct ip_tunnel_parm p;
1644
1645 err = -EADDRNOTAVAIL;
1646 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1647 goto err_exit;
1648
1649 memset(&p, 0, sizeof(p));
1650 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1651 p.iph.saddr = 0;
1652 p.iph.version = 4;
1653 p.iph.ihl = 5;
1654 p.iph.protocol = IPPROTO_IPV6;
1655 p.iph.ttl = 64;
1656 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1657
1658 oldfs = get_fs(); set_fs(KERNEL_DS);
1659 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1660 set_fs(oldfs);
1661
1662 if (err == 0) {
1663 err = -ENOBUFS;
1664 if ((dev = __dev_get_by_name(p.name)) == NULL)
1665 goto err_exit;
1666 err = dev_open(dev);
1667 }
1668 }
1669
1670err_exit:
1671 rtnl_unlock();
1672 return err;
1673}
1674
1675/*
1676 * Manual configuration of address on an interface
1677 */
1678static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen)
1679{
1680 struct inet6_ifaddr *ifp;
1681 struct inet6_dev *idev;
1682 struct net_device *dev;
1683 int scope;
1684
1685 ASSERT_RTNL();
1686
1687 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1688 return -ENODEV;
1689
1690 if (!(dev->flags&IFF_UP))
1691 return -ENETDOWN;
1692
1693 if ((idev = addrconf_add_dev(dev)) == NULL)
1694 return -ENOBUFS;
1695
1696 scope = ipv6_addr_scope(pfx);
1697
1698 ifp = ipv6_add_addr(idev, pfx, plen, scope, IFA_F_PERMANENT);
1699 if (!IS_ERR(ifp)) {
1700 addrconf_dad_start(ifp, 0);
1701 in6_ifa_put(ifp);
1702 return 0;
1703 }
1704
1705 return PTR_ERR(ifp);
1706}
1707
1708static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1709{
1710 struct inet6_ifaddr *ifp;
1711 struct inet6_dev *idev;
1712 struct net_device *dev;
1713
1714 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1715 return -ENODEV;
1716
1717 if ((idev = __in6_dev_get(dev)) == NULL)
1718 return -ENXIO;
1719
1720 read_lock_bh(&idev->lock);
1721 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1722 if (ifp->prefix_len == plen &&
1723 ipv6_addr_equal(pfx, &ifp->addr)) {
1724 in6_ifa_hold(ifp);
1725 read_unlock_bh(&idev->lock);
1726
1727 ipv6_del_addr(ifp);
1728
1729 /* If the last address is deleted administratively,
1730 disable IPv6 on this interface.
1731 */
1732 if (idev->addr_list == NULL)
1733 addrconf_ifdown(idev->dev, 1);
1734 return 0;
1735 }
1736 }
1737 read_unlock_bh(&idev->lock);
1738 return -EADDRNOTAVAIL;
1739}
1740
1741
1742int addrconf_add_ifaddr(void __user *arg)
1743{
1744 struct in6_ifreq ireq;
1745 int err;
1746
1747 if (!capable(CAP_NET_ADMIN))
1748 return -EPERM;
1749
1750 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1751 return -EFAULT;
1752
1753 rtnl_lock();
1754 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1755 rtnl_unlock();
1756 return err;
1757}
1758
1759int addrconf_del_ifaddr(void __user *arg)
1760{
1761 struct in6_ifreq ireq;
1762 int err;
1763
1764 if (!capable(CAP_NET_ADMIN))
1765 return -EPERM;
1766
1767 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1768 return -EFAULT;
1769
1770 rtnl_lock();
1771 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1772 rtnl_unlock();
1773 return err;
1774}
1775
1776static void sit_add_v4_addrs(struct inet6_dev *idev)
1777{
1778 struct inet6_ifaddr * ifp;
1779 struct in6_addr addr;
1780 struct net_device *dev;
1781 int scope;
1782
1783 ASSERT_RTNL();
1784
1785 memset(&addr, 0, sizeof(struct in6_addr));
1786 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
1787
1788 if (idev->dev->flags&IFF_POINTOPOINT) {
1789 addr.s6_addr32[0] = htonl(0xfe800000);
1790 scope = IFA_LINK;
1791 } else {
1792 scope = IPV6_ADDR_COMPATv4;
1793 }
1794
1795 if (addr.s6_addr32[3]) {
1796 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
1797 if (!IS_ERR(ifp)) {
1798 spin_lock_bh(&ifp->lock);
1799 ifp->flags &= ~IFA_F_TENTATIVE;
1800 spin_unlock_bh(&ifp->lock);
1801 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1802 in6_ifa_put(ifp);
1803 }
1804 return;
1805 }
1806
1807 for (dev = dev_base; dev != NULL; dev = dev->next) {
1808 struct in_device * in_dev = __in_dev_get(dev);
1809 if (in_dev && (dev->flags & IFF_UP)) {
1810 struct in_ifaddr * ifa;
1811
1812 int flag = scope;
1813
1814 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1815 int plen;
1816
1817 addr.s6_addr32[3] = ifa->ifa_local;
1818
1819 if (ifa->ifa_scope == RT_SCOPE_LINK)
1820 continue;
1821 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
1822 if (idev->dev->flags&IFF_POINTOPOINT)
1823 continue;
1824 flag |= IFA_HOST;
1825 }
1826 if (idev->dev->flags&IFF_POINTOPOINT)
1827 plen = 64;
1828 else
1829 plen = 96;
1830
1831 ifp = ipv6_add_addr(idev, &addr, plen, flag,
1832 IFA_F_PERMANENT);
1833 if (!IS_ERR(ifp)) {
1834 spin_lock_bh(&ifp->lock);
1835 ifp->flags &= ~IFA_F_TENTATIVE;
1836 spin_unlock_bh(&ifp->lock);
1837 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1838 in6_ifa_put(ifp);
1839 }
1840 }
1841 }
1842 }
1843}
1844
1845static void init_loopback(struct net_device *dev)
1846{
1847 struct inet6_dev *idev;
1848 struct inet6_ifaddr * ifp;
1849
1850 /* ::1 */
1851
1852 ASSERT_RTNL();
1853
1854 if ((idev = ipv6_find_idev(dev)) == NULL) {
1855 printk(KERN_DEBUG "init loopback: add_dev failed\n");
1856 return;
1857 }
1858
1859 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
1860 if (!IS_ERR(ifp)) {
1861 spin_lock_bh(&ifp->lock);
1862 ifp->flags &= ~IFA_F_TENTATIVE;
1863 spin_unlock_bh(&ifp->lock);
1864 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1865 in6_ifa_put(ifp);
1866 }
1867}
1868
1869static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
1870{
1871 struct inet6_ifaddr * ifp;
1872
1873 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
1874 if (!IS_ERR(ifp)) {
1875 addrconf_dad_start(ifp, 0);
1876 in6_ifa_put(ifp);
1877 }
1878}
1879
1880static void addrconf_dev_config(struct net_device *dev)
1881{
1882 struct in6_addr addr;
1883 struct inet6_dev * idev;
1884
1885 ASSERT_RTNL();
1886
1887 if ((dev->type != ARPHRD_ETHER) &&
1888 (dev->type != ARPHRD_FDDI) &&
1889 (dev->type != ARPHRD_IEEE802_TR) &&
1890 (dev->type != ARPHRD_ARCNET) &&
1891 (dev->type != ARPHRD_INFINIBAND)) {
1892 /* Alas, we support only Ethernet autoconfiguration. */
1893 return;
1894 }
1895
1896 idev = addrconf_add_dev(dev);
1897 if (idev == NULL)
1898 return;
1899
1900 memset(&addr, 0, sizeof(struct in6_addr));
1901 addr.s6_addr32[0] = htonl(0xFE800000);
1902
1903 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
1904 addrconf_add_linklocal(idev, &addr);
1905}
1906
1907static void addrconf_sit_config(struct net_device *dev)
1908{
1909 struct inet6_dev *idev;
1910
1911 ASSERT_RTNL();
1912
1913 /*
1914 * Configure the tunnel with one of our IPv4
1915 * addresses... we should configure all of
1916 * our v4 addrs in the tunnel
1917 */
1918
1919 if ((idev = ipv6_find_idev(dev)) == NULL) {
1920 printk(KERN_DEBUG "init sit: add_dev failed\n");
1921 return;
1922 }
1923
1924 sit_add_v4_addrs(idev);
1925
1926 if (dev->flags&IFF_POINTOPOINT) {
1927 addrconf_add_mroute(dev);
1928 addrconf_add_lroute(dev);
1929 } else
1930 sit_route_add(dev);
1931}
1932
1933static inline int
1934ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
1935{
1936 struct in6_addr lladdr;
1937
1938 if (!ipv6_get_lladdr(link_dev, &lladdr)) {
1939 addrconf_add_linklocal(idev, &lladdr);
1940 return 0;
1941 }
1942 return -1;
1943}
1944
1945static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
1946{
1947 struct net_device *link_dev;
1948
1949 /* first try to inherit the link-local address from the link device */
1950 if (idev->dev->iflink &&
1951 (link_dev = __dev_get_by_index(idev->dev->iflink))) {
1952 if (!ipv6_inherit_linklocal(idev, link_dev))
1953 return;
1954 }
1955 /* then try to inherit it from any device */
1956 for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
1957 if (!ipv6_inherit_linklocal(idev, link_dev))
1958 return;
1959 }
1960 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
1961}
1962
1963/*
1964 * Autoconfigure tunnel with a link-local address so routing protocols,
1965 * DHCPv6, MLD etc. can be run over the virtual link
1966 */
1967
1968static void addrconf_ip6_tnl_config(struct net_device *dev)
1969{
1970 struct inet6_dev *idev;
1971
1972 ASSERT_RTNL();
1973
1974 if ((idev = addrconf_add_dev(dev)) == NULL) {
1975 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
1976 return;
1977 }
1978 ip6_tnl_add_linklocal(idev);
1979 addrconf_add_mroute(dev);
1980}
1981
1982static int addrconf_notify(struct notifier_block *this, unsigned long event,
1983 void * data)
1984{
1985 struct net_device *dev = (struct net_device *) data;
1986 struct inet6_dev *idev = __in6_dev_get(dev);
1987
1988 switch(event) {
1989 case NETDEV_UP:
1990 switch(dev->type) {
1991 case ARPHRD_SIT:
1992 addrconf_sit_config(dev);
1993 break;
1994 case ARPHRD_TUNNEL6:
1995 addrconf_ip6_tnl_config(dev);
1996 break;
1997 case ARPHRD_LOOPBACK:
1998 init_loopback(dev);
1999 break;
2000
2001 default:
2002 addrconf_dev_config(dev);
2003 break;
2004 };
2005 if (idev) {
2006 /* If the MTU changed during the interface down, when the
2007 interface up, the changed MTU must be reflected in the
2008 idev as well as routers.
2009 */
2010 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2011 rt6_mtu_change(dev, dev->mtu);
2012 idev->cnf.mtu6 = dev->mtu;
2013 }
2014 idev->tstamp = jiffies;
2015 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2016 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2017 stop IPv6 on this interface.
2018 */
2019 if (dev->mtu < IPV6_MIN_MTU)
2020 addrconf_ifdown(dev, event != NETDEV_DOWN);
2021 }
2022 break;
2023
2024 case NETDEV_CHANGEMTU:
2025 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
2026 rt6_mtu_change(dev, dev->mtu);
2027 idev->cnf.mtu6 = dev->mtu;
2028 break;
2029 }
2030
2031 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2032
2033 case NETDEV_DOWN:
2034 case NETDEV_UNREGISTER:
2035 /*
2036 * Remove all addresses from this interface.
2037 */
2038 addrconf_ifdown(dev, event != NETDEV_DOWN);
2039 break;
2040 case NETDEV_CHANGE:
2041 break;
2042 case NETDEV_CHANGENAME:
2043#ifdef CONFIG_SYSCTL
2044 if (idev) {
2045 addrconf_sysctl_unregister(&idev->cnf);
2046 neigh_sysctl_unregister(idev->nd_parms);
2047 neigh_sysctl_register(dev, idev->nd_parms,
2048 NET_IPV6, NET_IPV6_NEIGH, "ipv6",
2049 &ndisc_ifinfo_sysctl_change,
2050 NULL);
2051 addrconf_sysctl_register(idev, &idev->cnf);
2052 }
2053#endif
2054 break;
2055 };
2056
2057 return NOTIFY_OK;
2058}
2059
2060/*
2061 * addrconf module should be notified of a device going up
2062 */
2063static struct notifier_block ipv6_dev_notf = {
2064 .notifier_call = addrconf_notify,
2065 .priority = 0
2066};
2067
2068static int addrconf_ifdown(struct net_device *dev, int how)
2069{
2070 struct inet6_dev *idev;
2071 struct inet6_ifaddr *ifa, **bifa;
2072 int i;
2073
2074 ASSERT_RTNL();
2075
2076 if (dev == &loopback_dev && how == 1)
2077 how = 0;
2078
2079 rt6_ifdown(dev);
2080 neigh_ifdown(&nd_tbl, dev);
2081
2082 idev = __in6_dev_get(dev);
2083 if (idev == NULL)
2084 return -ENODEV;
2085
2086 /* Step 1: remove reference to ipv6 device from parent device.
2087 Do not dev_put!
2088 */
2089 if (how == 1) {
2090 write_lock_bh(&addrconf_lock);
2091 dev->ip6_ptr = NULL;
2092 idev->dead = 1;
2093 write_unlock_bh(&addrconf_lock);
2094
2095 /* Step 1.5: remove snmp6 entry */
2096 snmp6_unregister_dev(idev);
2097
2098 }
2099
2100 /* Step 2: clear hash table */
2101 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2102 bifa = &inet6_addr_lst[i];
2103
2104 write_lock_bh(&addrconf_hash_lock);
2105 while ((ifa = *bifa) != NULL) {
2106 if (ifa->idev == idev) {
2107 *bifa = ifa->lst_next;
2108 ifa->lst_next = NULL;
2109 addrconf_del_timer(ifa);
2110 in6_ifa_put(ifa);
2111 continue;
2112 }
2113 bifa = &ifa->lst_next;
2114 }
2115 write_unlock_bh(&addrconf_hash_lock);
2116 }
2117
2118 write_lock_bh(&idev->lock);
2119
2120 /* Step 3: clear flags for stateless addrconf */
2121 if (how != 1)
2122 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD);
2123
2124 /* Step 4: clear address list */
2125#ifdef CONFIG_IPV6_PRIVACY
2126 if (how == 1 && del_timer(&idev->regen_timer))
2127 in6_dev_put(idev);
2128
2129 /* clear tempaddr list */
2130 while ((ifa = idev->tempaddr_list) != NULL) {
2131 idev->tempaddr_list = ifa->tmp_next;
2132 ifa->tmp_next = NULL;
2133 ifa->dead = 1;
2134 write_unlock_bh(&idev->lock);
2135 spin_lock_bh(&ifa->lock);
2136
2137 if (ifa->ifpub) {
2138 in6_ifa_put(ifa->ifpub);
2139 ifa->ifpub = NULL;
2140 }
2141 spin_unlock_bh(&ifa->lock);
2142 in6_ifa_put(ifa);
2143 write_lock_bh(&idev->lock);
2144 }
2145#endif
2146 while ((ifa = idev->addr_list) != NULL) {
2147 idev->addr_list = ifa->if_next;
2148 ifa->if_next = NULL;
2149 ifa->dead = 1;
2150 addrconf_del_timer(ifa);
2151 write_unlock_bh(&idev->lock);
2152
2153 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2154 in6_ifa_put(ifa);
2155
2156 write_lock_bh(&idev->lock);
2157 }
2158 write_unlock_bh(&idev->lock);
2159
2160 /* Step 5: Discard multicast list */
2161
2162 if (how == 1)
2163 ipv6_mc_destroy_dev(idev);
2164 else
2165 ipv6_mc_down(idev);
2166
2167 /* Step 5: netlink notification of this interface */
2168 idev->tstamp = jiffies;
2169 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2170
2171 /* Shot the device (if unregistered) */
2172
2173 if (how == 1) {
2174#ifdef CONFIG_SYSCTL
2175 addrconf_sysctl_unregister(&idev->cnf);
2176 neigh_sysctl_unregister(idev->nd_parms);
2177#endif
2178 neigh_parms_release(&nd_tbl, idev->nd_parms);
2179 neigh_ifdown(&nd_tbl, dev);
2180 in6_dev_put(idev);
2181 }
2182 return 0;
2183}
2184
2185static void addrconf_rs_timer(unsigned long data)
2186{
2187 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2188
2189 if (ifp->idev->cnf.forwarding)
2190 goto out;
2191
2192 if (ifp->idev->if_flags & IF_RA_RCVD) {
2193 /*
2194 * Announcement received after solicitation
2195 * was sent
2196 */
2197 goto out;
2198 }
2199
2200 spin_lock(&ifp->lock);
2201 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2202 struct in6_addr all_routers;
2203
2204 /* The wait after the last probe can be shorter */
2205 addrconf_mod_timer(ifp, AC_RS,
2206 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2207 ifp->idev->cnf.rtr_solicit_delay :
2208 ifp->idev->cnf.rtr_solicit_interval);
2209 spin_unlock(&ifp->lock);
2210
2211 ipv6_addr_all_routers(&all_routers);
2212
2213 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2214 } else {
2215 spin_unlock(&ifp->lock);
2216 /*
2217 * Note: we do not support deprecated "all on-link"
2218 * assumption any longer.
2219 */
2220 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2221 ifp->idev->dev->name);
2222 }
2223
2224out:
2225 in6_ifa_put(ifp);
2226}
2227
2228/*
2229 * Duplicate Address Detection
2230 */
2231static void addrconf_dad_start(struct inet6_ifaddr *ifp, int flags)
2232{
2233 struct inet6_dev *idev = ifp->idev;
2234 struct net_device *dev = idev->dev;
2235 unsigned long rand_num;
2236
2237 addrconf_join_solict(dev, &ifp->addr);
2238
2239 if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
2240 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0,
2241 flags);
2242
2243 net_srandom(ifp->addr.s6_addr32[3]);
2244 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2245
2246 read_lock_bh(&idev->lock);
2247 if (ifp->dead)
2248 goto out;
2249 spin_lock_bh(&ifp->lock);
2250
2251 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
2252 !(ifp->flags&IFA_F_TENTATIVE)) {
2253 ifp->flags &= ~IFA_F_TENTATIVE;
2254 spin_unlock_bh(&ifp->lock);
2255 read_unlock_bh(&idev->lock);
2256
2257 addrconf_dad_completed(ifp);
2258 return;
2259 }
2260
2261 ifp->probes = idev->cnf.dad_transmits;
2262 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2263
2264 spin_unlock_bh(&ifp->lock);
2265out:
2266 read_unlock_bh(&idev->lock);
2267}
2268
2269static void addrconf_dad_timer(unsigned long data)
2270{
2271 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2272 struct inet6_dev *idev = ifp->idev;
2273 struct in6_addr unspec;
2274 struct in6_addr mcaddr;
2275
2276 read_lock_bh(&idev->lock);
2277 if (idev->dead) {
2278 read_unlock_bh(&idev->lock);
2279 goto out;
2280 }
2281 spin_lock_bh(&ifp->lock);
2282 if (ifp->probes == 0) {
2283 /*
2284 * DAD was successful
2285 */
2286
2287 ifp->flags &= ~IFA_F_TENTATIVE;
2288 spin_unlock_bh(&ifp->lock);
2289 read_unlock_bh(&idev->lock);
2290
2291 addrconf_dad_completed(ifp);
2292
2293 goto out;
2294 }
2295
2296 ifp->probes--;
2297 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2298 spin_unlock_bh(&ifp->lock);
2299 read_unlock_bh(&idev->lock);
2300
2301 /* send a neighbour solicitation for our addr */
2302 memset(&unspec, 0, sizeof(unspec));
2303 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2304 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2305out:
2306 in6_ifa_put(ifp);
2307}
2308
2309static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2310{
2311 struct net_device * dev = ifp->idev->dev;
2312
2313 /*
2314 * Configure the address for reception. Now it is valid.
2315 */
2316
2317 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2318
2319 /* If added prefix is link local and forwarding is off,
2320 start sending router solicitations.
2321 */
2322
2323 if (ifp->idev->cnf.forwarding == 0 &&
2324 ifp->idev->cnf.rtr_solicits > 0 &&
2325 (dev->flags&IFF_LOOPBACK) == 0 &&
2326 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2327 struct in6_addr all_routers;
2328
2329 ipv6_addr_all_routers(&all_routers);
2330
2331 /*
2332 * If a host as already performed a random delay
2333 * [...] as part of DAD [...] there is no need
2334 * to delay again before sending the first RS
2335 */
2336 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2337
2338 spin_lock_bh(&ifp->lock);
2339 ifp->probes = 1;
2340 ifp->idev->if_flags |= IF_RS_SENT;
2341 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2342 spin_unlock_bh(&ifp->lock);
2343 }
2344}
2345
2346#ifdef CONFIG_PROC_FS
2347struct if6_iter_state {
2348 int bucket;
2349};
2350
2351static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2352{
2353 struct inet6_ifaddr *ifa = NULL;
2354 struct if6_iter_state *state = seq->private;
2355
2356 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2357 ifa = inet6_addr_lst[state->bucket];
2358 if (ifa)
2359 break;
2360 }
2361 return ifa;
2362}
2363
2364static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2365{
2366 struct if6_iter_state *state = seq->private;
2367
2368 ifa = ifa->lst_next;
2369try_again:
2370 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2371 ifa = inet6_addr_lst[state->bucket];
2372 goto try_again;
2373 }
2374 return ifa;
2375}
2376
2377static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2378{
2379 struct inet6_ifaddr *ifa = if6_get_first(seq);
2380
2381 if (ifa)
2382 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2383 --pos;
2384 return pos ? NULL : ifa;
2385}
2386
2387static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2388{
2389 read_lock_bh(&addrconf_hash_lock);
2390 return if6_get_idx(seq, *pos);
2391}
2392
2393static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2394{
2395 struct inet6_ifaddr *ifa;
2396
2397 ifa = if6_get_next(seq, v);
2398 ++*pos;
2399 return ifa;
2400}
2401
2402static void if6_seq_stop(struct seq_file *seq, void *v)
2403{
2404 read_unlock_bh(&addrconf_hash_lock);
2405}
2406
2407static int if6_seq_show(struct seq_file *seq, void *v)
2408{
2409 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2410 seq_printf(seq,
2411 "%04x%04x%04x%04x%04x%04x%04x%04x %02x %02x %02x %02x %8s\n",
2412 NIP6(ifp->addr),
2413 ifp->idev->dev->ifindex,
2414 ifp->prefix_len,
2415 ifp->scope,
2416 ifp->flags,
2417 ifp->idev->dev->name);
2418 return 0;
2419}
2420
2421static struct seq_operations if6_seq_ops = {
2422 .start = if6_seq_start,
2423 .next = if6_seq_next,
2424 .show = if6_seq_show,
2425 .stop = if6_seq_stop,
2426};
2427
2428static int if6_seq_open(struct inode *inode, struct file *file)
2429{
2430 struct seq_file *seq;
2431 int rc = -ENOMEM;
2432 struct if6_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2433
2434 if (!s)
2435 goto out;
2436 memset(s, 0, sizeof(*s));
2437
2438 rc = seq_open(file, &if6_seq_ops);
2439 if (rc)
2440 goto out_kfree;
2441
2442 seq = file->private_data;
2443 seq->private = s;
2444out:
2445 return rc;
2446out_kfree:
2447 kfree(s);
2448 goto out;
2449}
2450
2451static struct file_operations if6_fops = {
2452 .owner = THIS_MODULE,
2453 .open = if6_seq_open,
2454 .read = seq_read,
2455 .llseek = seq_lseek,
2456 .release = seq_release_private,
2457};
2458
2459int __init if6_proc_init(void)
2460{
2461 if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2462 return -ENOMEM;
2463 return 0;
2464}
2465
2466void if6_proc_exit(void)
2467{
2468 proc_net_remove("if_inet6");
2469}
2470#endif /* CONFIG_PROC_FS */
2471
2472/*
2473 * Periodic address status verification
2474 */
2475
2476static void addrconf_verify(unsigned long foo)
2477{
2478 struct inet6_ifaddr *ifp;
2479 unsigned long now, next;
2480 int i;
2481
2482 spin_lock_bh(&addrconf_verify_lock);
2483 now = jiffies;
2484 next = now + ADDR_CHECK_FREQUENCY;
2485
2486 del_timer(&addr_chk_timer);
2487
2488 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2489
2490restart:
2491 write_lock(&addrconf_hash_lock);
2492 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2493 unsigned long age;
2494#ifdef CONFIG_IPV6_PRIVACY
2495 unsigned long regen_advance;
2496#endif
2497
2498 if (ifp->flags & IFA_F_PERMANENT)
2499 continue;
2500
2501 spin_lock(&ifp->lock);
2502 age = (now - ifp->tstamp) / HZ;
2503
2504#ifdef CONFIG_IPV6_PRIVACY
2505 regen_advance = ifp->idev->cnf.regen_max_retry *
2506 ifp->idev->cnf.dad_transmits *
2507 ifp->idev->nd_parms->retrans_time / HZ;
2508#endif
2509
2510 if (age >= ifp->valid_lft) {
2511 spin_unlock(&ifp->lock);
2512 in6_ifa_hold(ifp);
2513 write_unlock(&addrconf_hash_lock);
2514 ipv6_del_addr(ifp);
2515 goto restart;
2516 } else if (age >= ifp->prefered_lft) {
2517 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2518 int deprecate = 0;
2519
2520 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2521 deprecate = 1;
2522 ifp->flags |= IFA_F_DEPRECATED;
2523 }
2524
2525 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2526 next = ifp->tstamp + ifp->valid_lft * HZ;
2527
2528 spin_unlock(&ifp->lock);
2529
2530 if (deprecate) {
2531 in6_ifa_hold(ifp);
2532 write_unlock(&addrconf_hash_lock);
2533
2534 ipv6_ifa_notify(0, ifp);
2535 in6_ifa_put(ifp);
2536 goto restart;
2537 }
2538#ifdef CONFIG_IPV6_PRIVACY
2539 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2540 !(ifp->flags&IFA_F_TENTATIVE)) {
2541 if (age >= ifp->prefered_lft - regen_advance) {
2542 struct inet6_ifaddr *ifpub = ifp->ifpub;
2543 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2544 next = ifp->tstamp + ifp->prefered_lft * HZ;
2545 if (!ifp->regen_count && ifpub) {
2546 ifp->regen_count++;
2547 in6_ifa_hold(ifp);
2548 in6_ifa_hold(ifpub);
2549 spin_unlock(&ifp->lock);
2550 write_unlock(&addrconf_hash_lock);
2551 ipv6_create_tempaddr(ifpub, ifp);
2552 in6_ifa_put(ifpub);
2553 in6_ifa_put(ifp);
2554 goto restart;
2555 }
2556 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2557 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2558 spin_unlock(&ifp->lock);
2559#endif
2560 } else {
2561 /* ifp->prefered_lft <= ifp->valid_lft */
2562 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2563 next = ifp->tstamp + ifp->prefered_lft * HZ;
2564 spin_unlock(&ifp->lock);
2565 }
2566 }
2567 write_unlock(&addrconf_hash_lock);
2568 }
2569
2570 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2571 add_timer(&addr_chk_timer);
2572 spin_unlock_bh(&addrconf_verify_lock);
2573}
2574
2575static int
2576inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2577{
2578 struct rtattr **rta = arg;
2579 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2580 struct in6_addr *pfx;
2581
2582 pfx = NULL;
2583 if (rta[IFA_ADDRESS-1]) {
2584 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2585 return -EINVAL;
2586 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2587 }
2588 if (rta[IFA_LOCAL-1]) {
2589 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2590 return -EINVAL;
2591 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2592 }
2593 if (pfx == NULL)
2594 return -EINVAL;
2595
2596 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2597}
2598
2599static int
2600inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2601{
2602 struct rtattr **rta = arg;
2603 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2604 struct in6_addr *pfx;
2605
2606 pfx = NULL;
2607 if (rta[IFA_ADDRESS-1]) {
2608 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2609 return -EINVAL;
2610 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2611 }
2612 if (rta[IFA_LOCAL-1]) {
2613 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2614 return -EINVAL;
2615 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2616 }
2617 if (pfx == NULL)
2618 return -EINVAL;
2619
2620 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2621}
2622
2623static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
2624 u32 pid, u32 seq, int event)
2625{
2626 struct ifaddrmsg *ifm;
2627 struct nlmsghdr *nlh;
2628 struct ifa_cacheinfo ci;
2629 unsigned char *b = skb->tail;
2630
2631 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
2632 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
2633 ifm = NLMSG_DATA(nlh);
2634 ifm->ifa_family = AF_INET6;
2635 ifm->ifa_prefixlen = ifa->prefix_len;
2636 ifm->ifa_flags = ifa->flags;
2637 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2638 if (ifa->scope&IFA_HOST)
2639 ifm->ifa_scope = RT_SCOPE_HOST;
2640 else if (ifa->scope&IFA_LINK)
2641 ifm->ifa_scope = RT_SCOPE_LINK;
2642 else if (ifa->scope&IFA_SITE)
2643 ifm->ifa_scope = RT_SCOPE_SITE;
2644 ifm->ifa_index = ifa->idev->dev->ifindex;
2645 RTA_PUT(skb, IFA_ADDRESS, 16, &ifa->addr);
2646 if (!(ifa->flags&IFA_F_PERMANENT)) {
2647 ci.ifa_prefered = ifa->prefered_lft;
2648 ci.ifa_valid = ifa->valid_lft;
2649 if (ci.ifa_prefered != INFINITY_LIFE_TIME) {
2650 long tval = (jiffies - ifa->tstamp)/HZ;
2651 ci.ifa_prefered -= tval;
2652 if (ci.ifa_valid != INFINITY_LIFE_TIME)
2653 ci.ifa_valid -= tval;
2654 }
2655 } else {
2656 ci.ifa_prefered = INFINITY_LIFE_TIME;
2657 ci.ifa_valid = INFINITY_LIFE_TIME;
2658 }
2659 ci.cstamp = (__u32)(TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) / HZ * 100
2660 + TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2661 ci.tstamp = (__u32)(TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) / HZ * 100
2662 + TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2663 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2664 nlh->nlmsg_len = skb->tail - b;
2665 return skb->len;
2666
2667nlmsg_failure:
2668rtattr_failure:
2669 skb_trim(skb, b - skb->data);
2670 return -1;
2671}
2672
2673static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
2674 u32 pid, u32 seq, int event)
2675{
2676 struct ifaddrmsg *ifm;
2677 struct nlmsghdr *nlh;
2678 struct ifa_cacheinfo ci;
2679 unsigned char *b = skb->tail;
2680
2681 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
2682 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
2683 ifm = NLMSG_DATA(nlh);
2684 ifm->ifa_family = AF_INET6;
2685 ifm->ifa_prefixlen = 128;
2686 ifm->ifa_flags = IFA_F_PERMANENT;
2687 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2688 if (ipv6_addr_scope(&ifmca->mca_addr)&IFA_SITE)
2689 ifm->ifa_scope = RT_SCOPE_SITE;
2690 ifm->ifa_index = ifmca->idev->dev->ifindex;
2691 RTA_PUT(skb, IFA_MULTICAST, 16, &ifmca->mca_addr);
2692 ci.cstamp = (__u32)(TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) / HZ
2693 * 100 + TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) % HZ
2694 * 100 / HZ);
2695 ci.tstamp = (__u32)(TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) / HZ
2696 * 100 + TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) % HZ
2697 * 100 / HZ);
2698 ci.ifa_prefered = INFINITY_LIFE_TIME;
2699 ci.ifa_valid = INFINITY_LIFE_TIME;
2700 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2701 nlh->nlmsg_len = skb->tail - b;
2702 return skb->len;
2703
2704nlmsg_failure:
2705rtattr_failure:
2706 skb_trim(skb, b - skb->data);
2707 return -1;
2708}
2709
2710static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
2711 u32 pid, u32 seq, int event)
2712{
2713 struct ifaddrmsg *ifm;
2714 struct nlmsghdr *nlh;
2715 struct ifa_cacheinfo ci;
2716 unsigned char *b = skb->tail;
2717
2718 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
2719 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
2720 ifm = NLMSG_DATA(nlh);
2721 ifm->ifa_family = AF_INET6;
2722 ifm->ifa_prefixlen = 128;
2723 ifm->ifa_flags = IFA_F_PERMANENT;
2724 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2725 if (ipv6_addr_scope(&ifaca->aca_addr)&IFA_SITE)
2726 ifm->ifa_scope = RT_SCOPE_SITE;
2727 ifm->ifa_index = ifaca->aca_idev->dev->ifindex;
2728 RTA_PUT(skb, IFA_ANYCAST, 16, &ifaca->aca_addr);
2729 ci.cstamp = (__u32)(TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) / HZ
2730 * 100 + TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) % HZ
2731 * 100 / HZ);
2732 ci.tstamp = (__u32)(TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) / HZ
2733 * 100 + TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) % HZ
2734 * 100 / HZ);
2735 ci.ifa_prefered = INFINITY_LIFE_TIME;
2736 ci.ifa_valid = INFINITY_LIFE_TIME;
2737 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2738 nlh->nlmsg_len = skb->tail - b;
2739 return skb->len;
2740
2741nlmsg_failure:
2742rtattr_failure:
2743 skb_trim(skb, b - skb->data);
2744 return -1;
2745}
2746
2747enum addr_type_t
2748{
2749 UNICAST_ADDR,
2750 MULTICAST_ADDR,
2751 ANYCAST_ADDR,
2752};
2753
2754static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
2755 enum addr_type_t type)
2756{
2757 int idx, ip_idx;
2758 int s_idx, s_ip_idx;
2759 int err = 1;
2760 struct net_device *dev;
2761 struct inet6_dev *idev = NULL;
2762 struct inet6_ifaddr *ifa;
2763 struct ifmcaddr6 *ifmca;
2764 struct ifacaddr6 *ifaca;
2765
2766 s_idx = cb->args[0];
2767 s_ip_idx = ip_idx = cb->args[1];
2768 read_lock(&dev_base_lock);
2769
2770 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
2771 if (idx < s_idx)
2772 continue;
2773 if (idx > s_idx)
2774 s_ip_idx = 0;
2775 ip_idx = 0;
2776 if ((idev = in6_dev_get(dev)) == NULL)
2777 continue;
2778 read_lock_bh(&idev->lock);
2779 switch (type) {
2780 case UNICAST_ADDR:
2781 /* unicast address */
2782 for (ifa = idev->addr_list; ifa;
2783 ifa = ifa->if_next, ip_idx++) {
2784 if (ip_idx < s_ip_idx)
2785 continue;
2786 if ((err = inet6_fill_ifaddr(skb, ifa,
2787 NETLINK_CB(cb->skb).pid,
2788 cb->nlh->nlmsg_seq, RTM_NEWADDR)) <= 0)
2789 goto done;
2790 }
2791 /* temp addr */
2792#ifdef CONFIG_IPV6_PRIVACY
2793 for (ifa = idev->tempaddr_list; ifa;
2794 ifa = ifa->tmp_next, ip_idx++) {
2795 if (ip_idx < s_ip_idx)
2796 continue;
2797 if ((err = inet6_fill_ifaddr(skb, ifa,
2798 NETLINK_CB(cb->skb).pid,
2799 cb->nlh->nlmsg_seq, RTM_NEWADDR)) <= 0)
2800 goto done;
2801 }
2802#endif
2803 break;
2804 case MULTICAST_ADDR:
2805 /* multicast address */
2806 for (ifmca = idev->mc_list; ifmca;
2807 ifmca = ifmca->next, ip_idx++) {
2808 if (ip_idx < s_ip_idx)
2809 continue;
2810 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
2811 NETLINK_CB(cb->skb).pid,
2812 cb->nlh->nlmsg_seq, RTM_GETMULTICAST)) <= 0)
2813 goto done;
2814 }
2815 break;
2816 case ANYCAST_ADDR:
2817 /* anycast address */
2818 for (ifaca = idev->ac_list; ifaca;
2819 ifaca = ifaca->aca_next, ip_idx++) {
2820 if (ip_idx < s_ip_idx)
2821 continue;
2822 if ((err = inet6_fill_ifacaddr(skb, ifaca,
2823 NETLINK_CB(cb->skb).pid,
2824 cb->nlh->nlmsg_seq, RTM_GETANYCAST)) <= 0)
2825 goto done;
2826 }
2827 break;
2828 default:
2829 break;
2830 }
2831 read_unlock_bh(&idev->lock);
2832 in6_dev_put(idev);
2833 }
2834done:
2835 if (err <= 0) {
2836 read_unlock_bh(&idev->lock);
2837 in6_dev_put(idev);
2838 }
2839 read_unlock(&dev_base_lock);
2840 cb->args[0] = idx;
2841 cb->args[1] = ip_idx;
2842 return skb->len;
2843}
2844
2845static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
2846{
2847 enum addr_type_t type = UNICAST_ADDR;
2848 return inet6_dump_addr(skb, cb, type);
2849}
2850
2851static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
2852{
2853 enum addr_type_t type = MULTICAST_ADDR;
2854 return inet6_dump_addr(skb, cb, type);
2855}
2856
2857
2858static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
2859{
2860 enum addr_type_t type = ANYCAST_ADDR;
2861 return inet6_dump_addr(skb, cb, type);
2862}
2863
2864static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
2865{
2866 struct sk_buff *skb;
2867 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
2868
2869 skb = alloc_skb(size, GFP_ATOMIC);
2870 if (!skb) {
2871 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFADDR, ENOBUFS);
2872 return;
2873 }
2874 if (inet6_fill_ifaddr(skb, ifa, 0, 0, event) < 0) {
2875 kfree_skb(skb);
2876 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFADDR, EINVAL);
2877 return;
2878 }
2879 NETLINK_CB(skb).dst_groups = RTMGRP_IPV6_IFADDR;
2880 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV6_IFADDR, GFP_ATOMIC);
2881}
2882
2883static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
2884 __s32 *array, int bytes)
2885{
2886 memset(array, 0, bytes);
2887 array[DEVCONF_FORWARDING] = cnf->forwarding;
2888 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
2889 array[DEVCONF_MTU6] = cnf->mtu6;
2890 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
2891 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
2892 array[DEVCONF_AUTOCONF] = cnf->autoconf;
2893 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
2894 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
2895 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
2896 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
2897 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
2898#ifdef CONFIG_IPV6_PRIVACY
2899 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
2900 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
2901 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
2902 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
2903 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
2904#endif
2905 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
2906}
2907
2908static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
2909 u32 pid, u32 seq, int event)
2910{
2911 struct net_device *dev = idev->dev;
2912 __s32 *array = NULL;
2913 struct ifinfomsg *r;
2914 struct nlmsghdr *nlh;
2915 unsigned char *b = skb->tail;
2916 struct rtattr *subattr;
2917 __u32 mtu = dev->mtu;
2918 struct ifla_cacheinfo ci;
2919
2920 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*r));
2921 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
2922 r = NLMSG_DATA(nlh);
2923 r->ifi_family = AF_INET6;
2924 r->ifi_type = dev->type;
2925 r->ifi_index = dev->ifindex;
2926 r->ifi_flags = dev_get_flags(dev);
2927 r->ifi_change = 0;
2928
2929 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
2930
2931 if (dev->addr_len)
2932 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
2933
2934 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
2935 if (dev->ifindex != dev->iflink)
2936 RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink);
2937
2938 subattr = (struct rtattr*)skb->tail;
2939
2940 RTA_PUT(skb, IFLA_PROTINFO, 0, NULL);
2941
2942 /* return the device flags */
2943 RTA_PUT(skb, IFLA_INET6_FLAGS, sizeof(__u32), &idev->if_flags);
2944
2945 /* return interface cacheinfo */
2946 ci.max_reasm_len = IPV6_MAXPLEN;
2947 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
2948 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2949 ci.reachable_time = idev->nd_parms->reachable_time;
2950 ci.retrans_time = idev->nd_parms->retrans_time;
2951 RTA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
2952
2953 /* return the device sysctl params */
2954 if ((array = kmalloc(DEVCONF_MAX * sizeof(*array), GFP_ATOMIC)) == NULL)
2955 goto rtattr_failure;
2956 ipv6_store_devconf(&idev->cnf, array, DEVCONF_MAX * sizeof(*array));
2957 RTA_PUT(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(*array), array);
2958
2959 /* XXX - Statistics/MC not implemented */
2960 subattr->rta_len = skb->tail - (u8*)subattr;
2961
2962 nlh->nlmsg_len = skb->tail - b;
2963 kfree(array);
2964 return skb->len;
2965
2966nlmsg_failure:
2967rtattr_failure:
2968 if (array)
2969 kfree(array);
2970 skb_trim(skb, b - skb->data);
2971 return -1;
2972}
2973
2974static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
2975{
2976 int idx, err;
2977 int s_idx = cb->args[0];
2978 struct net_device *dev;
2979 struct inet6_dev *idev;
2980
2981 read_lock(&dev_base_lock);
2982 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
2983 if (idx < s_idx)
2984 continue;
2985 if ((idev = in6_dev_get(dev)) == NULL)
2986 continue;
2987 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
2988 cb->nlh->nlmsg_seq, RTM_NEWLINK);
2989 in6_dev_put(idev);
2990 if (err <= 0)
2991 break;
2992 }
2993 read_unlock(&dev_base_lock);
2994 cb->args[0] = idx;
2995
2996 return skb->len;
2997}
2998
2999void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3000{
3001 struct sk_buff *skb;
3002 /* 128 bytes ?? */
3003 int size = NLMSG_SPACE(sizeof(struct ifinfomsg)+128);
3004
3005 skb = alloc_skb(size, GFP_ATOMIC);
3006 if (!skb) {
3007 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFINFO, ENOBUFS);
3008 return;
3009 }
3010 if (inet6_fill_ifinfo(skb, idev, 0, 0, event) < 0) {
3011 kfree_skb(skb);
3012 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFINFO, EINVAL);
3013 return;
3014 }
3015 NETLINK_CB(skb).dst_groups = RTMGRP_IPV6_IFINFO;
3016 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV6_IFINFO, GFP_ATOMIC);
3017}
3018
3019static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
3020 struct prefix_info *pinfo, u32 pid, u32 seq, int event)
3021{
3022 struct prefixmsg *pmsg;
3023 struct nlmsghdr *nlh;
3024 unsigned char *b = skb->tail;
3025 struct prefix_cacheinfo ci;
3026
3027 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*pmsg));
3028
3029 if (pid)
3030 nlh->nlmsg_flags |= NLM_F_MULTI;
3031
3032 pmsg = NLMSG_DATA(nlh);
3033 pmsg->prefix_family = AF_INET6;
3034 pmsg->prefix_ifindex = idev->dev->ifindex;
3035 pmsg->prefix_len = pinfo->prefix_len;
3036 pmsg->prefix_type = pinfo->type;
3037
3038 pmsg->prefix_flags = 0;
3039 if (pinfo->onlink)
3040 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3041 if (pinfo->autoconf)
3042 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3043
3044 RTA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
3045
3046 ci.preferred_time = ntohl(pinfo->prefered);
3047 ci.valid_time = ntohl(pinfo->valid);
3048 RTA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
3049
3050 nlh->nlmsg_len = skb->tail - b;
3051 return skb->len;
3052
3053nlmsg_failure:
3054rtattr_failure:
3055 skb_trim(skb, b - skb->data);
3056 return -1;
3057}
3058
3059static void inet6_prefix_notify(int event, struct inet6_dev *idev,
3060 struct prefix_info *pinfo)
3061{
3062 struct sk_buff *skb;
3063 int size = NLMSG_SPACE(sizeof(struct prefixmsg)+128);
3064
3065 skb = alloc_skb(size, GFP_ATOMIC);
3066 if (!skb) {
3067 netlink_set_err(rtnl, 0, RTMGRP_IPV6_PREFIX, ENOBUFS);
3068 return;
3069 }
3070 if (inet6_fill_prefix(skb, idev, pinfo, 0, 0, event) < 0) {
3071 kfree_skb(skb);
3072 netlink_set_err(rtnl, 0, RTMGRP_IPV6_PREFIX, EINVAL);
3073 return;
3074 }
3075 NETLINK_CB(skb).dst_groups = RTMGRP_IPV6_PREFIX;
3076 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV6_PREFIX, GFP_ATOMIC);
3077}
3078
3079static struct rtnetlink_link inet6_rtnetlink_table[RTM_MAX - RTM_BASE + 1] = {
3080 [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, },
3081 [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, },
3082 [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, },
3083 [RTM_GETADDR - RTM_BASE] = { .dumpit = inet6_dump_ifaddr, },
3084 [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
3085 [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
3086 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, },
3087 [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, },
3088 [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute,
3089 .dumpit = inet6_dump_fib, },
3090};
3091
3092static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3093{
3094 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3095
3096 switch (event) {
3097 case RTM_NEWADDR:
3098 dst_hold(&ifp->rt->u.dst);
3099 if (ip6_ins_rt(ifp->rt, NULL, NULL))
3100 dst_release(&ifp->rt->u.dst);
3101 if (ifp->idev->cnf.forwarding)
3102 addrconf_join_anycast(ifp);
3103 break;
3104 case RTM_DELADDR:
3105 if (ifp->idev->cnf.forwarding)
3106 addrconf_leave_anycast(ifp);
3107 addrconf_leave_solict(ifp->idev, &ifp->addr);
3108 dst_hold(&ifp->rt->u.dst);
3109 if (ip6_del_rt(ifp->rt, NULL, NULL))
3110 dst_free(&ifp->rt->u.dst);
3111 else
3112 dst_release(&ifp->rt->u.dst);
3113 break;
3114 }
3115}
3116
3117static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3118{
3119 read_lock_bh(&addrconf_lock);
3120 if (likely(ifp->idev->dead == 0))
3121 __ipv6_ifa_notify(event, ifp);
3122 read_unlock_bh(&addrconf_lock);
3123}
3124
3125#ifdef CONFIG_SYSCTL
3126
3127static
3128int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3129 void __user *buffer, size_t *lenp, loff_t *ppos)
3130{
3131 int *valp = ctl->data;
3132 int val = *valp;
3133 int ret;
3134
3135 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3136
3137 if (write && valp != &ipv6_devconf_dflt.forwarding) {
3138 if (valp != &ipv6_devconf.forwarding) {
3139 if ((!*valp) ^ (!val)) {
3140 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
3141 if (idev == NULL)
3142 return ret;
3143 dev_forward_change(idev);
3144 }
3145 } else {
3146 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3147 addrconf_forward_change();
3148 }
3149 if (*valp)
3150 rt6_purge_dflt_routers();
3151 }
3152
3153 return ret;
3154}
3155
3156static int addrconf_sysctl_forward_strategy(ctl_table *table,
3157 int __user *name, int nlen,
3158 void __user *oldval,
3159 size_t __user *oldlenp,
3160 void __user *newval, size_t newlen,
3161 void **context)
3162{
3163 int *valp = table->data;
3164 int new;
3165
3166 if (!newval || !newlen)
3167 return 0;
3168 if (newlen != sizeof(int))
3169 return -EINVAL;
3170 if (get_user(new, (int __user *)newval))
3171 return -EFAULT;
3172 if (new == *valp)
3173 return 0;
3174 if (oldval && oldlenp) {
3175 size_t len;
3176 if (get_user(len, oldlenp))
3177 return -EFAULT;
3178 if (len) {
3179 if (len > table->maxlen)
3180 len = table->maxlen;
3181 if (copy_to_user(oldval, valp, len))
3182 return -EFAULT;
3183 if (put_user(len, oldlenp))
3184 return -EFAULT;
3185 }
3186 }
3187
3188 if (valp != &ipv6_devconf_dflt.forwarding) {
3189 if (valp != &ipv6_devconf.forwarding) {
3190 struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
3191 int changed;
3192 if (unlikely(idev == NULL))
3193 return -ENODEV;
3194 changed = (!*valp) ^ (!new);
3195 *valp = new;
3196 if (changed)
3197 dev_forward_change(idev);
3198 } else {
3199 *valp = new;
3200 addrconf_forward_change();
3201 }
3202
3203 if (*valp)
3204 rt6_purge_dflt_routers();
3205 } else
3206 *valp = new;
3207
3208 return 1;
3209}
3210
3211static struct addrconf_sysctl_table
3212{
3213 struct ctl_table_header *sysctl_header;
3214 ctl_table addrconf_vars[__NET_IPV6_MAX];
3215 ctl_table addrconf_dev[2];
3216 ctl_table addrconf_conf_dir[2];
3217 ctl_table addrconf_proto_dir[2];
3218 ctl_table addrconf_root_dir[2];
3219} addrconf_sysctl = {
3220 .sysctl_header = NULL,
3221 .addrconf_vars = {
3222 {
3223 .ctl_name = NET_IPV6_FORWARDING,
3224 .procname = "forwarding",
3225 .data = &ipv6_devconf.forwarding,
3226 .maxlen = sizeof(int),
3227 .mode = 0644,
3228 .proc_handler = &addrconf_sysctl_forward,
3229 .strategy = &addrconf_sysctl_forward_strategy,
3230 },
3231 {
3232 .ctl_name = NET_IPV6_HOP_LIMIT,
3233 .procname = "hop_limit",
3234 .data = &ipv6_devconf.hop_limit,
3235 .maxlen = sizeof(int),
3236 .mode = 0644,
3237 .proc_handler = proc_dointvec,
3238 },
3239 {
3240 .ctl_name = NET_IPV6_MTU,
3241 .procname = "mtu",
3242 .data = &ipv6_devconf.mtu6,
3243 .maxlen = sizeof(int),
3244 .mode = 0644,
3245 .proc_handler = &proc_dointvec,
3246 },
3247 {
3248 .ctl_name = NET_IPV6_ACCEPT_RA,
3249 .procname = "accept_ra",
3250 .data = &ipv6_devconf.accept_ra,
3251 .maxlen = sizeof(int),
3252 .mode = 0644,
3253 .proc_handler = &proc_dointvec,
3254 },
3255 {
3256 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3257 .procname = "accept_redirects",
3258 .data = &ipv6_devconf.accept_redirects,
3259 .maxlen = sizeof(int),
3260 .mode = 0644,
3261 .proc_handler = &proc_dointvec,
3262 },
3263 {
3264 .ctl_name = NET_IPV6_AUTOCONF,
3265 .procname = "autoconf",
3266 .data = &ipv6_devconf.autoconf,
3267 .maxlen = sizeof(int),
3268 .mode = 0644,
3269 .proc_handler = &proc_dointvec,
3270 },
3271 {
3272 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3273 .procname = "dad_transmits",
3274 .data = &ipv6_devconf.dad_transmits,
3275 .maxlen = sizeof(int),
3276 .mode = 0644,
3277 .proc_handler = &proc_dointvec,
3278 },
3279 {
3280 .ctl_name = NET_IPV6_RTR_SOLICITS,
3281 .procname = "router_solicitations",
3282 .data = &ipv6_devconf.rtr_solicits,
3283 .maxlen = sizeof(int),
3284 .mode = 0644,
3285 .proc_handler = &proc_dointvec,
3286 },
3287 {
3288 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3289 .procname = "router_solicitation_interval",
3290 .data = &ipv6_devconf.rtr_solicit_interval,
3291 .maxlen = sizeof(int),
3292 .mode = 0644,
3293 .proc_handler = &proc_dointvec_jiffies,
3294 .strategy = &sysctl_jiffies,
3295 },
3296 {
3297 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3298 .procname = "router_solicitation_delay",
3299 .data = &ipv6_devconf.rtr_solicit_delay,
3300 .maxlen = sizeof(int),
3301 .mode = 0644,
3302 .proc_handler = &proc_dointvec_jiffies,
3303 .strategy = &sysctl_jiffies,
3304 },
3305 {
3306 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3307 .procname = "force_mld_version",
3308 .data = &ipv6_devconf.force_mld_version,
3309 .maxlen = sizeof(int),
3310 .mode = 0644,
3311 .proc_handler = &proc_dointvec,
3312 },
3313#ifdef CONFIG_IPV6_PRIVACY
3314 {
3315 .ctl_name = NET_IPV6_USE_TEMPADDR,
3316 .procname = "use_tempaddr",
3317 .data = &ipv6_devconf.use_tempaddr,
3318 .maxlen = sizeof(int),
3319 .mode = 0644,
3320 .proc_handler = &proc_dointvec,
3321 },
3322 {
3323 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3324 .procname = "temp_valid_lft",
3325 .data = &ipv6_devconf.temp_valid_lft,
3326 .maxlen = sizeof(int),
3327 .mode = 0644,
3328 .proc_handler = &proc_dointvec,
3329 },
3330 {
3331 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3332 .procname = "temp_prefered_lft",
3333 .data = &ipv6_devconf.temp_prefered_lft,
3334 .maxlen = sizeof(int),
3335 .mode = 0644,
3336 .proc_handler = &proc_dointvec,
3337 },
3338 {
3339 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3340 .procname = "regen_max_retry",
3341 .data = &ipv6_devconf.regen_max_retry,
3342 .maxlen = sizeof(int),
3343 .mode = 0644,
3344 .proc_handler = &proc_dointvec,
3345 },
3346 {
3347 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3348 .procname = "max_desync_factor",
3349 .data = &ipv6_devconf.max_desync_factor,
3350 .maxlen = sizeof(int),
3351 .mode = 0644,
3352 .proc_handler = &proc_dointvec,
3353 },
3354#endif
3355 {
3356 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3357 .procname = "max_addresses",
3358 .data = &ipv6_devconf.max_addresses,
3359 .maxlen = sizeof(int),
3360 .mode = 0644,
3361 .proc_handler = &proc_dointvec,
3362 },
3363 {
3364 .ctl_name = 0, /* sentinel */
3365 }
3366 },
3367 .addrconf_dev = {
3368 {
3369 .ctl_name = NET_PROTO_CONF_ALL,
3370 .procname = "all",
3371 .mode = 0555,
3372 .child = addrconf_sysctl.addrconf_vars,
3373 },
3374 {
3375 .ctl_name = 0, /* sentinel */
3376 }
3377 },
3378 .addrconf_conf_dir = {
3379 {
3380 .ctl_name = NET_IPV6_CONF,
3381 .procname = "conf",
3382 .mode = 0555,
3383 .child = addrconf_sysctl.addrconf_dev,
3384 },
3385 {
3386 .ctl_name = 0, /* sentinel */
3387 }
3388 },
3389 .addrconf_proto_dir = {
3390 {
3391 .ctl_name = NET_IPV6,
3392 .procname = "ipv6",
3393 .mode = 0555,
3394 .child = addrconf_sysctl.addrconf_conf_dir,
3395 },
3396 {
3397 .ctl_name = 0, /* sentinel */
3398 }
3399 },
3400 .addrconf_root_dir = {
3401 {
3402 .ctl_name = CTL_NET,
3403 .procname = "net",
3404 .mode = 0555,
3405 .child = addrconf_sysctl.addrconf_proto_dir,
3406 },
3407 {
3408 .ctl_name = 0, /* sentinel */
3409 }
3410 },
3411};
3412
3413static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
3414{
3415 int i;
3416 struct net_device *dev = idev ? idev->dev : NULL;
3417 struct addrconf_sysctl_table *t;
3418 char *dev_name = NULL;
3419
3420 t = kmalloc(sizeof(*t), GFP_KERNEL);
3421 if (t == NULL)
3422 return;
3423 memcpy(t, &addrconf_sysctl, sizeof(*t));
3424 for (i=0; t->addrconf_vars[i].data; i++) {
3425 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
3426 t->addrconf_vars[i].de = NULL;
3427 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
3428 }
3429 if (dev) {
3430 dev_name = dev->name;
3431 t->addrconf_dev[0].ctl_name = dev->ifindex;
3432 } else {
3433 dev_name = "default";
3434 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
3435 }
3436
3437 /*
3438 * Make a copy of dev_name, because '.procname' is regarded as const
3439 * by sysctl and we wouldn't want anyone to change it under our feet
3440 * (see SIOCSIFNAME).
3441 */
3442 dev_name = net_sysctl_strdup(dev_name);
3443 if (!dev_name)
3444 goto free;
3445
3446 t->addrconf_dev[0].procname = dev_name;
3447
3448 t->addrconf_dev[0].child = t->addrconf_vars;
3449 t->addrconf_dev[0].de = NULL;
3450 t->addrconf_conf_dir[0].child = t->addrconf_dev;
3451 t->addrconf_conf_dir[0].de = NULL;
3452 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
3453 t->addrconf_proto_dir[0].de = NULL;
3454 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
3455 t->addrconf_root_dir[0].de = NULL;
3456
3457 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
3458 if (t->sysctl_header == NULL)
3459 goto free_procname;
3460 else
3461 p->sysctl = t;
3462 return;
3463
3464 /* error path */
3465 free_procname:
3466 kfree(dev_name);
3467 free:
3468 kfree(t);
3469
3470 return;
3471}
3472
3473static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
3474{
3475 if (p->sysctl) {
3476 struct addrconf_sysctl_table *t = p->sysctl;
3477 p->sysctl = NULL;
3478 unregister_sysctl_table(t->sysctl_header);
3479 kfree(t->addrconf_dev[0].procname);
3480 kfree(t);
3481 }
3482}
3483
3484
3485#endif
3486
3487/*
3488 * Device notifier
3489 */
3490
3491int register_inet6addr_notifier(struct notifier_block *nb)
3492{
3493 return notifier_chain_register(&inet6addr_chain, nb);
3494}
3495
3496int unregister_inet6addr_notifier(struct notifier_block *nb)
3497{
3498 return notifier_chain_unregister(&inet6addr_chain,nb);
3499}
3500
3501/*
3502 * Init / cleanup code
3503 */
3504
3505int __init addrconf_init(void)
3506{
3507 int err = 0;
3508
3509 /* The addrconf netdev notifier requires that loopback_dev
3510 * has it's ipv6 private information allocated and setup
3511 * before it can bring up and give link-local addresses
3512 * to other devices which are up.
3513 *
3514 * Unfortunately, loopback_dev is not necessarily the first
3515 * entry in the global dev_base list of net devices. In fact,
3516 * it is likely to be the very last entry on that list.
3517 * So this causes the notifier registry below to try and
3518 * give link-local addresses to all devices besides loopback_dev
3519 * first, then loopback_dev, which cases all the non-loopback_dev
3520 * devices to fail to get a link-local address.
3521 *
3522 * So, as a temporary fix, allocate the ipv6 structure for
3523 * loopback_dev first by hand.
3524 * Longer term, all of the dependencies ipv6 has upon the loopback
3525 * device and it being up should be removed.
3526 */
3527 rtnl_lock();
3528 if (!ipv6_add_dev(&loopback_dev))
3529 err = -ENOMEM;
3530 rtnl_unlock();
3531 if (err)
3532 return err;
3533
3534 register_netdevice_notifier(&ipv6_dev_notf);
3535
3536#ifdef CONFIG_IPV6_PRIVACY
3537 md5_tfm = crypto_alloc_tfm("md5", 0);
3538 if (unlikely(md5_tfm == NULL))
3539 printk(KERN_WARNING
3540 "failed to load transform for md5\n");
3541#endif
3542
3543 addrconf_verify(0);
3544 rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
3545#ifdef CONFIG_SYSCTL
3546 addrconf_sysctl.sysctl_header =
3547 register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
3548 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
3549#endif
3550
3551 return 0;
3552}
3553
3554void __exit addrconf_cleanup(void)
3555{
3556 struct net_device *dev;
3557 struct inet6_dev *idev;
3558 struct inet6_ifaddr *ifa;
3559 int i;
3560
3561 unregister_netdevice_notifier(&ipv6_dev_notf);
3562
3563 rtnetlink_links[PF_INET6] = NULL;
3564#ifdef CONFIG_SYSCTL
3565 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
3566 addrconf_sysctl_unregister(&ipv6_devconf);
3567#endif
3568
3569 rtnl_lock();
3570
3571 /*
3572 * clean dev list.
3573 */
3574
3575 for (dev=dev_base; dev; dev=dev->next) {
3576 if ((idev = __in6_dev_get(dev)) == NULL)
3577 continue;
3578 addrconf_ifdown(dev, 1);
3579 }
3580 addrconf_ifdown(&loopback_dev, 2);
3581
3582 /*
3583 * Check hash table.
3584 */
3585
3586 write_lock_bh(&addrconf_hash_lock);
3587 for (i=0; i < IN6_ADDR_HSIZE; i++) {
3588 for (ifa=inet6_addr_lst[i]; ifa; ) {
3589 struct inet6_ifaddr *bifa;
3590
3591 bifa = ifa;
3592 ifa = ifa->lst_next;
3593 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
3594 /* Do not free it; something is wrong.
3595 Now we can investigate it with debugger.
3596 */
3597 }
3598 }
3599 write_unlock_bh(&addrconf_hash_lock);
3600
3601 del_timer(&addr_chk_timer);
3602
3603 rtnl_unlock();
3604
3605#ifdef CONFIG_IPV6_PRIVACY
3606 if (likely(md5_tfm != NULL)) {
3607 crypto_free_tfm(md5_tfm);
3608 md5_tfm = NULL;
3609 }
3610#endif
3611
3612#ifdef CONFIG_PROC_FS
3613 proc_net_remove("if_inet6");
3614#endif
3615}