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