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