blob: 452a82ce479681832698f3178b726f9cae6a2a69 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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.
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -080038 * YOSHIFUJI Hideaki @USAGI : improved source address
39 * selection; consider scope,
40 * status etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 */
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/errno.h>
44#include <linux/types.h>
45#include <linux/socket.h>
46#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/net.h>
48#include <linux/in6.h>
49#include <linux/netdevice.h>
Thomas Graf18237302006-08-04 23:04:54 -070050#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/if_arp.h>
52#include <linux/if_arcnet.h>
53#include <linux/if_infiniband.h>
54#include <linux/route.h>
55#include <linux/inetdevice.h>
56#include <linux/init.h>
57#ifdef CONFIG_SYSCTL
58#include <linux/sysctl.h>
59#endif
Randy Dunlap4fc268d2006-01-11 12:17:47 -080060#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/delay.h>
62#include <linux/notifier.h>
Paulo Marques543537b2005-06-23 00:09:02 -070063#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65#include <net/sock.h>
66#include <net/snmp.h>
67
68#include <net/ipv6.h>
69#include <net/protocol.h>
70#include <net/ndisc.h>
71#include <net/ip6_route.h>
72#include <net/addrconf.h>
73#include <net/tcp.h>
74#include <net/ip.h>
Thomas Graf5d620262006-08-15 00:35:02 -070075#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/if_tunnel.h>
77#include <linux/rtnetlink.h>
78
79#ifdef CONFIG_IPV6_PRIVACY
80#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#endif
82
83#include <asm/uaccess.h>
84
85#include <linux/proc_fs.h>
86#include <linux/seq_file.h>
87
88/* Set to 3 to get tracing... */
89#define ACONF_DEBUG 2
90
91#if ACONF_DEBUG >= 3
92#define ADBG(x) printk x
93#else
94#define ADBG(x)
95#endif
96
97#define INFINITY_LIFE_TIME 0xFFFFFFFF
98#define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
99
100#ifdef CONFIG_SYSCTL
101static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
102static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
103#endif
104
105#ifdef CONFIG_IPV6_PRIVACY
106static int __ipv6_regen_rndid(struct inet6_dev *idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900107static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static void ipv6_regen_rndid(unsigned long data);
109
110static int desync_factor = MAX_DESYNC_FACTOR * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#endif
112
113static int ipv6_count_addresses(struct inet6_dev *idev);
114
115/*
116 * Configured unicast address hash table
117 */
118static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];
119static DEFINE_RWLOCK(addrconf_hash_lock);
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121static void addrconf_verify(unsigned long);
122
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700123static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static DEFINE_SPINLOCK(addrconf_verify_lock);
125
126static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
127static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
128
129static int addrconf_ifdown(struct net_device *dev, int how);
130
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700131static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static void addrconf_dad_timer(unsigned long data);
133static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900134static void addrconf_dad_run(struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static void addrconf_rs_timer(unsigned long data);
136static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
137static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
138
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900139static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 struct prefix_info *pinfo);
141static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
142
Alan Sterne041c682006-03-27 01:16:30 -0800143static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Brian Haleyab32ea52006-09-22 14:15:41 -0700145struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 .forwarding = 0,
147 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
148 .mtu6 = IPV6_MIN_MTU,
149 .accept_ra = 1,
150 .accept_redirects = 1,
151 .autoconf = 1,
152 .force_mld_version = 0,
153 .dad_transmits = 1,
154 .rtr_solicits = MAX_RTR_SOLICITATIONS,
155 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
156 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
157#ifdef CONFIG_IPV6_PRIVACY
158 .use_tempaddr = 0,
159 .temp_valid_lft = TEMP_VALID_LIFETIME,
160 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
161 .regen_max_retry = REGEN_MAX_RETRY,
162 .max_desync_factor = MAX_DESYNC_FACTOR,
163#endif
164 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800165 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800166 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800167#ifdef CONFIG_IPV6_ROUTER_PREF
168 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800169 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800170#ifdef CONFIG_IPV6_ROUTE_INFO
171 .accept_ra_rt_info_max_plen = 0,
172#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800173#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700174 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700175 .accept_source_route = 0, /* we do not accept RH0 by default. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176};
177
Brian Haleyab32ea52006-09-22 14:15:41 -0700178static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 .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,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800197 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800198 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800199#ifdef CONFIG_IPV6_ROUTER_PREF
200 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800201 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800202#ifdef CONFIG_IPV6_ROUTE_INFO
203 .accept_ra_rt_info_max_plen = 0,
204#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800205#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700206 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700207 .accept_source_route = 0, /* we do not accept RH0 by default. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208};
209
210/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
211#if 0
212const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
213#endif
214const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216static void addrconf_del_timer(struct inet6_ifaddr *ifp)
217{
218 if (del_timer(&ifp->timer))
219 __in6_ifa_put(ifp);
220}
221
222enum addrconf_timer_t
223{
224 AC_NONE,
225 AC_DAD,
226 AC_RS,
227};
228
229static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
230 enum addrconf_timer_t what,
231 unsigned long when)
232{
233 if (!del_timer(&ifp->timer))
234 in6_ifa_hold(ifp);
235
236 switch (what) {
237 case AC_DAD:
238 ifp->timer.function = addrconf_dad_timer;
239 break;
240 case AC_RS:
241 ifp->timer.function = addrconf_rs_timer;
242 break;
243 default:;
244 }
245 ifp->timer.expires = jiffies + when;
246 add_timer(&ifp->timer);
247}
248
249/* Nobody refers to this device, we may destroy it. */
250
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700251static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
252{
253 struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
254 kfree(idev);
255}
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257void in6_dev_finish_destroy(struct inet6_dev *idev)
258{
259 struct net_device *dev = idev->dev;
260 BUG_TRAP(idev->addr_list==NULL);
261 BUG_TRAP(idev->mc_list==NULL);
262#ifdef NET_REFCNT_DEBUG
263 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
264#endif
265 dev_put(dev);
266 if (!idev->dead) {
267 printk("Freeing alive inet6 device %p\n", idev);
268 return;
269 }
270 snmp6_free_dev(idev);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700271 call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
274static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
275{
276 struct inet6_dev *ndev;
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800277 struct in6_addr maddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 ASSERT_RTNL();
280
281 if (dev->mtu < IPV6_MIN_MTU)
282 return NULL;
283
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900284 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900286 if (ndev == NULL)
287 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Ingo Oeser322f74a2006-03-20 23:01:47 -0800289 rwlock_init(&ndev->lock);
290 ndev->dev = dev;
291 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
292 ndev->cnf.mtu6 = dev->mtu;
293 ndev->cnf.sysctl = NULL;
294 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
295 if (ndev->nd_parms == NULL) {
296 kfree(ndev);
297 return NULL;
298 }
299 /* We refer to the device */
300 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Ingo Oeser322f74a2006-03-20 23:01:47 -0800302 if (snmp6_alloc_dev(ndev) < 0) {
303 ADBG((KERN_WARNING
304 "%s(): cannot allocate memory for statistics; dev=%s.\n",
305 __FUNCTION__, dev->name));
306 neigh_parms_release(&nd_tbl, ndev->nd_parms);
307 ndev->dead = 1;
308 in6_dev_finish_destroy(ndev);
309 return NULL;
310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Ingo Oeser322f74a2006-03-20 23:01:47 -0800312 if (snmp6_register_dev(ndev) < 0) {
313 ADBG((KERN_WARNING
314 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
315 __FUNCTION__, dev->name));
316 neigh_parms_release(&nd_tbl, ndev->nd_parms);
317 ndev->dead = 1;
318 in6_dev_finish_destroy(ndev);
319 return NULL;
320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Ingo Oeser322f74a2006-03-20 23:01:47 -0800322 /* One reference from device. We must do this before
323 * we invoke __ipv6_regen_rndid().
324 */
325 in6_dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327#ifdef CONFIG_IPV6_PRIVACY
Ingo Oeser322f74a2006-03-20 23:01:47 -0800328 init_timer(&ndev->regen_timer);
329 ndev->regen_timer.function = ipv6_regen_rndid;
330 ndev->regen_timer.data = (unsigned long) ndev;
331 if ((dev->flags&IFF_LOOPBACK) ||
332 dev->type == ARPHRD_TUNNEL ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700333#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
334 dev->type == ARPHRD_SIT ||
335#endif
336 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800337 printk(KERN_INFO
338 "%s: Disabled Privacy Extensions\n",
339 dev->name);
340 ndev->cnf.use_tempaddr = -1;
341 } else {
342 in6_dev_hold(ndev);
343 ipv6_regen_rndid((unsigned long) ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Ingo Oeser322f74a2006-03-20 23:01:47 -0800345#endif
346
Herbert Xu53aadcc2007-03-27 14:31:52 -0700347 if (netif_running(dev) && netif_carrier_ok(dev))
348 ndev->if_flags |= IF_READY;
349
Ingo Oeser322f74a2006-03-20 23:01:47 -0800350 ipv6_mc_init_dev(ndev);
351 ndev->tstamp = jiffies;
352#ifdef CONFIG_SYSCTL
353 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
354 NET_IPV6_NEIGH, "ipv6",
355 &ndisc_ifinfo_sysctl_change,
356 NULL);
357 addrconf_sysctl_register(ndev, &ndev->cnf);
358#endif
David L Stevens30c4cf52007-01-04 12:31:14 -0800359 /* protected by rtnl_lock */
360 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800361
362 /* Join all-node multicast group */
363 ipv6_addr_all_nodes(&maddr);
364 ipv6_dev_mc_inc(dev, &maddr);
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return ndev;
367}
368
369static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
370{
371 struct inet6_dev *idev;
372
373 ASSERT_RTNL();
374
375 if ((idev = __in6_dev_get(dev)) == NULL) {
376 if ((idev = ipv6_add_dev(dev)) == NULL)
377 return NULL;
378 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (dev->flags&IFF_UP)
381 ipv6_mc_up(idev);
382 return idev;
383}
384
385#ifdef CONFIG_SYSCTL
386static void dev_forward_change(struct inet6_dev *idev)
387{
388 struct net_device *dev;
389 struct inet6_ifaddr *ifa;
390 struct in6_addr addr;
391
392 if (!idev)
393 return;
394 dev = idev->dev;
395 if (dev && (dev->flags & IFF_MULTICAST)) {
396 ipv6_addr_all_routers(&addr);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (idev->cnf.forwarding)
399 ipv6_dev_mc_inc(dev, &addr);
400 else
401 ipv6_dev_mc_dec(dev, &addr);
402 }
403 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800404 if (ifa->flags&IFA_F_TENTATIVE)
405 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (idev->cnf.forwarding)
407 addrconf_join_anycast(ifa);
408 else
409 addrconf_leave_anycast(ifa);
410 }
411}
412
413
414static void addrconf_forward_change(void)
415{
416 struct net_device *dev;
417 struct inet6_dev *idev;
418
419 read_lock(&dev_base_lock);
420 for (dev=dev_base; dev; dev=dev->next) {
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700421 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 idev = __in6_dev_get(dev);
423 if (idev) {
424 int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
425 idev->cnf.forwarding = ipv6_devconf.forwarding;
426 if (changed)
427 dev_forward_change(idev);
428 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700429 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431 read_unlock(&dev_base_lock);
432}
433#endif
434
435/* Nobody refers to this ifaddr, destroy it */
436
437void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
438{
439 BUG_TRAP(ifp->if_next==NULL);
440 BUG_TRAP(ifp->lst_next==NULL);
441#ifdef NET_REFCNT_DEBUG
442 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
443#endif
444
445 in6_dev_put(ifp->idev);
446
447 if (del_timer(&ifp->timer))
448 printk("Timer is still running, when freeing ifa=%p\n", ifp);
449
450 if (!ifp->dead) {
451 printk("Freeing alive inet6 address %p\n", ifp);
452 return;
453 }
454 dst_release(&ifp->rt->u.dst);
455
456 kfree(ifp);
457}
458
Brian Haleye55ffac2006-07-10 15:25:51 -0700459static void
460ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
461{
462 struct inet6_ifaddr *ifa, **ifap;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700463 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700464
465 /*
466 * Each device address list is sorted in order of scope -
467 * global before linklocal.
468 */
469 for (ifap = &idev->addr_list; (ifa = *ifap) != NULL;
470 ifap = &ifa->if_next) {
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700471 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700472 break;
473 }
474
475 ifp->if_next = *ifap;
476 *ifap = ifp;
477}
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479/* On success it returns ifp with increased reference count */
480
481static struct inet6_ifaddr *
482ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700483 int scope, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 struct inet6_ifaddr *ifa = NULL;
486 struct rt6_info *rt;
487 int hash;
488 int err = 0;
489
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700490 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (idev->dead) {
492 err = -ENODEV; /*XXX*/
493 goto out2;
494 }
495
496 write_lock(&addrconf_hash_lock);
497
498 /* Ignore adding duplicate addresses on an interface */
499 if (ipv6_chk_same_addr(addr, idev->dev)) {
500 ADBG(("ipv6_add_addr: already assigned\n"));
501 err = -EEXIST;
502 goto out;
503 }
504
Ingo Oeser322f74a2006-03-20 23:01:47 -0800505 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 if (ifa == NULL) {
508 ADBG(("ipv6_add_addr: malloc failed\n"));
509 err = -ENOBUFS;
510 goto out;
511 }
512
513 rt = addrconf_dst_alloc(idev, addr, 0);
514 if (IS_ERR(rt)) {
515 err = PTR_ERR(rt);
516 goto out;
517 }
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 ipv6_addr_copy(&ifa->addr, addr);
520
521 spin_lock_init(&ifa->lock);
522 init_timer(&ifa->timer);
523 ifa->timer.data = (unsigned long) ifa;
524 ifa->scope = scope;
525 ifa->prefix_len = pfxlen;
526 ifa->flags = flags | IFA_F_TENTATIVE;
527 ifa->cstamp = ifa->tstamp = jiffies;
528
Keir Fraser57f5f542006-08-29 02:43:49 -0700529 ifa->rt = rt;
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 ifa->idev = idev;
532 in6_dev_hold(idev);
533 /* For caller */
534 in6_ifa_hold(ifa);
535
536 /* Add to big hash table */
537 hash = ipv6_addr_hash(addr);
538
539 ifa->lst_next = inet6_addr_lst[hash];
540 inet6_addr_lst[hash] = ifa;
541 in6_ifa_hold(ifa);
542 write_unlock(&addrconf_hash_lock);
543
544 write_lock(&idev->lock);
545 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -0700546 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548#ifdef CONFIG_IPV6_PRIVACY
549 if (ifa->flags&IFA_F_TEMPORARY) {
550 ifa->tmp_next = idev->tempaddr_list;
551 idev->tempaddr_list = ifa;
552 in6_ifa_hold(ifa);
553 }
554#endif
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 in6_ifa_hold(ifa);
557 write_unlock(&idev->lock);
558out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700559 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -0700561 if (likely(err == 0))
Alan Sterne041c682006-03-27 01:16:30 -0800562 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 else {
564 kfree(ifa);
565 ifa = ERR_PTR(err);
566 }
567
568 return ifa;
569out:
570 write_unlock(&addrconf_hash_lock);
571 goto out2;
572}
573
574/* This function wants to get referenced ifp and releases it before return */
575
576static void ipv6_del_addr(struct inet6_ifaddr *ifp)
577{
578 struct inet6_ifaddr *ifa, **ifap;
579 struct inet6_dev *idev = ifp->idev;
580 int hash;
581 int deleted = 0, onlink = 0;
582 unsigned long expires = jiffies;
583
584 hash = ipv6_addr_hash(&ifp->addr);
585
586 ifp->dead = 1;
587
588 write_lock_bh(&addrconf_hash_lock);
589 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
590 ifap = &ifa->lst_next) {
591 if (ifa == ifp) {
592 *ifap = ifa->lst_next;
593 __in6_ifa_put(ifp);
594 ifa->lst_next = NULL;
595 break;
596 }
597 }
598 write_unlock_bh(&addrconf_hash_lock);
599
600 write_lock_bh(&idev->lock);
601#ifdef CONFIG_IPV6_PRIVACY
602 if (ifp->flags&IFA_F_TEMPORARY) {
603 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
604 ifap = &ifa->tmp_next) {
605 if (ifa == ifp) {
606 *ifap = ifa->tmp_next;
607 if (ifp->ifpub) {
608 in6_ifa_put(ifp->ifpub);
609 ifp->ifpub = NULL;
610 }
611 __in6_ifa_put(ifp);
612 ifa->tmp_next = NULL;
613 break;
614 }
615 }
616 }
617#endif
618
Kristian Slavov1d142802005-12-21 18:47:24 -0800619 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (ifa == ifp) {
621 *ifap = ifa->if_next;
622 __in6_ifa_put(ifp);
623 ifa->if_next = NULL;
624 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
625 break;
626 deleted = 1;
Kristian Slavov1d142802005-12-21 18:47:24 -0800627 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 } else if (ifp->flags & IFA_F_PERMANENT) {
629 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
630 ifp->prefix_len)) {
631 if (ifa->flags & IFA_F_PERMANENT) {
632 onlink = 1;
633 if (deleted)
634 break;
635 } else {
636 unsigned long lifetime;
637
638 if (!onlink)
639 onlink = -1;
640
641 spin_lock(&ifa->lock);
642 lifetime = min_t(unsigned long,
643 ifa->valid_lft, 0x7fffffffUL/HZ);
644 if (time_before(expires,
645 ifa->tstamp + lifetime * HZ))
646 expires = ifa->tstamp + lifetime * HZ;
647 spin_unlock(&ifa->lock);
648 }
649 }
650 }
Kristian Slavov1d142802005-12-21 18:47:24 -0800651 ifap = &ifa->if_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653 write_unlock_bh(&idev->lock);
654
655 ipv6_ifa_notify(RTM_DELADDR, ifp);
656
Alan Sterne041c682006-03-27 01:16:30 -0800657 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 addrconf_del_timer(ifp);
660
661 /*
662 * Purge or update corresponding prefix
663 *
664 * 1) we don't purge prefix here if address was not permanent.
665 * prefix is managed by its own lifetime.
666 * 2) if there're no addresses, delete prefix.
667 * 3) if there're still other permanent address(es),
668 * corresponding prefix is still permanent.
669 * 4) otherwise, update prefix lifetime to the
670 * longest valid lifetime among the corresponding
671 * addresses on the device.
672 * Note: subsequent RA will update lifetime.
673 *
674 * --yoshfuji
675 */
676 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
677 struct in6_addr prefix;
678 struct rt6_info *rt;
679
680 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
681 rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
682
683 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
684 if (onlink == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700685 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 rt = NULL;
687 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
688 rt->rt6i_expires = expires;
689 rt->rt6i_flags |= RTF_EXPIRES;
690 }
691 }
692 dst_release(&rt->u.dst);
693 }
694
695 in6_ifa_put(ifp);
696}
697
698#ifdef CONFIG_IPV6_PRIVACY
699static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
700{
701 struct inet6_dev *idev = ifp->idev;
702 struct in6_addr addr, *tmpaddr;
703 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
704 int tmp_plen;
705 int ret = 0;
706 int max_addresses;
707
708 write_lock(&idev->lock);
709 if (ift) {
710 spin_lock_bh(&ift->lock);
711 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
712 spin_unlock_bh(&ift->lock);
713 tmpaddr = &addr;
714 } else {
715 tmpaddr = NULL;
716 }
717retry:
718 in6_dev_hold(idev);
719 if (idev->cnf.use_tempaddr <= 0) {
720 write_unlock(&idev->lock);
721 printk(KERN_INFO
722 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
723 in6_dev_put(idev);
724 ret = -1;
725 goto out;
726 }
727 spin_lock_bh(&ifp->lock);
728 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
729 idev->cnf.use_tempaddr = -1; /*XXX*/
730 spin_unlock_bh(&ifp->lock);
731 write_unlock(&idev->lock);
732 printk(KERN_WARNING
733 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
734 in6_dev_put(idev);
735 ret = -1;
736 goto out;
737 }
738 in6_ifa_hold(ifp);
739 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
740 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
741 spin_unlock_bh(&ifp->lock);
742 write_unlock(&idev->lock);
743 printk(KERN_WARNING
744 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
745 in6_ifa_put(ifp);
746 in6_dev_put(idev);
747 ret = -1;
748 goto out;
749 }
750 memcpy(&addr.s6_addr[8], idev->rndid, 8);
751 tmp_valid_lft = min_t(__u32,
752 ifp->valid_lft,
753 idev->cnf.temp_valid_lft);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900754 tmp_prefered_lft = min_t(__u32,
755 ifp->prefered_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 idev->cnf.temp_prefered_lft - desync_factor / HZ);
757 tmp_plen = ifp->prefix_len;
758 max_addresses = idev->cnf.max_addresses;
759 tmp_cstamp = ifp->cstamp;
760 tmp_tstamp = ifp->tstamp;
761 spin_unlock_bh(&ifp->lock);
762
763 write_unlock(&idev->lock);
764 ift = !max_addresses ||
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900765 ipv6_count_addresses(idev) < max_addresses ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 ipv6_add_addr(idev, &addr, tmp_plen,
767 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : NULL;
768 if (!ift || IS_ERR(ift)) {
769 in6_ifa_put(ifp);
770 in6_dev_put(idev);
771 printk(KERN_INFO
772 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
773 tmpaddr = &addr;
774 write_lock(&idev->lock);
775 goto retry;
776 }
777
778 spin_lock_bh(&ift->lock);
779 ift->ifpub = ifp;
780 ift->valid_lft = tmp_valid_lft;
781 ift->prefered_lft = tmp_prefered_lft;
782 ift->cstamp = tmp_cstamp;
783 ift->tstamp = tmp_tstamp;
784 spin_unlock_bh(&ift->lock);
785
786 addrconf_dad_start(ift, 0);
787 in6_ifa_put(ift);
788 in6_dev_put(idev);
789out:
790 return ret;
791}
792#endif
793
794/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800795 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 */
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800797struct ipv6_saddr_score {
798 int addr_type;
799 unsigned int attrs;
800 int matchlen;
Brian Haley0d27b422006-03-11 18:50:14 -0800801 int scope;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800802 unsigned int rule;
803};
804
805#define IPV6_SADDR_SCORE_LOCAL 0x0001
806#define IPV6_SADDR_SCORE_PREFERRED 0x0004
807#define IPV6_SADDR_SCORE_HOA 0x0008
808#define IPV6_SADDR_SCORE_OIF 0x0010
809#define IPV6_SADDR_SCORE_LABEL 0x0020
810#define IPV6_SADDR_SCORE_PRIVACY 0x0040
811
Dave Jonesb6f99a22007-03-22 12:27:49 -0700812static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800814 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
815 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
816 return 1;
817 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800820/* static matching label */
Dave Jonesb6f99a22007-03-22 12:27:49 -0700821static inline int ipv6_saddr_label(const struct in6_addr *addr, int type)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800822{
823 /*
824 * prefix (longest match) label
825 * -----------------------------
826 * ::1/128 0
827 * ::/0 1
828 * 2002::/16 2
829 * ::/96 3
830 * ::ffff:0:0/96 4
Łukasz Stelmach102128e2006-06-22 01:37:19 -0700831 * fc00::/7 5
832 * 2001::/32 6
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800833 */
834 if (type & IPV6_ADDR_LOOPBACK)
835 return 0;
836 else if (type & IPV6_ADDR_COMPATv4)
837 return 3;
838 else if (type & IPV6_ADDR_MAPPED)
839 return 4;
Łukasz Stelmach102128e2006-06-22 01:37:19 -0700840 else if (addr->s6_addr32[0] == htonl(0x20010000))
841 return 6;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800842 else if (addr->s6_addr16[0] == htons(0x2002))
843 return 2;
Łukasz Stelmach102128e2006-06-22 01:37:19 -0700844 else if ((addr->s6_addr[0] & 0xfe) == 0xfc)
845 return 5;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800846 return 1;
847}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800849int ipv6_dev_get_saddr(struct net_device *daddr_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 struct in6_addr *daddr, struct in6_addr *saddr)
851{
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800852 struct ipv6_saddr_score hiscore;
853 struct inet6_ifaddr *ifa_result = NULL;
854 int daddr_type = __ipv6_addr_type(daddr);
855 int daddr_scope = __ipv6_addr_src_scope(daddr_type);
856 u32 daddr_label = ipv6_saddr_label(daddr, daddr_type);
857 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800859 memset(&hiscore, 0, sizeof(hiscore));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 read_lock(&dev_base_lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700862 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 for (dev = dev_base; dev; dev=dev->next) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800865 struct inet6_dev *idev;
866 struct inet6_ifaddr *ifa;
867
868 /* Rule 0: Candidate Source Address (section 4)
869 * - multicast and link-local destination address,
870 * the set of candidate source address MUST only
871 * include addresses assigned to interfaces
872 * belonging to the same link as the outgoing
873 * interface.
874 * (- For site-local destination addresses, the
875 * set of candidate source addresses MUST only
876 * include addresses assigned to interfaces
877 * belonging to the same site as the outgoing
878 * interface.)
879 */
880 if ((daddr_type & IPV6_ADDR_MULTICAST ||
881 daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
882 daddr_dev && dev != daddr_dev)
883 continue;
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 idev = __in6_dev_get(dev);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800886 if (!idev)
887 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800889 read_lock_bh(&idev->lock);
890 for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
891 struct ipv6_saddr_score score;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800893 score.addr_type = __ipv6_addr_type(&ifa->addr);
894
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +0900895 /* Rule 0:
896 * - Tentative Address (RFC2462 section 5.4)
897 * - A tentative address is not considered
898 * "assigned to an interface" in the traditional
899 * sense.
900 * - Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800901 * - In any case, anycast addresses, multicast
902 * addresses, and the unspecified address MUST
903 * NOT be included in a candidate set.
904 */
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +0900905 if (ifa->flags & IFA_F_TENTATIVE)
906 continue;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800907 if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
908 score.addr_type & IPV6_ADDR_MULTICAST)) {
909 LIMIT_NETDEBUG(KERN_DEBUG
910 "ADDRCONF: unspecified / multicast address"
911 "assigned as unicast address on %s",
912 dev->name);
913 continue;
914 }
915
916 score.attrs = 0;
917 score.matchlen = 0;
918 score.scope = 0;
919 score.rule = 0;
920
921 if (ifa_result == NULL) {
922 /* record it if the first available entry */
923 goto record_it;
924 }
925
926 /* Rule 1: Prefer same address */
927 if (hiscore.rule < 1) {
928 if (ipv6_addr_equal(&ifa_result->addr, daddr))
929 hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
930 hiscore.rule++;
931 }
932 if (ipv6_addr_equal(&ifa->addr, daddr)) {
933 score.attrs |= IPV6_SADDR_SCORE_LOCAL;
934 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
935 score.rule = 1;
936 goto record_it;
937 }
938 } else {
939 if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
940 continue;
941 }
942
943 /* Rule 2: Prefer appropriate scope */
944 if (hiscore.rule < 2) {
945 hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
946 hiscore.rule++;
947 }
948 score.scope = __ipv6_addr_src_scope(score.addr_type);
949 if (hiscore.scope < score.scope) {
950 if (hiscore.scope < daddr_scope) {
951 score.rule = 2;
952 goto record_it;
953 } else
954 continue;
955 } else if (score.scope < hiscore.scope) {
956 if (score.scope < daddr_scope)
Brian Haleye55ffac2006-07-10 15:25:51 -0700957 break; /* addresses sorted by scope */
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800958 else {
959 score.rule = 2;
960 goto record_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800964 /* Rule 3: Avoid deprecated address */
965 if (hiscore.rule < 3) {
966 if (ipv6_saddr_preferred(hiscore.addr_type) ||
967 !(ifa_result->flags & IFA_F_DEPRECATED))
968 hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
969 hiscore.rule++;
970 }
971 if (ipv6_saddr_preferred(score.addr_type) ||
972 !(ifa->flags & IFA_F_DEPRECATED)) {
973 score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
974 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
975 score.rule = 3;
976 goto record_it;
977 }
978 } else {
979 if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
980 continue;
981 }
982
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -0700983 /* Rule 4: Prefer home address */
984#ifdef CONFIG_IPV6_MIP6
985 if (hiscore.rule < 4) {
986 if (ifa_result->flags & IFA_F_HOMEADDRESS)
987 hiscore.attrs |= IPV6_SADDR_SCORE_HOA;
988 hiscore.rule++;
989 }
990 if (ifa->flags & IFA_F_HOMEADDRESS) {
991 score.attrs |= IPV6_SADDR_SCORE_HOA;
992 if (!(ifa_result->flags & IFA_F_HOMEADDRESS)) {
993 score.rule = 4;
994 goto record_it;
995 }
996 } else {
997 if (hiscore.attrs & IPV6_SADDR_SCORE_HOA)
998 continue;
999 }
1000#else
YOSHIFUJI Hideaki220bbd72005-11-28 22:27:11 -08001001 if (hiscore.rule < 4)
1002 hiscore.rule++;
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07001003#endif
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001004
1005 /* Rule 5: Prefer outgoing interface */
1006 if (hiscore.rule < 5) {
1007 if (daddr_dev == NULL ||
1008 daddr_dev == ifa_result->idev->dev)
1009 hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1010 hiscore.rule++;
1011 }
1012 if (daddr_dev == NULL ||
1013 daddr_dev == ifa->idev->dev) {
1014 score.attrs |= IPV6_SADDR_SCORE_OIF;
1015 if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1016 score.rule = 5;
1017 goto record_it;
1018 }
1019 } else {
1020 if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1021 continue;
1022 }
1023
1024 /* Rule 6: Prefer matching label */
1025 if (hiscore.rule < 6) {
1026 if (ipv6_saddr_label(&ifa_result->addr, hiscore.addr_type) == daddr_label)
1027 hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1028 hiscore.rule++;
1029 }
1030 if (ipv6_saddr_label(&ifa->addr, score.addr_type) == daddr_label) {
1031 score.attrs |= IPV6_SADDR_SCORE_LABEL;
1032 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1033 score.rule = 6;
1034 goto record_it;
1035 }
1036 } else {
1037 if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1038 continue;
1039 }
1040
Peter Chubb44fd0262005-11-09 13:05:47 -08001041#ifdef CONFIG_IPV6_PRIVACY
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001042 /* Rule 7: Prefer public address
1043 * Note: prefer temprary address if use_tempaddr >= 2
1044 */
1045 if (hiscore.rule < 7) {
1046 if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1047 (ifa_result->idev->cnf.use_tempaddr >= 2))
1048 hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1049 hiscore.rule++;
1050 }
1051 if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1052 (ifa->idev->cnf.use_tempaddr >= 2)) {
1053 score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1054 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1055 score.rule = 7;
1056 goto record_it;
1057 }
1058 } else {
1059 if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1060 continue;
1061 }
YOSHIFUJI Hideaki5e2707f2006-06-22 01:41:18 -07001062#else
1063 if (hiscore.rule < 7)
1064 hiscore.rule++;
Peter Chubb44fd0262005-11-09 13:05:47 -08001065#endif
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001066 /* Rule 8: Use longest matching prefix */
Yan Zheng12da2a42005-11-14 21:42:46 -08001067 if (hiscore.rule < 8) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001068 hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
Yan Zheng12da2a42005-11-14 21:42:46 -08001069 hiscore.rule++;
1070 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001071 score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1072 if (score.matchlen > hiscore.matchlen) {
1073 score.rule = 8;
1074 goto record_it;
1075 }
1076#if 0
1077 else if (score.matchlen < hiscore.matchlen)
1078 continue;
1079#endif
1080
1081 /* Final Rule: choose first available one */
1082 continue;
1083record_it:
1084 if (ifa_result)
1085 in6_ifa_put(ifa_result);
1086 in6_ifa_hold(ifa);
1087 ifa_result = ifa;
1088 hiscore = score;
1089 }
1090 read_unlock_bh(&idev->lock);
1091 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001092 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 read_unlock(&dev_base_lock);
1094
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001095 if (!ifa_result)
1096 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001097
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001098 ipv6_addr_copy(saddr, &ifa_result->addr);
1099 in6_ifa_put(ifa_result);
1100 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102
1103
1104int ipv6_get_saddr(struct dst_entry *dst,
1105 struct in6_addr *daddr, struct in6_addr *saddr)
1106{
YOSHIFUJI Hideaki7a3025b2006-10-13 16:17:25 +09001107 return ipv6_dev_get_saddr(dst ? ip6_dst_idev(dst)->dev : NULL, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
1110
1111int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
1112{
1113 struct inet6_dev *idev;
1114 int err = -EADDRNOTAVAIL;
1115
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001116 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if ((idev = __in6_dev_get(dev)) != NULL) {
1118 struct inet6_ifaddr *ifp;
1119
1120 read_lock_bh(&idev->lock);
1121 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1122 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1123 ipv6_addr_copy(addr, &ifp->addr);
1124 err = 0;
1125 break;
1126 }
1127 }
1128 read_unlock_bh(&idev->lock);
1129 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001130 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return err;
1132}
1133
1134static int ipv6_count_addresses(struct inet6_dev *idev)
1135{
1136 int cnt = 0;
1137 struct inet6_ifaddr *ifp;
1138
1139 read_lock_bh(&idev->lock);
1140 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1141 cnt++;
1142 read_unlock_bh(&idev->lock);
1143 return cnt;
1144}
1145
1146int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
1147{
1148 struct inet6_ifaddr * ifp;
1149 u8 hash = ipv6_addr_hash(addr);
1150
1151 read_lock_bh(&addrconf_hash_lock);
1152 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1153 if (ipv6_addr_equal(&ifp->addr, addr) &&
1154 !(ifp->flags&IFA_F_TENTATIVE)) {
1155 if (dev == NULL || ifp->idev->dev == dev ||
1156 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1157 break;
1158 }
1159 }
1160 read_unlock_bh(&addrconf_hash_lock);
1161 return ifp != NULL;
1162}
1163
1164static
1165int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
1166{
1167 struct inet6_ifaddr * ifp;
1168 u8 hash = ipv6_addr_hash(addr);
1169
1170 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1171 if (ipv6_addr_equal(&ifp->addr, addr)) {
1172 if (dev == NULL || ifp->idev->dev == dev)
1173 break;
1174 }
1175 }
1176 return ifp != NULL;
1177}
1178
1179struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
1180{
1181 struct inet6_ifaddr * ifp;
1182 u8 hash = ipv6_addr_hash(addr);
1183
1184 read_lock_bh(&addrconf_hash_lock);
1185 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1186 if (ipv6_addr_equal(&ifp->addr, addr)) {
1187 if (dev == NULL || ifp->idev->dev == dev ||
1188 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1189 in6_ifa_hold(ifp);
1190 break;
1191 }
1192 }
1193 }
1194 read_unlock_bh(&addrconf_hash_lock);
1195
1196 return ifp;
1197}
1198
1199int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1200{
1201 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
Arnaldo Carvalho de Melo0fa1a532005-12-13 23:23:09 -08001202 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
Al Viro82103232006-09-27 18:44:10 -07001203 __be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1204 __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 int sk_ipv6only = ipv6_only_sock(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001206 int sk2_ipv6only = inet_v6_ipv6only(sk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1208 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1209
1210 if (!sk2_rcv_saddr && !sk_ipv6only)
1211 return 1;
1212
1213 if (addr_type2 == IPV6_ADDR_ANY &&
1214 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1215 return 1;
1216
1217 if (addr_type == IPV6_ADDR_ANY &&
1218 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1219 return 1;
1220
1221 if (sk2_rcv_saddr6 &&
1222 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1223 return 1;
1224
1225 if (addr_type == IPV6_ADDR_MAPPED &&
1226 !sk2_ipv6only &&
1227 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1228 return 1;
1229
1230 return 0;
1231}
1232
1233/* Gets referenced address, destroys ifaddr */
1234
Adrian Bunk9f5336e2006-01-07 13:24:25 -08001235static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (ifp->flags&IFA_F_PERMANENT) {
1238 spin_lock_bh(&ifp->lock);
1239 addrconf_del_timer(ifp);
1240 ifp->flags |= IFA_F_TENTATIVE;
1241 spin_unlock_bh(&ifp->lock);
1242 in6_ifa_put(ifp);
1243#ifdef CONFIG_IPV6_PRIVACY
1244 } else if (ifp->flags&IFA_F_TEMPORARY) {
1245 struct inet6_ifaddr *ifpub;
1246 spin_lock_bh(&ifp->lock);
1247 ifpub = ifp->ifpub;
1248 if (ifpub) {
1249 in6_ifa_hold(ifpub);
1250 spin_unlock_bh(&ifp->lock);
1251 ipv6_create_tempaddr(ifpub, ifp);
1252 in6_ifa_put(ifpub);
1253 } else {
1254 spin_unlock_bh(&ifp->lock);
1255 }
1256 ipv6_del_addr(ifp);
1257#endif
1258 } else
1259 ipv6_del_addr(ifp);
1260}
1261
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001262void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1263{
1264 if (net_ratelimit())
1265 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1266 addrconf_dad_stop(ifp);
1267}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269/* Join to solicited addr multicast group. */
1270
1271void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1272{
1273 struct in6_addr maddr;
1274
1275 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1276 return;
1277
1278 addrconf_addr_solict_mult(addr, &maddr);
1279 ipv6_dev_mc_inc(dev, &maddr);
1280}
1281
1282void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1283{
1284 struct in6_addr maddr;
1285
1286 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1287 return;
1288
1289 addrconf_addr_solict_mult(addr, &maddr);
1290 __ipv6_dev_mc_dec(idev, &maddr);
1291}
1292
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001293static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
1295 struct in6_addr addr;
1296 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1297 if (ipv6_addr_any(&addr))
1298 return;
1299 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1300}
1301
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001302static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 struct in6_addr addr;
1305 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1306 if (ipv6_addr_any(&addr))
1307 return;
1308 __ipv6_dev_ac_dec(ifp->idev, &addr);
1309}
1310
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001311static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1312{
1313 if (dev->addr_len != ETH_ALEN)
1314 return -1;
1315 memcpy(eui, dev->dev_addr, 3);
1316 memcpy(eui + 5, dev->dev_addr + 3, 3);
1317
1318 /*
1319 * The zSeries OSA network cards can be shared among various
1320 * OS instances, but the OSA cards have only one MAC address.
1321 * This leads to duplicate address conflicts in conjunction
1322 * with IPv6 if more than one instance uses the same card.
1323 *
1324 * The driver for these cards can deliver a unique 16-bit
1325 * identifier for each instance sharing the same card. It is
1326 * placed instead of 0xFFFE in the interface identifier. The
1327 * "u" bit of the interface identifier is not inverted in this
1328 * case. Hence the resulting interface identifier has local
1329 * scope according to RFC2373.
1330 */
1331 if (dev->dev_id) {
1332 eui[3] = (dev->dev_id >> 8) & 0xFF;
1333 eui[4] = dev->dev_id & 0xFF;
1334 } else {
1335 eui[3] = 0xFF;
1336 eui[4] = 0xFE;
1337 eui[0] ^= 2;
1338 }
1339 return 0;
1340}
1341
1342static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1343{
1344 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1345 if (dev->addr_len != ARCNET_ALEN)
1346 return -1;
1347 memset(eui, 0, 7);
1348 eui[7] = *(u8*)dev->dev_addr;
1349 return 0;
1350}
1351
1352static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1353{
1354 if (dev->addr_len != INFINIBAND_ALEN)
1355 return -1;
1356 memcpy(eui, dev->dev_addr + 12, 8);
1357 eui[0] |= 2;
1358 return 0;
1359}
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1362{
1363 switch (dev->type) {
1364 case ARPHRD_ETHER:
1365 case ARPHRD_FDDI:
1366 case ARPHRD_IEEE802_TR:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001367 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001369 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001371 return addrconf_ifid_infiniband(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
1373 return -1;
1374}
1375
1376static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1377{
1378 int err = -1;
1379 struct inet6_ifaddr *ifp;
1380
1381 read_lock_bh(&idev->lock);
1382 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1383 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1384 memcpy(eui, ifp->addr.s6_addr+8, 8);
1385 err = 0;
1386 break;
1387 }
1388 }
1389 read_unlock_bh(&idev->lock);
1390 return err;
1391}
1392
1393#ifdef CONFIG_IPV6_PRIVACY
1394/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1395static int __ipv6_regen_rndid(struct inet6_dev *idev)
1396{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08001398 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 /*
1402 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1403 * check if generated address is not inappropriate
1404 *
1405 * - Reserved subnet anycast (RFC 2526)
1406 * 11111101 11....11 1xxxxxxx
1407 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1408 * 00-00-5E-FE-xx-xx-xx-xx
1409 * - value 0
1410 * - XXX: already assigned to an address on the device
1411 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001412 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1414 (idev->rndid[7]&0x80))
1415 goto regen;
1416 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1417 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1418 goto regen;
1419 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1420 goto regen;
1421 }
1422
1423 return 0;
1424}
1425
1426static void ipv6_regen_rndid(unsigned long data)
1427{
1428 struct inet6_dev *idev = (struct inet6_dev *) data;
1429 unsigned long expires;
1430
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001431 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 write_lock_bh(&idev->lock);
1433
1434 if (idev->dead)
1435 goto out;
1436
1437 if (__ipv6_regen_rndid(idev) < 0)
1438 goto out;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 expires = jiffies +
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001441 idev->cnf.temp_prefered_lft * HZ -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1443 if (time_before(expires, jiffies)) {
1444 printk(KERN_WARNING
1445 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1446 idev->dev->name);
1447 goto out;
1448 }
1449
1450 if (!mod_timer(&idev->regen_timer, expires))
1451 in6_dev_hold(idev);
1452
1453out:
1454 write_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001455 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 in6_dev_put(idev);
1457}
1458
1459static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1460 int ret = 0;
1461
1462 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1463 ret = __ipv6_regen_rndid(idev);
1464 return ret;
1465}
1466#endif
1467
1468/*
1469 * Add prefix route.
1470 */
1471
1472static void
1473addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001474 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
Thomas Graf86872cb2006-08-22 00:01:08 -07001476 struct fib6_config cfg = {
1477 .fc_table = RT6_TABLE_PREFIX,
1478 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1479 .fc_ifindex = dev->ifindex,
1480 .fc_expires = expires,
1481 .fc_dst_len = plen,
1482 .fc_flags = RTF_UP | flags,
1483 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Thomas Graf86872cb2006-08-22 00:01:08 -07001485 ipv6_addr_copy(&cfg.fc_dst, pfx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487 /* Prevent useless cloning on PtP SIT.
1488 This thing is done here expecting that the whole
1489 class of non-broadcast devices need not cloning.
1490 */
Joerg Roedel0be669b2006-10-10 14:49:53 -07001491#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Thomas Graf86872cb2006-08-22 00:01:08 -07001492 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1493 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07001494#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Thomas Graf86872cb2006-08-22 00:01:08 -07001496 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
1498
1499/* Create "default" multicast route to the interface */
1500
1501static void addrconf_add_mroute(struct net_device *dev)
1502{
Thomas Graf86872cb2006-08-22 00:01:08 -07001503 struct fib6_config cfg = {
1504 .fc_table = RT6_TABLE_LOCAL,
1505 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1506 .fc_ifindex = dev->ifindex,
1507 .fc_dst_len = 8,
1508 .fc_flags = RTF_UP,
1509 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Thomas Graf86872cb2006-08-22 00:01:08 -07001511 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1512
1513 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514}
1515
Joerg Roedel0be669b2006-10-10 14:49:53 -07001516#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517static void sit_route_add(struct net_device *dev)
1518{
Thomas Graf86872cb2006-08-22 00:01:08 -07001519 struct fib6_config cfg = {
1520 .fc_table = RT6_TABLE_MAIN,
1521 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1522 .fc_ifindex = dev->ifindex,
1523 .fc_dst_len = 96,
1524 .fc_flags = RTF_UP | RTF_NONEXTHOP,
1525 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 /* prefix length - 96 bits "::d.d.d.d" */
Thomas Graf86872cb2006-08-22 00:01:08 -07001528 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
Joerg Roedel0be669b2006-10-10 14:49:53 -07001530#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532static void addrconf_add_lroute(struct net_device *dev)
1533{
1534 struct in6_addr addr;
1535
1536 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1537 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1538}
1539
1540static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1541{
1542 struct inet6_dev *idev;
1543
1544 ASSERT_RTNL();
1545
1546 if ((idev = ipv6_find_idev(dev)) == NULL)
1547 return NULL;
1548
1549 /* Add default multicast route */
1550 addrconf_add_mroute(dev);
1551
1552 /* Add link local route */
1553 addrconf_add_lroute(dev);
1554 return idev;
1555}
1556
1557void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1558{
1559 struct prefix_info *pinfo;
1560 __u32 valid_lft;
1561 __u32 prefered_lft;
1562 int addr_type;
1563 unsigned long rt_expires;
1564 struct inet6_dev *in6_dev;
1565
1566 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (len < sizeof(struct prefix_info)) {
1569 ADBG(("addrconf: prefix option too short\n"));
1570 return;
1571 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 /*
1574 * Validation checks ([ADDRCONF], page 19)
1575 */
1576
1577 addr_type = ipv6_addr_type(&pinfo->prefix);
1578
1579 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1580 return;
1581
1582 valid_lft = ntohl(pinfo->valid);
1583 prefered_lft = ntohl(pinfo->prefered);
1584
1585 if (prefered_lft > valid_lft) {
1586 if (net_ratelimit())
1587 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1588 return;
1589 }
1590
1591 in6_dev = in6_dev_get(dev);
1592
1593 if (in6_dev == NULL) {
1594 if (net_ratelimit())
1595 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1596 return;
1597 }
1598
1599 /*
1600 * Two things going on here:
1601 * 1) Add routes for on-link prefixes
1602 * 2) Configure prefixes with the auto flag set
1603 */
1604
1605 /* Avoid arithmetic overflow. Really, we could
1606 save rt_expires in seconds, likely valid_lft,
1607 but it would require division in fib gc, that it
1608 not good.
1609 */
1610 if (valid_lft >= 0x7FFFFFFF/HZ)
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001611 rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 else
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001613 rt_expires = valid_lft * HZ;
1614
1615 /*
1616 * We convert this (in jiffies) to clock_t later.
1617 * Avoid arithmetic overflow there as well.
1618 * Overflow can happen only if HZ < USER_HZ.
1619 */
1620 if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
1621 rt_expires = 0x7FFFFFFF / USER_HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623 if (pinfo->onlink) {
1624 struct rt6_info *rt;
1625 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1626
1627 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1628 if (rt->rt6i_flags&RTF_EXPIRES) {
1629 if (valid_lft == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001630 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 rt = NULL;
1632 } else {
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001633 rt->rt6i_expires = jiffies + rt_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
1635 }
1636 } else if (valid_lft) {
1637 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001638 dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
1640 if (rt)
1641 dst_release(&rt->u.dst);
1642 }
1643
1644 /* Try to figure out our local address for this prefix */
1645
1646 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1647 struct inet6_ifaddr * ifp;
1648 struct in6_addr addr;
1649 int create = 0, update_lft = 0;
1650
1651 if (pinfo->prefix_len == 64) {
1652 memcpy(&addr, &pinfo->prefix, 8);
1653 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1654 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1655 in6_dev_put(in6_dev);
1656 return;
1657 }
1658 goto ok;
1659 }
1660 if (net_ratelimit())
1661 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1662 pinfo->prefix_len);
1663 in6_dev_put(in6_dev);
1664 return;
1665
1666ok:
1667
1668 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1669
1670 if (ifp == NULL && valid_lft) {
1671 int max_addresses = in6_dev->cnf.max_addresses;
1672
1673 /* Do not allow to create too much of autoconfigured
1674 * addresses; this would be too easy way to crash kernel.
1675 */
1676 if (!max_addresses ||
1677 ipv6_count_addresses(in6_dev) < max_addresses)
1678 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1679 addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1680
1681 if (!ifp || IS_ERR(ifp)) {
1682 in6_dev_put(in6_dev);
1683 return;
1684 }
1685
1686 update_lft = create = 1;
1687 ifp->cstamp = jiffies;
1688 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1689 }
1690
1691 if (ifp) {
1692 int flags;
1693 unsigned long now;
1694#ifdef CONFIG_IPV6_PRIVACY
1695 struct inet6_ifaddr *ift;
1696#endif
1697 u32 stored_lft;
1698
1699 /* update lifetime (RFC2462 5.5.3 e) */
1700 spin_lock(&ifp->lock);
1701 now = jiffies;
1702 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1703 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1704 else
1705 stored_lft = 0;
1706 if (!update_lft && stored_lft) {
1707 if (valid_lft > MIN_VALID_LIFETIME ||
1708 valid_lft > stored_lft)
1709 update_lft = 1;
1710 else if (stored_lft <= MIN_VALID_LIFETIME) {
1711 /* valid_lft <= stored_lft is always true */
1712 /* XXX: IPsec */
1713 update_lft = 0;
1714 } else {
1715 valid_lft = MIN_VALID_LIFETIME;
1716 if (valid_lft < prefered_lft)
1717 prefered_lft = valid_lft;
1718 update_lft = 1;
1719 }
1720 }
1721
1722 if (update_lft) {
1723 ifp->valid_lft = valid_lft;
1724 ifp->prefered_lft = prefered_lft;
1725 ifp->tstamp = now;
1726 flags = ifp->flags;
1727 ifp->flags &= ~IFA_F_DEPRECATED;
1728 spin_unlock(&ifp->lock);
1729
1730 if (!(flags&IFA_F_TENTATIVE))
1731 ipv6_ifa_notify(0, ifp);
1732 } else
1733 spin_unlock(&ifp->lock);
1734
1735#ifdef CONFIG_IPV6_PRIVACY
1736 read_lock_bh(&in6_dev->lock);
1737 /* update all temporary addresses in the list */
1738 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1739 /*
1740 * When adjusting the lifetimes of an existing
1741 * temporary address, only lower the lifetimes.
1742 * Implementations must not increase the
1743 * lifetimes of an existing temporary address
1744 * when processing a Prefix Information Option.
1745 */
1746 spin_lock(&ift->lock);
1747 flags = ift->flags;
1748 if (ift->valid_lft > valid_lft &&
1749 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1750 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1751 if (ift->prefered_lft > prefered_lft &&
1752 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1753 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1754 spin_unlock(&ift->lock);
1755 if (!(flags&IFA_F_TENTATIVE))
1756 ipv6_ifa_notify(0, ift);
1757 }
1758
1759 if (create && in6_dev->cnf.use_tempaddr > 0) {
1760 /*
1761 * When a new public address is created as described in [ADDRCONF],
1762 * also create a new temporary address.
1763 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001764 read_unlock_bh(&in6_dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 ipv6_create_tempaddr(ifp, NULL);
1766 } else {
1767 read_unlock_bh(&in6_dev->lock);
1768 }
1769#endif
1770 in6_ifa_put(ifp);
1771 addrconf_verify(0);
1772 }
1773 }
1774 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1775 in6_dev_put(in6_dev);
1776}
1777
1778/*
1779 * Set destination address.
1780 * Special case for SIT interfaces where we create a new "virtual"
1781 * device.
1782 */
1783int addrconf_set_dstaddr(void __user *arg)
1784{
1785 struct in6_ifreq ireq;
1786 struct net_device *dev;
1787 int err = -EINVAL;
1788
1789 rtnl_lock();
1790
1791 err = -EFAULT;
1792 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1793 goto err_exit;
1794
1795 dev = __dev_get_by_index(ireq.ifr6_ifindex);
1796
1797 err = -ENODEV;
1798 if (dev == NULL)
1799 goto err_exit;
1800
Joerg Roedel0be669b2006-10-10 14:49:53 -07001801#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 if (dev->type == ARPHRD_SIT) {
1803 struct ifreq ifr;
1804 mm_segment_t oldfs;
1805 struct ip_tunnel_parm p;
1806
1807 err = -EADDRNOTAVAIL;
1808 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1809 goto err_exit;
1810
1811 memset(&p, 0, sizeof(p));
1812 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1813 p.iph.saddr = 0;
1814 p.iph.version = 4;
1815 p.iph.ihl = 5;
1816 p.iph.protocol = IPPROTO_IPV6;
1817 p.iph.ttl = 64;
1818 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1819
1820 oldfs = get_fs(); set_fs(KERNEL_DS);
1821 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1822 set_fs(oldfs);
1823
1824 if (err == 0) {
1825 err = -ENOBUFS;
1826 if ((dev = __dev_get_by_name(p.name)) == NULL)
1827 goto err_exit;
1828 err = dev_open(dev);
1829 }
1830 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07001831#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833err_exit:
1834 rtnl_unlock();
1835 return err;
1836}
1837
1838/*
1839 * Manual configuration of address on an interface
1840 */
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001841static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07001842 __u8 ifa_flags, __u32 prefered_lft, __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
1844 struct inet6_ifaddr *ifp;
1845 struct inet6_dev *idev;
1846 struct net_device *dev;
1847 int scope;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09001848 u32 flags = RTF_EXPIRES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001851
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001852 /* check the lifetime */
1853 if (!valid_lft || prefered_lft > valid_lft)
1854 return -EINVAL;
1855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1857 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 if ((idev = addrconf_add_dev(dev)) == NULL)
1860 return -ENOBUFS;
1861
1862 scope = ipv6_addr_scope(pfx);
1863
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09001864 if (valid_lft == INFINITY_LIFE_TIME) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001865 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09001866 flags = 0;
1867 } else if (valid_lft >= 0x7FFFFFFF/HZ)
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001868 valid_lft = 0x7FFFFFFF/HZ;
1869
1870 if (prefered_lft == 0)
1871 ifa_flags |= IFA_F_DEPRECATED;
1872 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
1873 (prefered_lft != INFINITY_LIFE_TIME))
1874 prefered_lft = 0x7FFFFFFF/HZ;
1875
1876 ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
1877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if (!IS_ERR(ifp)) {
Herbert Xu06aebfb2006-08-09 16:52:04 -07001879 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001880 ifp->valid_lft = valid_lft;
1881 ifp->prefered_lft = prefered_lft;
1882 ifp->tstamp = jiffies;
Herbert Xu06aebfb2006-08-09 16:52:04 -07001883 spin_unlock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001884
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09001885 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
1886 jiffies_to_clock_t(valid_lft * HZ), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 addrconf_dad_start(ifp, 0);
1888 in6_ifa_put(ifp);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001889 addrconf_verify(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 return 0;
1891 }
1892
1893 return PTR_ERR(ifp);
1894}
1895
1896static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1897{
1898 struct inet6_ifaddr *ifp;
1899 struct inet6_dev *idev;
1900 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1903 return -ENODEV;
1904
1905 if ((idev = __in6_dev_get(dev)) == NULL)
1906 return -ENXIO;
1907
1908 read_lock_bh(&idev->lock);
1909 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1910 if (ifp->prefix_len == plen &&
1911 ipv6_addr_equal(pfx, &ifp->addr)) {
1912 in6_ifa_hold(ifp);
1913 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 ipv6_del_addr(ifp);
1916
1917 /* If the last address is deleted administratively,
1918 disable IPv6 on this interface.
1919 */
1920 if (idev->addr_list == NULL)
1921 addrconf_ifdown(idev->dev, 1);
1922 return 0;
1923 }
1924 }
1925 read_unlock_bh(&idev->lock);
1926 return -EADDRNOTAVAIL;
1927}
1928
1929
1930int addrconf_add_ifaddr(void __user *arg)
1931{
1932 struct in6_ifreq ireq;
1933 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 if (!capable(CAP_NET_ADMIN))
1936 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1939 return -EFAULT;
1940
1941 rtnl_lock();
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001942 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07001943 IFA_F_PERMANENT, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 rtnl_unlock();
1945 return err;
1946}
1947
1948int addrconf_del_ifaddr(void __user *arg)
1949{
1950 struct in6_ifreq ireq;
1951 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if (!capable(CAP_NET_ADMIN))
1954 return -EPERM;
1955
1956 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1957 return -EFAULT;
1958
1959 rtnl_lock();
1960 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1961 rtnl_unlock();
1962 return err;
1963}
1964
Joerg Roedel0be669b2006-10-10 14:49:53 -07001965#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966static void sit_add_v4_addrs(struct inet6_dev *idev)
1967{
1968 struct inet6_ifaddr * ifp;
1969 struct in6_addr addr;
1970 struct net_device *dev;
1971 int scope;
1972
1973 ASSERT_RTNL();
1974
1975 memset(&addr, 0, sizeof(struct in6_addr));
1976 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
1977
1978 if (idev->dev->flags&IFF_POINTOPOINT) {
1979 addr.s6_addr32[0] = htonl(0xfe800000);
1980 scope = IFA_LINK;
1981 } else {
1982 scope = IPV6_ADDR_COMPATv4;
1983 }
1984
1985 if (addr.s6_addr32[3]) {
1986 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
1987 if (!IS_ERR(ifp)) {
1988 spin_lock_bh(&ifp->lock);
1989 ifp->flags &= ~IFA_F_TENTATIVE;
1990 spin_unlock_bh(&ifp->lock);
1991 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1992 in6_ifa_put(ifp);
1993 }
1994 return;
1995 }
1996
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001997 for (dev = dev_base; dev != NULL; dev = dev->next) {
Herbert Xue5ed6392005-10-03 14:35:55 -07001998 struct in_device * in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (in_dev && (dev->flags & IFF_UP)) {
2000 struct in_ifaddr * ifa;
2001
2002 int flag = scope;
2003
2004 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2005 int plen;
2006
2007 addr.s6_addr32[3] = ifa->ifa_local;
2008
2009 if (ifa->ifa_scope == RT_SCOPE_LINK)
2010 continue;
2011 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2012 if (idev->dev->flags&IFF_POINTOPOINT)
2013 continue;
2014 flag |= IFA_HOST;
2015 }
2016 if (idev->dev->flags&IFF_POINTOPOINT)
2017 plen = 64;
2018 else
2019 plen = 96;
2020
2021 ifp = ipv6_add_addr(idev, &addr, plen, flag,
2022 IFA_F_PERMANENT);
2023 if (!IS_ERR(ifp)) {
2024 spin_lock_bh(&ifp->lock);
2025 ifp->flags &= ~IFA_F_TENTATIVE;
2026 spin_unlock_bh(&ifp->lock);
2027 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2028 in6_ifa_put(ifp);
2029 }
2030 }
2031 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002034#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036static void init_loopback(struct net_device *dev)
2037{
2038 struct inet6_dev *idev;
2039 struct inet6_ifaddr * ifp;
2040
2041 /* ::1 */
2042
2043 ASSERT_RTNL();
2044
2045 if ((idev = ipv6_find_idev(dev)) == NULL) {
2046 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2047 return;
2048 }
2049
2050 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2051 if (!IS_ERR(ifp)) {
2052 spin_lock_bh(&ifp->lock);
2053 ifp->flags &= ~IFA_F_TENTATIVE;
2054 spin_unlock_bh(&ifp->lock);
2055 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2056 in6_ifa_put(ifp);
2057 }
2058}
2059
2060static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2061{
2062 struct inet6_ifaddr * ifp;
2063
2064 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
2065 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002066 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 addrconf_dad_start(ifp, 0);
2068 in6_ifa_put(ifp);
2069 }
2070}
2071
2072static void addrconf_dev_config(struct net_device *dev)
2073{
2074 struct in6_addr addr;
2075 struct inet6_dev * idev;
2076
2077 ASSERT_RTNL();
2078
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002079 if ((dev->type != ARPHRD_ETHER) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 (dev->type != ARPHRD_FDDI) &&
2081 (dev->type != ARPHRD_IEEE802_TR) &&
2082 (dev->type != ARPHRD_ARCNET) &&
2083 (dev->type != ARPHRD_INFINIBAND)) {
2084 /* Alas, we support only Ethernet autoconfiguration. */
2085 return;
2086 }
2087
2088 idev = addrconf_add_dev(dev);
2089 if (idev == NULL)
2090 return;
2091
2092 memset(&addr, 0, sizeof(struct in6_addr));
2093 addr.s6_addr32[0] = htonl(0xFE800000);
2094
2095 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2096 addrconf_add_linklocal(idev, &addr);
2097}
2098
Joerg Roedel0be669b2006-10-10 14:49:53 -07002099#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100static void addrconf_sit_config(struct net_device *dev)
2101{
2102 struct inet6_dev *idev;
2103
2104 ASSERT_RTNL();
2105
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002106 /*
2107 * Configure the tunnel with one of our IPv4
2108 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 * our v4 addrs in the tunnel
2110 */
2111
2112 if ((idev = ipv6_find_idev(dev)) == NULL) {
2113 printk(KERN_DEBUG "init sit: add_dev failed\n");
2114 return;
2115 }
2116
2117 sit_add_v4_addrs(idev);
2118
2119 if (dev->flags&IFF_POINTOPOINT) {
2120 addrconf_add_mroute(dev);
2121 addrconf_add_lroute(dev);
2122 } else
2123 sit_route_add(dev);
2124}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002125#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
2127static inline int
2128ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2129{
2130 struct in6_addr lladdr;
2131
2132 if (!ipv6_get_lladdr(link_dev, &lladdr)) {
2133 addrconf_add_linklocal(idev, &lladdr);
2134 return 0;
2135 }
2136 return -1;
2137}
2138
2139static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2140{
2141 struct net_device *link_dev;
2142
2143 /* first try to inherit the link-local address from the link device */
2144 if (idev->dev->iflink &&
2145 (link_dev = __dev_get_by_index(idev->dev->iflink))) {
2146 if (!ipv6_inherit_linklocal(idev, link_dev))
2147 return;
2148 }
2149 /* then try to inherit it from any device */
2150 for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
2151 if (!ipv6_inherit_linklocal(idev, link_dev))
2152 return;
2153 }
2154 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2155}
2156
2157/*
2158 * Autoconfigure tunnel with a link-local address so routing protocols,
2159 * DHCPv6, MLD etc. can be run over the virtual link
2160 */
2161
2162static void addrconf_ip6_tnl_config(struct net_device *dev)
2163{
2164 struct inet6_dev *idev;
2165
2166 ASSERT_RTNL();
2167
2168 if ((idev = addrconf_add_dev(dev)) == NULL) {
2169 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2170 return;
2171 }
2172 ip6_tnl_add_linklocal(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173}
2174
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002175static int addrconf_notify(struct notifier_block *this, unsigned long event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 void * data)
2177{
2178 struct net_device *dev = (struct net_device *) data;
2179 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002180 int run_pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 switch(event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002183 case NETDEV_REGISTER:
2184 if (!idev) {
2185 idev = ipv6_add_dev(dev);
2186 if (!idev)
2187 printk(KERN_WARNING "IPv6: add_dev failed for %s\n",
2188 dev->name);
2189 }
2190 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002192 case NETDEV_CHANGE:
2193 if (event == NETDEV_UP) {
2194 if (!netif_carrier_ok(dev)) {
2195 /* device is not ready yet. */
2196 printk(KERN_INFO
2197 "ADDRCONF(NETDEV_UP): %s: "
2198 "link is not ready\n",
2199 dev->name);
2200 break;
2201 }
Kristian Slavov99081042006-02-08 16:10:53 -08002202
2203 if (idev)
2204 idev->if_flags |= IF_READY;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002205 } else {
2206 if (!netif_carrier_ok(dev)) {
2207 /* device is still not ready. */
2208 break;
2209 }
2210
2211 if (idev) {
2212 if (idev->if_flags & IF_READY) {
2213 /* device is already configured. */
2214 break;
2215 }
2216 idev->if_flags |= IF_READY;
2217 }
2218
2219 printk(KERN_INFO
2220 "ADDRCONF(NETDEV_CHANGE): %s: "
2221 "link becomes ready\n",
2222 dev->name);
2223
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002224 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002225 }
2226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 switch(dev->type) {
Joerg Roedel0be669b2006-10-10 14:49:53 -07002228#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 case ARPHRD_SIT:
2230 addrconf_sit_config(dev);
2231 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002232#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 case ARPHRD_TUNNEL6:
2234 addrconf_ip6_tnl_config(dev);
2235 break;
2236 case ARPHRD_LOOPBACK:
2237 init_loopback(dev);
2238 break;
2239
2240 default:
2241 addrconf_dev_config(dev);
2242 break;
2243 };
2244 if (idev) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002245 if (run_pending)
2246 addrconf_dad_run(idev);
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 /* If the MTU changed during the interface down, when the
2249 interface up, the changed MTU must be reflected in the
2250 idev as well as routers.
2251 */
2252 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2253 rt6_mtu_change(dev, dev->mtu);
2254 idev->cnf.mtu6 = dev->mtu;
2255 }
2256 idev->tstamp = jiffies;
2257 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2258 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2259 stop IPv6 on this interface.
2260 */
2261 if (dev->mtu < IPV6_MIN_MTU)
2262 addrconf_ifdown(dev, event != NETDEV_DOWN);
2263 }
2264 break;
2265
2266 case NETDEV_CHANGEMTU:
2267 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
2268 rt6_mtu_change(dev, dev->mtu);
2269 idev->cnf.mtu6 = dev->mtu;
2270 break;
2271 }
2272
2273 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2274
2275 case NETDEV_DOWN:
2276 case NETDEV_UNREGISTER:
2277 /*
2278 * Remove all addresses from this interface.
2279 */
2280 addrconf_ifdown(dev, event != NETDEV_DOWN);
2281 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002282
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 case NETDEV_CHANGENAME:
2284#ifdef CONFIG_SYSCTL
2285 if (idev) {
2286 addrconf_sysctl_unregister(&idev->cnf);
2287 neigh_sysctl_unregister(idev->nd_parms);
2288 neigh_sysctl_register(dev, idev->nd_parms,
2289 NET_IPV6, NET_IPV6_NEIGH, "ipv6",
2290 &ndisc_ifinfo_sysctl_change,
2291 NULL);
2292 addrconf_sysctl_register(idev, &idev->cnf);
2293 }
2294#endif
2295 break;
2296 };
2297
2298 return NOTIFY_OK;
2299}
2300
2301/*
2302 * addrconf module should be notified of a device going up
2303 */
2304static struct notifier_block ipv6_dev_notf = {
2305 .notifier_call = addrconf_notify,
2306 .priority = 0
2307};
2308
2309static int addrconf_ifdown(struct net_device *dev, int how)
2310{
2311 struct inet6_dev *idev;
2312 struct inet6_ifaddr *ifa, **bifa;
2313 int i;
2314
2315 ASSERT_RTNL();
2316
2317 if (dev == &loopback_dev && how == 1)
2318 how = 0;
2319
2320 rt6_ifdown(dev);
2321 neigh_ifdown(&nd_tbl, dev);
2322
2323 idev = __in6_dev_get(dev);
2324 if (idev == NULL)
2325 return -ENODEV;
2326
2327 /* Step 1: remove reference to ipv6 device from parent device.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002328 Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 */
2330 if (how == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002332
2333 /* protected by rtnl_lock */
2334 rcu_assign_pointer(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
2336 /* Step 1.5: remove snmp6 entry */
2337 snmp6_unregister_dev(idev);
2338
2339 }
2340
2341 /* Step 2: clear hash table */
2342 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2343 bifa = &inet6_addr_lst[i];
2344
2345 write_lock_bh(&addrconf_hash_lock);
2346 while ((ifa = *bifa) != NULL) {
2347 if (ifa->idev == idev) {
2348 *bifa = ifa->lst_next;
2349 ifa->lst_next = NULL;
2350 addrconf_del_timer(ifa);
2351 in6_ifa_put(ifa);
2352 continue;
2353 }
2354 bifa = &ifa->lst_next;
2355 }
2356 write_unlock_bh(&addrconf_hash_lock);
2357 }
2358
2359 write_lock_bh(&idev->lock);
2360
2361 /* Step 3: clear flags for stateless addrconf */
2362 if (how != 1)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002363 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
2365 /* Step 4: clear address list */
2366#ifdef CONFIG_IPV6_PRIVACY
2367 if (how == 1 && del_timer(&idev->regen_timer))
2368 in6_dev_put(idev);
2369
2370 /* clear tempaddr list */
2371 while ((ifa = idev->tempaddr_list) != NULL) {
2372 idev->tempaddr_list = ifa->tmp_next;
2373 ifa->tmp_next = NULL;
2374 ifa->dead = 1;
2375 write_unlock_bh(&idev->lock);
2376 spin_lock_bh(&ifa->lock);
2377
2378 if (ifa->ifpub) {
2379 in6_ifa_put(ifa->ifpub);
2380 ifa->ifpub = NULL;
2381 }
2382 spin_unlock_bh(&ifa->lock);
2383 in6_ifa_put(ifa);
2384 write_lock_bh(&idev->lock);
2385 }
2386#endif
2387 while ((ifa = idev->addr_list) != NULL) {
2388 idev->addr_list = ifa->if_next;
2389 ifa->if_next = NULL;
2390 ifa->dead = 1;
2391 addrconf_del_timer(ifa);
2392 write_unlock_bh(&idev->lock);
2393
2394 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2395 in6_ifa_put(ifa);
2396
2397 write_lock_bh(&idev->lock);
2398 }
2399 write_unlock_bh(&idev->lock);
2400
2401 /* Step 5: Discard multicast list */
2402
2403 if (how == 1)
2404 ipv6_mc_destroy_dev(idev);
2405 else
2406 ipv6_mc_down(idev);
2407
2408 /* Step 5: netlink notification of this interface */
2409 idev->tstamp = jiffies;
Yan Zheng979ad662005-10-14 18:31:15 +08002410 inet6_ifinfo_notify(RTM_DELLINK, idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 /* Shot the device (if unregistered) */
2413
2414 if (how == 1) {
2415#ifdef CONFIG_SYSCTL
2416 addrconf_sysctl_unregister(&idev->cnf);
2417 neigh_sysctl_unregister(idev->nd_parms);
2418#endif
2419 neigh_parms_release(&nd_tbl, idev->nd_parms);
2420 neigh_ifdown(&nd_tbl, dev);
2421 in6_dev_put(idev);
2422 }
2423 return 0;
2424}
2425
2426static void addrconf_rs_timer(unsigned long data)
2427{
2428 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2429
2430 if (ifp->idev->cnf.forwarding)
2431 goto out;
2432
2433 if (ifp->idev->if_flags & IF_RA_RCVD) {
2434 /*
2435 * Announcement received after solicitation
2436 * was sent
2437 */
2438 goto out;
2439 }
2440
2441 spin_lock(&ifp->lock);
2442 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2443 struct in6_addr all_routers;
2444
2445 /* The wait after the last probe can be shorter */
2446 addrconf_mod_timer(ifp, AC_RS,
2447 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2448 ifp->idev->cnf.rtr_solicit_delay :
2449 ifp->idev->cnf.rtr_solicit_interval);
2450 spin_unlock(&ifp->lock);
2451
2452 ipv6_addr_all_routers(&all_routers);
2453
2454 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2455 } else {
2456 spin_unlock(&ifp->lock);
2457 /*
2458 * Note: we do not support deprecated "all on-link"
2459 * assumption any longer.
2460 */
2461 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2462 ifp->idev->dev->name);
2463 }
2464
2465out:
2466 in6_ifa_put(ifp);
2467}
2468
2469/*
2470 * Duplicate Address Detection
2471 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002472static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2473{
2474 unsigned long rand_num;
2475 struct inet6_dev *idev = ifp->idev;
2476
2477 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2478 ifp->probes = idev->cnf.dad_transmits;
2479 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2480}
2481
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002482static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483{
2484 struct inet6_dev *idev = ifp->idev;
2485 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
2487 addrconf_join_solict(dev, &ifp->addr);
2488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 net_srandom(ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
2491 read_lock_bh(&idev->lock);
2492 if (ifp->dead)
2493 goto out;
2494 spin_lock_bh(&ifp->lock);
2495
2496 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002497 !(ifp->flags&IFA_F_TENTATIVE) ||
2498 ifp->flags & IFA_F_NODAD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 ifp->flags &= ~IFA_F_TENTATIVE;
2500 spin_unlock_bh(&ifp->lock);
2501 read_unlock_bh(&idev->lock);
2502
2503 addrconf_dad_completed(ifp);
2504 return;
2505 }
2506
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002507 if (!(idev->if_flags & IF_READY)) {
YOSHIFUJI Hideaki3dd3bf82005-12-23 11:23:21 -08002508 spin_unlock_bh(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002509 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002510 /*
2511 * If the defice is not ready:
2512 * - keep it tentative if it is a permanent address.
2513 * - otherwise, kill it.
2514 */
2515 in6_ifa_hold(ifp);
2516 addrconf_dad_stop(ifp);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002517 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002518 }
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002519 addrconf_dad_kick(ifp);
2520 spin_unlock_bh(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521out:
2522 read_unlock_bh(&idev->lock);
2523}
2524
2525static void addrconf_dad_timer(unsigned long data)
2526{
2527 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2528 struct inet6_dev *idev = ifp->idev;
2529 struct in6_addr unspec;
2530 struct in6_addr mcaddr;
2531
2532 read_lock_bh(&idev->lock);
2533 if (idev->dead) {
2534 read_unlock_bh(&idev->lock);
2535 goto out;
2536 }
2537 spin_lock_bh(&ifp->lock);
2538 if (ifp->probes == 0) {
2539 /*
2540 * DAD was successful
2541 */
2542
2543 ifp->flags &= ~IFA_F_TENTATIVE;
2544 spin_unlock_bh(&ifp->lock);
2545 read_unlock_bh(&idev->lock);
2546
2547 addrconf_dad_completed(ifp);
2548
2549 goto out;
2550 }
2551
2552 ifp->probes--;
2553 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2554 spin_unlock_bh(&ifp->lock);
2555 read_unlock_bh(&idev->lock);
2556
2557 /* send a neighbour solicitation for our addr */
2558 memset(&unspec, 0, sizeof(unspec));
2559 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2560 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2561out:
2562 in6_ifa_put(ifp);
2563}
2564
2565static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2566{
2567 struct net_device * dev = ifp->idev->dev;
2568
2569 /*
2570 * Configure the address for reception. Now it is valid.
2571 */
2572
2573 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2574
2575 /* If added prefix is link local and forwarding is off,
2576 start sending router solicitations.
2577 */
2578
2579 if (ifp->idev->cnf.forwarding == 0 &&
2580 ifp->idev->cnf.rtr_solicits > 0 &&
2581 (dev->flags&IFF_LOOPBACK) == 0 &&
2582 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2583 struct in6_addr all_routers;
2584
2585 ipv6_addr_all_routers(&all_routers);
2586
2587 /*
2588 * If a host as already performed a random delay
2589 * [...] as part of DAD [...] there is no need
2590 * to delay again before sending the first RS
2591 */
2592 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2593
2594 spin_lock_bh(&ifp->lock);
2595 ifp->probes = 1;
2596 ifp->idev->if_flags |= IF_RS_SENT;
2597 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2598 spin_unlock_bh(&ifp->lock);
2599 }
2600}
2601
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002602static void addrconf_dad_run(struct inet6_dev *idev) {
2603 struct inet6_ifaddr *ifp;
2604
2605 read_lock_bh(&idev->lock);
2606 for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2607 spin_lock_bh(&ifp->lock);
2608 if (!(ifp->flags & IFA_F_TENTATIVE)) {
2609 spin_unlock_bh(&ifp->lock);
2610 continue;
2611 }
2612 spin_unlock_bh(&ifp->lock);
2613 addrconf_dad_kick(ifp);
2614 }
2615 read_unlock_bh(&idev->lock);
2616}
2617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618#ifdef CONFIG_PROC_FS
2619struct if6_iter_state {
2620 int bucket;
2621};
2622
2623static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2624{
2625 struct inet6_ifaddr *ifa = NULL;
2626 struct if6_iter_state *state = seq->private;
2627
2628 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2629 ifa = inet6_addr_lst[state->bucket];
2630 if (ifa)
2631 break;
2632 }
2633 return ifa;
2634}
2635
2636static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2637{
2638 struct if6_iter_state *state = seq->private;
2639
2640 ifa = ifa->lst_next;
2641try_again:
2642 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2643 ifa = inet6_addr_lst[state->bucket];
2644 goto try_again;
2645 }
2646 return ifa;
2647}
2648
2649static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2650{
2651 struct inet6_ifaddr *ifa = if6_get_first(seq);
2652
2653 if (ifa)
2654 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2655 --pos;
2656 return pos ? NULL : ifa;
2657}
2658
2659static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2660{
2661 read_lock_bh(&addrconf_hash_lock);
2662 return if6_get_idx(seq, *pos);
2663}
2664
2665static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2666{
2667 struct inet6_ifaddr *ifa;
2668
2669 ifa = if6_get_next(seq, v);
2670 ++*pos;
2671 return ifa;
2672}
2673
2674static void if6_seq_stop(struct seq_file *seq, void *v)
2675{
2676 read_unlock_bh(&addrconf_hash_lock);
2677}
2678
2679static int if6_seq_show(struct seq_file *seq, void *v)
2680{
2681 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2682 seq_printf(seq,
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002683 NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 NIP6(ifp->addr),
2685 ifp->idev->dev->ifindex,
2686 ifp->prefix_len,
2687 ifp->scope,
2688 ifp->flags,
2689 ifp->idev->dev->name);
2690 return 0;
2691}
2692
2693static struct seq_operations if6_seq_ops = {
2694 .start = if6_seq_start,
2695 .next = if6_seq_next,
2696 .show = if6_seq_show,
2697 .stop = if6_seq_stop,
2698};
2699
2700static int if6_seq_open(struct inode *inode, struct file *file)
2701{
2702 struct seq_file *seq;
2703 int rc = -ENOMEM;
Ingo Oeser322f74a2006-03-20 23:01:47 -08002704 struct if6_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
2706 if (!s)
2707 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
2709 rc = seq_open(file, &if6_seq_ops);
2710 if (rc)
2711 goto out_kfree;
2712
2713 seq = file->private_data;
2714 seq->private = s;
2715out:
2716 return rc;
2717out_kfree:
2718 kfree(s);
2719 goto out;
2720}
2721
Arjan van de Ven9a321442007-02-12 00:55:35 -08002722static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 .owner = THIS_MODULE,
2724 .open = if6_seq_open,
2725 .read = seq_read,
2726 .llseek = seq_lseek,
2727 .release = seq_release_private,
2728};
2729
2730int __init if6_proc_init(void)
2731{
2732 if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2733 return -ENOMEM;
2734 return 0;
2735}
2736
2737void if6_proc_exit(void)
2738{
2739 proc_net_remove("if_inet6");
2740}
2741#endif /* CONFIG_PROC_FS */
2742
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07002743#ifdef CONFIG_IPV6_MIP6
2744/* Check if address is a home address configured on any interface. */
2745int ipv6_chk_home_addr(struct in6_addr *addr)
2746{
2747 int ret = 0;
2748 struct inet6_ifaddr * ifp;
2749 u8 hash = ipv6_addr_hash(addr);
2750 read_lock_bh(&addrconf_hash_lock);
2751 for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) {
2752 if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
2753 (ifp->flags & IFA_F_HOMEADDRESS)) {
2754 ret = 1;
2755 break;
2756 }
2757 }
2758 read_unlock_bh(&addrconf_hash_lock);
2759 return ret;
2760}
2761#endif
2762
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763/*
2764 * Periodic address status verification
2765 */
2766
2767static void addrconf_verify(unsigned long foo)
2768{
2769 struct inet6_ifaddr *ifp;
2770 unsigned long now, next;
2771 int i;
2772
2773 spin_lock_bh(&addrconf_verify_lock);
2774 now = jiffies;
2775 next = now + ADDR_CHECK_FREQUENCY;
2776
2777 del_timer(&addr_chk_timer);
2778
2779 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2780
2781restart:
Yan Zheng5d5780d2005-11-20 13:42:20 -08002782 read_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2784 unsigned long age;
2785#ifdef CONFIG_IPV6_PRIVACY
2786 unsigned long regen_advance;
2787#endif
2788
2789 if (ifp->flags & IFA_F_PERMANENT)
2790 continue;
2791
2792 spin_lock(&ifp->lock);
2793 age = (now - ifp->tstamp) / HZ;
2794
2795#ifdef CONFIG_IPV6_PRIVACY
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002796 regen_advance = ifp->idev->cnf.regen_max_retry *
2797 ifp->idev->cnf.dad_transmits *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 ifp->idev->nd_parms->retrans_time / HZ;
2799#endif
2800
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09002801 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
2802 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 spin_unlock(&ifp->lock);
2804 in6_ifa_hold(ifp);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002805 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 ipv6_del_addr(ifp);
2807 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09002808 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
2809 spin_unlock(&ifp->lock);
2810 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 } else if (age >= ifp->prefered_lft) {
2812 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2813 int deprecate = 0;
2814
2815 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2816 deprecate = 1;
2817 ifp->flags |= IFA_F_DEPRECATED;
2818 }
2819
2820 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2821 next = ifp->tstamp + ifp->valid_lft * HZ;
2822
2823 spin_unlock(&ifp->lock);
2824
2825 if (deprecate) {
2826 in6_ifa_hold(ifp);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002827 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
2829 ipv6_ifa_notify(0, ifp);
2830 in6_ifa_put(ifp);
2831 goto restart;
2832 }
2833#ifdef CONFIG_IPV6_PRIVACY
2834 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2835 !(ifp->flags&IFA_F_TENTATIVE)) {
2836 if (age >= ifp->prefered_lft - regen_advance) {
2837 struct inet6_ifaddr *ifpub = ifp->ifpub;
2838 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2839 next = ifp->tstamp + ifp->prefered_lft * HZ;
2840 if (!ifp->regen_count && ifpub) {
2841 ifp->regen_count++;
2842 in6_ifa_hold(ifp);
2843 in6_ifa_hold(ifpub);
2844 spin_unlock(&ifp->lock);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002845 read_unlock(&addrconf_hash_lock);
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08002846 spin_lock(&ifpub->lock);
2847 ifpub->regen_count = 0;
2848 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 ipv6_create_tempaddr(ifpub, ifp);
2850 in6_ifa_put(ifpub);
2851 in6_ifa_put(ifp);
2852 goto restart;
2853 }
2854 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2855 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2856 spin_unlock(&ifp->lock);
2857#endif
2858 } else {
2859 /* ifp->prefered_lft <= ifp->valid_lft */
2860 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2861 next = ifp->tstamp + ifp->prefered_lft * HZ;
2862 spin_unlock(&ifp->lock);
2863 }
2864 }
Yan Zheng5d5780d2005-11-20 13:42:20 -08002865 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 }
2867
2868 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2869 add_timer(&addr_chk_timer);
2870 spin_unlock_bh(&addrconf_verify_lock);
2871}
2872
Thomas Graf461d8832006-09-18 00:09:49 -07002873static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
2874{
2875 struct in6_addr *pfx = NULL;
2876
2877 if (addr)
2878 pfx = nla_data(addr);
2879
2880 if (local) {
2881 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
2882 pfx = NULL;
2883 else
2884 pfx = nla_data(local);
2885 }
2886
2887 return pfx;
2888}
2889
2890static struct nla_policy ifa_ipv6_policy[IFA_MAX+1] __read_mostly = {
2891 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
2892 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
2893 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
2894};
2895
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896static int
2897inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2898{
Thomas Grafb933f712006-09-18 00:10:19 -07002899 struct ifaddrmsg *ifm;
2900 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 struct in6_addr *pfx;
Thomas Grafb933f712006-09-18 00:10:19 -07002902 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
Thomas Grafb933f712006-09-18 00:10:19 -07002904 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
2905 if (err < 0)
2906 return err;
2907
2908 ifm = nlmsg_data(nlh);
2909 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 if (pfx == NULL)
2911 return -EINVAL;
2912
2913 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2914}
2915
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002916static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
2917 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002918{
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002919 u32 flags = RTF_EXPIRES;
2920
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002921 if (!valid_lft || (prefered_lft > valid_lft))
2922 return -EINVAL;
2923
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002924 if (valid_lft == INFINITY_LIFE_TIME) {
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002925 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002926 flags = 0;
2927 } else if (valid_lft >= 0x7FFFFFFF/HZ)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002928 valid_lft = 0x7FFFFFFF/HZ;
2929
2930 if (prefered_lft == 0)
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002931 ifa_flags |= IFA_F_DEPRECATED;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002932 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
2933 (prefered_lft != INFINITY_LIFE_TIME))
2934 prefered_lft = 0x7FFFFFFF/HZ;
2935
2936 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07002937 ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002938 ifp->tstamp = jiffies;
2939 ifp->valid_lft = valid_lft;
2940 ifp->prefered_lft = prefered_lft;
2941
2942 spin_unlock_bh(&ifp->lock);
2943 if (!(ifp->flags&IFA_F_TENTATIVE))
2944 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002945
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002946 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
2947 jiffies_to_clock_t(valid_lft * HZ), flags);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002948 addrconf_verify(0);
2949
2950 return 0;
2951}
2952
2953static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2955{
Thomas Graf461d8832006-09-18 00:09:49 -07002956 struct ifaddrmsg *ifm;
2957 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 struct in6_addr *pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07002959 struct inet6_ifaddr *ifa;
2960 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002961 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
2962 u8 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07002963 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Thomas Graf461d8832006-09-18 00:09:49 -07002965 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
2966 if (err < 0)
2967 return err;
2968
2969 ifm = nlmsg_data(nlh);
2970 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 if (pfx == NULL)
2972 return -EINVAL;
2973
Thomas Graf461d8832006-09-18 00:09:49 -07002974 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002975 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07002976
2977 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002978 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07002979 preferred_lft = ci->ifa_prefered;
2980 } else {
2981 preferred_lft = INFINITY_LIFE_TIME;
2982 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002983 }
2984
Thomas Graf7198f8c2006-09-18 00:13:46 -07002985 dev = __dev_get_by_index(ifm->ifa_index);
2986 if (dev == NULL)
2987 return -ENODEV;
2988
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002989 /* We ignore other flags so far. */
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07002990 ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002991
Thomas Graf7198f8c2006-09-18 00:13:46 -07002992 ifa = ipv6_get_ifaddr(pfx, dev, 1);
2993 if (ifa == NULL) {
2994 /*
2995 * It would be best to check for !NLM_F_CREATE here but
2996 * userspace alreay relies on not having to provide this.
2997 */
2998 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002999 ifa_flags, preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003000 }
3001
Thomas Graf7198f8c2006-09-18 00:13:46 -07003002 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3003 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3004 err = -EEXIST;
3005 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003006 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003007
3008 in6_ifa_put(ifa);
3009
3010 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011}
3012
Thomas Graf101bb222006-09-18 00:11:52 -07003013static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3014 u8 scope, int ifindex)
3015{
3016 struct ifaddrmsg *ifm;
3017
3018 ifm = nlmsg_data(nlh);
3019 ifm->ifa_family = AF_INET6;
3020 ifm->ifa_prefixlen = prefixlen;
3021 ifm->ifa_flags = flags;
3022 ifm->ifa_scope = scope;
3023 ifm->ifa_index = ifindex;
3024}
3025
Thomas Graf85486af2006-09-18 00:11:24 -07003026static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3027 unsigned long tstamp, u32 preferred, u32 valid)
3028{
3029 struct ifa_cacheinfo ci;
3030
3031 ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100
3032 + TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3033 ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100
3034 + TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3035 ci.ifa_prefered = preferred;
3036 ci.ifa_valid = valid;
3037
3038 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3039}
3040
Thomas Graf101bb222006-09-18 00:11:52 -07003041static inline int rt_scope(int ifa_scope)
3042{
3043 if (ifa_scope & IFA_HOST)
3044 return RT_SCOPE_HOST;
3045 else if (ifa_scope & IFA_LINK)
3046 return RT_SCOPE_LINK;
3047 else if (ifa_scope & IFA_SITE)
3048 return RT_SCOPE_SITE;
3049 else
3050 return RT_SCOPE_UNIVERSE;
3051}
3052
Thomas Graf0ab68032006-09-18 00:12:35 -07003053static inline int inet6_ifaddr_msgsize(void)
3054{
Thomas Graf339bf982006-11-10 14:10:15 -08003055 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3056 + nla_total_size(16) /* IFA_ADDRESS */
3057 + nla_total_size(sizeof(struct ifa_cacheinfo));
Thomas Graf0ab68032006-09-18 00:12:35 -07003058}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003059
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003061 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07003064 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
Thomas Graf0ab68032006-09-18 00:12:35 -07003066 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3067 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003068 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003069
Thomas Graf101bb222006-09-18 00:11:52 -07003070 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3071 ifa->idev->dev->ifindex);
3072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 if (!(ifa->flags&IFA_F_PERMANENT)) {
Thomas Graf85486af2006-09-18 00:11:24 -07003074 preferred = ifa->prefered_lft;
3075 valid = ifa->valid_lft;
3076 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 long tval = (jiffies - ifa->tstamp)/HZ;
Thomas Graf85486af2006-09-18 00:11:24 -07003078 preferred -= tval;
3079 if (valid != INFINITY_LIFE_TIME)
3080 valid -= tval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 }
3082 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07003083 preferred = INFINITY_LIFE_TIME;
3084 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 }
Thomas Graf85486af2006-09-18 00:11:24 -07003086
Thomas Graf0ab68032006-09-18 00:12:35 -07003087 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
Patrick McHardy26932562007-01-31 23:16:40 -08003088 put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
3089 nlmsg_cancel(skb, nlh);
3090 return -EMSGSIZE;
3091 }
Thomas Graf85486af2006-09-18 00:11:24 -07003092
Thomas Graf0ab68032006-09-18 00:12:35 -07003093 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094}
3095
3096static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07003097 u32 pid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003100 u8 scope = RT_SCOPE_UNIVERSE;
3101 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
Thomas Graf101bb222006-09-18 00:11:52 -07003103 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3104 scope = RT_SCOPE_SITE;
3105
Thomas Graf0ab68032006-09-18 00:12:35 -07003106 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3107 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003108 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003109
Thomas Graf101bb222006-09-18 00:11:52 -07003110 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003111 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3112 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003113 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3114 nlmsg_cancel(skb, nlh);
3115 return -EMSGSIZE;
3116 }
Thomas Graf85486af2006-09-18 00:11:24 -07003117
Thomas Graf0ab68032006-09-18 00:12:35 -07003118 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119}
3120
3121static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003122 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003125 u8 scope = RT_SCOPE_UNIVERSE;
3126 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
Thomas Graf101bb222006-09-18 00:11:52 -07003128 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3129 scope = RT_SCOPE_SITE;
3130
Thomas Graf0ab68032006-09-18 00:12:35 -07003131 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3132 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003133 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003134
Thomas Graf101bb222006-09-18 00:11:52 -07003135 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003136 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3137 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003138 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3139 nlmsg_cancel(skb, nlh);
3140 return -EMSGSIZE;
3141 }
Thomas Graf85486af2006-09-18 00:11:24 -07003142
Thomas Graf0ab68032006-09-18 00:12:35 -07003143 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144}
3145
3146enum addr_type_t
3147{
3148 UNICAST_ADDR,
3149 MULTICAST_ADDR,
3150 ANYCAST_ADDR,
3151};
3152
3153static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3154 enum addr_type_t type)
3155{
3156 int idx, ip_idx;
3157 int s_idx, s_ip_idx;
3158 int err = 1;
3159 struct net_device *dev;
3160 struct inet6_dev *idev = NULL;
3161 struct inet6_ifaddr *ifa;
3162 struct ifmcaddr6 *ifmca;
3163 struct ifacaddr6 *ifaca;
3164
3165 s_idx = cb->args[0];
3166 s_ip_idx = ip_idx = cb->args[1];
3167 read_lock(&dev_base_lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003168
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
3170 if (idx < s_idx)
3171 continue;
3172 if (idx > s_idx)
3173 s_ip_idx = 0;
3174 ip_idx = 0;
3175 if ((idev = in6_dev_get(dev)) == NULL)
3176 continue;
3177 read_lock_bh(&idev->lock);
3178 switch (type) {
3179 case UNICAST_ADDR:
YOSHIFUJI Hideakiae9cda52005-06-28 13:00:30 -07003180 /* unicast address incl. temp addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 for (ifa = idev->addr_list; ifa;
3182 ifa = ifa->if_next, ip_idx++) {
3183 if (ip_idx < s_ip_idx)
3184 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003185 if ((err = inet6_fill_ifaddr(skb, ifa,
3186 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003187 cb->nlh->nlmsg_seq, RTM_NEWADDR,
3188 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 goto done;
3190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 break;
3192 case MULTICAST_ADDR:
3193 /* multicast address */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003194 for (ifmca = idev->mc_list; ifmca;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 ifmca = ifmca->next, ip_idx++) {
3196 if (ip_idx < s_ip_idx)
3197 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003198 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
3199 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003200 cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3201 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 goto done;
3203 }
3204 break;
3205 case ANYCAST_ADDR:
3206 /* anycast address */
3207 for (ifaca = idev->ac_list; ifaca;
3208 ifaca = ifaca->aca_next, ip_idx++) {
3209 if (ip_idx < s_ip_idx)
3210 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003211 if ((err = inet6_fill_ifacaddr(skb, ifaca,
3212 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003213 cb->nlh->nlmsg_seq, RTM_GETANYCAST,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003214 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 goto done;
3216 }
3217 break;
3218 default:
3219 break;
3220 }
3221 read_unlock_bh(&idev->lock);
3222 in6_dev_put(idev);
3223 }
3224done:
3225 if (err <= 0) {
3226 read_unlock_bh(&idev->lock);
3227 in6_dev_put(idev);
3228 }
3229 read_unlock(&dev_base_lock);
3230 cb->args[0] = idx;
3231 cb->args[1] = ip_idx;
3232 return skb->len;
3233}
3234
3235static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3236{
3237 enum addr_type_t type = UNICAST_ADDR;
3238 return inet6_dump_addr(skb, cb, type);
3239}
3240
3241static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3242{
3243 enum addr_type_t type = MULTICAST_ADDR;
3244 return inet6_dump_addr(skb, cb, type);
3245}
3246
3247
3248static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3249{
3250 enum addr_type_t type = ANYCAST_ADDR;
3251 return inet6_dump_addr(skb, cb, type);
3252}
3253
Thomas Graf1b29fc22006-09-18 00:10:50 -07003254static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
3255 void *arg)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003256{
Thomas Graf1b29fc22006-09-18 00:10:50 -07003257 struct ifaddrmsg *ifm;
3258 struct nlattr *tb[IFA_MAX+1];
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003259 struct in6_addr *addr = NULL;
3260 struct net_device *dev = NULL;
3261 struct inet6_ifaddr *ifa;
3262 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003263 int err;
3264
Thomas Graf1b29fc22006-09-18 00:10:50 -07003265 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3266 if (err < 0)
3267 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003268
Thomas Graf1b29fc22006-09-18 00:10:50 -07003269 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3270 if (addr == NULL) {
3271 err = -EINVAL;
3272 goto errout;
3273 }
3274
3275 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003276 if (ifm->ifa_index)
3277 dev = __dev_get_by_index(ifm->ifa_index);
3278
Thomas Graf1b29fc22006-09-18 00:10:50 -07003279 if ((ifa = ipv6_get_ifaddr(addr, dev, 1)) == NULL) {
3280 err = -EADDRNOTAVAIL;
3281 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003282 }
3283
Thomas Graf0ab68032006-09-18 00:12:35 -07003284 if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07003285 err = -ENOBUFS;
3286 goto errout_ifa;
3287 }
3288
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003289 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
3290 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003291 if (err < 0) {
3292 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3293 WARN_ON(err == -EMSGSIZE);
3294 kfree_skb(skb);
3295 goto errout_ifa;
3296 }
Thomas Graf2942e902006-08-15 00:30:25 -07003297 err = rtnl_unicast(skb, NETLINK_CB(in_skb).pid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003298errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003299 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003300errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003301 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003302}
3303
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3305{
3306 struct sk_buff *skb;
Thomas Graf5d620262006-08-15 00:35:02 -07003307 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
Thomas Graf0ab68032006-09-18 00:12:35 -07003309 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Thomas Graf5d620262006-08-15 00:35:02 -07003310 if (skb == NULL)
3311 goto errout;
3312
3313 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003314 if (err < 0) {
3315 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3316 WARN_ON(err == -EMSGSIZE);
3317 kfree_skb(skb);
3318 goto errout;
3319 }
Thomas Graf5d620262006-08-15 00:35:02 -07003320 err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3321errout:
3322 if (err < 0)
3323 rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324}
3325
Dave Jonesb6f99a22007-03-22 12:27:49 -07003326static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 __s32 *array, int bytes)
3328{
Thomas Graf04561c12006-11-14 19:53:58 -08003329 BUG_ON(bytes < (DEVCONF_MAX * 4));
3330
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 memset(array, 0, bytes);
3332 array[DEVCONF_FORWARDING] = cnf->forwarding;
3333 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3334 array[DEVCONF_MTU6] = cnf->mtu6;
3335 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3336 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3337 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3338 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3339 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3340 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3341 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3342 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3343#ifdef CONFIG_IPV6_PRIVACY
3344 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3345 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3346 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3347 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3348 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3349#endif
3350 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003351 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003352 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003353#ifdef CONFIG_IPV6_ROUTER_PREF
3354 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08003355 array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval;
Neil Hormanfa03ef32007-01-30 14:30:10 -08003356#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08003357 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
3358#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003359#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07003360 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07003361 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362}
3363
Thomas Graf339bf982006-11-10 14:10:15 -08003364static inline size_t inet6_if_nlmsg_size(void)
3365{
3366 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3367 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3368 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3369 + nla_total_size(4) /* IFLA_MTU */
3370 + nla_total_size(4) /* IFLA_LINK */
3371 + nla_total_size( /* IFLA_PROTINFO */
3372 nla_total_size(4) /* IFLA_INET6_FLAGS */
3373 + nla_total_size(sizeof(struct ifla_cacheinfo))
3374 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
3375 );
3376}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003377
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003378static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003379 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380{
Thomas Graf04561c12006-11-14 19:53:58 -08003381 struct net_device *dev = idev->dev;
3382 struct nlattr *conf;
3383 struct ifinfomsg *hdr;
3384 struct nlmsghdr *nlh;
3385 void *protoinfo;
3386 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387
Thomas Graf04561c12006-11-14 19:53:58 -08003388 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
3389 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003390 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391
Thomas Graf04561c12006-11-14 19:53:58 -08003392 hdr = nlmsg_data(nlh);
3393 hdr->ifi_family = AF_INET6;
3394 hdr->__ifi_pad = 0;
3395 hdr->ifi_type = dev->type;
3396 hdr->ifi_index = dev->ifindex;
3397 hdr->ifi_flags = dev_get_flags(dev);
3398 hdr->ifi_change = 0;
3399
3400 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
3402 if (dev->addr_len)
Thomas Graf04561c12006-11-14 19:53:58 -08003403 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Thomas Graf04561c12006-11-14 19:53:58 -08003405 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 if (dev->ifindex != dev->iflink)
Thomas Graf04561c12006-11-14 19:53:58 -08003407 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
Thomas Graf04561c12006-11-14 19:53:58 -08003409 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
3410 if (protoinfo == NULL)
3411 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412
Thomas Graf04561c12006-11-14 19:53:58 -08003413 NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 ci.max_reasm_len = IPV6_MAXPLEN;
3416 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3417 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3418 ci.reachable_time = idev->nd_parms->reachable_time;
3419 ci.retrans_time = idev->nd_parms->retrans_time;
Thomas Graf04561c12006-11-14 19:53:58 -08003420 NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3421
3422 conf = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
3423 if (conf == NULL)
3424 goto nla_put_failure;
3425 ipv6_store_devconf(&idev->cnf, nla_data(conf), nla_len(conf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
3427 /* XXX - Statistics/MC not implemented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
Thomas Graf04561c12006-11-14 19:53:58 -08003429 nla_nest_end(skb, protoinfo);
3430 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431
Thomas Graf04561c12006-11-14 19:53:58 -08003432nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08003433 nlmsg_cancel(skb, nlh);
3434 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435}
3436
3437static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3438{
3439 int idx, err;
3440 int s_idx = cb->args[0];
3441 struct net_device *dev;
3442 struct inet6_dev *idev;
3443
3444 read_lock(&dev_base_lock);
3445 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
3446 if (idx < s_idx)
3447 continue;
3448 if ((idev = in6_dev_get(dev)) == NULL)
3449 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003450 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003451 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 in6_dev_put(idev);
3453 if (err <= 0)
3454 break;
3455 }
3456 read_unlock(&dev_base_lock);
3457 cb->args[0] = idx;
3458
3459 return skb->len;
3460}
3461
3462void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3463{
3464 struct sk_buff *skb;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003465 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003466
Thomas Graf339bf982006-11-10 14:10:15 -08003467 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003468 if (skb == NULL)
3469 goto errout;
3470
3471 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003472 if (err < 0) {
3473 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
3474 WARN_ON(err == -EMSGSIZE);
3475 kfree_skb(skb);
3476 goto errout;
3477 }
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003478 err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3479errout:
3480 if (err < 0)
3481 rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482}
3483
Thomas Graf339bf982006-11-10 14:10:15 -08003484static inline size_t inet6_prefix_nlmsg_size(void)
3485{
3486 return NLMSG_ALIGN(sizeof(struct prefixmsg))
3487 + nla_total_size(sizeof(struct in6_addr))
3488 + nla_total_size(sizeof(struct prefix_cacheinfo));
3489}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003490
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Thomas Graf6051e2f2006-11-14 19:54:19 -08003492 struct prefix_info *pinfo, u32 pid, u32 seq,
3493 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494{
Thomas Graf6051e2f2006-11-14 19:54:19 -08003495 struct prefixmsg *pmsg;
3496 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 struct prefix_cacheinfo ci;
3498
Thomas Graf6051e2f2006-11-14 19:54:19 -08003499 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
3500 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003501 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08003502
3503 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07003505 pmsg->prefix_pad1 = 0;
3506 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 pmsg->prefix_ifindex = idev->dev->ifindex;
3508 pmsg->prefix_len = pinfo->prefix_len;
3509 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07003510 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 pmsg->prefix_flags = 0;
3512 if (pinfo->onlink)
3513 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3514 if (pinfo->autoconf)
3515 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3516
Thomas Graf6051e2f2006-11-14 19:54:19 -08003517 NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
3519 ci.preferred_time = ntohl(pinfo->prefered);
3520 ci.valid_time = ntohl(pinfo->valid);
Thomas Graf6051e2f2006-11-14 19:54:19 -08003521 NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
Thomas Graf6051e2f2006-11-14 19:54:19 -08003523 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
Thomas Graf6051e2f2006-11-14 19:54:19 -08003525nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08003526 nlmsg_cancel(skb, nlh);
3527 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528}
3529
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003530static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 struct prefix_info *pinfo)
3532{
3533 struct sk_buff *skb;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003534 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
Thomas Graf339bf982006-11-10 14:10:15 -08003536 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003537 if (skb == NULL)
3538 goto errout;
3539
3540 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003541 if (err < 0) {
3542 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
3543 WARN_ON(err == -EMSGSIZE);
3544 kfree_skb(skb);
3545 goto errout;
3546 }
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003547 err = rtnl_notify(skb, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
3548errout:
3549 if (err < 0)
3550 rtnl_set_sk_err(RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551}
3552
Thomas Grafdb46edc2005-05-03 14:29:39 -07003553static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, },
3555 [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, },
3556 [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, },
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003557 [RTM_GETADDR - RTM_BASE] = { .doit = inet6_rtm_getaddr,
3558 .dumpit = inet6_dump_ifaddr, },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
3560 [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
3561 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, },
3562 [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, },
3563 [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute,
3564 .dumpit = inet6_dump_fib, },
David S. Miller8423a9a2006-08-07 21:54:37 -07003565#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Thomas Graf101367c2006-08-04 03:39:02 -07003566 [RTM_GETRULE - RTM_BASE] = { .dumpit = fib6_rules_dump, },
David S. Miller8423a9a2006-08-07 21:54:37 -07003567#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568};
3569
3570static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3571{
3572 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3573
3574 switch (event) {
3575 case RTM_NEWADDR:
Thomas Graf40e22e82006-08-22 00:00:45 -07003576 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 if (ifp->idev->cnf.forwarding)
3578 addrconf_join_anycast(ifp);
3579 break;
3580 case RTM_DELADDR:
3581 if (ifp->idev->cnf.forwarding)
3582 addrconf_leave_anycast(ifp);
3583 addrconf_leave_solict(ifp->idev, &ifp->addr);
3584 dst_hold(&ifp->rt->u.dst);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003585 if (ip6_del_rt(ifp->rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 dst_free(&ifp->rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 break;
3588 }
3589}
3590
3591static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3592{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003593 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 if (likely(ifp->idev->dead == 0))
3595 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003596 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597}
3598
3599#ifdef CONFIG_SYSCTL
3600
3601static
3602int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3603 void __user *buffer, size_t *lenp, loff_t *ppos)
3604{
3605 int *valp = ctl->data;
3606 int val = *valp;
3607 int ret;
3608
3609 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3610
3611 if (write && valp != &ipv6_devconf_dflt.forwarding) {
3612 if (valp != &ipv6_devconf.forwarding) {
3613 if ((!*valp) ^ (!val)) {
3614 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
3615 if (idev == NULL)
3616 return ret;
3617 dev_forward_change(idev);
3618 }
3619 } else {
3620 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3621 addrconf_forward_change();
3622 }
3623 if (*valp)
3624 rt6_purge_dflt_routers();
3625 }
3626
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003627 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628}
3629
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003630static int addrconf_sysctl_forward_strategy(ctl_table *table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 int __user *name, int nlen,
3632 void __user *oldval,
3633 size_t __user *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -08003634 void __user *newval, size_t newlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635{
3636 int *valp = table->data;
3637 int new;
3638
3639 if (!newval || !newlen)
3640 return 0;
3641 if (newlen != sizeof(int))
3642 return -EINVAL;
3643 if (get_user(new, (int __user *)newval))
3644 return -EFAULT;
3645 if (new == *valp)
3646 return 0;
3647 if (oldval && oldlenp) {
3648 size_t len;
3649 if (get_user(len, oldlenp))
3650 return -EFAULT;
3651 if (len) {
3652 if (len > table->maxlen)
3653 len = table->maxlen;
3654 if (copy_to_user(oldval, valp, len))
3655 return -EFAULT;
3656 if (put_user(len, oldlenp))
3657 return -EFAULT;
3658 }
3659 }
3660
3661 if (valp != &ipv6_devconf_dflt.forwarding) {
3662 if (valp != &ipv6_devconf.forwarding) {
3663 struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
3664 int changed;
3665 if (unlikely(idev == NULL))
3666 return -ENODEV;
3667 changed = (!*valp) ^ (!new);
3668 *valp = new;
3669 if (changed)
3670 dev_forward_change(idev);
3671 } else {
3672 *valp = new;
3673 addrconf_forward_change();
3674 }
3675
3676 if (*valp)
3677 rt6_purge_dflt_routers();
3678 } else
3679 *valp = new;
3680
3681 return 1;
3682}
3683
3684static struct addrconf_sysctl_table
3685{
3686 struct ctl_table_header *sysctl_header;
3687 ctl_table addrconf_vars[__NET_IPV6_MAX];
3688 ctl_table addrconf_dev[2];
3689 ctl_table addrconf_conf_dir[2];
3690 ctl_table addrconf_proto_dir[2];
3691 ctl_table addrconf_root_dir[2];
Brian Haleyab32ea52006-09-22 14:15:41 -07003692} addrconf_sysctl __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 .sysctl_header = NULL,
3694 .addrconf_vars = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003695 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 .ctl_name = NET_IPV6_FORWARDING,
3697 .procname = "forwarding",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003698 .data = &ipv6_devconf.forwarding,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 .maxlen = sizeof(int),
3700 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003701 .proc_handler = &addrconf_sysctl_forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 .strategy = &addrconf_sysctl_forward_strategy,
3703 },
3704 {
3705 .ctl_name = NET_IPV6_HOP_LIMIT,
3706 .procname = "hop_limit",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003707 .data = &ipv6_devconf.hop_limit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 .maxlen = sizeof(int),
3709 .mode = 0644,
3710 .proc_handler = proc_dointvec,
3711 },
3712 {
3713 .ctl_name = NET_IPV6_MTU,
3714 .procname = "mtu",
3715 .data = &ipv6_devconf.mtu6,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003716 .maxlen = sizeof(int),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003718 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 },
3720 {
3721 .ctl_name = NET_IPV6_ACCEPT_RA,
3722 .procname = "accept_ra",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003723 .data = &ipv6_devconf.accept_ra,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 .maxlen = sizeof(int),
3725 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003726 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 },
3728 {
3729 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3730 .procname = "accept_redirects",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003731 .data = &ipv6_devconf.accept_redirects,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 .maxlen = sizeof(int),
3733 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003734 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 },
3736 {
3737 .ctl_name = NET_IPV6_AUTOCONF,
3738 .procname = "autoconf",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003739 .data = &ipv6_devconf.autoconf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 .maxlen = sizeof(int),
3741 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003742 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 },
3744 {
3745 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3746 .procname = "dad_transmits",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003747 .data = &ipv6_devconf.dad_transmits,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 .maxlen = sizeof(int),
3749 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003750 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 },
3752 {
3753 .ctl_name = NET_IPV6_RTR_SOLICITS,
3754 .procname = "router_solicitations",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003755 .data = &ipv6_devconf.rtr_solicits,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 .maxlen = sizeof(int),
3757 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003758 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 },
3760 {
3761 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3762 .procname = "router_solicitation_interval",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003763 .data = &ipv6_devconf.rtr_solicit_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 .maxlen = sizeof(int),
3765 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003766 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 .strategy = &sysctl_jiffies,
3768 },
3769 {
3770 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3771 .procname = "router_solicitation_delay",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003772 .data = &ipv6_devconf.rtr_solicit_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 .maxlen = sizeof(int),
3774 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003775 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 .strategy = &sysctl_jiffies,
3777 },
3778 {
3779 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3780 .procname = "force_mld_version",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003781 .data = &ipv6_devconf.force_mld_version,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 .maxlen = sizeof(int),
3783 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003784 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 },
3786#ifdef CONFIG_IPV6_PRIVACY
3787 {
3788 .ctl_name = NET_IPV6_USE_TEMPADDR,
3789 .procname = "use_tempaddr",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003790 .data = &ipv6_devconf.use_tempaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 .maxlen = sizeof(int),
3792 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003793 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 },
3795 {
3796 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3797 .procname = "temp_valid_lft",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003798 .data = &ipv6_devconf.temp_valid_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 .maxlen = sizeof(int),
3800 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003801 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 },
3803 {
3804 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3805 .procname = "temp_prefered_lft",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003806 .data = &ipv6_devconf.temp_prefered_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 .maxlen = sizeof(int),
3808 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003809 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 },
3811 {
3812 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3813 .procname = "regen_max_retry",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003814 .data = &ipv6_devconf.regen_max_retry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 .maxlen = sizeof(int),
3816 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003817 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 },
3819 {
3820 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3821 .procname = "max_desync_factor",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003822 .data = &ipv6_devconf.max_desync_factor,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 .maxlen = sizeof(int),
3824 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003825 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 },
3827#endif
3828 {
3829 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3830 .procname = "max_addresses",
3831 .data = &ipv6_devconf.max_addresses,
3832 .maxlen = sizeof(int),
3833 .mode = 0644,
3834 .proc_handler = &proc_dointvec,
3835 },
3836 {
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003837 .ctl_name = NET_IPV6_ACCEPT_RA_DEFRTR,
3838 .procname = "accept_ra_defrtr",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003839 .data = &ipv6_devconf.accept_ra_defrtr,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003840 .maxlen = sizeof(int),
3841 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003842 .proc_handler = &proc_dointvec,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003843 },
3844 {
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003845 .ctl_name = NET_IPV6_ACCEPT_RA_PINFO,
3846 .procname = "accept_ra_pinfo",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003847 .data = &ipv6_devconf.accept_ra_pinfo,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003848 .maxlen = sizeof(int),
3849 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003850 .proc_handler = &proc_dointvec,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003851 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003852#ifdef CONFIG_IPV6_ROUTER_PREF
3853 {
3854 .ctl_name = NET_IPV6_ACCEPT_RA_RTR_PREF,
3855 .procname = "accept_ra_rtr_pref",
3856 .data = &ipv6_devconf.accept_ra_rtr_pref,
3857 .maxlen = sizeof(int),
3858 .mode = 0644,
3859 .proc_handler = &proc_dointvec,
3860 },
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08003861 {
3862 .ctl_name = NET_IPV6_RTR_PROBE_INTERVAL,
3863 .procname = "router_probe_interval",
3864 .data = &ipv6_devconf.rtr_probe_interval,
3865 .maxlen = sizeof(int),
3866 .mode = 0644,
3867 .proc_handler = &proc_dointvec_jiffies,
3868 .strategy = &sysctl_jiffies,
3869 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08003870#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08003871 {
3872 .ctl_name = NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN,
3873 .procname = "accept_ra_rt_info_max_plen",
3874 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
3875 .maxlen = sizeof(int),
3876 .mode = 0644,
3877 .proc_handler = &proc_dointvec,
3878 },
3879#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003880#endif
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003881 {
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07003882 .ctl_name = NET_IPV6_PROXY_NDP,
3883 .procname = "proxy_ndp",
3884 .data = &ipv6_devconf.proxy_ndp,
3885 .maxlen = sizeof(int),
3886 .mode = 0644,
3887 .proc_handler = &proc_dointvec,
3888 },
3889 {
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07003890 .ctl_name = NET_IPV6_ACCEPT_SOURCE_ROUTE,
3891 .procname = "accept_source_route",
3892 .data = &ipv6_devconf.accept_source_route,
3893 .maxlen = sizeof(int),
3894 .mode = 0644,
3895 .proc_handler = &proc_dointvec,
3896 },
3897 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 .ctl_name = 0, /* sentinel */
3899 }
3900 },
3901 .addrconf_dev = {
3902 {
3903 .ctl_name = NET_PROTO_CONF_ALL,
3904 .procname = "all",
3905 .mode = 0555,
3906 .child = addrconf_sysctl.addrconf_vars,
3907 },
3908 {
3909 .ctl_name = 0, /* sentinel */
3910 }
3911 },
3912 .addrconf_conf_dir = {
3913 {
3914 .ctl_name = NET_IPV6_CONF,
3915 .procname = "conf",
3916 .mode = 0555,
3917 .child = addrconf_sysctl.addrconf_dev,
3918 },
3919 {
3920 .ctl_name = 0, /* sentinel */
3921 }
3922 },
3923 .addrconf_proto_dir = {
3924 {
3925 .ctl_name = NET_IPV6,
3926 .procname = "ipv6",
3927 .mode = 0555,
3928 .child = addrconf_sysctl.addrconf_conf_dir,
3929 },
3930 {
3931 .ctl_name = 0, /* sentinel */
3932 }
3933 },
3934 .addrconf_root_dir = {
3935 {
3936 .ctl_name = CTL_NET,
3937 .procname = "net",
3938 .mode = 0555,
3939 .child = addrconf_sysctl.addrconf_proto_dir,
3940 },
3941 {
3942 .ctl_name = 0, /* sentinel */
3943 }
3944 },
3945};
3946
3947static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
3948{
3949 int i;
3950 struct net_device *dev = idev ? idev->dev : NULL;
3951 struct addrconf_sysctl_table *t;
3952 char *dev_name = NULL;
3953
Arnaldo Carvalho de Meloaf879cc2006-11-17 12:14:37 -02003954 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 if (t == NULL)
3956 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 for (i=0; t->addrconf_vars[i].data; i++) {
3958 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
3960 }
3961 if (dev) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003962 dev_name = dev->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 t->addrconf_dev[0].ctl_name = dev->ifindex;
3964 } else {
3965 dev_name = "default";
3966 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
3967 }
3968
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003969 /*
3970 * Make a copy of dev_name, because '.procname' is regarded as const
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 * by sysctl and we wouldn't want anyone to change it under our feet
3972 * (see SIOCSIFNAME).
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003973 */
Paulo Marques543537b2005-06-23 00:09:02 -07003974 dev_name = kstrdup(dev_name, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 if (!dev_name)
3976 goto free;
3977
3978 t->addrconf_dev[0].procname = dev_name;
3979
3980 t->addrconf_dev[0].child = t->addrconf_vars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 t->addrconf_conf_dir[0].child = t->addrconf_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08003985 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 if (t->sysctl_header == NULL)
3987 goto free_procname;
3988 else
3989 p->sysctl = t;
3990 return;
3991
3992 /* error path */
3993 free_procname:
3994 kfree(dev_name);
3995 free:
3996 kfree(t);
3997
3998 return;
3999}
4000
4001static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
4002{
4003 if (p->sysctl) {
4004 struct addrconf_sysctl_table *t = p->sysctl;
4005 p->sysctl = NULL;
4006 unregister_sysctl_table(t->sysctl_header);
4007 kfree(t->addrconf_dev[0].procname);
4008 kfree(t);
4009 }
4010}
4011
4012
4013#endif
4014
4015/*
4016 * Device notifier
4017 */
4018
4019int register_inet6addr_notifier(struct notifier_block *nb)
4020{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004021 return atomic_notifier_chain_register(&inet6addr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022}
4023
4024int unregister_inet6addr_notifier(struct notifier_block *nb)
4025{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004026 return atomic_notifier_chain_unregister(&inet6addr_chain,nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027}
4028
4029/*
4030 * Init / cleanup code
4031 */
4032
4033int __init addrconf_init(void)
4034{
4035 int err = 0;
4036
4037 /* The addrconf netdev notifier requires that loopback_dev
4038 * has it's ipv6 private information allocated and setup
4039 * before it can bring up and give link-local addresses
4040 * to other devices which are up.
4041 *
4042 * Unfortunately, loopback_dev is not necessarily the first
4043 * entry in the global dev_base list of net devices. In fact,
4044 * it is likely to be the very last entry on that list.
4045 * So this causes the notifier registry below to try and
4046 * give link-local addresses to all devices besides loopback_dev
4047 * first, then loopback_dev, which cases all the non-loopback_dev
4048 * devices to fail to get a link-local address.
4049 *
4050 * So, as a temporary fix, allocate the ipv6 structure for
4051 * loopback_dev first by hand.
4052 * Longer term, all of the dependencies ipv6 has upon the loopback
4053 * device and it being up should be removed.
4054 */
4055 rtnl_lock();
4056 if (!ipv6_add_dev(&loopback_dev))
4057 err = -ENOMEM;
4058 rtnl_unlock();
4059 if (err)
4060 return err;
4061
Herbert Xuc62dba92005-09-26 15:10:16 -07004062 ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
4063
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 register_netdevice_notifier(&ipv6_dev_notf);
4065
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 addrconf_verify(0);
4067 rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
4068#ifdef CONFIG_SYSCTL
4069 addrconf_sysctl.sysctl_header =
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08004070 register_sysctl_table(addrconf_sysctl.addrconf_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
4072#endif
4073
4074 return 0;
4075}
4076
4077void __exit addrconf_cleanup(void)
4078{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004079 struct net_device *dev;
4080 struct inet6_dev *idev;
4081 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 int i;
4083
4084 unregister_netdevice_notifier(&ipv6_dev_notf);
4085
4086 rtnetlink_links[PF_INET6] = NULL;
4087#ifdef CONFIG_SYSCTL
4088 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
4089 addrconf_sysctl_unregister(&ipv6_devconf);
4090#endif
4091
4092 rtnl_lock();
4093
4094 /*
4095 * clean dev list.
4096 */
4097
4098 for (dev=dev_base; dev; dev=dev->next) {
4099 if ((idev = __in6_dev_get(dev)) == NULL)
4100 continue;
4101 addrconf_ifdown(dev, 1);
4102 }
4103 addrconf_ifdown(&loopback_dev, 2);
4104
4105 /*
4106 * Check hash table.
4107 */
4108
4109 write_lock_bh(&addrconf_hash_lock);
4110 for (i=0; i < IN6_ADDR_HSIZE; i++) {
4111 for (ifa=inet6_addr_lst[i]; ifa; ) {
4112 struct inet6_ifaddr *bifa;
4113
4114 bifa = ifa;
4115 ifa = ifa->lst_next;
4116 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
4117 /* Do not free it; something is wrong.
4118 Now we can investigate it with debugger.
4119 */
4120 }
4121 }
4122 write_unlock_bh(&addrconf_hash_lock);
4123
4124 del_timer(&addr_chk_timer);
4125
4126 rtnl_unlock();
4127
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128#ifdef CONFIG_PROC_FS
4129 proc_net_remove("if_inet6");
4130#endif
4131}