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