blob: f6ac65d36559cf15825a5e63d0a182cd9a080f6e [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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175};
176
Brian Haleyab32ea52006-09-22 14:15:41 -0700177static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 .forwarding = 0,
179 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
180 .mtu6 = IPV6_MIN_MTU,
181 .accept_ra = 1,
182 .accept_redirects = 1,
183 .autoconf = 1,
184 .dad_transmits = 1,
185 .rtr_solicits = MAX_RTR_SOLICITATIONS,
186 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
187 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
188#ifdef CONFIG_IPV6_PRIVACY
189 .use_tempaddr = 0,
190 .temp_valid_lft = TEMP_VALID_LIFETIME,
191 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
192 .regen_max_retry = REGEN_MAX_RETRY,
193 .max_desync_factor = MAX_DESYNC_FACTOR,
194#endif
195 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800196 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800197 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800198#ifdef CONFIG_IPV6_ROUTER_PREF
199 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800200 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800201#ifdef CONFIG_IPV6_ROUTE_INFO
202 .accept_ra_rt_info_max_plen = 0,
203#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800204#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700205 .proxy_ndp = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206};
207
208/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
209#if 0
210const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
211#endif
212const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214static void addrconf_del_timer(struct inet6_ifaddr *ifp)
215{
216 if (del_timer(&ifp->timer))
217 __in6_ifa_put(ifp);
218}
219
220enum addrconf_timer_t
221{
222 AC_NONE,
223 AC_DAD,
224 AC_RS,
225};
226
227static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
228 enum addrconf_timer_t what,
229 unsigned long when)
230{
231 if (!del_timer(&ifp->timer))
232 in6_ifa_hold(ifp);
233
234 switch (what) {
235 case AC_DAD:
236 ifp->timer.function = addrconf_dad_timer;
237 break;
238 case AC_RS:
239 ifp->timer.function = addrconf_rs_timer;
240 break;
241 default:;
242 }
243 ifp->timer.expires = jiffies + when;
244 add_timer(&ifp->timer);
245}
246
247/* Nobody refers to this device, we may destroy it. */
248
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700249static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
250{
251 struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
252 kfree(idev);
253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255void in6_dev_finish_destroy(struct inet6_dev *idev)
256{
257 struct net_device *dev = idev->dev;
258 BUG_TRAP(idev->addr_list==NULL);
259 BUG_TRAP(idev->mc_list==NULL);
260#ifdef NET_REFCNT_DEBUG
261 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
262#endif
263 dev_put(dev);
264 if (!idev->dead) {
265 printk("Freeing alive inet6 device %p\n", idev);
266 return;
267 }
268 snmp6_free_dev(idev);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700269 call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
273{
274 struct inet6_dev *ndev;
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800275 struct in6_addr maddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 ASSERT_RTNL();
278
279 if (dev->mtu < IPV6_MIN_MTU)
280 return NULL;
281
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900282 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900284 if (ndev == NULL)
285 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Ingo Oeser322f74a2006-03-20 23:01:47 -0800287 rwlock_init(&ndev->lock);
288 ndev->dev = dev;
289 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
290 ndev->cnf.mtu6 = dev->mtu;
291 ndev->cnf.sysctl = NULL;
292 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
293 if (ndev->nd_parms == NULL) {
294 kfree(ndev);
295 return NULL;
296 }
297 /* We refer to the device */
298 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Ingo Oeser322f74a2006-03-20 23:01:47 -0800300 if (snmp6_alloc_dev(ndev) < 0) {
301 ADBG((KERN_WARNING
302 "%s(): cannot allocate memory for statistics; dev=%s.\n",
303 __FUNCTION__, dev->name));
304 neigh_parms_release(&nd_tbl, ndev->nd_parms);
305 ndev->dead = 1;
306 in6_dev_finish_destroy(ndev);
307 return NULL;
308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Ingo Oeser322f74a2006-03-20 23:01:47 -0800310 if (snmp6_register_dev(ndev) < 0) {
311 ADBG((KERN_WARNING
312 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
313 __FUNCTION__, dev->name));
314 neigh_parms_release(&nd_tbl, ndev->nd_parms);
315 ndev->dead = 1;
316 in6_dev_finish_destroy(ndev);
317 return NULL;
318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Ingo Oeser322f74a2006-03-20 23:01:47 -0800320 /* One reference from device. We must do this before
321 * we invoke __ipv6_regen_rndid().
322 */
323 in6_dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325#ifdef CONFIG_IPV6_PRIVACY
Ingo Oeser322f74a2006-03-20 23:01:47 -0800326 init_timer(&ndev->regen_timer);
327 ndev->regen_timer.function = ipv6_regen_rndid;
328 ndev->regen_timer.data = (unsigned long) ndev;
329 if ((dev->flags&IFF_LOOPBACK) ||
330 dev->type == ARPHRD_TUNNEL ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700331#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
332 dev->type == ARPHRD_SIT ||
333#endif
334 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800335 printk(KERN_INFO
336 "%s: Disabled Privacy Extensions\n",
337 dev->name);
338 ndev->cnf.use_tempaddr = -1;
339 } else {
340 in6_dev_hold(ndev);
341 ipv6_regen_rndid((unsigned long) ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
Ingo Oeser322f74a2006-03-20 23:01:47 -0800343#endif
344
345 if (netif_carrier_ok(dev))
346 ndev->if_flags |= IF_READY;
347
Ingo Oeser322f74a2006-03-20 23:01:47 -0800348
349 ipv6_mc_init_dev(ndev);
350 ndev->tstamp = jiffies;
351#ifdef CONFIG_SYSCTL
352 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
353 NET_IPV6_NEIGH, "ipv6",
354 &ndisc_ifinfo_sysctl_change,
355 NULL);
356 addrconf_sysctl_register(ndev, &ndev->cnf);
357#endif
David L Stevens30c4cf52007-01-04 12:31:14 -0800358 /* protected by rtnl_lock */
359 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800360
361 /* Join all-node multicast group */
362 ipv6_addr_all_nodes(&maddr);
363 ipv6_dev_mc_inc(dev, &maddr);
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return ndev;
366}
367
368static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
369{
370 struct inet6_dev *idev;
371
372 ASSERT_RTNL();
373
374 if ((idev = __in6_dev_get(dev)) == NULL) {
375 if ((idev = ipv6_add_dev(dev)) == NULL)
376 return NULL;
377 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (dev->flags&IFF_UP)
380 ipv6_mc_up(idev);
381 return idev;
382}
383
384#ifdef CONFIG_SYSCTL
385static void dev_forward_change(struct inet6_dev *idev)
386{
387 struct net_device *dev;
388 struct inet6_ifaddr *ifa;
389 struct in6_addr addr;
390
391 if (!idev)
392 return;
393 dev = idev->dev;
394 if (dev && (dev->flags & IFF_MULTICAST)) {
395 ipv6_addr_all_routers(&addr);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (idev->cnf.forwarding)
398 ipv6_dev_mc_inc(dev, &addr);
399 else
400 ipv6_dev_mc_dec(dev, &addr);
401 }
402 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
403 if (idev->cnf.forwarding)
404 addrconf_join_anycast(ifa);
405 else
406 addrconf_leave_anycast(ifa);
407 }
408}
409
410
411static void addrconf_forward_change(void)
412{
413 struct net_device *dev;
414 struct inet6_dev *idev;
415
416 read_lock(&dev_base_lock);
417 for (dev=dev_base; dev; dev=dev->next) {
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700418 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 idev = __in6_dev_get(dev);
420 if (idev) {
421 int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
422 idev->cnf.forwarding = ipv6_devconf.forwarding;
423 if (changed)
424 dev_forward_change(idev);
425 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700426 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428 read_unlock(&dev_base_lock);
429}
430#endif
431
432/* Nobody refers to this ifaddr, destroy it */
433
434void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
435{
436 BUG_TRAP(ifp->if_next==NULL);
437 BUG_TRAP(ifp->lst_next==NULL);
438#ifdef NET_REFCNT_DEBUG
439 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
440#endif
441
442 in6_dev_put(ifp->idev);
443
444 if (del_timer(&ifp->timer))
445 printk("Timer is still running, when freeing ifa=%p\n", ifp);
446
447 if (!ifp->dead) {
448 printk("Freeing alive inet6 address %p\n", ifp);
449 return;
450 }
451 dst_release(&ifp->rt->u.dst);
452
453 kfree(ifp);
454}
455
Brian Haleye55ffac2006-07-10 15:25:51 -0700456static void
457ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
458{
459 struct inet6_ifaddr *ifa, **ifap;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700460 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700461
462 /*
463 * Each device address list is sorted in order of scope -
464 * global before linklocal.
465 */
466 for (ifap = &idev->addr_list; (ifa = *ifap) != NULL;
467 ifap = &ifa->if_next) {
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700468 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700469 break;
470 }
471
472 ifp->if_next = *ifap;
473 *ifap = ifp;
474}
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476/* On success it returns ifp with increased reference count */
477
478static struct inet6_ifaddr *
479ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700480 int scope, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 struct inet6_ifaddr *ifa = NULL;
483 struct rt6_info *rt;
484 int hash;
485 int err = 0;
486
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700487 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (idev->dead) {
489 err = -ENODEV; /*XXX*/
490 goto out2;
491 }
492
493 write_lock(&addrconf_hash_lock);
494
495 /* Ignore adding duplicate addresses on an interface */
496 if (ipv6_chk_same_addr(addr, idev->dev)) {
497 ADBG(("ipv6_add_addr: already assigned\n"));
498 err = -EEXIST;
499 goto out;
500 }
501
Ingo Oeser322f74a2006-03-20 23:01:47 -0800502 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 if (ifa == NULL) {
505 ADBG(("ipv6_add_addr: malloc failed\n"));
506 err = -ENOBUFS;
507 goto out;
508 }
509
510 rt = addrconf_dst_alloc(idev, addr, 0);
511 if (IS_ERR(rt)) {
512 err = PTR_ERR(rt);
513 goto out;
514 }
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 ipv6_addr_copy(&ifa->addr, addr);
517
518 spin_lock_init(&ifa->lock);
519 init_timer(&ifa->timer);
520 ifa->timer.data = (unsigned long) ifa;
521 ifa->scope = scope;
522 ifa->prefix_len = pfxlen;
523 ifa->flags = flags | IFA_F_TENTATIVE;
524 ifa->cstamp = ifa->tstamp = jiffies;
525
Keir Fraser57f5f542006-08-29 02:43:49 -0700526 ifa->rt = rt;
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 ifa->idev = idev;
529 in6_dev_hold(idev);
530 /* For caller */
531 in6_ifa_hold(ifa);
532
533 /* Add to big hash table */
534 hash = ipv6_addr_hash(addr);
535
536 ifa->lst_next = inet6_addr_lst[hash];
537 inet6_addr_lst[hash] = ifa;
538 in6_ifa_hold(ifa);
539 write_unlock(&addrconf_hash_lock);
540
541 write_lock(&idev->lock);
542 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -0700543 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545#ifdef CONFIG_IPV6_PRIVACY
546 if (ifa->flags&IFA_F_TEMPORARY) {
547 ifa->tmp_next = idev->tempaddr_list;
548 idev->tempaddr_list = ifa;
549 in6_ifa_hold(ifa);
550 }
551#endif
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 in6_ifa_hold(ifa);
554 write_unlock(&idev->lock);
555out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700556 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -0700558 if (likely(err == 0))
Alan Sterne041c682006-03-27 01:16:30 -0800559 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 else {
561 kfree(ifa);
562 ifa = ERR_PTR(err);
563 }
564
565 return ifa;
566out:
567 write_unlock(&addrconf_hash_lock);
568 goto out2;
569}
570
571/* This function wants to get referenced ifp and releases it before return */
572
573static void ipv6_del_addr(struct inet6_ifaddr *ifp)
574{
575 struct inet6_ifaddr *ifa, **ifap;
576 struct inet6_dev *idev = ifp->idev;
577 int hash;
578 int deleted = 0, onlink = 0;
579 unsigned long expires = jiffies;
580
581 hash = ipv6_addr_hash(&ifp->addr);
582
583 ifp->dead = 1;
584
585 write_lock_bh(&addrconf_hash_lock);
586 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
587 ifap = &ifa->lst_next) {
588 if (ifa == ifp) {
589 *ifap = ifa->lst_next;
590 __in6_ifa_put(ifp);
591 ifa->lst_next = NULL;
592 break;
593 }
594 }
595 write_unlock_bh(&addrconf_hash_lock);
596
597 write_lock_bh(&idev->lock);
598#ifdef CONFIG_IPV6_PRIVACY
599 if (ifp->flags&IFA_F_TEMPORARY) {
600 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
601 ifap = &ifa->tmp_next) {
602 if (ifa == ifp) {
603 *ifap = ifa->tmp_next;
604 if (ifp->ifpub) {
605 in6_ifa_put(ifp->ifpub);
606 ifp->ifpub = NULL;
607 }
608 __in6_ifa_put(ifp);
609 ifa->tmp_next = NULL;
610 break;
611 }
612 }
613 }
614#endif
615
Kristian Slavov1d142802005-12-21 18:47:24 -0800616 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (ifa == ifp) {
618 *ifap = ifa->if_next;
619 __in6_ifa_put(ifp);
620 ifa->if_next = NULL;
621 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
622 break;
623 deleted = 1;
Kristian Slavov1d142802005-12-21 18:47:24 -0800624 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 } else if (ifp->flags & IFA_F_PERMANENT) {
626 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
627 ifp->prefix_len)) {
628 if (ifa->flags & IFA_F_PERMANENT) {
629 onlink = 1;
630 if (deleted)
631 break;
632 } else {
633 unsigned long lifetime;
634
635 if (!onlink)
636 onlink = -1;
637
638 spin_lock(&ifa->lock);
639 lifetime = min_t(unsigned long,
640 ifa->valid_lft, 0x7fffffffUL/HZ);
641 if (time_before(expires,
642 ifa->tstamp + lifetime * HZ))
643 expires = ifa->tstamp + lifetime * HZ;
644 spin_unlock(&ifa->lock);
645 }
646 }
647 }
Kristian Slavov1d142802005-12-21 18:47:24 -0800648 ifap = &ifa->if_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650 write_unlock_bh(&idev->lock);
651
652 ipv6_ifa_notify(RTM_DELADDR, ifp);
653
Alan Sterne041c682006-03-27 01:16:30 -0800654 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 addrconf_del_timer(ifp);
657
658 /*
659 * Purge or update corresponding prefix
660 *
661 * 1) we don't purge prefix here if address was not permanent.
662 * prefix is managed by its own lifetime.
663 * 2) if there're no addresses, delete prefix.
664 * 3) if there're still other permanent address(es),
665 * corresponding prefix is still permanent.
666 * 4) otherwise, update prefix lifetime to the
667 * longest valid lifetime among the corresponding
668 * addresses on the device.
669 * Note: subsequent RA will update lifetime.
670 *
671 * --yoshfuji
672 */
673 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
674 struct in6_addr prefix;
675 struct rt6_info *rt;
676
677 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
678 rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
679
680 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
681 if (onlink == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700682 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 rt = NULL;
684 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
685 rt->rt6i_expires = expires;
686 rt->rt6i_flags |= RTF_EXPIRES;
687 }
688 }
689 dst_release(&rt->u.dst);
690 }
691
692 in6_ifa_put(ifp);
693}
694
695#ifdef CONFIG_IPV6_PRIVACY
696static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
697{
698 struct inet6_dev *idev = ifp->idev;
699 struct in6_addr addr, *tmpaddr;
700 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
701 int tmp_plen;
702 int ret = 0;
703 int max_addresses;
704
705 write_lock(&idev->lock);
706 if (ift) {
707 spin_lock_bh(&ift->lock);
708 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
709 spin_unlock_bh(&ift->lock);
710 tmpaddr = &addr;
711 } else {
712 tmpaddr = NULL;
713 }
714retry:
715 in6_dev_hold(idev);
716 if (idev->cnf.use_tempaddr <= 0) {
717 write_unlock(&idev->lock);
718 printk(KERN_INFO
719 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
720 in6_dev_put(idev);
721 ret = -1;
722 goto out;
723 }
724 spin_lock_bh(&ifp->lock);
725 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
726 idev->cnf.use_tempaddr = -1; /*XXX*/
727 spin_unlock_bh(&ifp->lock);
728 write_unlock(&idev->lock);
729 printk(KERN_WARNING
730 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
731 in6_dev_put(idev);
732 ret = -1;
733 goto out;
734 }
735 in6_ifa_hold(ifp);
736 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
737 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
738 spin_unlock_bh(&ifp->lock);
739 write_unlock(&idev->lock);
740 printk(KERN_WARNING
741 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
742 in6_ifa_put(ifp);
743 in6_dev_put(idev);
744 ret = -1;
745 goto out;
746 }
747 memcpy(&addr.s6_addr[8], idev->rndid, 8);
748 tmp_valid_lft = min_t(__u32,
749 ifp->valid_lft,
750 idev->cnf.temp_valid_lft);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900751 tmp_prefered_lft = min_t(__u32,
752 ifp->prefered_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 idev->cnf.temp_prefered_lft - desync_factor / HZ);
754 tmp_plen = ifp->prefix_len;
755 max_addresses = idev->cnf.max_addresses;
756 tmp_cstamp = ifp->cstamp;
757 tmp_tstamp = ifp->tstamp;
758 spin_unlock_bh(&ifp->lock);
759
760 write_unlock(&idev->lock);
761 ift = !max_addresses ||
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900762 ipv6_count_addresses(idev) < max_addresses ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 ipv6_add_addr(idev, &addr, tmp_plen,
764 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : NULL;
765 if (!ift || IS_ERR(ift)) {
766 in6_ifa_put(ifp);
767 in6_dev_put(idev);
768 printk(KERN_INFO
769 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
770 tmpaddr = &addr;
771 write_lock(&idev->lock);
772 goto retry;
773 }
774
775 spin_lock_bh(&ift->lock);
776 ift->ifpub = ifp;
777 ift->valid_lft = tmp_valid_lft;
778 ift->prefered_lft = tmp_prefered_lft;
779 ift->cstamp = tmp_cstamp;
780 ift->tstamp = tmp_tstamp;
781 spin_unlock_bh(&ift->lock);
782
783 addrconf_dad_start(ift, 0);
784 in6_ifa_put(ift);
785 in6_dev_put(idev);
786out:
787 return ret;
788}
789#endif
790
791/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800792 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 */
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800794struct ipv6_saddr_score {
795 int addr_type;
796 unsigned int attrs;
797 int matchlen;
Brian Haley0d27b422006-03-11 18:50:14 -0800798 int scope;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800799 unsigned int rule;
800};
801
802#define IPV6_SADDR_SCORE_LOCAL 0x0001
803#define IPV6_SADDR_SCORE_PREFERRED 0x0004
804#define IPV6_SADDR_SCORE_HOA 0x0008
805#define IPV6_SADDR_SCORE_OIF 0x0010
806#define IPV6_SADDR_SCORE_LABEL 0x0020
807#define IPV6_SADDR_SCORE_PRIVACY 0x0040
808
809static int inline ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800811 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
812 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
813 return 1;
814 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800817/* static matching label */
818static int inline ipv6_saddr_label(const struct in6_addr *addr, int type)
819{
820 /*
821 * prefix (longest match) label
822 * -----------------------------
823 * ::1/128 0
824 * ::/0 1
825 * 2002::/16 2
826 * ::/96 3
827 * ::ffff:0:0/96 4
Łukasz Stelmach102128e2006-06-22 01:37:19 -0700828 * fc00::/7 5
829 * 2001::/32 6
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800830 */
831 if (type & IPV6_ADDR_LOOPBACK)
832 return 0;
833 else if (type & IPV6_ADDR_COMPATv4)
834 return 3;
835 else if (type & IPV6_ADDR_MAPPED)
836 return 4;
Łukasz Stelmach102128e2006-06-22 01:37:19 -0700837 else if (addr->s6_addr32[0] == htonl(0x20010000))
838 return 6;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800839 else if (addr->s6_addr16[0] == htons(0x2002))
840 return 2;
Łukasz Stelmach102128e2006-06-22 01:37:19 -0700841 else if ((addr->s6_addr[0] & 0xfe) == 0xfc)
842 return 5;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800843 return 1;
844}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800846int ipv6_dev_get_saddr(struct net_device *daddr_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 struct in6_addr *daddr, struct in6_addr *saddr)
848{
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800849 struct ipv6_saddr_score hiscore;
850 struct inet6_ifaddr *ifa_result = NULL;
851 int daddr_type = __ipv6_addr_type(daddr);
852 int daddr_scope = __ipv6_addr_src_scope(daddr_type);
853 u32 daddr_label = ipv6_saddr_label(daddr, daddr_type);
854 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800856 memset(&hiscore, 0, sizeof(hiscore));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 read_lock(&dev_base_lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700859 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 for (dev = dev_base; dev; dev=dev->next) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800862 struct inet6_dev *idev;
863 struct inet6_ifaddr *ifa;
864
865 /* Rule 0: Candidate Source Address (section 4)
866 * - multicast and link-local destination address,
867 * the set of candidate source address MUST only
868 * include addresses assigned to interfaces
869 * belonging to the same link as the outgoing
870 * interface.
871 * (- For site-local destination addresses, the
872 * set of candidate source addresses MUST only
873 * include addresses assigned to interfaces
874 * belonging to the same site as the outgoing
875 * interface.)
876 */
877 if ((daddr_type & IPV6_ADDR_MULTICAST ||
878 daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
879 daddr_dev && dev != daddr_dev)
880 continue;
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 idev = __in6_dev_get(dev);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800883 if (!idev)
884 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800886 read_lock_bh(&idev->lock);
887 for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
888 struct ipv6_saddr_score score;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800890 score.addr_type = __ipv6_addr_type(&ifa->addr);
891
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +0900892 /* Rule 0:
893 * - Tentative Address (RFC2462 section 5.4)
894 * - A tentative address is not considered
895 * "assigned to an interface" in the traditional
896 * sense.
897 * - Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800898 * - In any case, anycast addresses, multicast
899 * addresses, and the unspecified address MUST
900 * NOT be included in a candidate set.
901 */
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +0900902 if (ifa->flags & IFA_F_TENTATIVE)
903 continue;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800904 if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
905 score.addr_type & IPV6_ADDR_MULTICAST)) {
906 LIMIT_NETDEBUG(KERN_DEBUG
907 "ADDRCONF: unspecified / multicast address"
908 "assigned as unicast address on %s",
909 dev->name);
910 continue;
911 }
912
913 score.attrs = 0;
914 score.matchlen = 0;
915 score.scope = 0;
916 score.rule = 0;
917
918 if (ifa_result == NULL) {
919 /* record it if the first available entry */
920 goto record_it;
921 }
922
923 /* Rule 1: Prefer same address */
924 if (hiscore.rule < 1) {
925 if (ipv6_addr_equal(&ifa_result->addr, daddr))
926 hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
927 hiscore.rule++;
928 }
929 if (ipv6_addr_equal(&ifa->addr, daddr)) {
930 score.attrs |= IPV6_SADDR_SCORE_LOCAL;
931 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
932 score.rule = 1;
933 goto record_it;
934 }
935 } else {
936 if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
937 continue;
938 }
939
940 /* Rule 2: Prefer appropriate scope */
941 if (hiscore.rule < 2) {
942 hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
943 hiscore.rule++;
944 }
945 score.scope = __ipv6_addr_src_scope(score.addr_type);
946 if (hiscore.scope < score.scope) {
947 if (hiscore.scope < daddr_scope) {
948 score.rule = 2;
949 goto record_it;
950 } else
951 continue;
952 } else if (score.scope < hiscore.scope) {
953 if (score.scope < daddr_scope)
Brian Haleye55ffac2006-07-10 15:25:51 -0700954 break; /* addresses sorted by scope */
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800955 else {
956 score.rule = 2;
957 goto record_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800961 /* Rule 3: Avoid deprecated address */
962 if (hiscore.rule < 3) {
963 if (ipv6_saddr_preferred(hiscore.addr_type) ||
964 !(ifa_result->flags & IFA_F_DEPRECATED))
965 hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
966 hiscore.rule++;
967 }
968 if (ipv6_saddr_preferred(score.addr_type) ||
969 !(ifa->flags & IFA_F_DEPRECATED)) {
970 score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
971 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
972 score.rule = 3;
973 goto record_it;
974 }
975 } else {
976 if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
977 continue;
978 }
979
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -0700980 /* Rule 4: Prefer home address */
981#ifdef CONFIG_IPV6_MIP6
982 if (hiscore.rule < 4) {
983 if (ifa_result->flags & IFA_F_HOMEADDRESS)
984 hiscore.attrs |= IPV6_SADDR_SCORE_HOA;
985 hiscore.rule++;
986 }
987 if (ifa->flags & IFA_F_HOMEADDRESS) {
988 score.attrs |= IPV6_SADDR_SCORE_HOA;
989 if (!(ifa_result->flags & IFA_F_HOMEADDRESS)) {
990 score.rule = 4;
991 goto record_it;
992 }
993 } else {
994 if (hiscore.attrs & IPV6_SADDR_SCORE_HOA)
995 continue;
996 }
997#else
YOSHIFUJI Hideaki220bbd72005-11-28 22:27:11 -0800998 if (hiscore.rule < 4)
999 hiscore.rule++;
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07001000#endif
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001001
1002 /* Rule 5: Prefer outgoing interface */
1003 if (hiscore.rule < 5) {
1004 if (daddr_dev == NULL ||
1005 daddr_dev == ifa_result->idev->dev)
1006 hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1007 hiscore.rule++;
1008 }
1009 if (daddr_dev == NULL ||
1010 daddr_dev == ifa->idev->dev) {
1011 score.attrs |= IPV6_SADDR_SCORE_OIF;
1012 if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1013 score.rule = 5;
1014 goto record_it;
1015 }
1016 } else {
1017 if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1018 continue;
1019 }
1020
1021 /* Rule 6: Prefer matching label */
1022 if (hiscore.rule < 6) {
1023 if (ipv6_saddr_label(&ifa_result->addr, hiscore.addr_type) == daddr_label)
1024 hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1025 hiscore.rule++;
1026 }
1027 if (ipv6_saddr_label(&ifa->addr, score.addr_type) == daddr_label) {
1028 score.attrs |= IPV6_SADDR_SCORE_LABEL;
1029 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1030 score.rule = 6;
1031 goto record_it;
1032 }
1033 } else {
1034 if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1035 continue;
1036 }
1037
Peter Chubb44fd0262005-11-09 13:05:47 -08001038#ifdef CONFIG_IPV6_PRIVACY
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001039 /* Rule 7: Prefer public address
1040 * Note: prefer temprary address if use_tempaddr >= 2
1041 */
1042 if (hiscore.rule < 7) {
1043 if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1044 (ifa_result->idev->cnf.use_tempaddr >= 2))
1045 hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1046 hiscore.rule++;
1047 }
1048 if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1049 (ifa->idev->cnf.use_tempaddr >= 2)) {
1050 score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1051 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1052 score.rule = 7;
1053 goto record_it;
1054 }
1055 } else {
1056 if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1057 continue;
1058 }
YOSHIFUJI Hideaki5e2707f2006-06-22 01:41:18 -07001059#else
1060 if (hiscore.rule < 7)
1061 hiscore.rule++;
Peter Chubb44fd0262005-11-09 13:05:47 -08001062#endif
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001063 /* Rule 8: Use longest matching prefix */
Yan Zheng12da2a42005-11-14 21:42:46 -08001064 if (hiscore.rule < 8) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001065 hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
Yan Zheng12da2a42005-11-14 21:42:46 -08001066 hiscore.rule++;
1067 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001068 score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1069 if (score.matchlen > hiscore.matchlen) {
1070 score.rule = 8;
1071 goto record_it;
1072 }
1073#if 0
1074 else if (score.matchlen < hiscore.matchlen)
1075 continue;
1076#endif
1077
1078 /* Final Rule: choose first available one */
1079 continue;
1080record_it:
1081 if (ifa_result)
1082 in6_ifa_put(ifa_result);
1083 in6_ifa_hold(ifa);
1084 ifa_result = ifa;
1085 hiscore = score;
1086 }
1087 read_unlock_bh(&idev->lock);
1088 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001089 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 read_unlock(&dev_base_lock);
1091
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001092 if (!ifa_result)
1093 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001094
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001095 ipv6_addr_copy(saddr, &ifa_result->addr);
1096 in6_ifa_put(ifa_result);
1097 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
1100
1101int ipv6_get_saddr(struct dst_entry *dst,
1102 struct in6_addr *daddr, struct in6_addr *saddr)
1103{
YOSHIFUJI Hideaki7a3025b2006-10-13 16:17:25 +09001104 return ipv6_dev_get_saddr(dst ? ip6_dst_idev(dst)->dev : NULL, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
1107
1108int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
1109{
1110 struct inet6_dev *idev;
1111 int err = -EADDRNOTAVAIL;
1112
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001113 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if ((idev = __in6_dev_get(dev)) != NULL) {
1115 struct inet6_ifaddr *ifp;
1116
1117 read_lock_bh(&idev->lock);
1118 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1119 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1120 ipv6_addr_copy(addr, &ifp->addr);
1121 err = 0;
1122 break;
1123 }
1124 }
1125 read_unlock_bh(&idev->lock);
1126 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001127 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return err;
1129}
1130
1131static int ipv6_count_addresses(struct inet6_dev *idev)
1132{
1133 int cnt = 0;
1134 struct inet6_ifaddr *ifp;
1135
1136 read_lock_bh(&idev->lock);
1137 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1138 cnt++;
1139 read_unlock_bh(&idev->lock);
1140 return cnt;
1141}
1142
1143int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
1144{
1145 struct inet6_ifaddr * ifp;
1146 u8 hash = ipv6_addr_hash(addr);
1147
1148 read_lock_bh(&addrconf_hash_lock);
1149 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1150 if (ipv6_addr_equal(&ifp->addr, addr) &&
1151 !(ifp->flags&IFA_F_TENTATIVE)) {
1152 if (dev == NULL || ifp->idev->dev == dev ||
1153 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1154 break;
1155 }
1156 }
1157 read_unlock_bh(&addrconf_hash_lock);
1158 return ifp != NULL;
1159}
1160
1161static
1162int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
1163{
1164 struct inet6_ifaddr * ifp;
1165 u8 hash = ipv6_addr_hash(addr);
1166
1167 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1168 if (ipv6_addr_equal(&ifp->addr, addr)) {
1169 if (dev == NULL || ifp->idev->dev == dev)
1170 break;
1171 }
1172 }
1173 return ifp != NULL;
1174}
1175
1176struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
1177{
1178 struct inet6_ifaddr * ifp;
1179 u8 hash = ipv6_addr_hash(addr);
1180
1181 read_lock_bh(&addrconf_hash_lock);
1182 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1183 if (ipv6_addr_equal(&ifp->addr, addr)) {
1184 if (dev == NULL || ifp->idev->dev == dev ||
1185 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1186 in6_ifa_hold(ifp);
1187 break;
1188 }
1189 }
1190 }
1191 read_unlock_bh(&addrconf_hash_lock);
1192
1193 return ifp;
1194}
1195
1196int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1197{
1198 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
Arnaldo Carvalho de Melo0fa1a532005-12-13 23:23:09 -08001199 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
Al Viro82103232006-09-27 18:44:10 -07001200 __be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1201 __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 int sk_ipv6only = ipv6_only_sock(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001203 int sk2_ipv6only = inet_v6_ipv6only(sk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1205 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1206
1207 if (!sk2_rcv_saddr && !sk_ipv6only)
1208 return 1;
1209
1210 if (addr_type2 == IPV6_ADDR_ANY &&
1211 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1212 return 1;
1213
1214 if (addr_type == IPV6_ADDR_ANY &&
1215 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1216 return 1;
1217
1218 if (sk2_rcv_saddr6 &&
1219 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1220 return 1;
1221
1222 if (addr_type == IPV6_ADDR_MAPPED &&
1223 !sk2_ipv6only &&
1224 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1225 return 1;
1226
1227 return 0;
1228}
1229
1230/* Gets referenced address, destroys ifaddr */
1231
Adrian Bunk9f5336e2006-01-07 13:24:25 -08001232static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (ifp->flags&IFA_F_PERMANENT) {
1235 spin_lock_bh(&ifp->lock);
1236 addrconf_del_timer(ifp);
1237 ifp->flags |= IFA_F_TENTATIVE;
1238 spin_unlock_bh(&ifp->lock);
1239 in6_ifa_put(ifp);
1240#ifdef CONFIG_IPV6_PRIVACY
1241 } else if (ifp->flags&IFA_F_TEMPORARY) {
1242 struct inet6_ifaddr *ifpub;
1243 spin_lock_bh(&ifp->lock);
1244 ifpub = ifp->ifpub;
1245 if (ifpub) {
1246 in6_ifa_hold(ifpub);
1247 spin_unlock_bh(&ifp->lock);
1248 ipv6_create_tempaddr(ifpub, ifp);
1249 in6_ifa_put(ifpub);
1250 } else {
1251 spin_unlock_bh(&ifp->lock);
1252 }
1253 ipv6_del_addr(ifp);
1254#endif
1255 } else
1256 ipv6_del_addr(ifp);
1257}
1258
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001259void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1260{
1261 if (net_ratelimit())
1262 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1263 addrconf_dad_stop(ifp);
1264}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266/* Join to solicited addr multicast group. */
1267
1268void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1269{
1270 struct in6_addr maddr;
1271
1272 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1273 return;
1274
1275 addrconf_addr_solict_mult(addr, &maddr);
1276 ipv6_dev_mc_inc(dev, &maddr);
1277}
1278
1279void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1280{
1281 struct in6_addr maddr;
1282
1283 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1284 return;
1285
1286 addrconf_addr_solict_mult(addr, &maddr);
1287 __ipv6_dev_mc_dec(idev, &maddr);
1288}
1289
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001290static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
1292 struct in6_addr addr;
1293 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1294 if (ipv6_addr_any(&addr))
1295 return;
1296 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1297}
1298
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001299static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 struct in6_addr addr;
1302 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1303 if (ipv6_addr_any(&addr))
1304 return;
1305 __ipv6_dev_ac_dec(ifp->idev, &addr);
1306}
1307
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001308static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1309{
1310 if (dev->addr_len != ETH_ALEN)
1311 return -1;
1312 memcpy(eui, dev->dev_addr, 3);
1313 memcpy(eui + 5, dev->dev_addr + 3, 3);
1314
1315 /*
1316 * The zSeries OSA network cards can be shared among various
1317 * OS instances, but the OSA cards have only one MAC address.
1318 * This leads to duplicate address conflicts in conjunction
1319 * with IPv6 if more than one instance uses the same card.
1320 *
1321 * The driver for these cards can deliver a unique 16-bit
1322 * identifier for each instance sharing the same card. It is
1323 * placed instead of 0xFFFE in the interface identifier. The
1324 * "u" bit of the interface identifier is not inverted in this
1325 * case. Hence the resulting interface identifier has local
1326 * scope according to RFC2373.
1327 */
1328 if (dev->dev_id) {
1329 eui[3] = (dev->dev_id >> 8) & 0xFF;
1330 eui[4] = dev->dev_id & 0xFF;
1331 } else {
1332 eui[3] = 0xFF;
1333 eui[4] = 0xFE;
1334 eui[0] ^= 2;
1335 }
1336 return 0;
1337}
1338
1339static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1340{
1341 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1342 if (dev->addr_len != ARCNET_ALEN)
1343 return -1;
1344 memset(eui, 0, 7);
1345 eui[7] = *(u8*)dev->dev_addr;
1346 return 0;
1347}
1348
1349static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1350{
1351 if (dev->addr_len != INFINIBAND_ALEN)
1352 return -1;
1353 memcpy(eui, dev->dev_addr + 12, 8);
1354 eui[0] |= 2;
1355 return 0;
1356}
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1359{
1360 switch (dev->type) {
1361 case ARPHRD_ETHER:
1362 case ARPHRD_FDDI:
1363 case ARPHRD_IEEE802_TR:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001364 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001366 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001368 return addrconf_ifid_infiniband(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
1370 return -1;
1371}
1372
1373static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1374{
1375 int err = -1;
1376 struct inet6_ifaddr *ifp;
1377
1378 read_lock_bh(&idev->lock);
1379 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1380 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1381 memcpy(eui, ifp->addr.s6_addr+8, 8);
1382 err = 0;
1383 break;
1384 }
1385 }
1386 read_unlock_bh(&idev->lock);
1387 return err;
1388}
1389
1390#ifdef CONFIG_IPV6_PRIVACY
1391/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1392static int __ipv6_regen_rndid(struct inet6_dev *idev)
1393{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08001395 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 /*
1399 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1400 * check if generated address is not inappropriate
1401 *
1402 * - Reserved subnet anycast (RFC 2526)
1403 * 11111101 11....11 1xxxxxxx
1404 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1405 * 00-00-5E-FE-xx-xx-xx-xx
1406 * - value 0
1407 * - XXX: already assigned to an address on the device
1408 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001409 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1411 (idev->rndid[7]&0x80))
1412 goto regen;
1413 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1414 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1415 goto regen;
1416 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1417 goto regen;
1418 }
1419
1420 return 0;
1421}
1422
1423static void ipv6_regen_rndid(unsigned long data)
1424{
1425 struct inet6_dev *idev = (struct inet6_dev *) data;
1426 unsigned long expires;
1427
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001428 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 write_lock_bh(&idev->lock);
1430
1431 if (idev->dead)
1432 goto out;
1433
1434 if (__ipv6_regen_rndid(idev) < 0)
1435 goto out;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 expires = jiffies +
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001438 idev->cnf.temp_prefered_lft * HZ -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1440 if (time_before(expires, jiffies)) {
1441 printk(KERN_WARNING
1442 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1443 idev->dev->name);
1444 goto out;
1445 }
1446
1447 if (!mod_timer(&idev->regen_timer, expires))
1448 in6_dev_hold(idev);
1449
1450out:
1451 write_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001452 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 in6_dev_put(idev);
1454}
1455
1456static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1457 int ret = 0;
1458
1459 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1460 ret = __ipv6_regen_rndid(idev);
1461 return ret;
1462}
1463#endif
1464
1465/*
1466 * Add prefix route.
1467 */
1468
1469static void
1470addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001471 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Thomas Graf86872cb2006-08-22 00:01:08 -07001473 struct fib6_config cfg = {
1474 .fc_table = RT6_TABLE_PREFIX,
1475 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1476 .fc_ifindex = dev->ifindex,
1477 .fc_expires = expires,
1478 .fc_dst_len = plen,
1479 .fc_flags = RTF_UP | flags,
1480 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Thomas Graf86872cb2006-08-22 00:01:08 -07001482 ipv6_addr_copy(&cfg.fc_dst, pfx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 /* Prevent useless cloning on PtP SIT.
1485 This thing is done here expecting that the whole
1486 class of non-broadcast devices need not cloning.
1487 */
Joerg Roedel0be669b2006-10-10 14:49:53 -07001488#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Thomas Graf86872cb2006-08-22 00:01:08 -07001489 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1490 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07001491#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Thomas Graf86872cb2006-08-22 00:01:08 -07001493 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
1496/* Create "default" multicast route to the interface */
1497
1498static void addrconf_add_mroute(struct net_device *dev)
1499{
Thomas Graf86872cb2006-08-22 00:01:08 -07001500 struct fib6_config cfg = {
1501 .fc_table = RT6_TABLE_LOCAL,
1502 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1503 .fc_ifindex = dev->ifindex,
1504 .fc_dst_len = 8,
1505 .fc_flags = RTF_UP,
1506 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Thomas Graf86872cb2006-08-22 00:01:08 -07001508 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1509
1510 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
1512
Joerg Roedel0be669b2006-10-10 14:49:53 -07001513#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514static void sit_route_add(struct net_device *dev)
1515{
Thomas Graf86872cb2006-08-22 00:01:08 -07001516 struct fib6_config cfg = {
1517 .fc_table = RT6_TABLE_MAIN,
1518 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1519 .fc_ifindex = dev->ifindex,
1520 .fc_dst_len = 96,
1521 .fc_flags = RTF_UP | RTF_NONEXTHOP,
1522 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 /* prefix length - 96 bits "::d.d.d.d" */
Thomas Graf86872cb2006-08-22 00:01:08 -07001525 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
Joerg Roedel0be669b2006-10-10 14:49:53 -07001527#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529static void addrconf_add_lroute(struct net_device *dev)
1530{
1531 struct in6_addr addr;
1532
1533 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1534 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1535}
1536
1537static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1538{
1539 struct inet6_dev *idev;
1540
1541 ASSERT_RTNL();
1542
1543 if ((idev = ipv6_find_idev(dev)) == NULL)
1544 return NULL;
1545
1546 /* Add default multicast route */
1547 addrconf_add_mroute(dev);
1548
1549 /* Add link local route */
1550 addrconf_add_lroute(dev);
1551 return idev;
1552}
1553
1554void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1555{
1556 struct prefix_info *pinfo;
1557 __u32 valid_lft;
1558 __u32 prefered_lft;
1559 int addr_type;
1560 unsigned long rt_expires;
1561 struct inet6_dev *in6_dev;
1562
1563 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 if (len < sizeof(struct prefix_info)) {
1566 ADBG(("addrconf: prefix option too short\n"));
1567 return;
1568 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 /*
1571 * Validation checks ([ADDRCONF], page 19)
1572 */
1573
1574 addr_type = ipv6_addr_type(&pinfo->prefix);
1575
1576 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1577 return;
1578
1579 valid_lft = ntohl(pinfo->valid);
1580 prefered_lft = ntohl(pinfo->prefered);
1581
1582 if (prefered_lft > valid_lft) {
1583 if (net_ratelimit())
1584 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1585 return;
1586 }
1587
1588 in6_dev = in6_dev_get(dev);
1589
1590 if (in6_dev == NULL) {
1591 if (net_ratelimit())
1592 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1593 return;
1594 }
1595
1596 /*
1597 * Two things going on here:
1598 * 1) Add routes for on-link prefixes
1599 * 2) Configure prefixes with the auto flag set
1600 */
1601
1602 /* Avoid arithmetic overflow. Really, we could
1603 save rt_expires in seconds, likely valid_lft,
1604 but it would require division in fib gc, that it
1605 not good.
1606 */
1607 if (valid_lft >= 0x7FFFFFFF/HZ)
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001608 rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 else
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001610 rt_expires = valid_lft * HZ;
1611
1612 /*
1613 * We convert this (in jiffies) to clock_t later.
1614 * Avoid arithmetic overflow there as well.
1615 * Overflow can happen only if HZ < USER_HZ.
1616 */
1617 if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
1618 rt_expires = 0x7FFFFFFF / USER_HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
1620 if (pinfo->onlink) {
1621 struct rt6_info *rt;
1622 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1623
1624 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1625 if (rt->rt6i_flags&RTF_EXPIRES) {
1626 if (valid_lft == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001627 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 rt = NULL;
1629 } else {
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001630 rt->rt6i_expires = jiffies + rt_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 }
1632 }
1633 } else if (valid_lft) {
1634 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001635 dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
1637 if (rt)
1638 dst_release(&rt->u.dst);
1639 }
1640
1641 /* Try to figure out our local address for this prefix */
1642
1643 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1644 struct inet6_ifaddr * ifp;
1645 struct in6_addr addr;
1646 int create = 0, update_lft = 0;
1647
1648 if (pinfo->prefix_len == 64) {
1649 memcpy(&addr, &pinfo->prefix, 8);
1650 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1651 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1652 in6_dev_put(in6_dev);
1653 return;
1654 }
1655 goto ok;
1656 }
1657 if (net_ratelimit())
1658 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1659 pinfo->prefix_len);
1660 in6_dev_put(in6_dev);
1661 return;
1662
1663ok:
1664
1665 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1666
1667 if (ifp == NULL && valid_lft) {
1668 int max_addresses = in6_dev->cnf.max_addresses;
1669
1670 /* Do not allow to create too much of autoconfigured
1671 * addresses; this would be too easy way to crash kernel.
1672 */
1673 if (!max_addresses ||
1674 ipv6_count_addresses(in6_dev) < max_addresses)
1675 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1676 addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1677
1678 if (!ifp || IS_ERR(ifp)) {
1679 in6_dev_put(in6_dev);
1680 return;
1681 }
1682
1683 update_lft = create = 1;
1684 ifp->cstamp = jiffies;
1685 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1686 }
1687
1688 if (ifp) {
1689 int flags;
1690 unsigned long now;
1691#ifdef CONFIG_IPV6_PRIVACY
1692 struct inet6_ifaddr *ift;
1693#endif
1694 u32 stored_lft;
1695
1696 /* update lifetime (RFC2462 5.5.3 e) */
1697 spin_lock(&ifp->lock);
1698 now = jiffies;
1699 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1700 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1701 else
1702 stored_lft = 0;
1703 if (!update_lft && stored_lft) {
1704 if (valid_lft > MIN_VALID_LIFETIME ||
1705 valid_lft > stored_lft)
1706 update_lft = 1;
1707 else if (stored_lft <= MIN_VALID_LIFETIME) {
1708 /* valid_lft <= stored_lft is always true */
1709 /* XXX: IPsec */
1710 update_lft = 0;
1711 } else {
1712 valid_lft = MIN_VALID_LIFETIME;
1713 if (valid_lft < prefered_lft)
1714 prefered_lft = valid_lft;
1715 update_lft = 1;
1716 }
1717 }
1718
1719 if (update_lft) {
1720 ifp->valid_lft = valid_lft;
1721 ifp->prefered_lft = prefered_lft;
1722 ifp->tstamp = now;
1723 flags = ifp->flags;
1724 ifp->flags &= ~IFA_F_DEPRECATED;
1725 spin_unlock(&ifp->lock);
1726
1727 if (!(flags&IFA_F_TENTATIVE))
1728 ipv6_ifa_notify(0, ifp);
1729 } else
1730 spin_unlock(&ifp->lock);
1731
1732#ifdef CONFIG_IPV6_PRIVACY
1733 read_lock_bh(&in6_dev->lock);
1734 /* update all temporary addresses in the list */
1735 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1736 /*
1737 * When adjusting the lifetimes of an existing
1738 * temporary address, only lower the lifetimes.
1739 * Implementations must not increase the
1740 * lifetimes of an existing temporary address
1741 * when processing a Prefix Information Option.
1742 */
1743 spin_lock(&ift->lock);
1744 flags = ift->flags;
1745 if (ift->valid_lft > valid_lft &&
1746 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1747 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1748 if (ift->prefered_lft > prefered_lft &&
1749 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1750 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1751 spin_unlock(&ift->lock);
1752 if (!(flags&IFA_F_TENTATIVE))
1753 ipv6_ifa_notify(0, ift);
1754 }
1755
1756 if (create && in6_dev->cnf.use_tempaddr > 0) {
1757 /*
1758 * When a new public address is created as described in [ADDRCONF],
1759 * also create a new temporary address.
1760 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001761 read_unlock_bh(&in6_dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 ipv6_create_tempaddr(ifp, NULL);
1763 } else {
1764 read_unlock_bh(&in6_dev->lock);
1765 }
1766#endif
1767 in6_ifa_put(ifp);
1768 addrconf_verify(0);
1769 }
1770 }
1771 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1772 in6_dev_put(in6_dev);
1773}
1774
1775/*
1776 * Set destination address.
1777 * Special case for SIT interfaces where we create a new "virtual"
1778 * device.
1779 */
1780int addrconf_set_dstaddr(void __user *arg)
1781{
1782 struct in6_ifreq ireq;
1783 struct net_device *dev;
1784 int err = -EINVAL;
1785
1786 rtnl_lock();
1787
1788 err = -EFAULT;
1789 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1790 goto err_exit;
1791
1792 dev = __dev_get_by_index(ireq.ifr6_ifindex);
1793
1794 err = -ENODEV;
1795 if (dev == NULL)
1796 goto err_exit;
1797
Joerg Roedel0be669b2006-10-10 14:49:53 -07001798#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 if (dev->type == ARPHRD_SIT) {
1800 struct ifreq ifr;
1801 mm_segment_t oldfs;
1802 struct ip_tunnel_parm p;
1803
1804 err = -EADDRNOTAVAIL;
1805 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1806 goto err_exit;
1807
1808 memset(&p, 0, sizeof(p));
1809 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1810 p.iph.saddr = 0;
1811 p.iph.version = 4;
1812 p.iph.ihl = 5;
1813 p.iph.protocol = IPPROTO_IPV6;
1814 p.iph.ttl = 64;
1815 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1816
1817 oldfs = get_fs(); set_fs(KERNEL_DS);
1818 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1819 set_fs(oldfs);
1820
1821 if (err == 0) {
1822 err = -ENOBUFS;
1823 if ((dev = __dev_get_by_name(p.name)) == NULL)
1824 goto err_exit;
1825 err = dev_open(dev);
1826 }
1827 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07001828#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830err_exit:
1831 rtnl_unlock();
1832 return err;
1833}
1834
1835/*
1836 * Manual configuration of address on an interface
1837 */
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001838static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07001839 __u8 ifa_flags, __u32 prefered_lft, __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
1841 struct inet6_ifaddr *ifp;
1842 struct inet6_dev *idev;
1843 struct net_device *dev;
1844 int scope;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09001845 u32 flags = RTF_EXPIRES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001848
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001849 /* check the lifetime */
1850 if (!valid_lft || prefered_lft > valid_lft)
1851 return -EINVAL;
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1854 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 if ((idev = addrconf_add_dev(dev)) == NULL)
1857 return -ENOBUFS;
1858
1859 scope = ipv6_addr_scope(pfx);
1860
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09001861 if (valid_lft == INFINITY_LIFE_TIME) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001862 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09001863 flags = 0;
1864 } else if (valid_lft >= 0x7FFFFFFF/HZ)
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001865 valid_lft = 0x7FFFFFFF/HZ;
1866
1867 if (prefered_lft == 0)
1868 ifa_flags |= IFA_F_DEPRECATED;
1869 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
1870 (prefered_lft != INFINITY_LIFE_TIME))
1871 prefered_lft = 0x7FFFFFFF/HZ;
1872
1873 ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
1874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 if (!IS_ERR(ifp)) {
Herbert Xu06aebfb2006-08-09 16:52:04 -07001876 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001877 ifp->valid_lft = valid_lft;
1878 ifp->prefered_lft = prefered_lft;
1879 ifp->tstamp = jiffies;
Herbert Xu06aebfb2006-08-09 16:52:04 -07001880 spin_unlock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001881
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09001882 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
1883 jiffies_to_clock_t(valid_lft * HZ), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 addrconf_dad_start(ifp, 0);
1885 in6_ifa_put(ifp);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001886 addrconf_verify(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 return 0;
1888 }
1889
1890 return PTR_ERR(ifp);
1891}
1892
1893static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1894{
1895 struct inet6_ifaddr *ifp;
1896 struct inet6_dev *idev;
1897 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1900 return -ENODEV;
1901
1902 if ((idev = __in6_dev_get(dev)) == NULL)
1903 return -ENXIO;
1904
1905 read_lock_bh(&idev->lock);
1906 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1907 if (ifp->prefix_len == plen &&
1908 ipv6_addr_equal(pfx, &ifp->addr)) {
1909 in6_ifa_hold(ifp);
1910 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 ipv6_del_addr(ifp);
1913
1914 /* If the last address is deleted administratively,
1915 disable IPv6 on this interface.
1916 */
1917 if (idev->addr_list == NULL)
1918 addrconf_ifdown(idev->dev, 1);
1919 return 0;
1920 }
1921 }
1922 read_unlock_bh(&idev->lock);
1923 return -EADDRNOTAVAIL;
1924}
1925
1926
1927int addrconf_add_ifaddr(void __user *arg)
1928{
1929 struct in6_ifreq ireq;
1930 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 if (!capable(CAP_NET_ADMIN))
1933 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1936 return -EFAULT;
1937
1938 rtnl_lock();
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001939 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07001940 IFA_F_PERMANENT, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 rtnl_unlock();
1942 return err;
1943}
1944
1945int addrconf_del_ifaddr(void __user *arg)
1946{
1947 struct in6_ifreq ireq;
1948 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 if (!capable(CAP_NET_ADMIN))
1951 return -EPERM;
1952
1953 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1954 return -EFAULT;
1955
1956 rtnl_lock();
1957 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1958 rtnl_unlock();
1959 return err;
1960}
1961
Joerg Roedel0be669b2006-10-10 14:49:53 -07001962#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963static void sit_add_v4_addrs(struct inet6_dev *idev)
1964{
1965 struct inet6_ifaddr * ifp;
1966 struct in6_addr addr;
1967 struct net_device *dev;
1968 int scope;
1969
1970 ASSERT_RTNL();
1971
1972 memset(&addr, 0, sizeof(struct in6_addr));
1973 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
1974
1975 if (idev->dev->flags&IFF_POINTOPOINT) {
1976 addr.s6_addr32[0] = htonl(0xfe800000);
1977 scope = IFA_LINK;
1978 } else {
1979 scope = IPV6_ADDR_COMPATv4;
1980 }
1981
1982 if (addr.s6_addr32[3]) {
1983 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
1984 if (!IS_ERR(ifp)) {
1985 spin_lock_bh(&ifp->lock);
1986 ifp->flags &= ~IFA_F_TENTATIVE;
1987 spin_unlock_bh(&ifp->lock);
1988 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1989 in6_ifa_put(ifp);
1990 }
1991 return;
1992 }
1993
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001994 for (dev = dev_base; dev != NULL; dev = dev->next) {
Herbert Xue5ed6392005-10-03 14:35:55 -07001995 struct in_device * in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if (in_dev && (dev->flags & IFF_UP)) {
1997 struct in_ifaddr * ifa;
1998
1999 int flag = scope;
2000
2001 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2002 int plen;
2003
2004 addr.s6_addr32[3] = ifa->ifa_local;
2005
2006 if (ifa->ifa_scope == RT_SCOPE_LINK)
2007 continue;
2008 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2009 if (idev->dev->flags&IFF_POINTOPOINT)
2010 continue;
2011 flag |= IFA_HOST;
2012 }
2013 if (idev->dev->flags&IFF_POINTOPOINT)
2014 plen = 64;
2015 else
2016 plen = 96;
2017
2018 ifp = ipv6_add_addr(idev, &addr, plen, flag,
2019 IFA_F_PERMANENT);
2020 if (!IS_ERR(ifp)) {
2021 spin_lock_bh(&ifp->lock);
2022 ifp->flags &= ~IFA_F_TENTATIVE;
2023 spin_unlock_bh(&ifp->lock);
2024 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2025 in6_ifa_put(ifp);
2026 }
2027 }
2028 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002031#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
2033static void init_loopback(struct net_device *dev)
2034{
2035 struct inet6_dev *idev;
2036 struct inet6_ifaddr * ifp;
2037
2038 /* ::1 */
2039
2040 ASSERT_RTNL();
2041
2042 if ((idev = ipv6_find_idev(dev)) == NULL) {
2043 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2044 return;
2045 }
2046
2047 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2048 if (!IS_ERR(ifp)) {
2049 spin_lock_bh(&ifp->lock);
2050 ifp->flags &= ~IFA_F_TENTATIVE;
2051 spin_unlock_bh(&ifp->lock);
2052 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2053 in6_ifa_put(ifp);
2054 }
2055}
2056
2057static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2058{
2059 struct inet6_ifaddr * ifp;
2060
2061 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
2062 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002063 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 addrconf_dad_start(ifp, 0);
2065 in6_ifa_put(ifp);
2066 }
2067}
2068
2069static void addrconf_dev_config(struct net_device *dev)
2070{
2071 struct in6_addr addr;
2072 struct inet6_dev * idev;
2073
2074 ASSERT_RTNL();
2075
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002076 if ((dev->type != ARPHRD_ETHER) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 (dev->type != ARPHRD_FDDI) &&
2078 (dev->type != ARPHRD_IEEE802_TR) &&
2079 (dev->type != ARPHRD_ARCNET) &&
2080 (dev->type != ARPHRD_INFINIBAND)) {
2081 /* Alas, we support only Ethernet autoconfiguration. */
2082 return;
2083 }
2084
2085 idev = addrconf_add_dev(dev);
2086 if (idev == NULL)
2087 return;
2088
2089 memset(&addr, 0, sizeof(struct in6_addr));
2090 addr.s6_addr32[0] = htonl(0xFE800000);
2091
2092 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2093 addrconf_add_linklocal(idev, &addr);
2094}
2095
Joerg Roedel0be669b2006-10-10 14:49:53 -07002096#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097static void addrconf_sit_config(struct net_device *dev)
2098{
2099 struct inet6_dev *idev;
2100
2101 ASSERT_RTNL();
2102
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002103 /*
2104 * Configure the tunnel with one of our IPv4
2105 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 * our v4 addrs in the tunnel
2107 */
2108
2109 if ((idev = ipv6_find_idev(dev)) == NULL) {
2110 printk(KERN_DEBUG "init sit: add_dev failed\n");
2111 return;
2112 }
2113
2114 sit_add_v4_addrs(idev);
2115
2116 if (dev->flags&IFF_POINTOPOINT) {
2117 addrconf_add_mroute(dev);
2118 addrconf_add_lroute(dev);
2119 } else
2120 sit_route_add(dev);
2121}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002122#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124static inline int
2125ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2126{
2127 struct in6_addr lladdr;
2128
2129 if (!ipv6_get_lladdr(link_dev, &lladdr)) {
2130 addrconf_add_linklocal(idev, &lladdr);
2131 return 0;
2132 }
2133 return -1;
2134}
2135
2136static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2137{
2138 struct net_device *link_dev;
2139
2140 /* first try to inherit the link-local address from the link device */
2141 if (idev->dev->iflink &&
2142 (link_dev = __dev_get_by_index(idev->dev->iflink))) {
2143 if (!ipv6_inherit_linklocal(idev, link_dev))
2144 return;
2145 }
2146 /* then try to inherit it from any device */
2147 for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
2148 if (!ipv6_inherit_linklocal(idev, link_dev))
2149 return;
2150 }
2151 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2152}
2153
2154/*
2155 * Autoconfigure tunnel with a link-local address so routing protocols,
2156 * DHCPv6, MLD etc. can be run over the virtual link
2157 */
2158
2159static void addrconf_ip6_tnl_config(struct net_device *dev)
2160{
2161 struct inet6_dev *idev;
2162
2163 ASSERT_RTNL();
2164
2165 if ((idev = addrconf_add_dev(dev)) == NULL) {
2166 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2167 return;
2168 }
2169 ip6_tnl_add_linklocal(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
2171
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002172static int addrconf_notify(struct notifier_block *this, unsigned long event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 void * data)
2174{
2175 struct net_device *dev = (struct net_device *) data;
2176 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002177 int run_pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179 switch(event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002180 case NETDEV_REGISTER:
2181 if (!idev) {
2182 idev = ipv6_add_dev(dev);
2183 if (!idev)
2184 printk(KERN_WARNING "IPv6: add_dev failed for %s\n",
2185 dev->name);
2186 }
2187 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002189 case NETDEV_CHANGE:
2190 if (event == NETDEV_UP) {
2191 if (!netif_carrier_ok(dev)) {
2192 /* device is not ready yet. */
2193 printk(KERN_INFO
2194 "ADDRCONF(NETDEV_UP): %s: "
2195 "link is not ready\n",
2196 dev->name);
2197 break;
2198 }
Kristian Slavov99081042006-02-08 16:10:53 -08002199
2200 if (idev)
2201 idev->if_flags |= IF_READY;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002202 } else {
2203 if (!netif_carrier_ok(dev)) {
2204 /* device is still not ready. */
2205 break;
2206 }
2207
2208 if (idev) {
2209 if (idev->if_flags & IF_READY) {
2210 /* device is already configured. */
2211 break;
2212 }
2213 idev->if_flags |= IF_READY;
2214 }
2215
2216 printk(KERN_INFO
2217 "ADDRCONF(NETDEV_CHANGE): %s: "
2218 "link becomes ready\n",
2219 dev->name);
2220
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002221 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002222 }
2223
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 switch(dev->type) {
Joerg Roedel0be669b2006-10-10 14:49:53 -07002225#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 case ARPHRD_SIT:
2227 addrconf_sit_config(dev);
2228 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002229#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 case ARPHRD_TUNNEL6:
2231 addrconf_ip6_tnl_config(dev);
2232 break;
2233 case ARPHRD_LOOPBACK:
2234 init_loopback(dev);
2235 break;
2236
2237 default:
2238 addrconf_dev_config(dev);
2239 break;
2240 };
2241 if (idev) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002242 if (run_pending)
2243 addrconf_dad_run(idev);
2244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 /* If the MTU changed during the interface down, when the
2246 interface up, the changed MTU must be reflected in the
2247 idev as well as routers.
2248 */
2249 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2250 rt6_mtu_change(dev, dev->mtu);
2251 idev->cnf.mtu6 = dev->mtu;
2252 }
2253 idev->tstamp = jiffies;
2254 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2255 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2256 stop IPv6 on this interface.
2257 */
2258 if (dev->mtu < IPV6_MIN_MTU)
2259 addrconf_ifdown(dev, event != NETDEV_DOWN);
2260 }
2261 break;
2262
2263 case NETDEV_CHANGEMTU:
2264 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
2265 rt6_mtu_change(dev, dev->mtu);
2266 idev->cnf.mtu6 = dev->mtu;
2267 break;
2268 }
2269
2270 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2271
2272 case NETDEV_DOWN:
2273 case NETDEV_UNREGISTER:
2274 /*
2275 * Remove all addresses from this interface.
2276 */
2277 addrconf_ifdown(dev, event != NETDEV_DOWN);
2278 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 case NETDEV_CHANGENAME:
2281#ifdef CONFIG_SYSCTL
2282 if (idev) {
2283 addrconf_sysctl_unregister(&idev->cnf);
2284 neigh_sysctl_unregister(idev->nd_parms);
2285 neigh_sysctl_register(dev, idev->nd_parms,
2286 NET_IPV6, NET_IPV6_NEIGH, "ipv6",
2287 &ndisc_ifinfo_sysctl_change,
2288 NULL);
2289 addrconf_sysctl_register(idev, &idev->cnf);
2290 }
2291#endif
2292 break;
2293 };
2294
2295 return NOTIFY_OK;
2296}
2297
2298/*
2299 * addrconf module should be notified of a device going up
2300 */
2301static struct notifier_block ipv6_dev_notf = {
2302 .notifier_call = addrconf_notify,
2303 .priority = 0
2304};
2305
2306static int addrconf_ifdown(struct net_device *dev, int how)
2307{
2308 struct inet6_dev *idev;
2309 struct inet6_ifaddr *ifa, **bifa;
2310 int i;
2311
2312 ASSERT_RTNL();
2313
2314 if (dev == &loopback_dev && how == 1)
2315 how = 0;
2316
2317 rt6_ifdown(dev);
2318 neigh_ifdown(&nd_tbl, dev);
2319
2320 idev = __in6_dev_get(dev);
2321 if (idev == NULL)
2322 return -ENODEV;
2323
2324 /* Step 1: remove reference to ipv6 device from parent device.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002325 Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 */
2327 if (how == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002329
2330 /* protected by rtnl_lock */
2331 rcu_assign_pointer(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
2333 /* Step 1.5: remove snmp6 entry */
2334 snmp6_unregister_dev(idev);
2335
2336 }
2337
2338 /* Step 2: clear hash table */
2339 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2340 bifa = &inet6_addr_lst[i];
2341
2342 write_lock_bh(&addrconf_hash_lock);
2343 while ((ifa = *bifa) != NULL) {
2344 if (ifa->idev == idev) {
2345 *bifa = ifa->lst_next;
2346 ifa->lst_next = NULL;
2347 addrconf_del_timer(ifa);
2348 in6_ifa_put(ifa);
2349 continue;
2350 }
2351 bifa = &ifa->lst_next;
2352 }
2353 write_unlock_bh(&addrconf_hash_lock);
2354 }
2355
2356 write_lock_bh(&idev->lock);
2357
2358 /* Step 3: clear flags for stateless addrconf */
2359 if (how != 1)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002360 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
2362 /* Step 4: clear address list */
2363#ifdef CONFIG_IPV6_PRIVACY
2364 if (how == 1 && del_timer(&idev->regen_timer))
2365 in6_dev_put(idev);
2366
2367 /* clear tempaddr list */
2368 while ((ifa = idev->tempaddr_list) != NULL) {
2369 idev->tempaddr_list = ifa->tmp_next;
2370 ifa->tmp_next = NULL;
2371 ifa->dead = 1;
2372 write_unlock_bh(&idev->lock);
2373 spin_lock_bh(&ifa->lock);
2374
2375 if (ifa->ifpub) {
2376 in6_ifa_put(ifa->ifpub);
2377 ifa->ifpub = NULL;
2378 }
2379 spin_unlock_bh(&ifa->lock);
2380 in6_ifa_put(ifa);
2381 write_lock_bh(&idev->lock);
2382 }
2383#endif
2384 while ((ifa = idev->addr_list) != NULL) {
2385 idev->addr_list = ifa->if_next;
2386 ifa->if_next = NULL;
2387 ifa->dead = 1;
2388 addrconf_del_timer(ifa);
2389 write_unlock_bh(&idev->lock);
2390
2391 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2392 in6_ifa_put(ifa);
2393
2394 write_lock_bh(&idev->lock);
2395 }
2396 write_unlock_bh(&idev->lock);
2397
2398 /* Step 5: Discard multicast list */
2399
2400 if (how == 1)
2401 ipv6_mc_destroy_dev(idev);
2402 else
2403 ipv6_mc_down(idev);
2404
2405 /* Step 5: netlink notification of this interface */
2406 idev->tstamp = jiffies;
Yan Zheng979ad662005-10-14 18:31:15 +08002407 inet6_ifinfo_notify(RTM_DELLINK, idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002408
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 /* Shot the device (if unregistered) */
2410
2411 if (how == 1) {
2412#ifdef CONFIG_SYSCTL
2413 addrconf_sysctl_unregister(&idev->cnf);
2414 neigh_sysctl_unregister(idev->nd_parms);
2415#endif
2416 neigh_parms_release(&nd_tbl, idev->nd_parms);
2417 neigh_ifdown(&nd_tbl, dev);
2418 in6_dev_put(idev);
2419 }
2420 return 0;
2421}
2422
2423static void addrconf_rs_timer(unsigned long data)
2424{
2425 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2426
2427 if (ifp->idev->cnf.forwarding)
2428 goto out;
2429
2430 if (ifp->idev->if_flags & IF_RA_RCVD) {
2431 /*
2432 * Announcement received after solicitation
2433 * was sent
2434 */
2435 goto out;
2436 }
2437
2438 spin_lock(&ifp->lock);
2439 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2440 struct in6_addr all_routers;
2441
2442 /* The wait after the last probe can be shorter */
2443 addrconf_mod_timer(ifp, AC_RS,
2444 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2445 ifp->idev->cnf.rtr_solicit_delay :
2446 ifp->idev->cnf.rtr_solicit_interval);
2447 spin_unlock(&ifp->lock);
2448
2449 ipv6_addr_all_routers(&all_routers);
2450
2451 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2452 } else {
2453 spin_unlock(&ifp->lock);
2454 /*
2455 * Note: we do not support deprecated "all on-link"
2456 * assumption any longer.
2457 */
2458 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2459 ifp->idev->dev->name);
2460 }
2461
2462out:
2463 in6_ifa_put(ifp);
2464}
2465
2466/*
2467 * Duplicate Address Detection
2468 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002469static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2470{
2471 unsigned long rand_num;
2472 struct inet6_dev *idev = ifp->idev;
2473
2474 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2475 ifp->probes = idev->cnf.dad_transmits;
2476 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2477}
2478
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002479static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480{
2481 struct inet6_dev *idev = ifp->idev;
2482 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
2484 addrconf_join_solict(dev, &ifp->addr);
2485
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 net_srandom(ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
2488 read_lock_bh(&idev->lock);
2489 if (ifp->dead)
2490 goto out;
2491 spin_lock_bh(&ifp->lock);
2492
2493 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002494 !(ifp->flags&IFA_F_TENTATIVE) ||
2495 ifp->flags & IFA_F_NODAD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 ifp->flags &= ~IFA_F_TENTATIVE;
2497 spin_unlock_bh(&ifp->lock);
2498 read_unlock_bh(&idev->lock);
2499
2500 addrconf_dad_completed(ifp);
2501 return;
2502 }
2503
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002504 if (!(idev->if_flags & IF_READY)) {
YOSHIFUJI Hideaki3dd3bf82005-12-23 11:23:21 -08002505 spin_unlock_bh(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002506 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002507 /*
2508 * If the defice is not ready:
2509 * - keep it tentative if it is a permanent address.
2510 * - otherwise, kill it.
2511 */
2512 in6_ifa_hold(ifp);
2513 addrconf_dad_stop(ifp);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002514 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002515 }
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002516 addrconf_dad_kick(ifp);
2517 spin_unlock_bh(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518out:
2519 read_unlock_bh(&idev->lock);
2520}
2521
2522static void addrconf_dad_timer(unsigned long data)
2523{
2524 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2525 struct inet6_dev *idev = ifp->idev;
2526 struct in6_addr unspec;
2527 struct in6_addr mcaddr;
2528
2529 read_lock_bh(&idev->lock);
2530 if (idev->dead) {
2531 read_unlock_bh(&idev->lock);
2532 goto out;
2533 }
2534 spin_lock_bh(&ifp->lock);
2535 if (ifp->probes == 0) {
2536 /*
2537 * DAD was successful
2538 */
2539
2540 ifp->flags &= ~IFA_F_TENTATIVE;
2541 spin_unlock_bh(&ifp->lock);
2542 read_unlock_bh(&idev->lock);
2543
2544 addrconf_dad_completed(ifp);
2545
2546 goto out;
2547 }
2548
2549 ifp->probes--;
2550 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2551 spin_unlock_bh(&ifp->lock);
2552 read_unlock_bh(&idev->lock);
2553
2554 /* send a neighbour solicitation for our addr */
2555 memset(&unspec, 0, sizeof(unspec));
2556 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2557 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2558out:
2559 in6_ifa_put(ifp);
2560}
2561
2562static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2563{
2564 struct net_device * dev = ifp->idev->dev;
2565
2566 /*
2567 * Configure the address for reception. Now it is valid.
2568 */
2569
2570 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2571
2572 /* If added prefix is link local and forwarding is off,
2573 start sending router solicitations.
2574 */
2575
2576 if (ifp->idev->cnf.forwarding == 0 &&
2577 ifp->idev->cnf.rtr_solicits > 0 &&
2578 (dev->flags&IFF_LOOPBACK) == 0 &&
2579 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2580 struct in6_addr all_routers;
2581
2582 ipv6_addr_all_routers(&all_routers);
2583
2584 /*
2585 * If a host as already performed a random delay
2586 * [...] as part of DAD [...] there is no need
2587 * to delay again before sending the first RS
2588 */
2589 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2590
2591 spin_lock_bh(&ifp->lock);
2592 ifp->probes = 1;
2593 ifp->idev->if_flags |= IF_RS_SENT;
2594 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2595 spin_unlock_bh(&ifp->lock);
2596 }
2597}
2598
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002599static void addrconf_dad_run(struct inet6_dev *idev) {
2600 struct inet6_ifaddr *ifp;
2601
2602 read_lock_bh(&idev->lock);
2603 for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2604 spin_lock_bh(&ifp->lock);
2605 if (!(ifp->flags & IFA_F_TENTATIVE)) {
2606 spin_unlock_bh(&ifp->lock);
2607 continue;
2608 }
2609 spin_unlock_bh(&ifp->lock);
2610 addrconf_dad_kick(ifp);
2611 }
2612 read_unlock_bh(&idev->lock);
2613}
2614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615#ifdef CONFIG_PROC_FS
2616struct if6_iter_state {
2617 int bucket;
2618};
2619
2620static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2621{
2622 struct inet6_ifaddr *ifa = NULL;
2623 struct if6_iter_state *state = seq->private;
2624
2625 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2626 ifa = inet6_addr_lst[state->bucket];
2627 if (ifa)
2628 break;
2629 }
2630 return ifa;
2631}
2632
2633static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2634{
2635 struct if6_iter_state *state = seq->private;
2636
2637 ifa = ifa->lst_next;
2638try_again:
2639 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2640 ifa = inet6_addr_lst[state->bucket];
2641 goto try_again;
2642 }
2643 return ifa;
2644}
2645
2646static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2647{
2648 struct inet6_ifaddr *ifa = if6_get_first(seq);
2649
2650 if (ifa)
2651 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2652 --pos;
2653 return pos ? NULL : ifa;
2654}
2655
2656static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2657{
2658 read_lock_bh(&addrconf_hash_lock);
2659 return if6_get_idx(seq, *pos);
2660}
2661
2662static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2663{
2664 struct inet6_ifaddr *ifa;
2665
2666 ifa = if6_get_next(seq, v);
2667 ++*pos;
2668 return ifa;
2669}
2670
2671static void if6_seq_stop(struct seq_file *seq, void *v)
2672{
2673 read_unlock_bh(&addrconf_hash_lock);
2674}
2675
2676static int if6_seq_show(struct seq_file *seq, void *v)
2677{
2678 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2679 seq_printf(seq,
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002680 NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 NIP6(ifp->addr),
2682 ifp->idev->dev->ifindex,
2683 ifp->prefix_len,
2684 ifp->scope,
2685 ifp->flags,
2686 ifp->idev->dev->name);
2687 return 0;
2688}
2689
2690static struct seq_operations if6_seq_ops = {
2691 .start = if6_seq_start,
2692 .next = if6_seq_next,
2693 .show = if6_seq_show,
2694 .stop = if6_seq_stop,
2695};
2696
2697static int if6_seq_open(struct inode *inode, struct file *file)
2698{
2699 struct seq_file *seq;
2700 int rc = -ENOMEM;
Ingo Oeser322f74a2006-03-20 23:01:47 -08002701 struct if6_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
2703 if (!s)
2704 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
2706 rc = seq_open(file, &if6_seq_ops);
2707 if (rc)
2708 goto out_kfree;
2709
2710 seq = file->private_data;
2711 seq->private = s;
2712out:
2713 return rc;
2714out_kfree:
2715 kfree(s);
2716 goto out;
2717}
2718
Arjan van de Ven9a321442007-02-12 00:55:35 -08002719static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 .owner = THIS_MODULE,
2721 .open = if6_seq_open,
2722 .read = seq_read,
2723 .llseek = seq_lseek,
2724 .release = seq_release_private,
2725};
2726
2727int __init if6_proc_init(void)
2728{
2729 if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2730 return -ENOMEM;
2731 return 0;
2732}
2733
2734void if6_proc_exit(void)
2735{
2736 proc_net_remove("if_inet6");
2737}
2738#endif /* CONFIG_PROC_FS */
2739
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07002740#ifdef CONFIG_IPV6_MIP6
2741/* Check if address is a home address configured on any interface. */
2742int ipv6_chk_home_addr(struct in6_addr *addr)
2743{
2744 int ret = 0;
2745 struct inet6_ifaddr * ifp;
2746 u8 hash = ipv6_addr_hash(addr);
2747 read_lock_bh(&addrconf_hash_lock);
2748 for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) {
2749 if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
2750 (ifp->flags & IFA_F_HOMEADDRESS)) {
2751 ret = 1;
2752 break;
2753 }
2754 }
2755 read_unlock_bh(&addrconf_hash_lock);
2756 return ret;
2757}
2758#endif
2759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760/*
2761 * Periodic address status verification
2762 */
2763
2764static void addrconf_verify(unsigned long foo)
2765{
2766 struct inet6_ifaddr *ifp;
2767 unsigned long now, next;
2768 int i;
2769
2770 spin_lock_bh(&addrconf_verify_lock);
2771 now = jiffies;
2772 next = now + ADDR_CHECK_FREQUENCY;
2773
2774 del_timer(&addr_chk_timer);
2775
2776 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2777
2778restart:
Yan Zheng5d5780d2005-11-20 13:42:20 -08002779 read_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2781 unsigned long age;
2782#ifdef CONFIG_IPV6_PRIVACY
2783 unsigned long regen_advance;
2784#endif
2785
2786 if (ifp->flags & IFA_F_PERMANENT)
2787 continue;
2788
2789 spin_lock(&ifp->lock);
2790 age = (now - ifp->tstamp) / HZ;
2791
2792#ifdef CONFIG_IPV6_PRIVACY
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002793 regen_advance = ifp->idev->cnf.regen_max_retry *
2794 ifp->idev->cnf.dad_transmits *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 ifp->idev->nd_parms->retrans_time / HZ;
2796#endif
2797
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09002798 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
2799 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 spin_unlock(&ifp->lock);
2801 in6_ifa_hold(ifp);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002802 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 ipv6_del_addr(ifp);
2804 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09002805 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
2806 spin_unlock(&ifp->lock);
2807 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 } else if (age >= ifp->prefered_lft) {
2809 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2810 int deprecate = 0;
2811
2812 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2813 deprecate = 1;
2814 ifp->flags |= IFA_F_DEPRECATED;
2815 }
2816
2817 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2818 next = ifp->tstamp + ifp->valid_lft * HZ;
2819
2820 spin_unlock(&ifp->lock);
2821
2822 if (deprecate) {
2823 in6_ifa_hold(ifp);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002824 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
2826 ipv6_ifa_notify(0, ifp);
2827 in6_ifa_put(ifp);
2828 goto restart;
2829 }
2830#ifdef CONFIG_IPV6_PRIVACY
2831 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2832 !(ifp->flags&IFA_F_TENTATIVE)) {
2833 if (age >= ifp->prefered_lft - regen_advance) {
2834 struct inet6_ifaddr *ifpub = ifp->ifpub;
2835 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2836 next = ifp->tstamp + ifp->prefered_lft * HZ;
2837 if (!ifp->regen_count && ifpub) {
2838 ifp->regen_count++;
2839 in6_ifa_hold(ifp);
2840 in6_ifa_hold(ifpub);
2841 spin_unlock(&ifp->lock);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002842 read_unlock(&addrconf_hash_lock);
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08002843 spin_lock(&ifpub->lock);
2844 ifpub->regen_count = 0;
2845 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 ipv6_create_tempaddr(ifpub, ifp);
2847 in6_ifa_put(ifpub);
2848 in6_ifa_put(ifp);
2849 goto restart;
2850 }
2851 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2852 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2853 spin_unlock(&ifp->lock);
2854#endif
2855 } else {
2856 /* ifp->prefered_lft <= ifp->valid_lft */
2857 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2858 next = ifp->tstamp + ifp->prefered_lft * HZ;
2859 spin_unlock(&ifp->lock);
2860 }
2861 }
Yan Zheng5d5780d2005-11-20 13:42:20 -08002862 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 }
2864
2865 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2866 add_timer(&addr_chk_timer);
2867 spin_unlock_bh(&addrconf_verify_lock);
2868}
2869
Thomas Graf461d8832006-09-18 00:09:49 -07002870static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
2871{
2872 struct in6_addr *pfx = NULL;
2873
2874 if (addr)
2875 pfx = nla_data(addr);
2876
2877 if (local) {
2878 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
2879 pfx = NULL;
2880 else
2881 pfx = nla_data(local);
2882 }
2883
2884 return pfx;
2885}
2886
2887static struct nla_policy ifa_ipv6_policy[IFA_MAX+1] __read_mostly = {
2888 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
2889 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
2890 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
2891};
2892
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893static int
2894inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2895{
Thomas Grafb933f712006-09-18 00:10:19 -07002896 struct ifaddrmsg *ifm;
2897 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 struct in6_addr *pfx;
Thomas Grafb933f712006-09-18 00:10:19 -07002899 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
Thomas Grafb933f712006-09-18 00:10:19 -07002901 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
2902 if (err < 0)
2903 return err;
2904
2905 ifm = nlmsg_data(nlh);
2906 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 if (pfx == NULL)
2908 return -EINVAL;
2909
2910 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2911}
2912
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002913static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
2914 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002915{
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002916 u32 flags = RTF_EXPIRES;
2917
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002918 if (!valid_lft || (prefered_lft > valid_lft))
2919 return -EINVAL;
2920
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002921 if (valid_lft == INFINITY_LIFE_TIME) {
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002922 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002923 flags = 0;
2924 } else if (valid_lft >= 0x7FFFFFFF/HZ)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002925 valid_lft = 0x7FFFFFFF/HZ;
2926
2927 if (prefered_lft == 0)
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002928 ifa_flags |= IFA_F_DEPRECATED;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002929 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
2930 (prefered_lft != INFINITY_LIFE_TIME))
2931 prefered_lft = 0x7FFFFFFF/HZ;
2932
2933 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07002934 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 +09002935 ifp->tstamp = jiffies;
2936 ifp->valid_lft = valid_lft;
2937 ifp->prefered_lft = prefered_lft;
2938
2939 spin_unlock_bh(&ifp->lock);
2940 if (!(ifp->flags&IFA_F_TENTATIVE))
2941 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002942
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002943 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
2944 jiffies_to_clock_t(valid_lft * HZ), flags);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002945 addrconf_verify(0);
2946
2947 return 0;
2948}
2949
2950static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2952{
Thomas Graf461d8832006-09-18 00:09:49 -07002953 struct ifaddrmsg *ifm;
2954 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 struct in6_addr *pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07002956 struct inet6_ifaddr *ifa;
2957 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002958 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
2959 u8 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07002960 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Thomas Graf461d8832006-09-18 00:09:49 -07002962 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
2963 if (err < 0)
2964 return err;
2965
2966 ifm = nlmsg_data(nlh);
2967 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 if (pfx == NULL)
2969 return -EINVAL;
2970
Thomas Graf461d8832006-09-18 00:09:49 -07002971 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002972 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07002973
2974 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002975 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07002976 preferred_lft = ci->ifa_prefered;
2977 } else {
2978 preferred_lft = INFINITY_LIFE_TIME;
2979 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002980 }
2981
Thomas Graf7198f8c2006-09-18 00:13:46 -07002982 dev = __dev_get_by_index(ifm->ifa_index);
2983 if (dev == NULL)
2984 return -ENODEV;
2985
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002986 /* We ignore other flags so far. */
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07002987 ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002988
Thomas Graf7198f8c2006-09-18 00:13:46 -07002989 ifa = ipv6_get_ifaddr(pfx, dev, 1);
2990 if (ifa == NULL) {
2991 /*
2992 * It would be best to check for !NLM_F_CREATE here but
2993 * userspace alreay relies on not having to provide this.
2994 */
2995 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002996 ifa_flags, preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002997 }
2998
Thomas Graf7198f8c2006-09-18 00:13:46 -07002999 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3000 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3001 err = -EEXIST;
3002 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003003 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003004
3005 in6_ifa_put(ifa);
3006
3007 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008}
3009
Thomas Graf101bb222006-09-18 00:11:52 -07003010static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3011 u8 scope, int ifindex)
3012{
3013 struct ifaddrmsg *ifm;
3014
3015 ifm = nlmsg_data(nlh);
3016 ifm->ifa_family = AF_INET6;
3017 ifm->ifa_prefixlen = prefixlen;
3018 ifm->ifa_flags = flags;
3019 ifm->ifa_scope = scope;
3020 ifm->ifa_index = ifindex;
3021}
3022
Thomas Graf85486af2006-09-18 00:11:24 -07003023static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3024 unsigned long tstamp, u32 preferred, u32 valid)
3025{
3026 struct ifa_cacheinfo ci;
3027
3028 ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100
3029 + TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3030 ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100
3031 + TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3032 ci.ifa_prefered = preferred;
3033 ci.ifa_valid = valid;
3034
3035 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3036}
3037
Thomas Graf101bb222006-09-18 00:11:52 -07003038static inline int rt_scope(int ifa_scope)
3039{
3040 if (ifa_scope & IFA_HOST)
3041 return RT_SCOPE_HOST;
3042 else if (ifa_scope & IFA_LINK)
3043 return RT_SCOPE_LINK;
3044 else if (ifa_scope & IFA_SITE)
3045 return RT_SCOPE_SITE;
3046 else
3047 return RT_SCOPE_UNIVERSE;
3048}
3049
Thomas Graf0ab68032006-09-18 00:12:35 -07003050static inline int inet6_ifaddr_msgsize(void)
3051{
Thomas Graf339bf982006-11-10 14:10:15 -08003052 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3053 + nla_total_size(16) /* IFA_ADDRESS */
3054 + nla_total_size(sizeof(struct ifa_cacheinfo));
Thomas Graf0ab68032006-09-18 00:12:35 -07003055}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003056
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003058 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07003061 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Thomas Graf0ab68032006-09-18 00:12:35 -07003063 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3064 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003065 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003066
Thomas Graf101bb222006-09-18 00:11:52 -07003067 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3068 ifa->idev->dev->ifindex);
3069
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 if (!(ifa->flags&IFA_F_PERMANENT)) {
Thomas Graf85486af2006-09-18 00:11:24 -07003071 preferred = ifa->prefered_lft;
3072 valid = ifa->valid_lft;
3073 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 long tval = (jiffies - ifa->tstamp)/HZ;
Thomas Graf85486af2006-09-18 00:11:24 -07003075 preferred -= tval;
3076 if (valid != INFINITY_LIFE_TIME)
3077 valid -= tval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 }
3079 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07003080 preferred = INFINITY_LIFE_TIME;
3081 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 }
Thomas Graf85486af2006-09-18 00:11:24 -07003083
Thomas Graf0ab68032006-09-18 00:12:35 -07003084 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
Patrick McHardy26932562007-01-31 23:16:40 -08003085 put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
3086 nlmsg_cancel(skb, nlh);
3087 return -EMSGSIZE;
3088 }
Thomas Graf85486af2006-09-18 00:11:24 -07003089
Thomas Graf0ab68032006-09-18 00:12:35 -07003090 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091}
3092
3093static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07003094 u32 pid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003097 u8 scope = RT_SCOPE_UNIVERSE;
3098 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Thomas Graf101bb222006-09-18 00:11:52 -07003100 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3101 scope = RT_SCOPE_SITE;
3102
Thomas Graf0ab68032006-09-18 00:12:35 -07003103 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3104 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003105 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003106
Thomas Graf101bb222006-09-18 00:11:52 -07003107 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003108 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3109 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003110 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3111 nlmsg_cancel(skb, nlh);
3112 return -EMSGSIZE;
3113 }
Thomas Graf85486af2006-09-18 00:11:24 -07003114
Thomas Graf0ab68032006-09-18 00:12:35 -07003115 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116}
3117
3118static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003119 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003122 u8 scope = RT_SCOPE_UNIVERSE;
3123 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
Thomas Graf101bb222006-09-18 00:11:52 -07003125 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3126 scope = RT_SCOPE_SITE;
3127
Thomas Graf0ab68032006-09-18 00:12:35 -07003128 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3129 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003130 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003131
Thomas Graf101bb222006-09-18 00:11:52 -07003132 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003133 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3134 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003135 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3136 nlmsg_cancel(skb, nlh);
3137 return -EMSGSIZE;
3138 }
Thomas Graf85486af2006-09-18 00:11:24 -07003139
Thomas Graf0ab68032006-09-18 00:12:35 -07003140 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141}
3142
3143enum addr_type_t
3144{
3145 UNICAST_ADDR,
3146 MULTICAST_ADDR,
3147 ANYCAST_ADDR,
3148};
3149
3150static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3151 enum addr_type_t type)
3152{
3153 int idx, ip_idx;
3154 int s_idx, s_ip_idx;
3155 int err = 1;
3156 struct net_device *dev;
3157 struct inet6_dev *idev = NULL;
3158 struct inet6_ifaddr *ifa;
3159 struct ifmcaddr6 *ifmca;
3160 struct ifacaddr6 *ifaca;
3161
3162 s_idx = cb->args[0];
3163 s_ip_idx = ip_idx = cb->args[1];
3164 read_lock(&dev_base_lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003165
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
3167 if (idx < s_idx)
3168 continue;
3169 if (idx > s_idx)
3170 s_ip_idx = 0;
3171 ip_idx = 0;
3172 if ((idev = in6_dev_get(dev)) == NULL)
3173 continue;
3174 read_lock_bh(&idev->lock);
3175 switch (type) {
3176 case UNICAST_ADDR:
YOSHIFUJI Hideakiae9cda52005-06-28 13:00:30 -07003177 /* unicast address incl. temp addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 for (ifa = idev->addr_list; ifa;
3179 ifa = ifa->if_next, ip_idx++) {
3180 if (ip_idx < s_ip_idx)
3181 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003182 if ((err = inet6_fill_ifaddr(skb, ifa,
3183 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003184 cb->nlh->nlmsg_seq, RTM_NEWADDR,
3185 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 goto done;
3187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 break;
3189 case MULTICAST_ADDR:
3190 /* multicast address */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003191 for (ifmca = idev->mc_list; ifmca;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 ifmca = ifmca->next, ip_idx++) {
3193 if (ip_idx < s_ip_idx)
3194 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003195 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
3196 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003197 cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3198 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 goto done;
3200 }
3201 break;
3202 case ANYCAST_ADDR:
3203 /* anycast address */
3204 for (ifaca = idev->ac_list; ifaca;
3205 ifaca = ifaca->aca_next, ip_idx++) {
3206 if (ip_idx < s_ip_idx)
3207 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003208 if ((err = inet6_fill_ifacaddr(skb, ifaca,
3209 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003210 cb->nlh->nlmsg_seq, RTM_GETANYCAST,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003211 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 goto done;
3213 }
3214 break;
3215 default:
3216 break;
3217 }
3218 read_unlock_bh(&idev->lock);
3219 in6_dev_put(idev);
3220 }
3221done:
3222 if (err <= 0) {
3223 read_unlock_bh(&idev->lock);
3224 in6_dev_put(idev);
3225 }
3226 read_unlock(&dev_base_lock);
3227 cb->args[0] = idx;
3228 cb->args[1] = ip_idx;
3229 return skb->len;
3230}
3231
3232static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3233{
3234 enum addr_type_t type = UNICAST_ADDR;
3235 return inet6_dump_addr(skb, cb, type);
3236}
3237
3238static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3239{
3240 enum addr_type_t type = MULTICAST_ADDR;
3241 return inet6_dump_addr(skb, cb, type);
3242}
3243
3244
3245static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3246{
3247 enum addr_type_t type = ANYCAST_ADDR;
3248 return inet6_dump_addr(skb, cb, type);
3249}
3250
Thomas Graf1b29fc22006-09-18 00:10:50 -07003251static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
3252 void *arg)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003253{
Thomas Graf1b29fc22006-09-18 00:10:50 -07003254 struct ifaddrmsg *ifm;
3255 struct nlattr *tb[IFA_MAX+1];
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003256 struct in6_addr *addr = NULL;
3257 struct net_device *dev = NULL;
3258 struct inet6_ifaddr *ifa;
3259 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003260 int err;
3261
Thomas Graf1b29fc22006-09-18 00:10:50 -07003262 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3263 if (err < 0)
3264 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003265
Thomas Graf1b29fc22006-09-18 00:10:50 -07003266 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3267 if (addr == NULL) {
3268 err = -EINVAL;
3269 goto errout;
3270 }
3271
3272 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003273 if (ifm->ifa_index)
3274 dev = __dev_get_by_index(ifm->ifa_index);
3275
Thomas Graf1b29fc22006-09-18 00:10:50 -07003276 if ((ifa = ipv6_get_ifaddr(addr, dev, 1)) == NULL) {
3277 err = -EADDRNOTAVAIL;
3278 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003279 }
3280
Thomas Graf0ab68032006-09-18 00:12:35 -07003281 if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07003282 err = -ENOBUFS;
3283 goto errout_ifa;
3284 }
3285
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003286 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
3287 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003288 if (err < 0) {
3289 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3290 WARN_ON(err == -EMSGSIZE);
3291 kfree_skb(skb);
3292 goto errout_ifa;
3293 }
Thomas Graf2942e902006-08-15 00:30:25 -07003294 err = rtnl_unicast(skb, NETLINK_CB(in_skb).pid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003295errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003296 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003297errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003298 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003299}
3300
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3302{
3303 struct sk_buff *skb;
Thomas Graf5d620262006-08-15 00:35:02 -07003304 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
Thomas Graf0ab68032006-09-18 00:12:35 -07003306 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Thomas Graf5d620262006-08-15 00:35:02 -07003307 if (skb == NULL)
3308 goto errout;
3309
3310 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003311 if (err < 0) {
3312 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3313 WARN_ON(err == -EMSGSIZE);
3314 kfree_skb(skb);
3315 goto errout;
3316 }
Thomas Graf5d620262006-08-15 00:35:02 -07003317 err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3318errout:
3319 if (err < 0)
3320 rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321}
3322
3323static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
3324 __s32 *array, int bytes)
3325{
Thomas Graf04561c12006-11-14 19:53:58 -08003326 BUG_ON(bytes < (DEVCONF_MAX * 4));
3327
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 memset(array, 0, bytes);
3329 array[DEVCONF_FORWARDING] = cnf->forwarding;
3330 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3331 array[DEVCONF_MTU6] = cnf->mtu6;
3332 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3333 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3334 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3335 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3336 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3337 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3338 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3339 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3340#ifdef CONFIG_IPV6_PRIVACY
3341 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3342 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3343 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3344 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3345 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3346#endif
3347 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003348 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003349 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003350#ifdef CONFIG_IPV6_ROUTER_PREF
3351 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08003352 array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval;
Neil Hormanfa03ef32007-01-30 14:30:10 -08003353#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08003354 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
3355#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003356#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07003357 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358}
3359
Thomas Graf339bf982006-11-10 14:10:15 -08003360static inline size_t inet6_if_nlmsg_size(void)
3361{
3362 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3363 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3364 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3365 + nla_total_size(4) /* IFLA_MTU */
3366 + nla_total_size(4) /* IFLA_LINK */
3367 + nla_total_size( /* IFLA_PROTINFO */
3368 nla_total_size(4) /* IFLA_INET6_FLAGS */
3369 + nla_total_size(sizeof(struct ifla_cacheinfo))
3370 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
3371 );
3372}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003373
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003374static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003375 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376{
Thomas Graf04561c12006-11-14 19:53:58 -08003377 struct net_device *dev = idev->dev;
3378 struct nlattr *conf;
3379 struct ifinfomsg *hdr;
3380 struct nlmsghdr *nlh;
3381 void *protoinfo;
3382 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383
Thomas Graf04561c12006-11-14 19:53:58 -08003384 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
3385 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003386 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387
Thomas Graf04561c12006-11-14 19:53:58 -08003388 hdr = nlmsg_data(nlh);
3389 hdr->ifi_family = AF_INET6;
3390 hdr->__ifi_pad = 0;
3391 hdr->ifi_type = dev->type;
3392 hdr->ifi_index = dev->ifindex;
3393 hdr->ifi_flags = dev_get_flags(dev);
3394 hdr->ifi_change = 0;
3395
3396 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
3398 if (dev->addr_len)
Thomas Graf04561c12006-11-14 19:53:58 -08003399 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
Thomas Graf04561c12006-11-14 19:53:58 -08003401 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 if (dev->ifindex != dev->iflink)
Thomas Graf04561c12006-11-14 19:53:58 -08003403 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Thomas Graf04561c12006-11-14 19:53:58 -08003405 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
3406 if (protoinfo == NULL)
3407 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
Thomas Graf04561c12006-11-14 19:53:58 -08003409 NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 ci.max_reasm_len = IPV6_MAXPLEN;
3412 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3413 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3414 ci.reachable_time = idev->nd_parms->reachable_time;
3415 ci.retrans_time = idev->nd_parms->retrans_time;
Thomas Graf04561c12006-11-14 19:53:58 -08003416 NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3417
3418 conf = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
3419 if (conf == NULL)
3420 goto nla_put_failure;
3421 ipv6_store_devconf(&idev->cnf, nla_data(conf), nla_len(conf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422
3423 /* XXX - Statistics/MC not implemented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
Thomas Graf04561c12006-11-14 19:53:58 -08003425 nla_nest_end(skb, protoinfo);
3426 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
Thomas Graf04561c12006-11-14 19:53:58 -08003428nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08003429 nlmsg_cancel(skb, nlh);
3430 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431}
3432
3433static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3434{
3435 int idx, err;
3436 int s_idx = cb->args[0];
3437 struct net_device *dev;
3438 struct inet6_dev *idev;
3439
3440 read_lock(&dev_base_lock);
3441 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
3442 if (idx < s_idx)
3443 continue;
3444 if ((idev = in6_dev_get(dev)) == NULL)
3445 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003446 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003447 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 in6_dev_put(idev);
3449 if (err <= 0)
3450 break;
3451 }
3452 read_unlock(&dev_base_lock);
3453 cb->args[0] = idx;
3454
3455 return skb->len;
3456}
3457
3458void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3459{
3460 struct sk_buff *skb;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003461 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003462
Thomas Graf339bf982006-11-10 14:10:15 -08003463 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003464 if (skb == NULL)
3465 goto errout;
3466
3467 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003468 if (err < 0) {
3469 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
3470 WARN_ON(err == -EMSGSIZE);
3471 kfree_skb(skb);
3472 goto errout;
3473 }
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003474 err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3475errout:
3476 if (err < 0)
3477 rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478}
3479
Thomas Graf339bf982006-11-10 14:10:15 -08003480static inline size_t inet6_prefix_nlmsg_size(void)
3481{
3482 return NLMSG_ALIGN(sizeof(struct prefixmsg))
3483 + nla_total_size(sizeof(struct in6_addr))
3484 + nla_total_size(sizeof(struct prefix_cacheinfo));
3485}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003486
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Thomas Graf6051e2f2006-11-14 19:54:19 -08003488 struct prefix_info *pinfo, u32 pid, u32 seq,
3489 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490{
Thomas Graf6051e2f2006-11-14 19:54:19 -08003491 struct prefixmsg *pmsg;
3492 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 struct prefix_cacheinfo ci;
3494
Thomas Graf6051e2f2006-11-14 19:54:19 -08003495 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
3496 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003497 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08003498
3499 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07003501 pmsg->prefix_pad1 = 0;
3502 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 pmsg->prefix_ifindex = idev->dev->ifindex;
3504 pmsg->prefix_len = pinfo->prefix_len;
3505 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07003506 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 pmsg->prefix_flags = 0;
3508 if (pinfo->onlink)
3509 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3510 if (pinfo->autoconf)
3511 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3512
Thomas Graf6051e2f2006-11-14 19:54:19 -08003513 NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514
3515 ci.preferred_time = ntohl(pinfo->prefered);
3516 ci.valid_time = ntohl(pinfo->valid);
Thomas Graf6051e2f2006-11-14 19:54:19 -08003517 NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
Thomas Graf6051e2f2006-11-14 19:54:19 -08003519 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520
Thomas Graf6051e2f2006-11-14 19:54:19 -08003521nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08003522 nlmsg_cancel(skb, nlh);
3523 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524}
3525
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003526static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 struct prefix_info *pinfo)
3528{
3529 struct sk_buff *skb;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003530 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
Thomas Graf339bf982006-11-10 14:10:15 -08003532 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003533 if (skb == NULL)
3534 goto errout;
3535
3536 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003537 if (err < 0) {
3538 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
3539 WARN_ON(err == -EMSGSIZE);
3540 kfree_skb(skb);
3541 goto errout;
3542 }
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003543 err = rtnl_notify(skb, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
3544errout:
3545 if (err < 0)
3546 rtnl_set_sk_err(RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547}
3548
Thomas Grafdb46edc2005-05-03 14:29:39 -07003549static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, },
3551 [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, },
3552 [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, },
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003553 [RTM_GETADDR - RTM_BASE] = { .doit = inet6_rtm_getaddr,
3554 .dumpit = inet6_dump_ifaddr, },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
3556 [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
3557 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, },
3558 [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, },
3559 [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute,
3560 .dumpit = inet6_dump_fib, },
David S. Miller8423a9a2006-08-07 21:54:37 -07003561#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Thomas Graf101367c2006-08-04 03:39:02 -07003562 [RTM_GETRULE - RTM_BASE] = { .dumpit = fib6_rules_dump, },
David S. Miller8423a9a2006-08-07 21:54:37 -07003563#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564};
3565
3566static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3567{
3568 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3569
3570 switch (event) {
3571 case RTM_NEWADDR:
Thomas Graf40e22e82006-08-22 00:00:45 -07003572 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 if (ifp->idev->cnf.forwarding)
3574 addrconf_join_anycast(ifp);
3575 break;
3576 case RTM_DELADDR:
3577 if (ifp->idev->cnf.forwarding)
3578 addrconf_leave_anycast(ifp);
3579 addrconf_leave_solict(ifp->idev, &ifp->addr);
3580 dst_hold(&ifp->rt->u.dst);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003581 if (ip6_del_rt(ifp->rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 dst_free(&ifp->rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 break;
3584 }
3585}
3586
3587static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3588{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003589 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 if (likely(ifp->idev->dead == 0))
3591 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003592 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593}
3594
3595#ifdef CONFIG_SYSCTL
3596
3597static
3598int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3599 void __user *buffer, size_t *lenp, loff_t *ppos)
3600{
3601 int *valp = ctl->data;
3602 int val = *valp;
3603 int ret;
3604
3605 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3606
3607 if (write && valp != &ipv6_devconf_dflt.forwarding) {
3608 if (valp != &ipv6_devconf.forwarding) {
3609 if ((!*valp) ^ (!val)) {
3610 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
3611 if (idev == NULL)
3612 return ret;
3613 dev_forward_change(idev);
3614 }
3615 } else {
3616 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3617 addrconf_forward_change();
3618 }
3619 if (*valp)
3620 rt6_purge_dflt_routers();
3621 }
3622
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003623 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624}
3625
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003626static int addrconf_sysctl_forward_strategy(ctl_table *table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 int __user *name, int nlen,
3628 void __user *oldval,
3629 size_t __user *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -08003630 void __user *newval, size_t newlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631{
3632 int *valp = table->data;
3633 int new;
3634
3635 if (!newval || !newlen)
3636 return 0;
3637 if (newlen != sizeof(int))
3638 return -EINVAL;
3639 if (get_user(new, (int __user *)newval))
3640 return -EFAULT;
3641 if (new == *valp)
3642 return 0;
3643 if (oldval && oldlenp) {
3644 size_t len;
3645 if (get_user(len, oldlenp))
3646 return -EFAULT;
3647 if (len) {
3648 if (len > table->maxlen)
3649 len = table->maxlen;
3650 if (copy_to_user(oldval, valp, len))
3651 return -EFAULT;
3652 if (put_user(len, oldlenp))
3653 return -EFAULT;
3654 }
3655 }
3656
3657 if (valp != &ipv6_devconf_dflt.forwarding) {
3658 if (valp != &ipv6_devconf.forwarding) {
3659 struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
3660 int changed;
3661 if (unlikely(idev == NULL))
3662 return -ENODEV;
3663 changed = (!*valp) ^ (!new);
3664 *valp = new;
3665 if (changed)
3666 dev_forward_change(idev);
3667 } else {
3668 *valp = new;
3669 addrconf_forward_change();
3670 }
3671
3672 if (*valp)
3673 rt6_purge_dflt_routers();
3674 } else
3675 *valp = new;
3676
3677 return 1;
3678}
3679
3680static struct addrconf_sysctl_table
3681{
3682 struct ctl_table_header *sysctl_header;
3683 ctl_table addrconf_vars[__NET_IPV6_MAX];
3684 ctl_table addrconf_dev[2];
3685 ctl_table addrconf_conf_dir[2];
3686 ctl_table addrconf_proto_dir[2];
3687 ctl_table addrconf_root_dir[2];
Brian Haleyab32ea52006-09-22 14:15:41 -07003688} addrconf_sysctl __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 .sysctl_header = NULL,
3690 .addrconf_vars = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003691 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 .ctl_name = NET_IPV6_FORWARDING,
3693 .procname = "forwarding",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003694 .data = &ipv6_devconf.forwarding,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 .maxlen = sizeof(int),
3696 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003697 .proc_handler = &addrconf_sysctl_forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 .strategy = &addrconf_sysctl_forward_strategy,
3699 },
3700 {
3701 .ctl_name = NET_IPV6_HOP_LIMIT,
3702 .procname = "hop_limit",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003703 .data = &ipv6_devconf.hop_limit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 .maxlen = sizeof(int),
3705 .mode = 0644,
3706 .proc_handler = proc_dointvec,
3707 },
3708 {
3709 .ctl_name = NET_IPV6_MTU,
3710 .procname = "mtu",
3711 .data = &ipv6_devconf.mtu6,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003712 .maxlen = sizeof(int),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003714 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 },
3716 {
3717 .ctl_name = NET_IPV6_ACCEPT_RA,
3718 .procname = "accept_ra",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003719 .data = &ipv6_devconf.accept_ra,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 .maxlen = sizeof(int),
3721 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003722 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 },
3724 {
3725 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3726 .procname = "accept_redirects",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003727 .data = &ipv6_devconf.accept_redirects,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 .maxlen = sizeof(int),
3729 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003730 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 },
3732 {
3733 .ctl_name = NET_IPV6_AUTOCONF,
3734 .procname = "autoconf",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003735 .data = &ipv6_devconf.autoconf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 .maxlen = sizeof(int),
3737 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003738 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 },
3740 {
3741 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3742 .procname = "dad_transmits",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003743 .data = &ipv6_devconf.dad_transmits,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 .maxlen = sizeof(int),
3745 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003746 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 },
3748 {
3749 .ctl_name = NET_IPV6_RTR_SOLICITS,
3750 .procname = "router_solicitations",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003751 .data = &ipv6_devconf.rtr_solicits,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 .maxlen = sizeof(int),
3753 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003754 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 },
3756 {
3757 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3758 .procname = "router_solicitation_interval",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003759 .data = &ipv6_devconf.rtr_solicit_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 .maxlen = sizeof(int),
3761 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003762 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 .strategy = &sysctl_jiffies,
3764 },
3765 {
3766 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3767 .procname = "router_solicitation_delay",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003768 .data = &ipv6_devconf.rtr_solicit_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 .maxlen = sizeof(int),
3770 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003771 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 .strategy = &sysctl_jiffies,
3773 },
3774 {
3775 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3776 .procname = "force_mld_version",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003777 .data = &ipv6_devconf.force_mld_version,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 .maxlen = sizeof(int),
3779 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003780 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 },
3782#ifdef CONFIG_IPV6_PRIVACY
3783 {
3784 .ctl_name = NET_IPV6_USE_TEMPADDR,
3785 .procname = "use_tempaddr",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003786 .data = &ipv6_devconf.use_tempaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 .maxlen = sizeof(int),
3788 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003789 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 },
3791 {
3792 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3793 .procname = "temp_valid_lft",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003794 .data = &ipv6_devconf.temp_valid_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 .maxlen = sizeof(int),
3796 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003797 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 },
3799 {
3800 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3801 .procname = "temp_prefered_lft",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003802 .data = &ipv6_devconf.temp_prefered_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 .maxlen = sizeof(int),
3804 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003805 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 },
3807 {
3808 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3809 .procname = "regen_max_retry",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003810 .data = &ipv6_devconf.regen_max_retry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 .maxlen = sizeof(int),
3812 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003813 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 },
3815 {
3816 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3817 .procname = "max_desync_factor",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003818 .data = &ipv6_devconf.max_desync_factor,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 .maxlen = sizeof(int),
3820 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003821 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 },
3823#endif
3824 {
3825 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3826 .procname = "max_addresses",
3827 .data = &ipv6_devconf.max_addresses,
3828 .maxlen = sizeof(int),
3829 .mode = 0644,
3830 .proc_handler = &proc_dointvec,
3831 },
3832 {
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003833 .ctl_name = NET_IPV6_ACCEPT_RA_DEFRTR,
3834 .procname = "accept_ra_defrtr",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003835 .data = &ipv6_devconf.accept_ra_defrtr,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003836 .maxlen = sizeof(int),
3837 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003838 .proc_handler = &proc_dointvec,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003839 },
3840 {
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003841 .ctl_name = NET_IPV6_ACCEPT_RA_PINFO,
3842 .procname = "accept_ra_pinfo",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003843 .data = &ipv6_devconf.accept_ra_pinfo,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003844 .maxlen = sizeof(int),
3845 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003846 .proc_handler = &proc_dointvec,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003847 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003848#ifdef CONFIG_IPV6_ROUTER_PREF
3849 {
3850 .ctl_name = NET_IPV6_ACCEPT_RA_RTR_PREF,
3851 .procname = "accept_ra_rtr_pref",
3852 .data = &ipv6_devconf.accept_ra_rtr_pref,
3853 .maxlen = sizeof(int),
3854 .mode = 0644,
3855 .proc_handler = &proc_dointvec,
3856 },
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08003857 {
3858 .ctl_name = NET_IPV6_RTR_PROBE_INTERVAL,
3859 .procname = "router_probe_interval",
3860 .data = &ipv6_devconf.rtr_probe_interval,
3861 .maxlen = sizeof(int),
3862 .mode = 0644,
3863 .proc_handler = &proc_dointvec_jiffies,
3864 .strategy = &sysctl_jiffies,
3865 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08003866#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08003867 {
3868 .ctl_name = NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN,
3869 .procname = "accept_ra_rt_info_max_plen",
3870 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
3871 .maxlen = sizeof(int),
3872 .mode = 0644,
3873 .proc_handler = &proc_dointvec,
3874 },
3875#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003876#endif
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003877 {
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07003878 .ctl_name = NET_IPV6_PROXY_NDP,
3879 .procname = "proxy_ndp",
3880 .data = &ipv6_devconf.proxy_ndp,
3881 .maxlen = sizeof(int),
3882 .mode = 0644,
3883 .proc_handler = &proc_dointvec,
3884 },
3885 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 .ctl_name = 0, /* sentinel */
3887 }
3888 },
3889 .addrconf_dev = {
3890 {
3891 .ctl_name = NET_PROTO_CONF_ALL,
3892 .procname = "all",
3893 .mode = 0555,
3894 .child = addrconf_sysctl.addrconf_vars,
3895 },
3896 {
3897 .ctl_name = 0, /* sentinel */
3898 }
3899 },
3900 .addrconf_conf_dir = {
3901 {
3902 .ctl_name = NET_IPV6_CONF,
3903 .procname = "conf",
3904 .mode = 0555,
3905 .child = addrconf_sysctl.addrconf_dev,
3906 },
3907 {
3908 .ctl_name = 0, /* sentinel */
3909 }
3910 },
3911 .addrconf_proto_dir = {
3912 {
3913 .ctl_name = NET_IPV6,
3914 .procname = "ipv6",
3915 .mode = 0555,
3916 .child = addrconf_sysctl.addrconf_conf_dir,
3917 },
3918 {
3919 .ctl_name = 0, /* sentinel */
3920 }
3921 },
3922 .addrconf_root_dir = {
3923 {
3924 .ctl_name = CTL_NET,
3925 .procname = "net",
3926 .mode = 0555,
3927 .child = addrconf_sysctl.addrconf_proto_dir,
3928 },
3929 {
3930 .ctl_name = 0, /* sentinel */
3931 }
3932 },
3933};
3934
3935static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
3936{
3937 int i;
3938 struct net_device *dev = idev ? idev->dev : NULL;
3939 struct addrconf_sysctl_table *t;
3940 char *dev_name = NULL;
3941
Arnaldo Carvalho de Meloaf879cc2006-11-17 12:14:37 -02003942 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 if (t == NULL)
3944 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 for (i=0; t->addrconf_vars[i].data; i++) {
3946 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
3948 }
3949 if (dev) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003950 dev_name = dev->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 t->addrconf_dev[0].ctl_name = dev->ifindex;
3952 } else {
3953 dev_name = "default";
3954 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
3955 }
3956
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003957 /*
3958 * Make a copy of dev_name, because '.procname' is regarded as const
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 * by sysctl and we wouldn't want anyone to change it under our feet
3960 * (see SIOCSIFNAME).
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003961 */
Paulo Marques543537b2005-06-23 00:09:02 -07003962 dev_name = kstrdup(dev_name, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 if (!dev_name)
3964 goto free;
3965
3966 t->addrconf_dev[0].procname = dev_name;
3967
3968 t->addrconf_dev[0].child = t->addrconf_vars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 t->addrconf_conf_dir[0].child = t->addrconf_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08003973 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 if (t->sysctl_header == NULL)
3975 goto free_procname;
3976 else
3977 p->sysctl = t;
3978 return;
3979
3980 /* error path */
3981 free_procname:
3982 kfree(dev_name);
3983 free:
3984 kfree(t);
3985
3986 return;
3987}
3988
3989static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
3990{
3991 if (p->sysctl) {
3992 struct addrconf_sysctl_table *t = p->sysctl;
3993 p->sysctl = NULL;
3994 unregister_sysctl_table(t->sysctl_header);
3995 kfree(t->addrconf_dev[0].procname);
3996 kfree(t);
3997 }
3998}
3999
4000
4001#endif
4002
4003/*
4004 * Device notifier
4005 */
4006
4007int register_inet6addr_notifier(struct notifier_block *nb)
4008{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004009 return atomic_notifier_chain_register(&inet6addr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010}
4011
4012int unregister_inet6addr_notifier(struct notifier_block *nb)
4013{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004014 return atomic_notifier_chain_unregister(&inet6addr_chain,nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015}
4016
4017/*
4018 * Init / cleanup code
4019 */
4020
4021int __init addrconf_init(void)
4022{
4023 int err = 0;
4024
4025 /* The addrconf netdev notifier requires that loopback_dev
4026 * has it's ipv6 private information allocated and setup
4027 * before it can bring up and give link-local addresses
4028 * to other devices which are up.
4029 *
4030 * Unfortunately, loopback_dev is not necessarily the first
4031 * entry in the global dev_base list of net devices. In fact,
4032 * it is likely to be the very last entry on that list.
4033 * So this causes the notifier registry below to try and
4034 * give link-local addresses to all devices besides loopback_dev
4035 * first, then loopback_dev, which cases all the non-loopback_dev
4036 * devices to fail to get a link-local address.
4037 *
4038 * So, as a temporary fix, allocate the ipv6 structure for
4039 * loopback_dev first by hand.
4040 * Longer term, all of the dependencies ipv6 has upon the loopback
4041 * device and it being up should be removed.
4042 */
4043 rtnl_lock();
4044 if (!ipv6_add_dev(&loopback_dev))
4045 err = -ENOMEM;
4046 rtnl_unlock();
4047 if (err)
4048 return err;
4049
Herbert Xuc62dba92005-09-26 15:10:16 -07004050 ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
4051
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 register_netdevice_notifier(&ipv6_dev_notf);
4053
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 addrconf_verify(0);
4055 rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
4056#ifdef CONFIG_SYSCTL
4057 addrconf_sysctl.sysctl_header =
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08004058 register_sysctl_table(addrconf_sysctl.addrconf_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
4060#endif
4061
4062 return 0;
4063}
4064
4065void __exit addrconf_cleanup(void)
4066{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004067 struct net_device *dev;
4068 struct inet6_dev *idev;
4069 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 int i;
4071
4072 unregister_netdevice_notifier(&ipv6_dev_notf);
4073
4074 rtnetlink_links[PF_INET6] = NULL;
4075#ifdef CONFIG_SYSCTL
4076 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
4077 addrconf_sysctl_unregister(&ipv6_devconf);
4078#endif
4079
4080 rtnl_lock();
4081
4082 /*
4083 * clean dev list.
4084 */
4085
4086 for (dev=dev_base; dev; dev=dev->next) {
4087 if ((idev = __in6_dev_get(dev)) == NULL)
4088 continue;
4089 addrconf_ifdown(dev, 1);
4090 }
4091 addrconf_ifdown(&loopback_dev, 2);
4092
4093 /*
4094 * Check hash table.
4095 */
4096
4097 write_lock_bh(&addrconf_hash_lock);
4098 for (i=0; i < IN6_ADDR_HSIZE; i++) {
4099 for (ifa=inet6_addr_lst[i]; ifa; ) {
4100 struct inet6_ifaddr *bifa;
4101
4102 bifa = ifa;
4103 ifa = ifa->lst_next;
4104 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
4105 /* Do not free it; something is wrong.
4106 Now we can investigate it with debugger.
4107 */
4108 }
4109 }
4110 write_unlock_bh(&addrconf_hash_lock);
4111
4112 del_timer(&addr_chk_timer);
4113
4114 rtnl_unlock();
4115
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116#ifdef CONFIG_PROC_FS
4117 proc_net_remove("if_inet6");
4118#endif
4119}