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