blob: e16f1bba5dffd3295200cc8bfa66440e69f7541b [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) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800403 if (ifa->flags&IFA_F_TENTATIVE)
404 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (idev->cnf.forwarding)
406 addrconf_join_anycast(ifa);
407 else
408 addrconf_leave_anycast(ifa);
409 }
410}
411
412
413static void addrconf_forward_change(void)
414{
415 struct net_device *dev;
416 struct inet6_dev *idev;
417
418 read_lock(&dev_base_lock);
419 for (dev=dev_base; dev; dev=dev->next) {
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700420 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 idev = __in6_dev_get(dev);
422 if (idev) {
423 int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
424 idev->cnf.forwarding = ipv6_devconf.forwarding;
425 if (changed)
426 dev_forward_change(idev);
427 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700428 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430 read_unlock(&dev_base_lock);
431}
432#endif
433
434/* Nobody refers to this ifaddr, destroy it */
435
436void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
437{
438 BUG_TRAP(ifp->if_next==NULL);
439 BUG_TRAP(ifp->lst_next==NULL);
440#ifdef NET_REFCNT_DEBUG
441 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
442#endif
443
444 in6_dev_put(ifp->idev);
445
446 if (del_timer(&ifp->timer))
447 printk("Timer is still running, when freeing ifa=%p\n", ifp);
448
449 if (!ifp->dead) {
450 printk("Freeing alive inet6 address %p\n", ifp);
451 return;
452 }
453 dst_release(&ifp->rt->u.dst);
454
455 kfree(ifp);
456}
457
Brian Haleye55ffac2006-07-10 15:25:51 -0700458static void
459ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
460{
461 struct inet6_ifaddr *ifa, **ifap;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700462 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700463
464 /*
465 * Each device address list is sorted in order of scope -
466 * global before linklocal.
467 */
468 for (ifap = &idev->addr_list; (ifa = *ifap) != NULL;
469 ifap = &ifa->if_next) {
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700470 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700471 break;
472 }
473
474 ifp->if_next = *ifap;
475 *ifap = ifp;
476}
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478/* On success it returns ifp with increased reference count */
479
480static struct inet6_ifaddr *
481ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700482 int scope, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 struct inet6_ifaddr *ifa = NULL;
485 struct rt6_info *rt;
486 int hash;
487 int err = 0;
488
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700489 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (idev->dead) {
491 err = -ENODEV; /*XXX*/
492 goto out2;
493 }
494
495 write_lock(&addrconf_hash_lock);
496
497 /* Ignore adding duplicate addresses on an interface */
498 if (ipv6_chk_same_addr(addr, idev->dev)) {
499 ADBG(("ipv6_add_addr: already assigned\n"));
500 err = -EEXIST;
501 goto out;
502 }
503
Ingo Oeser322f74a2006-03-20 23:01:47 -0800504 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 if (ifa == NULL) {
507 ADBG(("ipv6_add_addr: malloc failed\n"));
508 err = -ENOBUFS;
509 goto out;
510 }
511
512 rt = addrconf_dst_alloc(idev, addr, 0);
513 if (IS_ERR(rt)) {
514 err = PTR_ERR(rt);
515 goto out;
516 }
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 ipv6_addr_copy(&ifa->addr, addr);
519
520 spin_lock_init(&ifa->lock);
521 init_timer(&ifa->timer);
522 ifa->timer.data = (unsigned long) ifa;
523 ifa->scope = scope;
524 ifa->prefix_len = pfxlen;
525 ifa->flags = flags | IFA_F_TENTATIVE;
526 ifa->cstamp = ifa->tstamp = jiffies;
527
Keir Fraser57f5f542006-08-29 02:43:49 -0700528 ifa->rt = rt;
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 ifa->idev = idev;
531 in6_dev_hold(idev);
532 /* For caller */
533 in6_ifa_hold(ifa);
534
535 /* Add to big hash table */
536 hash = ipv6_addr_hash(addr);
537
538 ifa->lst_next = inet6_addr_lst[hash];
539 inet6_addr_lst[hash] = ifa;
540 in6_ifa_hold(ifa);
541 write_unlock(&addrconf_hash_lock);
542
543 write_lock(&idev->lock);
544 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -0700545 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547#ifdef CONFIG_IPV6_PRIVACY
548 if (ifa->flags&IFA_F_TEMPORARY) {
549 ifa->tmp_next = idev->tempaddr_list;
550 idev->tempaddr_list = ifa;
551 in6_ifa_hold(ifa);
552 }
553#endif
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 in6_ifa_hold(ifa);
556 write_unlock(&idev->lock);
557out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700558 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -0700560 if (likely(err == 0))
Alan Sterne041c682006-03-27 01:16:30 -0800561 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 else {
563 kfree(ifa);
564 ifa = ERR_PTR(err);
565 }
566
567 return ifa;
568out:
569 write_unlock(&addrconf_hash_lock);
570 goto out2;
571}
572
573/* This function wants to get referenced ifp and releases it before return */
574
575static void ipv6_del_addr(struct inet6_ifaddr *ifp)
576{
577 struct inet6_ifaddr *ifa, **ifap;
578 struct inet6_dev *idev = ifp->idev;
579 int hash;
580 int deleted = 0, onlink = 0;
581 unsigned long expires = jiffies;
582
583 hash = ipv6_addr_hash(&ifp->addr);
584
585 ifp->dead = 1;
586
587 write_lock_bh(&addrconf_hash_lock);
588 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
589 ifap = &ifa->lst_next) {
590 if (ifa == ifp) {
591 *ifap = ifa->lst_next;
592 __in6_ifa_put(ifp);
593 ifa->lst_next = NULL;
594 break;
595 }
596 }
597 write_unlock_bh(&addrconf_hash_lock);
598
599 write_lock_bh(&idev->lock);
600#ifdef CONFIG_IPV6_PRIVACY
601 if (ifp->flags&IFA_F_TEMPORARY) {
602 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
603 ifap = &ifa->tmp_next) {
604 if (ifa == ifp) {
605 *ifap = ifa->tmp_next;
606 if (ifp->ifpub) {
607 in6_ifa_put(ifp->ifpub);
608 ifp->ifpub = NULL;
609 }
610 __in6_ifa_put(ifp);
611 ifa->tmp_next = NULL;
612 break;
613 }
614 }
615 }
616#endif
617
Kristian Slavov1d142802005-12-21 18:47:24 -0800618 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (ifa == ifp) {
620 *ifap = ifa->if_next;
621 __in6_ifa_put(ifp);
622 ifa->if_next = NULL;
623 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
624 break;
625 deleted = 1;
Kristian Slavov1d142802005-12-21 18:47:24 -0800626 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 } else if (ifp->flags & IFA_F_PERMANENT) {
628 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
629 ifp->prefix_len)) {
630 if (ifa->flags & IFA_F_PERMANENT) {
631 onlink = 1;
632 if (deleted)
633 break;
634 } else {
635 unsigned long lifetime;
636
637 if (!onlink)
638 onlink = -1;
639
640 spin_lock(&ifa->lock);
641 lifetime = min_t(unsigned long,
642 ifa->valid_lft, 0x7fffffffUL/HZ);
643 if (time_before(expires,
644 ifa->tstamp + lifetime * HZ))
645 expires = ifa->tstamp + lifetime * HZ;
646 spin_unlock(&ifa->lock);
647 }
648 }
649 }
Kristian Slavov1d142802005-12-21 18:47:24 -0800650 ifap = &ifa->if_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652 write_unlock_bh(&idev->lock);
653
654 ipv6_ifa_notify(RTM_DELADDR, ifp);
655
Alan Sterne041c682006-03-27 01:16:30 -0800656 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 addrconf_del_timer(ifp);
659
660 /*
661 * Purge or update corresponding prefix
662 *
663 * 1) we don't purge prefix here if address was not permanent.
664 * prefix is managed by its own lifetime.
665 * 2) if there're no addresses, delete prefix.
666 * 3) if there're still other permanent address(es),
667 * corresponding prefix is still permanent.
668 * 4) otherwise, update prefix lifetime to the
669 * longest valid lifetime among the corresponding
670 * addresses on the device.
671 * Note: subsequent RA will update lifetime.
672 *
673 * --yoshfuji
674 */
675 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
676 struct in6_addr prefix;
677 struct rt6_info *rt;
678
679 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
680 rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
681
682 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
683 if (onlink == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700684 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 rt = NULL;
686 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
687 rt->rt6i_expires = expires;
688 rt->rt6i_flags |= RTF_EXPIRES;
689 }
690 }
691 dst_release(&rt->u.dst);
692 }
693
694 in6_ifa_put(ifp);
695}
696
697#ifdef CONFIG_IPV6_PRIVACY
698static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
699{
700 struct inet6_dev *idev = ifp->idev;
701 struct in6_addr addr, *tmpaddr;
702 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
703 int tmp_plen;
704 int ret = 0;
705 int max_addresses;
706
707 write_lock(&idev->lock);
708 if (ift) {
709 spin_lock_bh(&ift->lock);
710 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
711 spin_unlock_bh(&ift->lock);
712 tmpaddr = &addr;
713 } else {
714 tmpaddr = NULL;
715 }
716retry:
717 in6_dev_hold(idev);
718 if (idev->cnf.use_tempaddr <= 0) {
719 write_unlock(&idev->lock);
720 printk(KERN_INFO
721 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
722 in6_dev_put(idev);
723 ret = -1;
724 goto out;
725 }
726 spin_lock_bh(&ifp->lock);
727 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
728 idev->cnf.use_tempaddr = -1; /*XXX*/
729 spin_unlock_bh(&ifp->lock);
730 write_unlock(&idev->lock);
731 printk(KERN_WARNING
732 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
733 in6_dev_put(idev);
734 ret = -1;
735 goto out;
736 }
737 in6_ifa_hold(ifp);
738 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
739 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
740 spin_unlock_bh(&ifp->lock);
741 write_unlock(&idev->lock);
742 printk(KERN_WARNING
743 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
744 in6_ifa_put(ifp);
745 in6_dev_put(idev);
746 ret = -1;
747 goto out;
748 }
749 memcpy(&addr.s6_addr[8], idev->rndid, 8);
750 tmp_valid_lft = min_t(__u32,
751 ifp->valid_lft,
752 idev->cnf.temp_valid_lft);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900753 tmp_prefered_lft = min_t(__u32,
754 ifp->prefered_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 idev->cnf.temp_prefered_lft - desync_factor / HZ);
756 tmp_plen = ifp->prefix_len;
757 max_addresses = idev->cnf.max_addresses;
758 tmp_cstamp = ifp->cstamp;
759 tmp_tstamp = ifp->tstamp;
760 spin_unlock_bh(&ifp->lock);
761
762 write_unlock(&idev->lock);
763 ift = !max_addresses ||
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900764 ipv6_count_addresses(idev) < max_addresses ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 ipv6_add_addr(idev, &addr, tmp_plen,
766 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : NULL;
767 if (!ift || IS_ERR(ift)) {
768 in6_ifa_put(ifp);
769 in6_dev_put(idev);
770 printk(KERN_INFO
771 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
772 tmpaddr = &addr;
773 write_lock(&idev->lock);
774 goto retry;
775 }
776
777 spin_lock_bh(&ift->lock);
778 ift->ifpub = ifp;
779 ift->valid_lft = tmp_valid_lft;
780 ift->prefered_lft = tmp_prefered_lft;
781 ift->cstamp = tmp_cstamp;
782 ift->tstamp = tmp_tstamp;
783 spin_unlock_bh(&ift->lock);
784
785 addrconf_dad_start(ift, 0);
786 in6_ifa_put(ift);
787 in6_dev_put(idev);
788out:
789 return ret;
790}
791#endif
792
793/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800794 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 */
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800796struct ipv6_saddr_score {
797 int addr_type;
798 unsigned int attrs;
799 int matchlen;
Brian Haley0d27b422006-03-11 18:50:14 -0800800 int scope;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800801 unsigned int rule;
802};
803
804#define IPV6_SADDR_SCORE_LOCAL 0x0001
805#define IPV6_SADDR_SCORE_PREFERRED 0x0004
806#define IPV6_SADDR_SCORE_HOA 0x0008
807#define IPV6_SADDR_SCORE_OIF 0x0010
808#define IPV6_SADDR_SCORE_LABEL 0x0020
809#define IPV6_SADDR_SCORE_PRIVACY 0x0040
810
811static int inline ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800813 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
814 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
815 return 1;
816 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800819/* static matching label */
820static int inline ipv6_saddr_label(const struct in6_addr *addr, int type)
821{
822 /*
823 * prefix (longest match) label
824 * -----------------------------
825 * ::1/128 0
826 * ::/0 1
827 * 2002::/16 2
828 * ::/96 3
829 * ::ffff:0:0/96 4
Łukasz Stelmach102128e2006-06-22 01:37:19 -0700830 * fc00::/7 5
831 * 2001::/32 6
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800832 */
833 if (type & IPV6_ADDR_LOOPBACK)
834 return 0;
835 else if (type & IPV6_ADDR_COMPATv4)
836 return 3;
837 else if (type & IPV6_ADDR_MAPPED)
838 return 4;
Łukasz Stelmach102128e2006-06-22 01:37:19 -0700839 else if (addr->s6_addr32[0] == htonl(0x20010000))
840 return 6;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800841 else if (addr->s6_addr16[0] == htons(0x2002))
842 return 2;
Łukasz Stelmach102128e2006-06-22 01:37:19 -0700843 else if ((addr->s6_addr[0] & 0xfe) == 0xfc)
844 return 5;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800845 return 1;
846}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800848int ipv6_dev_get_saddr(struct net_device *daddr_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 struct in6_addr *daddr, struct in6_addr *saddr)
850{
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800851 struct ipv6_saddr_score hiscore;
852 struct inet6_ifaddr *ifa_result = NULL;
853 int daddr_type = __ipv6_addr_type(daddr);
854 int daddr_scope = __ipv6_addr_src_scope(daddr_type);
855 u32 daddr_label = ipv6_saddr_label(daddr, daddr_type);
856 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800858 memset(&hiscore, 0, sizeof(hiscore));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 read_lock(&dev_base_lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700861 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 for (dev = dev_base; dev; dev=dev->next) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800864 struct inet6_dev *idev;
865 struct inet6_ifaddr *ifa;
866
867 /* Rule 0: Candidate Source Address (section 4)
868 * - multicast and link-local destination address,
869 * the set of candidate source address MUST only
870 * include addresses assigned to interfaces
871 * belonging to the same link as the outgoing
872 * interface.
873 * (- For site-local destination addresses, the
874 * set of candidate source addresses MUST only
875 * include addresses assigned to interfaces
876 * belonging to the same site as the outgoing
877 * interface.)
878 */
879 if ((daddr_type & IPV6_ADDR_MULTICAST ||
880 daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
881 daddr_dev && dev != daddr_dev)
882 continue;
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 idev = __in6_dev_get(dev);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800885 if (!idev)
886 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800888 read_lock_bh(&idev->lock);
889 for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
890 struct ipv6_saddr_score score;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800892 score.addr_type = __ipv6_addr_type(&ifa->addr);
893
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +0900894 /* Rule 0:
895 * - Tentative Address (RFC2462 section 5.4)
896 * - A tentative address is not considered
897 * "assigned to an interface" in the traditional
898 * sense.
899 * - Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800900 * - In any case, anycast addresses, multicast
901 * addresses, and the unspecified address MUST
902 * NOT be included in a candidate set.
903 */
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +0900904 if (ifa->flags & IFA_F_TENTATIVE)
905 continue;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800906 if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
907 score.addr_type & IPV6_ADDR_MULTICAST)) {
908 LIMIT_NETDEBUG(KERN_DEBUG
909 "ADDRCONF: unspecified / multicast address"
910 "assigned as unicast address on %s",
911 dev->name);
912 continue;
913 }
914
915 score.attrs = 0;
916 score.matchlen = 0;
917 score.scope = 0;
918 score.rule = 0;
919
920 if (ifa_result == NULL) {
921 /* record it if the first available entry */
922 goto record_it;
923 }
924
925 /* Rule 1: Prefer same address */
926 if (hiscore.rule < 1) {
927 if (ipv6_addr_equal(&ifa_result->addr, daddr))
928 hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
929 hiscore.rule++;
930 }
931 if (ipv6_addr_equal(&ifa->addr, daddr)) {
932 score.attrs |= IPV6_SADDR_SCORE_LOCAL;
933 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
934 score.rule = 1;
935 goto record_it;
936 }
937 } else {
938 if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
939 continue;
940 }
941
942 /* Rule 2: Prefer appropriate scope */
943 if (hiscore.rule < 2) {
944 hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
945 hiscore.rule++;
946 }
947 score.scope = __ipv6_addr_src_scope(score.addr_type);
948 if (hiscore.scope < score.scope) {
949 if (hiscore.scope < daddr_scope) {
950 score.rule = 2;
951 goto record_it;
952 } else
953 continue;
954 } else if (score.scope < hiscore.scope) {
955 if (score.scope < daddr_scope)
Brian Haleye55ffac2006-07-10 15:25:51 -0700956 break; /* addresses sorted by scope */
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800957 else {
958 score.rule = 2;
959 goto record_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800963 /* Rule 3: Avoid deprecated address */
964 if (hiscore.rule < 3) {
965 if (ipv6_saddr_preferred(hiscore.addr_type) ||
966 !(ifa_result->flags & IFA_F_DEPRECATED))
967 hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
968 hiscore.rule++;
969 }
970 if (ipv6_saddr_preferred(score.addr_type) ||
971 !(ifa->flags & IFA_F_DEPRECATED)) {
972 score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
973 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
974 score.rule = 3;
975 goto record_it;
976 }
977 } else {
978 if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
979 continue;
980 }
981
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -0700982 /* Rule 4: Prefer home address */
983#ifdef CONFIG_IPV6_MIP6
984 if (hiscore.rule < 4) {
985 if (ifa_result->flags & IFA_F_HOMEADDRESS)
986 hiscore.attrs |= IPV6_SADDR_SCORE_HOA;
987 hiscore.rule++;
988 }
989 if (ifa->flags & IFA_F_HOMEADDRESS) {
990 score.attrs |= IPV6_SADDR_SCORE_HOA;
991 if (!(ifa_result->flags & IFA_F_HOMEADDRESS)) {
992 score.rule = 4;
993 goto record_it;
994 }
995 } else {
996 if (hiscore.attrs & IPV6_SADDR_SCORE_HOA)
997 continue;
998 }
999#else
YOSHIFUJI Hideaki220bbd72005-11-28 22:27:11 -08001000 if (hiscore.rule < 4)
1001 hiscore.rule++;
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07001002#endif
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001003
1004 /* Rule 5: Prefer outgoing interface */
1005 if (hiscore.rule < 5) {
1006 if (daddr_dev == NULL ||
1007 daddr_dev == ifa_result->idev->dev)
1008 hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1009 hiscore.rule++;
1010 }
1011 if (daddr_dev == NULL ||
1012 daddr_dev == ifa->idev->dev) {
1013 score.attrs |= IPV6_SADDR_SCORE_OIF;
1014 if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1015 score.rule = 5;
1016 goto record_it;
1017 }
1018 } else {
1019 if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1020 continue;
1021 }
1022
1023 /* Rule 6: Prefer matching label */
1024 if (hiscore.rule < 6) {
1025 if (ipv6_saddr_label(&ifa_result->addr, hiscore.addr_type) == daddr_label)
1026 hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1027 hiscore.rule++;
1028 }
1029 if (ipv6_saddr_label(&ifa->addr, score.addr_type) == daddr_label) {
1030 score.attrs |= IPV6_SADDR_SCORE_LABEL;
1031 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1032 score.rule = 6;
1033 goto record_it;
1034 }
1035 } else {
1036 if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1037 continue;
1038 }
1039
Peter Chubb44fd0262005-11-09 13:05:47 -08001040#ifdef CONFIG_IPV6_PRIVACY
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001041 /* Rule 7: Prefer public address
1042 * Note: prefer temprary address if use_tempaddr >= 2
1043 */
1044 if (hiscore.rule < 7) {
1045 if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1046 (ifa_result->idev->cnf.use_tempaddr >= 2))
1047 hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1048 hiscore.rule++;
1049 }
1050 if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1051 (ifa->idev->cnf.use_tempaddr >= 2)) {
1052 score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1053 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1054 score.rule = 7;
1055 goto record_it;
1056 }
1057 } else {
1058 if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1059 continue;
1060 }
YOSHIFUJI Hideaki5e2707f2006-06-22 01:41:18 -07001061#else
1062 if (hiscore.rule < 7)
1063 hiscore.rule++;
Peter Chubb44fd0262005-11-09 13:05:47 -08001064#endif
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001065 /* Rule 8: Use longest matching prefix */
Yan Zheng12da2a42005-11-14 21:42:46 -08001066 if (hiscore.rule < 8) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001067 hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
Yan Zheng12da2a42005-11-14 21:42:46 -08001068 hiscore.rule++;
1069 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001070 score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1071 if (score.matchlen > hiscore.matchlen) {
1072 score.rule = 8;
1073 goto record_it;
1074 }
1075#if 0
1076 else if (score.matchlen < hiscore.matchlen)
1077 continue;
1078#endif
1079
1080 /* Final Rule: choose first available one */
1081 continue;
1082record_it:
1083 if (ifa_result)
1084 in6_ifa_put(ifa_result);
1085 in6_ifa_hold(ifa);
1086 ifa_result = ifa;
1087 hiscore = score;
1088 }
1089 read_unlock_bh(&idev->lock);
1090 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001091 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 read_unlock(&dev_base_lock);
1093
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001094 if (!ifa_result)
1095 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001096
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001097 ipv6_addr_copy(saddr, &ifa_result->addr);
1098 in6_ifa_put(ifa_result);
1099 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
1102
1103int ipv6_get_saddr(struct dst_entry *dst,
1104 struct in6_addr *daddr, struct in6_addr *saddr)
1105{
YOSHIFUJI Hideaki7a3025b2006-10-13 16:17:25 +09001106 return ipv6_dev_get_saddr(dst ? ip6_dst_idev(dst)->dev : NULL, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
1108
1109
1110int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
1111{
1112 struct inet6_dev *idev;
1113 int err = -EADDRNOTAVAIL;
1114
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001115 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if ((idev = __in6_dev_get(dev)) != NULL) {
1117 struct inet6_ifaddr *ifp;
1118
1119 read_lock_bh(&idev->lock);
1120 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1121 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1122 ipv6_addr_copy(addr, &ifp->addr);
1123 err = 0;
1124 break;
1125 }
1126 }
1127 read_unlock_bh(&idev->lock);
1128 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001129 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return err;
1131}
1132
1133static int ipv6_count_addresses(struct inet6_dev *idev)
1134{
1135 int cnt = 0;
1136 struct inet6_ifaddr *ifp;
1137
1138 read_lock_bh(&idev->lock);
1139 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1140 cnt++;
1141 read_unlock_bh(&idev->lock);
1142 return cnt;
1143}
1144
1145int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
1146{
1147 struct inet6_ifaddr * ifp;
1148 u8 hash = ipv6_addr_hash(addr);
1149
1150 read_lock_bh(&addrconf_hash_lock);
1151 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1152 if (ipv6_addr_equal(&ifp->addr, addr) &&
1153 !(ifp->flags&IFA_F_TENTATIVE)) {
1154 if (dev == NULL || ifp->idev->dev == dev ||
1155 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1156 break;
1157 }
1158 }
1159 read_unlock_bh(&addrconf_hash_lock);
1160 return ifp != NULL;
1161}
1162
1163static
1164int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
1165{
1166 struct inet6_ifaddr * ifp;
1167 u8 hash = ipv6_addr_hash(addr);
1168
1169 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1170 if (ipv6_addr_equal(&ifp->addr, addr)) {
1171 if (dev == NULL || ifp->idev->dev == dev)
1172 break;
1173 }
1174 }
1175 return ifp != NULL;
1176}
1177
1178struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
1179{
1180 struct inet6_ifaddr * ifp;
1181 u8 hash = ipv6_addr_hash(addr);
1182
1183 read_lock_bh(&addrconf_hash_lock);
1184 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1185 if (ipv6_addr_equal(&ifp->addr, addr)) {
1186 if (dev == NULL || ifp->idev->dev == dev ||
1187 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1188 in6_ifa_hold(ifp);
1189 break;
1190 }
1191 }
1192 }
1193 read_unlock_bh(&addrconf_hash_lock);
1194
1195 return ifp;
1196}
1197
1198int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1199{
1200 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
Arnaldo Carvalho de Melo0fa1a532005-12-13 23:23:09 -08001201 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
Al Viro82103232006-09-27 18:44:10 -07001202 __be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1203 __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 int sk_ipv6only = ipv6_only_sock(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001205 int sk2_ipv6only = inet_v6_ipv6only(sk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1207 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1208
1209 if (!sk2_rcv_saddr && !sk_ipv6only)
1210 return 1;
1211
1212 if (addr_type2 == IPV6_ADDR_ANY &&
1213 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1214 return 1;
1215
1216 if (addr_type == IPV6_ADDR_ANY &&
1217 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1218 return 1;
1219
1220 if (sk2_rcv_saddr6 &&
1221 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1222 return 1;
1223
1224 if (addr_type == IPV6_ADDR_MAPPED &&
1225 !sk2_ipv6only &&
1226 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1227 return 1;
1228
1229 return 0;
1230}
1231
1232/* Gets referenced address, destroys ifaddr */
1233
Adrian Bunk9f5336e2006-01-07 13:24:25 -08001234static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (ifp->flags&IFA_F_PERMANENT) {
1237 spin_lock_bh(&ifp->lock);
1238 addrconf_del_timer(ifp);
1239 ifp->flags |= IFA_F_TENTATIVE;
1240 spin_unlock_bh(&ifp->lock);
1241 in6_ifa_put(ifp);
1242#ifdef CONFIG_IPV6_PRIVACY
1243 } else if (ifp->flags&IFA_F_TEMPORARY) {
1244 struct inet6_ifaddr *ifpub;
1245 spin_lock_bh(&ifp->lock);
1246 ifpub = ifp->ifpub;
1247 if (ifpub) {
1248 in6_ifa_hold(ifpub);
1249 spin_unlock_bh(&ifp->lock);
1250 ipv6_create_tempaddr(ifpub, ifp);
1251 in6_ifa_put(ifpub);
1252 } else {
1253 spin_unlock_bh(&ifp->lock);
1254 }
1255 ipv6_del_addr(ifp);
1256#endif
1257 } else
1258 ipv6_del_addr(ifp);
1259}
1260
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001261void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1262{
1263 if (net_ratelimit())
1264 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1265 addrconf_dad_stop(ifp);
1266}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268/* Join to solicited addr multicast group. */
1269
1270void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1271{
1272 struct in6_addr maddr;
1273
1274 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1275 return;
1276
1277 addrconf_addr_solict_mult(addr, &maddr);
1278 ipv6_dev_mc_inc(dev, &maddr);
1279}
1280
1281void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1282{
1283 struct in6_addr maddr;
1284
1285 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1286 return;
1287
1288 addrconf_addr_solict_mult(addr, &maddr);
1289 __ipv6_dev_mc_dec(idev, &maddr);
1290}
1291
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001292static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
1294 struct in6_addr addr;
1295 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1296 if (ipv6_addr_any(&addr))
1297 return;
1298 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1299}
1300
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001301static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
1303 struct in6_addr addr;
1304 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1305 if (ipv6_addr_any(&addr))
1306 return;
1307 __ipv6_dev_ac_dec(ifp->idev, &addr);
1308}
1309
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001310static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1311{
1312 if (dev->addr_len != ETH_ALEN)
1313 return -1;
1314 memcpy(eui, dev->dev_addr, 3);
1315 memcpy(eui + 5, dev->dev_addr + 3, 3);
1316
1317 /*
1318 * The zSeries OSA network cards can be shared among various
1319 * OS instances, but the OSA cards have only one MAC address.
1320 * This leads to duplicate address conflicts in conjunction
1321 * with IPv6 if more than one instance uses the same card.
1322 *
1323 * The driver for these cards can deliver a unique 16-bit
1324 * identifier for each instance sharing the same card. It is
1325 * placed instead of 0xFFFE in the interface identifier. The
1326 * "u" bit of the interface identifier is not inverted in this
1327 * case. Hence the resulting interface identifier has local
1328 * scope according to RFC2373.
1329 */
1330 if (dev->dev_id) {
1331 eui[3] = (dev->dev_id >> 8) & 0xFF;
1332 eui[4] = dev->dev_id & 0xFF;
1333 } else {
1334 eui[3] = 0xFF;
1335 eui[4] = 0xFE;
1336 eui[0] ^= 2;
1337 }
1338 return 0;
1339}
1340
1341static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1342{
1343 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1344 if (dev->addr_len != ARCNET_ALEN)
1345 return -1;
1346 memset(eui, 0, 7);
1347 eui[7] = *(u8*)dev->dev_addr;
1348 return 0;
1349}
1350
1351static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1352{
1353 if (dev->addr_len != INFINIBAND_ALEN)
1354 return -1;
1355 memcpy(eui, dev->dev_addr + 12, 8);
1356 eui[0] |= 2;
1357 return 0;
1358}
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1361{
1362 switch (dev->type) {
1363 case ARPHRD_ETHER:
1364 case ARPHRD_FDDI:
1365 case ARPHRD_IEEE802_TR:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001366 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001368 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001370 return addrconf_ifid_infiniband(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
1372 return -1;
1373}
1374
1375static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1376{
1377 int err = -1;
1378 struct inet6_ifaddr *ifp;
1379
1380 read_lock_bh(&idev->lock);
1381 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1382 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1383 memcpy(eui, ifp->addr.s6_addr+8, 8);
1384 err = 0;
1385 break;
1386 }
1387 }
1388 read_unlock_bh(&idev->lock);
1389 return err;
1390}
1391
1392#ifdef CONFIG_IPV6_PRIVACY
1393/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1394static int __ipv6_regen_rndid(struct inet6_dev *idev)
1395{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08001397 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 /*
1401 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1402 * check if generated address is not inappropriate
1403 *
1404 * - Reserved subnet anycast (RFC 2526)
1405 * 11111101 11....11 1xxxxxxx
1406 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1407 * 00-00-5E-FE-xx-xx-xx-xx
1408 * - value 0
1409 * - XXX: already assigned to an address on the device
1410 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001411 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1413 (idev->rndid[7]&0x80))
1414 goto regen;
1415 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1416 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1417 goto regen;
1418 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1419 goto regen;
1420 }
1421
1422 return 0;
1423}
1424
1425static void ipv6_regen_rndid(unsigned long data)
1426{
1427 struct inet6_dev *idev = (struct inet6_dev *) data;
1428 unsigned long expires;
1429
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001430 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 write_lock_bh(&idev->lock);
1432
1433 if (idev->dead)
1434 goto out;
1435
1436 if (__ipv6_regen_rndid(idev) < 0)
1437 goto out;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 expires = jiffies +
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001440 idev->cnf.temp_prefered_lft * HZ -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1442 if (time_before(expires, jiffies)) {
1443 printk(KERN_WARNING
1444 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1445 idev->dev->name);
1446 goto out;
1447 }
1448
1449 if (!mod_timer(&idev->regen_timer, expires))
1450 in6_dev_hold(idev);
1451
1452out:
1453 write_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001454 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 in6_dev_put(idev);
1456}
1457
1458static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1459 int ret = 0;
1460
1461 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1462 ret = __ipv6_regen_rndid(idev);
1463 return ret;
1464}
1465#endif
1466
1467/*
1468 * Add prefix route.
1469 */
1470
1471static void
1472addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001473 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Thomas Graf86872cb2006-08-22 00:01:08 -07001475 struct fib6_config cfg = {
1476 .fc_table = RT6_TABLE_PREFIX,
1477 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1478 .fc_ifindex = dev->ifindex,
1479 .fc_expires = expires,
1480 .fc_dst_len = plen,
1481 .fc_flags = RTF_UP | flags,
1482 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Thomas Graf86872cb2006-08-22 00:01:08 -07001484 ipv6_addr_copy(&cfg.fc_dst, pfx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 /* Prevent useless cloning on PtP SIT.
1487 This thing is done here expecting that the whole
1488 class of non-broadcast devices need not cloning.
1489 */
Joerg Roedel0be669b2006-10-10 14:49:53 -07001490#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Thomas Graf86872cb2006-08-22 00:01:08 -07001491 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1492 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07001493#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Thomas Graf86872cb2006-08-22 00:01:08 -07001495 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
1497
1498/* Create "default" multicast route to the interface */
1499
1500static void addrconf_add_mroute(struct net_device *dev)
1501{
Thomas Graf86872cb2006-08-22 00:01:08 -07001502 struct fib6_config cfg = {
1503 .fc_table = RT6_TABLE_LOCAL,
1504 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1505 .fc_ifindex = dev->ifindex,
1506 .fc_dst_len = 8,
1507 .fc_flags = RTF_UP,
1508 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Thomas Graf86872cb2006-08-22 00:01:08 -07001510 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1511
1512 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
Joerg Roedel0be669b2006-10-10 14:49:53 -07001515#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516static void sit_route_add(struct net_device *dev)
1517{
Thomas Graf86872cb2006-08-22 00:01:08 -07001518 struct fib6_config cfg = {
1519 .fc_table = RT6_TABLE_MAIN,
1520 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1521 .fc_ifindex = dev->ifindex,
1522 .fc_dst_len = 96,
1523 .fc_flags = RTF_UP | RTF_NONEXTHOP,
1524 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 /* prefix length - 96 bits "::d.d.d.d" */
Thomas Graf86872cb2006-08-22 00:01:08 -07001527 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
Joerg Roedel0be669b2006-10-10 14:49:53 -07001529#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531static void addrconf_add_lroute(struct net_device *dev)
1532{
1533 struct in6_addr addr;
1534
1535 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1536 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1537}
1538
1539static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1540{
1541 struct inet6_dev *idev;
1542
1543 ASSERT_RTNL();
1544
1545 if ((idev = ipv6_find_idev(dev)) == NULL)
1546 return NULL;
1547
1548 /* Add default multicast route */
1549 addrconf_add_mroute(dev);
1550
1551 /* Add link local route */
1552 addrconf_add_lroute(dev);
1553 return idev;
1554}
1555
1556void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1557{
1558 struct prefix_info *pinfo;
1559 __u32 valid_lft;
1560 __u32 prefered_lft;
1561 int addr_type;
1562 unsigned long rt_expires;
1563 struct inet6_dev *in6_dev;
1564
1565 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 if (len < sizeof(struct prefix_info)) {
1568 ADBG(("addrconf: prefix option too short\n"));
1569 return;
1570 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 /*
1573 * Validation checks ([ADDRCONF], page 19)
1574 */
1575
1576 addr_type = ipv6_addr_type(&pinfo->prefix);
1577
1578 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1579 return;
1580
1581 valid_lft = ntohl(pinfo->valid);
1582 prefered_lft = ntohl(pinfo->prefered);
1583
1584 if (prefered_lft > valid_lft) {
1585 if (net_ratelimit())
1586 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1587 return;
1588 }
1589
1590 in6_dev = in6_dev_get(dev);
1591
1592 if (in6_dev == NULL) {
1593 if (net_ratelimit())
1594 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1595 return;
1596 }
1597
1598 /*
1599 * Two things going on here:
1600 * 1) Add routes for on-link prefixes
1601 * 2) Configure prefixes with the auto flag set
1602 */
1603
1604 /* Avoid arithmetic overflow. Really, we could
1605 save rt_expires in seconds, likely valid_lft,
1606 but it would require division in fib gc, that it
1607 not good.
1608 */
1609 if (valid_lft >= 0x7FFFFFFF/HZ)
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001610 rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 else
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001612 rt_expires = valid_lft * HZ;
1613
1614 /*
1615 * We convert this (in jiffies) to clock_t later.
1616 * Avoid arithmetic overflow there as well.
1617 * Overflow can happen only if HZ < USER_HZ.
1618 */
1619 if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
1620 rt_expires = 0x7FFFFFFF / USER_HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 if (pinfo->onlink) {
1623 struct rt6_info *rt;
1624 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1625
1626 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1627 if (rt->rt6i_flags&RTF_EXPIRES) {
1628 if (valid_lft == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001629 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 rt = NULL;
1631 } else {
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001632 rt->rt6i_expires = jiffies + rt_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 }
1634 }
1635 } else if (valid_lft) {
1636 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001637 dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
1639 if (rt)
1640 dst_release(&rt->u.dst);
1641 }
1642
1643 /* Try to figure out our local address for this prefix */
1644
1645 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1646 struct inet6_ifaddr * ifp;
1647 struct in6_addr addr;
1648 int create = 0, update_lft = 0;
1649
1650 if (pinfo->prefix_len == 64) {
1651 memcpy(&addr, &pinfo->prefix, 8);
1652 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1653 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1654 in6_dev_put(in6_dev);
1655 return;
1656 }
1657 goto ok;
1658 }
1659 if (net_ratelimit())
1660 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1661 pinfo->prefix_len);
1662 in6_dev_put(in6_dev);
1663 return;
1664
1665ok:
1666
1667 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1668
1669 if (ifp == NULL && valid_lft) {
1670 int max_addresses = in6_dev->cnf.max_addresses;
1671
1672 /* Do not allow to create too much of autoconfigured
1673 * addresses; this would be too easy way to crash kernel.
1674 */
1675 if (!max_addresses ||
1676 ipv6_count_addresses(in6_dev) < max_addresses)
1677 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1678 addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1679
1680 if (!ifp || IS_ERR(ifp)) {
1681 in6_dev_put(in6_dev);
1682 return;
1683 }
1684
1685 update_lft = create = 1;
1686 ifp->cstamp = jiffies;
1687 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1688 }
1689
1690 if (ifp) {
1691 int flags;
1692 unsigned long now;
1693#ifdef CONFIG_IPV6_PRIVACY
1694 struct inet6_ifaddr *ift;
1695#endif
1696 u32 stored_lft;
1697
1698 /* update lifetime (RFC2462 5.5.3 e) */
1699 spin_lock(&ifp->lock);
1700 now = jiffies;
1701 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1702 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1703 else
1704 stored_lft = 0;
1705 if (!update_lft && stored_lft) {
1706 if (valid_lft > MIN_VALID_LIFETIME ||
1707 valid_lft > stored_lft)
1708 update_lft = 1;
1709 else if (stored_lft <= MIN_VALID_LIFETIME) {
1710 /* valid_lft <= stored_lft is always true */
1711 /* XXX: IPsec */
1712 update_lft = 0;
1713 } else {
1714 valid_lft = MIN_VALID_LIFETIME;
1715 if (valid_lft < prefered_lft)
1716 prefered_lft = valid_lft;
1717 update_lft = 1;
1718 }
1719 }
1720
1721 if (update_lft) {
1722 ifp->valid_lft = valid_lft;
1723 ifp->prefered_lft = prefered_lft;
1724 ifp->tstamp = now;
1725 flags = ifp->flags;
1726 ifp->flags &= ~IFA_F_DEPRECATED;
1727 spin_unlock(&ifp->lock);
1728
1729 if (!(flags&IFA_F_TENTATIVE))
1730 ipv6_ifa_notify(0, ifp);
1731 } else
1732 spin_unlock(&ifp->lock);
1733
1734#ifdef CONFIG_IPV6_PRIVACY
1735 read_lock_bh(&in6_dev->lock);
1736 /* update all temporary addresses in the list */
1737 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1738 /*
1739 * When adjusting the lifetimes of an existing
1740 * temporary address, only lower the lifetimes.
1741 * Implementations must not increase the
1742 * lifetimes of an existing temporary address
1743 * when processing a Prefix Information Option.
1744 */
1745 spin_lock(&ift->lock);
1746 flags = ift->flags;
1747 if (ift->valid_lft > valid_lft &&
1748 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1749 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1750 if (ift->prefered_lft > prefered_lft &&
1751 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1752 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1753 spin_unlock(&ift->lock);
1754 if (!(flags&IFA_F_TENTATIVE))
1755 ipv6_ifa_notify(0, ift);
1756 }
1757
1758 if (create && in6_dev->cnf.use_tempaddr > 0) {
1759 /*
1760 * When a new public address is created as described in [ADDRCONF],
1761 * also create a new temporary address.
1762 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001763 read_unlock_bh(&in6_dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 ipv6_create_tempaddr(ifp, NULL);
1765 } else {
1766 read_unlock_bh(&in6_dev->lock);
1767 }
1768#endif
1769 in6_ifa_put(ifp);
1770 addrconf_verify(0);
1771 }
1772 }
1773 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1774 in6_dev_put(in6_dev);
1775}
1776
1777/*
1778 * Set destination address.
1779 * Special case for SIT interfaces where we create a new "virtual"
1780 * device.
1781 */
1782int addrconf_set_dstaddr(void __user *arg)
1783{
1784 struct in6_ifreq ireq;
1785 struct net_device *dev;
1786 int err = -EINVAL;
1787
1788 rtnl_lock();
1789
1790 err = -EFAULT;
1791 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1792 goto err_exit;
1793
1794 dev = __dev_get_by_index(ireq.ifr6_ifindex);
1795
1796 err = -ENODEV;
1797 if (dev == NULL)
1798 goto err_exit;
1799
Joerg Roedel0be669b2006-10-10 14:49:53 -07001800#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (dev->type == ARPHRD_SIT) {
1802 struct ifreq ifr;
1803 mm_segment_t oldfs;
1804 struct ip_tunnel_parm p;
1805
1806 err = -EADDRNOTAVAIL;
1807 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1808 goto err_exit;
1809
1810 memset(&p, 0, sizeof(p));
1811 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1812 p.iph.saddr = 0;
1813 p.iph.version = 4;
1814 p.iph.ihl = 5;
1815 p.iph.protocol = IPPROTO_IPV6;
1816 p.iph.ttl = 64;
1817 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1818
1819 oldfs = get_fs(); set_fs(KERNEL_DS);
1820 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1821 set_fs(oldfs);
1822
1823 if (err == 0) {
1824 err = -ENOBUFS;
1825 if ((dev = __dev_get_by_name(p.name)) == NULL)
1826 goto err_exit;
1827 err = dev_open(dev);
1828 }
1829 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07001830#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
1832err_exit:
1833 rtnl_unlock();
1834 return err;
1835}
1836
1837/*
1838 * Manual configuration of address on an interface
1839 */
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001840static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07001841 __u8 ifa_flags, __u32 prefered_lft, __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
1843 struct inet6_ifaddr *ifp;
1844 struct inet6_dev *idev;
1845 struct net_device *dev;
1846 int scope;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09001847 u32 flags = RTF_EXPIRES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001850
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001851 /* check the lifetime */
1852 if (!valid_lft || prefered_lft > valid_lft)
1853 return -EINVAL;
1854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1856 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 if ((idev = addrconf_add_dev(dev)) == NULL)
1859 return -ENOBUFS;
1860
1861 scope = ipv6_addr_scope(pfx);
1862
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09001863 if (valid_lft == INFINITY_LIFE_TIME) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001864 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09001865 flags = 0;
1866 } else if (valid_lft >= 0x7FFFFFFF/HZ)
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001867 valid_lft = 0x7FFFFFFF/HZ;
1868
1869 if (prefered_lft == 0)
1870 ifa_flags |= IFA_F_DEPRECATED;
1871 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
1872 (prefered_lft != INFINITY_LIFE_TIME))
1873 prefered_lft = 0x7FFFFFFF/HZ;
1874
1875 ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 if (!IS_ERR(ifp)) {
Herbert Xu06aebfb2006-08-09 16:52:04 -07001878 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001879 ifp->valid_lft = valid_lft;
1880 ifp->prefered_lft = prefered_lft;
1881 ifp->tstamp = jiffies;
Herbert Xu06aebfb2006-08-09 16:52:04 -07001882 spin_unlock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001883
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09001884 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
1885 jiffies_to_clock_t(valid_lft * HZ), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 addrconf_dad_start(ifp, 0);
1887 in6_ifa_put(ifp);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001888 addrconf_verify(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 return 0;
1890 }
1891
1892 return PTR_ERR(ifp);
1893}
1894
1895static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1896{
1897 struct inet6_ifaddr *ifp;
1898 struct inet6_dev *idev;
1899 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1902 return -ENODEV;
1903
1904 if ((idev = __in6_dev_get(dev)) == NULL)
1905 return -ENXIO;
1906
1907 read_lock_bh(&idev->lock);
1908 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1909 if (ifp->prefix_len == plen &&
1910 ipv6_addr_equal(pfx, &ifp->addr)) {
1911 in6_ifa_hold(ifp);
1912 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 ipv6_del_addr(ifp);
1915
1916 /* If the last address is deleted administratively,
1917 disable IPv6 on this interface.
1918 */
1919 if (idev->addr_list == NULL)
1920 addrconf_ifdown(idev->dev, 1);
1921 return 0;
1922 }
1923 }
1924 read_unlock_bh(&idev->lock);
1925 return -EADDRNOTAVAIL;
1926}
1927
1928
1929int addrconf_add_ifaddr(void __user *arg)
1930{
1931 struct in6_ifreq ireq;
1932 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (!capable(CAP_NET_ADMIN))
1935 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1938 return -EFAULT;
1939
1940 rtnl_lock();
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09001941 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07001942 IFA_F_PERMANENT, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 rtnl_unlock();
1944 return err;
1945}
1946
1947int addrconf_del_ifaddr(void __user *arg)
1948{
1949 struct in6_ifreq ireq;
1950 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (!capable(CAP_NET_ADMIN))
1953 return -EPERM;
1954
1955 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1956 return -EFAULT;
1957
1958 rtnl_lock();
1959 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1960 rtnl_unlock();
1961 return err;
1962}
1963
Joerg Roedel0be669b2006-10-10 14:49:53 -07001964#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965static void sit_add_v4_addrs(struct inet6_dev *idev)
1966{
1967 struct inet6_ifaddr * ifp;
1968 struct in6_addr addr;
1969 struct net_device *dev;
1970 int scope;
1971
1972 ASSERT_RTNL();
1973
1974 memset(&addr, 0, sizeof(struct in6_addr));
1975 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
1976
1977 if (idev->dev->flags&IFF_POINTOPOINT) {
1978 addr.s6_addr32[0] = htonl(0xfe800000);
1979 scope = IFA_LINK;
1980 } else {
1981 scope = IPV6_ADDR_COMPATv4;
1982 }
1983
1984 if (addr.s6_addr32[3]) {
1985 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
1986 if (!IS_ERR(ifp)) {
1987 spin_lock_bh(&ifp->lock);
1988 ifp->flags &= ~IFA_F_TENTATIVE;
1989 spin_unlock_bh(&ifp->lock);
1990 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1991 in6_ifa_put(ifp);
1992 }
1993 return;
1994 }
1995
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001996 for (dev = dev_base; dev != NULL; dev = dev->next) {
Herbert Xue5ed6392005-10-03 14:35:55 -07001997 struct in_device * in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 if (in_dev && (dev->flags & IFF_UP)) {
1999 struct in_ifaddr * ifa;
2000
2001 int flag = scope;
2002
2003 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2004 int plen;
2005
2006 addr.s6_addr32[3] = ifa->ifa_local;
2007
2008 if (ifa->ifa_scope == RT_SCOPE_LINK)
2009 continue;
2010 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2011 if (idev->dev->flags&IFF_POINTOPOINT)
2012 continue;
2013 flag |= IFA_HOST;
2014 }
2015 if (idev->dev->flags&IFF_POINTOPOINT)
2016 plen = 64;
2017 else
2018 plen = 96;
2019
2020 ifp = ipv6_add_addr(idev, &addr, plen, flag,
2021 IFA_F_PERMANENT);
2022 if (!IS_ERR(ifp)) {
2023 spin_lock_bh(&ifp->lock);
2024 ifp->flags &= ~IFA_F_TENTATIVE;
2025 spin_unlock_bh(&ifp->lock);
2026 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2027 in6_ifa_put(ifp);
2028 }
2029 }
2030 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002033#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035static void init_loopback(struct net_device *dev)
2036{
2037 struct inet6_dev *idev;
2038 struct inet6_ifaddr * ifp;
2039
2040 /* ::1 */
2041
2042 ASSERT_RTNL();
2043
2044 if ((idev = ipv6_find_idev(dev)) == NULL) {
2045 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2046 return;
2047 }
2048
2049 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2050 if (!IS_ERR(ifp)) {
2051 spin_lock_bh(&ifp->lock);
2052 ifp->flags &= ~IFA_F_TENTATIVE;
2053 spin_unlock_bh(&ifp->lock);
2054 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2055 in6_ifa_put(ifp);
2056 }
2057}
2058
2059static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2060{
2061 struct inet6_ifaddr * ifp;
2062
2063 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
2064 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002065 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 addrconf_dad_start(ifp, 0);
2067 in6_ifa_put(ifp);
2068 }
2069}
2070
2071static void addrconf_dev_config(struct net_device *dev)
2072{
2073 struct in6_addr addr;
2074 struct inet6_dev * idev;
2075
2076 ASSERT_RTNL();
2077
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002078 if ((dev->type != ARPHRD_ETHER) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 (dev->type != ARPHRD_FDDI) &&
2080 (dev->type != ARPHRD_IEEE802_TR) &&
2081 (dev->type != ARPHRD_ARCNET) &&
2082 (dev->type != ARPHRD_INFINIBAND)) {
2083 /* Alas, we support only Ethernet autoconfiguration. */
2084 return;
2085 }
2086
2087 idev = addrconf_add_dev(dev);
2088 if (idev == NULL)
2089 return;
2090
2091 memset(&addr, 0, sizeof(struct in6_addr));
2092 addr.s6_addr32[0] = htonl(0xFE800000);
2093
2094 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2095 addrconf_add_linklocal(idev, &addr);
2096}
2097
Joerg Roedel0be669b2006-10-10 14:49:53 -07002098#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099static void addrconf_sit_config(struct net_device *dev)
2100{
2101 struct inet6_dev *idev;
2102
2103 ASSERT_RTNL();
2104
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002105 /*
2106 * Configure the tunnel with one of our IPv4
2107 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 * our v4 addrs in the tunnel
2109 */
2110
2111 if ((idev = ipv6_find_idev(dev)) == NULL) {
2112 printk(KERN_DEBUG "init sit: add_dev failed\n");
2113 return;
2114 }
2115
2116 sit_add_v4_addrs(idev);
2117
2118 if (dev->flags&IFF_POINTOPOINT) {
2119 addrconf_add_mroute(dev);
2120 addrconf_add_lroute(dev);
2121 } else
2122 sit_route_add(dev);
2123}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002124#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126static inline int
2127ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2128{
2129 struct in6_addr lladdr;
2130
2131 if (!ipv6_get_lladdr(link_dev, &lladdr)) {
2132 addrconf_add_linklocal(idev, &lladdr);
2133 return 0;
2134 }
2135 return -1;
2136}
2137
2138static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2139{
2140 struct net_device *link_dev;
2141
2142 /* first try to inherit the link-local address from the link device */
2143 if (idev->dev->iflink &&
2144 (link_dev = __dev_get_by_index(idev->dev->iflink))) {
2145 if (!ipv6_inherit_linklocal(idev, link_dev))
2146 return;
2147 }
2148 /* then try to inherit it from any device */
2149 for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
2150 if (!ipv6_inherit_linklocal(idev, link_dev))
2151 return;
2152 }
2153 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2154}
2155
2156/*
2157 * Autoconfigure tunnel with a link-local address so routing protocols,
2158 * DHCPv6, MLD etc. can be run over the virtual link
2159 */
2160
2161static void addrconf_ip6_tnl_config(struct net_device *dev)
2162{
2163 struct inet6_dev *idev;
2164
2165 ASSERT_RTNL();
2166
2167 if ((idev = addrconf_add_dev(dev)) == NULL) {
2168 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2169 return;
2170 }
2171 ip6_tnl_add_linklocal(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
2173
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002174static int addrconf_notify(struct notifier_block *this, unsigned long event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 void * data)
2176{
2177 struct net_device *dev = (struct net_device *) data;
2178 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002179 int run_pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 switch(event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002182 case NETDEV_REGISTER:
2183 if (!idev) {
2184 idev = ipv6_add_dev(dev);
2185 if (!idev)
2186 printk(KERN_WARNING "IPv6: add_dev failed for %s\n",
2187 dev->name);
2188 }
2189 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002191 case NETDEV_CHANGE:
2192 if (event == NETDEV_UP) {
2193 if (!netif_carrier_ok(dev)) {
2194 /* device is not ready yet. */
2195 printk(KERN_INFO
2196 "ADDRCONF(NETDEV_UP): %s: "
2197 "link is not ready\n",
2198 dev->name);
2199 break;
2200 }
Kristian Slavov99081042006-02-08 16:10:53 -08002201
2202 if (idev)
2203 idev->if_flags |= IF_READY;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002204 } else {
2205 if (!netif_carrier_ok(dev)) {
2206 /* device is still not ready. */
2207 break;
2208 }
2209
2210 if (idev) {
2211 if (idev->if_flags & IF_READY) {
2212 /* device is already configured. */
2213 break;
2214 }
2215 idev->if_flags |= IF_READY;
2216 }
2217
2218 printk(KERN_INFO
2219 "ADDRCONF(NETDEV_CHANGE): %s: "
2220 "link becomes ready\n",
2221 dev->name);
2222
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002223 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002224 }
2225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 switch(dev->type) {
Joerg Roedel0be669b2006-10-10 14:49:53 -07002227#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 case ARPHRD_SIT:
2229 addrconf_sit_config(dev);
2230 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002231#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 case ARPHRD_TUNNEL6:
2233 addrconf_ip6_tnl_config(dev);
2234 break;
2235 case ARPHRD_LOOPBACK:
2236 init_loopback(dev);
2237 break;
2238
2239 default:
2240 addrconf_dev_config(dev);
2241 break;
2242 };
2243 if (idev) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002244 if (run_pending)
2245 addrconf_dad_run(idev);
2246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 /* If the MTU changed during the interface down, when the
2248 interface up, the changed MTU must be reflected in the
2249 idev as well as routers.
2250 */
2251 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2252 rt6_mtu_change(dev, dev->mtu);
2253 idev->cnf.mtu6 = dev->mtu;
2254 }
2255 idev->tstamp = jiffies;
2256 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2257 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2258 stop IPv6 on this interface.
2259 */
2260 if (dev->mtu < IPV6_MIN_MTU)
2261 addrconf_ifdown(dev, event != NETDEV_DOWN);
2262 }
2263 break;
2264
2265 case NETDEV_CHANGEMTU:
2266 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
2267 rt6_mtu_change(dev, dev->mtu);
2268 idev->cnf.mtu6 = dev->mtu;
2269 break;
2270 }
2271
2272 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2273
2274 case NETDEV_DOWN:
2275 case NETDEV_UNREGISTER:
2276 /*
2277 * Remove all addresses from this interface.
2278 */
2279 addrconf_ifdown(dev, event != NETDEV_DOWN);
2280 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 case NETDEV_CHANGENAME:
2283#ifdef CONFIG_SYSCTL
2284 if (idev) {
2285 addrconf_sysctl_unregister(&idev->cnf);
2286 neigh_sysctl_unregister(idev->nd_parms);
2287 neigh_sysctl_register(dev, idev->nd_parms,
2288 NET_IPV6, NET_IPV6_NEIGH, "ipv6",
2289 &ndisc_ifinfo_sysctl_change,
2290 NULL);
2291 addrconf_sysctl_register(idev, &idev->cnf);
2292 }
2293#endif
2294 break;
2295 };
2296
2297 return NOTIFY_OK;
2298}
2299
2300/*
2301 * addrconf module should be notified of a device going up
2302 */
2303static struct notifier_block ipv6_dev_notf = {
2304 .notifier_call = addrconf_notify,
2305 .priority = 0
2306};
2307
2308static int addrconf_ifdown(struct net_device *dev, int how)
2309{
2310 struct inet6_dev *idev;
2311 struct inet6_ifaddr *ifa, **bifa;
2312 int i;
2313
2314 ASSERT_RTNL();
2315
2316 if (dev == &loopback_dev && how == 1)
2317 how = 0;
2318
2319 rt6_ifdown(dev);
2320 neigh_ifdown(&nd_tbl, dev);
2321
2322 idev = __in6_dev_get(dev);
2323 if (idev == NULL)
2324 return -ENODEV;
2325
2326 /* Step 1: remove reference to ipv6 device from parent device.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002327 Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 */
2329 if (how == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002331
2332 /* protected by rtnl_lock */
2333 rcu_assign_pointer(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335 /* Step 1.5: remove snmp6 entry */
2336 snmp6_unregister_dev(idev);
2337
2338 }
2339
2340 /* Step 2: clear hash table */
2341 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2342 bifa = &inet6_addr_lst[i];
2343
2344 write_lock_bh(&addrconf_hash_lock);
2345 while ((ifa = *bifa) != NULL) {
2346 if (ifa->idev == idev) {
2347 *bifa = ifa->lst_next;
2348 ifa->lst_next = NULL;
2349 addrconf_del_timer(ifa);
2350 in6_ifa_put(ifa);
2351 continue;
2352 }
2353 bifa = &ifa->lst_next;
2354 }
2355 write_unlock_bh(&addrconf_hash_lock);
2356 }
2357
2358 write_lock_bh(&idev->lock);
2359
2360 /* Step 3: clear flags for stateless addrconf */
2361 if (how != 1)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002362 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
2364 /* Step 4: clear address list */
2365#ifdef CONFIG_IPV6_PRIVACY
2366 if (how == 1 && del_timer(&idev->regen_timer))
2367 in6_dev_put(idev);
2368
2369 /* clear tempaddr list */
2370 while ((ifa = idev->tempaddr_list) != NULL) {
2371 idev->tempaddr_list = ifa->tmp_next;
2372 ifa->tmp_next = NULL;
2373 ifa->dead = 1;
2374 write_unlock_bh(&idev->lock);
2375 spin_lock_bh(&ifa->lock);
2376
2377 if (ifa->ifpub) {
2378 in6_ifa_put(ifa->ifpub);
2379 ifa->ifpub = NULL;
2380 }
2381 spin_unlock_bh(&ifa->lock);
2382 in6_ifa_put(ifa);
2383 write_lock_bh(&idev->lock);
2384 }
2385#endif
2386 while ((ifa = idev->addr_list) != NULL) {
2387 idev->addr_list = ifa->if_next;
2388 ifa->if_next = NULL;
2389 ifa->dead = 1;
2390 addrconf_del_timer(ifa);
2391 write_unlock_bh(&idev->lock);
2392
2393 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2394 in6_ifa_put(ifa);
2395
2396 write_lock_bh(&idev->lock);
2397 }
2398 write_unlock_bh(&idev->lock);
2399
2400 /* Step 5: Discard multicast list */
2401
2402 if (how == 1)
2403 ipv6_mc_destroy_dev(idev);
2404 else
2405 ipv6_mc_down(idev);
2406
2407 /* Step 5: netlink notification of this interface */
2408 idev->tstamp = jiffies;
Yan Zheng979ad662005-10-14 18:31:15 +08002409 inet6_ifinfo_notify(RTM_DELLINK, idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 /* Shot the device (if unregistered) */
2412
2413 if (how == 1) {
2414#ifdef CONFIG_SYSCTL
2415 addrconf_sysctl_unregister(&idev->cnf);
2416 neigh_sysctl_unregister(idev->nd_parms);
2417#endif
2418 neigh_parms_release(&nd_tbl, idev->nd_parms);
2419 neigh_ifdown(&nd_tbl, dev);
2420 in6_dev_put(idev);
2421 }
2422 return 0;
2423}
2424
2425static void addrconf_rs_timer(unsigned long data)
2426{
2427 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2428
2429 if (ifp->idev->cnf.forwarding)
2430 goto out;
2431
2432 if (ifp->idev->if_flags & IF_RA_RCVD) {
2433 /*
2434 * Announcement received after solicitation
2435 * was sent
2436 */
2437 goto out;
2438 }
2439
2440 spin_lock(&ifp->lock);
2441 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2442 struct in6_addr all_routers;
2443
2444 /* The wait after the last probe can be shorter */
2445 addrconf_mod_timer(ifp, AC_RS,
2446 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2447 ifp->idev->cnf.rtr_solicit_delay :
2448 ifp->idev->cnf.rtr_solicit_interval);
2449 spin_unlock(&ifp->lock);
2450
2451 ipv6_addr_all_routers(&all_routers);
2452
2453 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2454 } else {
2455 spin_unlock(&ifp->lock);
2456 /*
2457 * Note: we do not support deprecated "all on-link"
2458 * assumption any longer.
2459 */
2460 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2461 ifp->idev->dev->name);
2462 }
2463
2464out:
2465 in6_ifa_put(ifp);
2466}
2467
2468/*
2469 * Duplicate Address Detection
2470 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002471static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2472{
2473 unsigned long rand_num;
2474 struct inet6_dev *idev = ifp->idev;
2475
2476 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2477 ifp->probes = idev->cnf.dad_transmits;
2478 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2479}
2480
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002481static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
2483 struct inet6_dev *idev = ifp->idev;
2484 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
2486 addrconf_join_solict(dev, &ifp->addr);
2487
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 net_srandom(ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
2490 read_lock_bh(&idev->lock);
2491 if (ifp->dead)
2492 goto out;
2493 spin_lock_bh(&ifp->lock);
2494
2495 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002496 !(ifp->flags&IFA_F_TENTATIVE) ||
2497 ifp->flags & IFA_F_NODAD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 ifp->flags &= ~IFA_F_TENTATIVE;
2499 spin_unlock_bh(&ifp->lock);
2500 read_unlock_bh(&idev->lock);
2501
2502 addrconf_dad_completed(ifp);
2503 return;
2504 }
2505
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002506 if (!(idev->if_flags & IF_READY)) {
YOSHIFUJI Hideaki3dd3bf82005-12-23 11:23:21 -08002507 spin_unlock_bh(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002508 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002509 /*
2510 * If the defice is not ready:
2511 * - keep it tentative if it is a permanent address.
2512 * - otherwise, kill it.
2513 */
2514 in6_ifa_hold(ifp);
2515 addrconf_dad_stop(ifp);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002516 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002517 }
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002518 addrconf_dad_kick(ifp);
2519 spin_unlock_bh(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520out:
2521 read_unlock_bh(&idev->lock);
2522}
2523
2524static void addrconf_dad_timer(unsigned long data)
2525{
2526 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2527 struct inet6_dev *idev = ifp->idev;
2528 struct in6_addr unspec;
2529 struct in6_addr mcaddr;
2530
2531 read_lock_bh(&idev->lock);
2532 if (idev->dead) {
2533 read_unlock_bh(&idev->lock);
2534 goto out;
2535 }
2536 spin_lock_bh(&ifp->lock);
2537 if (ifp->probes == 0) {
2538 /*
2539 * DAD was successful
2540 */
2541
2542 ifp->flags &= ~IFA_F_TENTATIVE;
2543 spin_unlock_bh(&ifp->lock);
2544 read_unlock_bh(&idev->lock);
2545
2546 addrconf_dad_completed(ifp);
2547
2548 goto out;
2549 }
2550
2551 ifp->probes--;
2552 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2553 spin_unlock_bh(&ifp->lock);
2554 read_unlock_bh(&idev->lock);
2555
2556 /* send a neighbour solicitation for our addr */
2557 memset(&unspec, 0, sizeof(unspec));
2558 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2559 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2560out:
2561 in6_ifa_put(ifp);
2562}
2563
2564static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2565{
2566 struct net_device * dev = ifp->idev->dev;
2567
2568 /*
2569 * Configure the address for reception. Now it is valid.
2570 */
2571
2572 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2573
2574 /* If added prefix is link local and forwarding is off,
2575 start sending router solicitations.
2576 */
2577
2578 if (ifp->idev->cnf.forwarding == 0 &&
2579 ifp->idev->cnf.rtr_solicits > 0 &&
2580 (dev->flags&IFF_LOOPBACK) == 0 &&
2581 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2582 struct in6_addr all_routers;
2583
2584 ipv6_addr_all_routers(&all_routers);
2585
2586 /*
2587 * If a host as already performed a random delay
2588 * [...] as part of DAD [...] there is no need
2589 * to delay again before sending the first RS
2590 */
2591 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2592
2593 spin_lock_bh(&ifp->lock);
2594 ifp->probes = 1;
2595 ifp->idev->if_flags |= IF_RS_SENT;
2596 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2597 spin_unlock_bh(&ifp->lock);
2598 }
2599}
2600
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002601static void addrconf_dad_run(struct inet6_dev *idev) {
2602 struct inet6_ifaddr *ifp;
2603
2604 read_lock_bh(&idev->lock);
2605 for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2606 spin_lock_bh(&ifp->lock);
2607 if (!(ifp->flags & IFA_F_TENTATIVE)) {
2608 spin_unlock_bh(&ifp->lock);
2609 continue;
2610 }
2611 spin_unlock_bh(&ifp->lock);
2612 addrconf_dad_kick(ifp);
2613 }
2614 read_unlock_bh(&idev->lock);
2615}
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617#ifdef CONFIG_PROC_FS
2618struct if6_iter_state {
2619 int bucket;
2620};
2621
2622static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2623{
2624 struct inet6_ifaddr *ifa = NULL;
2625 struct if6_iter_state *state = seq->private;
2626
2627 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2628 ifa = inet6_addr_lst[state->bucket];
2629 if (ifa)
2630 break;
2631 }
2632 return ifa;
2633}
2634
2635static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2636{
2637 struct if6_iter_state *state = seq->private;
2638
2639 ifa = ifa->lst_next;
2640try_again:
2641 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2642 ifa = inet6_addr_lst[state->bucket];
2643 goto try_again;
2644 }
2645 return ifa;
2646}
2647
2648static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2649{
2650 struct inet6_ifaddr *ifa = if6_get_first(seq);
2651
2652 if (ifa)
2653 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2654 --pos;
2655 return pos ? NULL : ifa;
2656}
2657
2658static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2659{
2660 read_lock_bh(&addrconf_hash_lock);
2661 return if6_get_idx(seq, *pos);
2662}
2663
2664static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2665{
2666 struct inet6_ifaddr *ifa;
2667
2668 ifa = if6_get_next(seq, v);
2669 ++*pos;
2670 return ifa;
2671}
2672
2673static void if6_seq_stop(struct seq_file *seq, void *v)
2674{
2675 read_unlock_bh(&addrconf_hash_lock);
2676}
2677
2678static int if6_seq_show(struct seq_file *seq, void *v)
2679{
2680 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2681 seq_printf(seq,
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002682 NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 NIP6(ifp->addr),
2684 ifp->idev->dev->ifindex,
2685 ifp->prefix_len,
2686 ifp->scope,
2687 ifp->flags,
2688 ifp->idev->dev->name);
2689 return 0;
2690}
2691
2692static struct seq_operations if6_seq_ops = {
2693 .start = if6_seq_start,
2694 .next = if6_seq_next,
2695 .show = if6_seq_show,
2696 .stop = if6_seq_stop,
2697};
2698
2699static int if6_seq_open(struct inode *inode, struct file *file)
2700{
2701 struct seq_file *seq;
2702 int rc = -ENOMEM;
Ingo Oeser322f74a2006-03-20 23:01:47 -08002703 struct if6_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
2705 if (!s)
2706 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
2708 rc = seq_open(file, &if6_seq_ops);
2709 if (rc)
2710 goto out_kfree;
2711
2712 seq = file->private_data;
2713 seq->private = s;
2714out:
2715 return rc;
2716out_kfree:
2717 kfree(s);
2718 goto out;
2719}
2720
Arjan van de Ven9a321442007-02-12 00:55:35 -08002721static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 .owner = THIS_MODULE,
2723 .open = if6_seq_open,
2724 .read = seq_read,
2725 .llseek = seq_lseek,
2726 .release = seq_release_private,
2727};
2728
2729int __init if6_proc_init(void)
2730{
2731 if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2732 return -ENOMEM;
2733 return 0;
2734}
2735
2736void if6_proc_exit(void)
2737{
2738 proc_net_remove("if_inet6");
2739}
2740#endif /* CONFIG_PROC_FS */
2741
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07002742#ifdef CONFIG_IPV6_MIP6
2743/* Check if address is a home address configured on any interface. */
2744int ipv6_chk_home_addr(struct in6_addr *addr)
2745{
2746 int ret = 0;
2747 struct inet6_ifaddr * ifp;
2748 u8 hash = ipv6_addr_hash(addr);
2749 read_lock_bh(&addrconf_hash_lock);
2750 for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) {
2751 if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
2752 (ifp->flags & IFA_F_HOMEADDRESS)) {
2753 ret = 1;
2754 break;
2755 }
2756 }
2757 read_unlock_bh(&addrconf_hash_lock);
2758 return ret;
2759}
2760#endif
2761
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762/*
2763 * Periodic address status verification
2764 */
2765
2766static void addrconf_verify(unsigned long foo)
2767{
2768 struct inet6_ifaddr *ifp;
2769 unsigned long now, next;
2770 int i;
2771
2772 spin_lock_bh(&addrconf_verify_lock);
2773 now = jiffies;
2774 next = now + ADDR_CHECK_FREQUENCY;
2775
2776 del_timer(&addr_chk_timer);
2777
2778 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2779
2780restart:
Yan Zheng5d5780d2005-11-20 13:42:20 -08002781 read_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2783 unsigned long age;
2784#ifdef CONFIG_IPV6_PRIVACY
2785 unsigned long regen_advance;
2786#endif
2787
2788 if (ifp->flags & IFA_F_PERMANENT)
2789 continue;
2790
2791 spin_lock(&ifp->lock);
2792 age = (now - ifp->tstamp) / HZ;
2793
2794#ifdef CONFIG_IPV6_PRIVACY
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002795 regen_advance = ifp->idev->cnf.regen_max_retry *
2796 ifp->idev->cnf.dad_transmits *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 ifp->idev->nd_parms->retrans_time / HZ;
2798#endif
2799
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09002800 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
2801 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 spin_unlock(&ifp->lock);
2803 in6_ifa_hold(ifp);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002804 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 ipv6_del_addr(ifp);
2806 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09002807 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
2808 spin_unlock(&ifp->lock);
2809 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 } else if (age >= ifp->prefered_lft) {
2811 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2812 int deprecate = 0;
2813
2814 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2815 deprecate = 1;
2816 ifp->flags |= IFA_F_DEPRECATED;
2817 }
2818
2819 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2820 next = ifp->tstamp + ifp->valid_lft * HZ;
2821
2822 spin_unlock(&ifp->lock);
2823
2824 if (deprecate) {
2825 in6_ifa_hold(ifp);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002826 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
2828 ipv6_ifa_notify(0, ifp);
2829 in6_ifa_put(ifp);
2830 goto restart;
2831 }
2832#ifdef CONFIG_IPV6_PRIVACY
2833 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2834 !(ifp->flags&IFA_F_TENTATIVE)) {
2835 if (age >= ifp->prefered_lft - regen_advance) {
2836 struct inet6_ifaddr *ifpub = ifp->ifpub;
2837 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2838 next = ifp->tstamp + ifp->prefered_lft * HZ;
2839 if (!ifp->regen_count && ifpub) {
2840 ifp->regen_count++;
2841 in6_ifa_hold(ifp);
2842 in6_ifa_hold(ifpub);
2843 spin_unlock(&ifp->lock);
Yan Zheng5d5780d2005-11-20 13:42:20 -08002844 read_unlock(&addrconf_hash_lock);
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08002845 spin_lock(&ifpub->lock);
2846 ifpub->regen_count = 0;
2847 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 ipv6_create_tempaddr(ifpub, ifp);
2849 in6_ifa_put(ifpub);
2850 in6_ifa_put(ifp);
2851 goto restart;
2852 }
2853 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2854 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2855 spin_unlock(&ifp->lock);
2856#endif
2857 } else {
2858 /* ifp->prefered_lft <= ifp->valid_lft */
2859 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2860 next = ifp->tstamp + ifp->prefered_lft * HZ;
2861 spin_unlock(&ifp->lock);
2862 }
2863 }
Yan Zheng5d5780d2005-11-20 13:42:20 -08002864 read_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 }
2866
2867 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2868 add_timer(&addr_chk_timer);
2869 spin_unlock_bh(&addrconf_verify_lock);
2870}
2871
Thomas Graf461d8832006-09-18 00:09:49 -07002872static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
2873{
2874 struct in6_addr *pfx = NULL;
2875
2876 if (addr)
2877 pfx = nla_data(addr);
2878
2879 if (local) {
2880 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
2881 pfx = NULL;
2882 else
2883 pfx = nla_data(local);
2884 }
2885
2886 return pfx;
2887}
2888
2889static struct nla_policy ifa_ipv6_policy[IFA_MAX+1] __read_mostly = {
2890 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
2891 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
2892 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
2893};
2894
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895static int
2896inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2897{
Thomas Grafb933f712006-09-18 00:10:19 -07002898 struct ifaddrmsg *ifm;
2899 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 struct in6_addr *pfx;
Thomas Grafb933f712006-09-18 00:10:19 -07002901 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
Thomas Grafb933f712006-09-18 00:10:19 -07002903 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
2904 if (err < 0)
2905 return err;
2906
2907 ifm = nlmsg_data(nlh);
2908 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 if (pfx == NULL)
2910 return -EINVAL;
2911
2912 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2913}
2914
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002915static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
2916 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002917{
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002918 u32 flags = RTF_EXPIRES;
2919
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002920 if (!valid_lft || (prefered_lft > valid_lft))
2921 return -EINVAL;
2922
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002923 if (valid_lft == INFINITY_LIFE_TIME) {
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002924 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002925 flags = 0;
2926 } else if (valid_lft >= 0x7FFFFFFF/HZ)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002927 valid_lft = 0x7FFFFFFF/HZ;
2928
2929 if (prefered_lft == 0)
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002930 ifa_flags |= IFA_F_DEPRECATED;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002931 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
2932 (prefered_lft != INFINITY_LIFE_TIME))
2933 prefered_lft = 0x7FFFFFFF/HZ;
2934
2935 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07002936 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 +09002937 ifp->tstamp = jiffies;
2938 ifp->valid_lft = valid_lft;
2939 ifp->prefered_lft = prefered_lft;
2940
2941 spin_unlock_bh(&ifp->lock);
2942 if (!(ifp->flags&IFA_F_TENTATIVE))
2943 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002944
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002945 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
2946 jiffies_to_clock_t(valid_lft * HZ), flags);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002947 addrconf_verify(0);
2948
2949 return 0;
2950}
2951
2952static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2954{
Thomas Graf461d8832006-09-18 00:09:49 -07002955 struct ifaddrmsg *ifm;
2956 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 struct in6_addr *pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07002958 struct inet6_ifaddr *ifa;
2959 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002960 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
2961 u8 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07002962 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Thomas Graf461d8832006-09-18 00:09:49 -07002964 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
2965 if (err < 0)
2966 return err;
2967
2968 ifm = nlmsg_data(nlh);
2969 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 if (pfx == NULL)
2971 return -EINVAL;
2972
Thomas Graf461d8832006-09-18 00:09:49 -07002973 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002974 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07002975
2976 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002977 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07002978 preferred_lft = ci->ifa_prefered;
2979 } else {
2980 preferred_lft = INFINITY_LIFE_TIME;
2981 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002982 }
2983
Thomas Graf7198f8c2006-09-18 00:13:46 -07002984 dev = __dev_get_by_index(ifm->ifa_index);
2985 if (dev == NULL)
2986 return -ENODEV;
2987
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002988 /* We ignore other flags so far. */
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07002989 ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002990
Thomas Graf7198f8c2006-09-18 00:13:46 -07002991 ifa = ipv6_get_ifaddr(pfx, dev, 1);
2992 if (ifa == NULL) {
2993 /*
2994 * It would be best to check for !NLM_F_CREATE here but
2995 * userspace alreay relies on not having to provide this.
2996 */
2997 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002998 ifa_flags, preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09002999 }
3000
Thomas Graf7198f8c2006-09-18 00:13:46 -07003001 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3002 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3003 err = -EEXIST;
3004 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003005 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003006
3007 in6_ifa_put(ifa);
3008
3009 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010}
3011
Thomas Graf101bb222006-09-18 00:11:52 -07003012static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3013 u8 scope, int ifindex)
3014{
3015 struct ifaddrmsg *ifm;
3016
3017 ifm = nlmsg_data(nlh);
3018 ifm->ifa_family = AF_INET6;
3019 ifm->ifa_prefixlen = prefixlen;
3020 ifm->ifa_flags = flags;
3021 ifm->ifa_scope = scope;
3022 ifm->ifa_index = ifindex;
3023}
3024
Thomas Graf85486af2006-09-18 00:11:24 -07003025static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3026 unsigned long tstamp, u32 preferred, u32 valid)
3027{
3028 struct ifa_cacheinfo ci;
3029
3030 ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100
3031 + TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3032 ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100
3033 + TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3034 ci.ifa_prefered = preferred;
3035 ci.ifa_valid = valid;
3036
3037 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3038}
3039
Thomas Graf101bb222006-09-18 00:11:52 -07003040static inline int rt_scope(int ifa_scope)
3041{
3042 if (ifa_scope & IFA_HOST)
3043 return RT_SCOPE_HOST;
3044 else if (ifa_scope & IFA_LINK)
3045 return RT_SCOPE_LINK;
3046 else if (ifa_scope & IFA_SITE)
3047 return RT_SCOPE_SITE;
3048 else
3049 return RT_SCOPE_UNIVERSE;
3050}
3051
Thomas Graf0ab68032006-09-18 00:12:35 -07003052static inline int inet6_ifaddr_msgsize(void)
3053{
Thomas Graf339bf982006-11-10 14:10:15 -08003054 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3055 + nla_total_size(16) /* IFA_ADDRESS */
3056 + nla_total_size(sizeof(struct ifa_cacheinfo));
Thomas Graf0ab68032006-09-18 00:12:35 -07003057}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003058
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003060 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07003063 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
Thomas Graf0ab68032006-09-18 00:12:35 -07003065 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3066 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003067 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003068
Thomas Graf101bb222006-09-18 00:11:52 -07003069 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3070 ifa->idev->dev->ifindex);
3071
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 if (!(ifa->flags&IFA_F_PERMANENT)) {
Thomas Graf85486af2006-09-18 00:11:24 -07003073 preferred = ifa->prefered_lft;
3074 valid = ifa->valid_lft;
3075 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 long tval = (jiffies - ifa->tstamp)/HZ;
Thomas Graf85486af2006-09-18 00:11:24 -07003077 preferred -= tval;
3078 if (valid != INFINITY_LIFE_TIME)
3079 valid -= tval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 }
3081 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07003082 preferred = INFINITY_LIFE_TIME;
3083 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 }
Thomas Graf85486af2006-09-18 00:11:24 -07003085
Thomas Graf0ab68032006-09-18 00:12:35 -07003086 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
Patrick McHardy26932562007-01-31 23:16:40 -08003087 put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
3088 nlmsg_cancel(skb, nlh);
3089 return -EMSGSIZE;
3090 }
Thomas Graf85486af2006-09-18 00:11:24 -07003091
Thomas Graf0ab68032006-09-18 00:12:35 -07003092 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093}
3094
3095static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07003096 u32 pid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003099 u8 scope = RT_SCOPE_UNIVERSE;
3100 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Thomas Graf101bb222006-09-18 00:11:52 -07003102 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3103 scope = RT_SCOPE_SITE;
3104
Thomas Graf0ab68032006-09-18 00:12:35 -07003105 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3106 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003107 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003108
Thomas Graf101bb222006-09-18 00:11:52 -07003109 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003110 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3111 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003112 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3113 nlmsg_cancel(skb, nlh);
3114 return -EMSGSIZE;
3115 }
Thomas Graf85486af2006-09-18 00:11:24 -07003116
Thomas Graf0ab68032006-09-18 00:12:35 -07003117 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118}
3119
3120static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003121 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003124 u8 scope = RT_SCOPE_UNIVERSE;
3125 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Thomas Graf101bb222006-09-18 00:11:52 -07003127 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3128 scope = RT_SCOPE_SITE;
3129
Thomas Graf0ab68032006-09-18 00:12:35 -07003130 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3131 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003132 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003133
Thomas Graf101bb222006-09-18 00:11:52 -07003134 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003135 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3136 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003137 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3138 nlmsg_cancel(skb, nlh);
3139 return -EMSGSIZE;
3140 }
Thomas Graf85486af2006-09-18 00:11:24 -07003141
Thomas Graf0ab68032006-09-18 00:12:35 -07003142 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143}
3144
3145enum addr_type_t
3146{
3147 UNICAST_ADDR,
3148 MULTICAST_ADDR,
3149 ANYCAST_ADDR,
3150};
3151
3152static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3153 enum addr_type_t type)
3154{
3155 int idx, ip_idx;
3156 int s_idx, s_ip_idx;
3157 int err = 1;
3158 struct net_device *dev;
3159 struct inet6_dev *idev = NULL;
3160 struct inet6_ifaddr *ifa;
3161 struct ifmcaddr6 *ifmca;
3162 struct ifacaddr6 *ifaca;
3163
3164 s_idx = cb->args[0];
3165 s_ip_idx = ip_idx = cb->args[1];
3166 read_lock(&dev_base_lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003167
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
3169 if (idx < s_idx)
3170 continue;
3171 if (idx > s_idx)
3172 s_ip_idx = 0;
3173 ip_idx = 0;
3174 if ((idev = in6_dev_get(dev)) == NULL)
3175 continue;
3176 read_lock_bh(&idev->lock);
3177 switch (type) {
3178 case UNICAST_ADDR:
YOSHIFUJI Hideakiae9cda52005-06-28 13:00:30 -07003179 /* unicast address incl. temp addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 for (ifa = idev->addr_list; ifa;
3181 ifa = ifa->if_next, ip_idx++) {
3182 if (ip_idx < s_ip_idx)
3183 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003184 if ((err = inet6_fill_ifaddr(skb, ifa,
3185 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003186 cb->nlh->nlmsg_seq, RTM_NEWADDR,
3187 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 goto done;
3189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 break;
3191 case MULTICAST_ADDR:
3192 /* multicast address */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003193 for (ifmca = idev->mc_list; ifmca;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 ifmca = ifmca->next, ip_idx++) {
3195 if (ip_idx < s_ip_idx)
3196 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003197 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
3198 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003199 cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3200 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 goto done;
3202 }
3203 break;
3204 case ANYCAST_ADDR:
3205 /* anycast address */
3206 for (ifaca = idev->ac_list; ifaca;
3207 ifaca = ifaca->aca_next, ip_idx++) {
3208 if (ip_idx < s_ip_idx)
3209 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003210 if ((err = inet6_fill_ifacaddr(skb, ifaca,
3211 NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003212 cb->nlh->nlmsg_seq, RTM_GETANYCAST,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003213 NLM_F_MULTI)) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 goto done;
3215 }
3216 break;
3217 default:
3218 break;
3219 }
3220 read_unlock_bh(&idev->lock);
3221 in6_dev_put(idev);
3222 }
3223done:
3224 if (err <= 0) {
3225 read_unlock_bh(&idev->lock);
3226 in6_dev_put(idev);
3227 }
3228 read_unlock(&dev_base_lock);
3229 cb->args[0] = idx;
3230 cb->args[1] = ip_idx;
3231 return skb->len;
3232}
3233
3234static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3235{
3236 enum addr_type_t type = UNICAST_ADDR;
3237 return inet6_dump_addr(skb, cb, type);
3238}
3239
3240static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3241{
3242 enum addr_type_t type = MULTICAST_ADDR;
3243 return inet6_dump_addr(skb, cb, type);
3244}
3245
3246
3247static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3248{
3249 enum addr_type_t type = ANYCAST_ADDR;
3250 return inet6_dump_addr(skb, cb, type);
3251}
3252
Thomas Graf1b29fc22006-09-18 00:10:50 -07003253static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
3254 void *arg)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003255{
Thomas Graf1b29fc22006-09-18 00:10:50 -07003256 struct ifaddrmsg *ifm;
3257 struct nlattr *tb[IFA_MAX+1];
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003258 struct in6_addr *addr = NULL;
3259 struct net_device *dev = NULL;
3260 struct inet6_ifaddr *ifa;
3261 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003262 int err;
3263
Thomas Graf1b29fc22006-09-18 00:10:50 -07003264 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3265 if (err < 0)
3266 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003267
Thomas Graf1b29fc22006-09-18 00:10:50 -07003268 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3269 if (addr == NULL) {
3270 err = -EINVAL;
3271 goto errout;
3272 }
3273
3274 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003275 if (ifm->ifa_index)
3276 dev = __dev_get_by_index(ifm->ifa_index);
3277
Thomas Graf1b29fc22006-09-18 00:10:50 -07003278 if ((ifa = ipv6_get_ifaddr(addr, dev, 1)) == NULL) {
3279 err = -EADDRNOTAVAIL;
3280 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003281 }
3282
Thomas Graf0ab68032006-09-18 00:12:35 -07003283 if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07003284 err = -ENOBUFS;
3285 goto errout_ifa;
3286 }
3287
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003288 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
3289 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003290 if (err < 0) {
3291 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3292 WARN_ON(err == -EMSGSIZE);
3293 kfree_skb(skb);
3294 goto errout_ifa;
3295 }
Thomas Graf2942e902006-08-15 00:30:25 -07003296 err = rtnl_unicast(skb, NETLINK_CB(in_skb).pid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003297errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003298 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003299errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003300 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003301}
3302
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3304{
3305 struct sk_buff *skb;
Thomas Graf5d620262006-08-15 00:35:02 -07003306 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307
Thomas Graf0ab68032006-09-18 00:12:35 -07003308 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Thomas Graf5d620262006-08-15 00:35:02 -07003309 if (skb == NULL)
3310 goto errout;
3311
3312 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003313 if (err < 0) {
3314 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3315 WARN_ON(err == -EMSGSIZE);
3316 kfree_skb(skb);
3317 goto errout;
3318 }
Thomas Graf5d620262006-08-15 00:35:02 -07003319 err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3320errout:
3321 if (err < 0)
3322 rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323}
3324
3325static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
3326 __s32 *array, int bytes)
3327{
Thomas Graf04561c12006-11-14 19:53:58 -08003328 BUG_ON(bytes < (DEVCONF_MAX * 4));
3329
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 memset(array, 0, bytes);
3331 array[DEVCONF_FORWARDING] = cnf->forwarding;
3332 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3333 array[DEVCONF_MTU6] = cnf->mtu6;
3334 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3335 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3336 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3337 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3338 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3339 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3340 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3341 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3342#ifdef CONFIG_IPV6_PRIVACY
3343 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3344 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3345 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3346 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3347 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3348#endif
3349 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003350 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003351 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003352#ifdef CONFIG_IPV6_ROUTER_PREF
3353 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08003354 array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval;
Neil Hormanfa03ef32007-01-30 14:30:10 -08003355#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08003356 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
3357#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003358#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07003359 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360}
3361
Thomas Graf339bf982006-11-10 14:10:15 -08003362static inline size_t inet6_if_nlmsg_size(void)
3363{
3364 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3365 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3366 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3367 + nla_total_size(4) /* IFLA_MTU */
3368 + nla_total_size(4) /* IFLA_LINK */
3369 + nla_total_size( /* IFLA_PROTINFO */
3370 nla_total_size(4) /* IFLA_INET6_FLAGS */
3371 + nla_total_size(sizeof(struct ifla_cacheinfo))
3372 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
3373 );
3374}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003375
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003376static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003377 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378{
Thomas Graf04561c12006-11-14 19:53:58 -08003379 struct net_device *dev = idev->dev;
3380 struct nlattr *conf;
3381 struct ifinfomsg *hdr;
3382 struct nlmsghdr *nlh;
3383 void *protoinfo;
3384 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
Thomas Graf04561c12006-11-14 19:53:58 -08003386 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
3387 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003388 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
Thomas Graf04561c12006-11-14 19:53:58 -08003390 hdr = nlmsg_data(nlh);
3391 hdr->ifi_family = AF_INET6;
3392 hdr->__ifi_pad = 0;
3393 hdr->ifi_type = dev->type;
3394 hdr->ifi_index = dev->ifindex;
3395 hdr->ifi_flags = dev_get_flags(dev);
3396 hdr->ifi_change = 0;
3397
3398 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
3400 if (dev->addr_len)
Thomas Graf04561c12006-11-14 19:53:58 -08003401 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402
Thomas Graf04561c12006-11-14 19:53:58 -08003403 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 if (dev->ifindex != dev->iflink)
Thomas Graf04561c12006-11-14 19:53:58 -08003405 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
Thomas Graf04561c12006-11-14 19:53:58 -08003407 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
3408 if (protoinfo == NULL)
3409 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Thomas Graf04561c12006-11-14 19:53:58 -08003411 NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 ci.max_reasm_len = IPV6_MAXPLEN;
3414 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3415 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3416 ci.reachable_time = idev->nd_parms->reachable_time;
3417 ci.retrans_time = idev->nd_parms->retrans_time;
Thomas Graf04561c12006-11-14 19:53:58 -08003418 NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3419
3420 conf = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
3421 if (conf == NULL)
3422 goto nla_put_failure;
3423 ipv6_store_devconf(&idev->cnf, nla_data(conf), nla_len(conf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
3425 /* XXX - Statistics/MC not implemented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
Thomas Graf04561c12006-11-14 19:53:58 -08003427 nla_nest_end(skb, protoinfo);
3428 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429
Thomas Graf04561c12006-11-14 19:53:58 -08003430nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08003431 nlmsg_cancel(skb, nlh);
3432 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433}
3434
3435static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3436{
3437 int idx, err;
3438 int s_idx = cb->args[0];
3439 struct net_device *dev;
3440 struct inet6_dev *idev;
3441
3442 read_lock(&dev_base_lock);
3443 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
3444 if (idx < s_idx)
3445 continue;
3446 if ((idev = in6_dev_get(dev)) == NULL)
3447 continue;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003448 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003449 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 in6_dev_put(idev);
3451 if (err <= 0)
3452 break;
3453 }
3454 read_unlock(&dev_base_lock);
3455 cb->args[0] = idx;
3456
3457 return skb->len;
3458}
3459
3460void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3461{
3462 struct sk_buff *skb;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003463 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003464
Thomas Graf339bf982006-11-10 14:10:15 -08003465 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003466 if (skb == NULL)
3467 goto errout;
3468
3469 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003470 if (err < 0) {
3471 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
3472 WARN_ON(err == -EMSGSIZE);
3473 kfree_skb(skb);
3474 goto errout;
3475 }
Thomas Graf8d7a76c2006-08-15 00:35:47 -07003476 err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3477errout:
3478 if (err < 0)
3479 rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480}
3481
Thomas Graf339bf982006-11-10 14:10:15 -08003482static inline size_t inet6_prefix_nlmsg_size(void)
3483{
3484 return NLMSG_ALIGN(sizeof(struct prefixmsg))
3485 + nla_total_size(sizeof(struct in6_addr))
3486 + nla_total_size(sizeof(struct prefix_cacheinfo));
3487}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003488
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Thomas Graf6051e2f2006-11-14 19:54:19 -08003490 struct prefix_info *pinfo, u32 pid, u32 seq,
3491 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492{
Thomas Graf6051e2f2006-11-14 19:54:19 -08003493 struct prefixmsg *pmsg;
3494 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 struct prefix_cacheinfo ci;
3496
Thomas Graf6051e2f2006-11-14 19:54:19 -08003497 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
3498 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003499 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08003500
3501 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07003503 pmsg->prefix_pad1 = 0;
3504 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 pmsg->prefix_ifindex = idev->dev->ifindex;
3506 pmsg->prefix_len = pinfo->prefix_len;
3507 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07003508 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 pmsg->prefix_flags = 0;
3510 if (pinfo->onlink)
3511 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3512 if (pinfo->autoconf)
3513 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3514
Thomas Graf6051e2f2006-11-14 19:54:19 -08003515 NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
3517 ci.preferred_time = ntohl(pinfo->prefered);
3518 ci.valid_time = ntohl(pinfo->valid);
Thomas Graf6051e2f2006-11-14 19:54:19 -08003519 NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520
Thomas Graf6051e2f2006-11-14 19:54:19 -08003521 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
Thomas Graf6051e2f2006-11-14 19:54:19 -08003523nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08003524 nlmsg_cancel(skb, nlh);
3525 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526}
3527
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003528static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 struct prefix_info *pinfo)
3530{
3531 struct sk_buff *skb;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003532 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
Thomas Graf339bf982006-11-10 14:10:15 -08003534 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003535 if (skb == NULL)
3536 goto errout;
3537
3538 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003539 if (err < 0) {
3540 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
3541 WARN_ON(err == -EMSGSIZE);
3542 kfree_skb(skb);
3543 goto errout;
3544 }
Thomas Graf8c384bfa2006-08-15 00:36:07 -07003545 err = rtnl_notify(skb, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
3546errout:
3547 if (err < 0)
3548 rtnl_set_sk_err(RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549}
3550
Thomas Grafdb46edc2005-05-03 14:29:39 -07003551static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, },
3553 [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, },
3554 [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, },
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003555 [RTM_GETADDR - RTM_BASE] = { .doit = inet6_rtm_getaddr,
3556 .dumpit = inet6_dump_ifaddr, },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
3558 [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
3559 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, },
3560 [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, },
3561 [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute,
3562 .dumpit = inet6_dump_fib, },
David S. Miller8423a9a2006-08-07 21:54:37 -07003563#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Thomas Graf101367c2006-08-04 03:39:02 -07003564 [RTM_GETRULE - RTM_BASE] = { .dumpit = fib6_rules_dump, },
David S. Miller8423a9a2006-08-07 21:54:37 -07003565#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566};
3567
3568static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3569{
3570 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3571
3572 switch (event) {
3573 case RTM_NEWADDR:
Thomas Graf40e22e82006-08-22 00:00:45 -07003574 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 if (ifp->idev->cnf.forwarding)
3576 addrconf_join_anycast(ifp);
3577 break;
3578 case RTM_DELADDR:
3579 if (ifp->idev->cnf.forwarding)
3580 addrconf_leave_anycast(ifp);
3581 addrconf_leave_solict(ifp->idev, &ifp->addr);
3582 dst_hold(&ifp->rt->u.dst);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003583 if (ip6_del_rt(ifp->rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 dst_free(&ifp->rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 break;
3586 }
3587}
3588
3589static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3590{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003591 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 if (likely(ifp->idev->dead == 0))
3593 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003594 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595}
3596
3597#ifdef CONFIG_SYSCTL
3598
3599static
3600int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3601 void __user *buffer, size_t *lenp, loff_t *ppos)
3602{
3603 int *valp = ctl->data;
3604 int val = *valp;
3605 int ret;
3606
3607 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3608
3609 if (write && valp != &ipv6_devconf_dflt.forwarding) {
3610 if (valp != &ipv6_devconf.forwarding) {
3611 if ((!*valp) ^ (!val)) {
3612 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
3613 if (idev == NULL)
3614 return ret;
3615 dev_forward_change(idev);
3616 }
3617 } else {
3618 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3619 addrconf_forward_change();
3620 }
3621 if (*valp)
3622 rt6_purge_dflt_routers();
3623 }
3624
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003625 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626}
3627
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003628static int addrconf_sysctl_forward_strategy(ctl_table *table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 int __user *name, int nlen,
3630 void __user *oldval,
3631 size_t __user *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -08003632 void __user *newval, size_t newlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633{
3634 int *valp = table->data;
3635 int new;
3636
3637 if (!newval || !newlen)
3638 return 0;
3639 if (newlen != sizeof(int))
3640 return -EINVAL;
3641 if (get_user(new, (int __user *)newval))
3642 return -EFAULT;
3643 if (new == *valp)
3644 return 0;
3645 if (oldval && oldlenp) {
3646 size_t len;
3647 if (get_user(len, oldlenp))
3648 return -EFAULT;
3649 if (len) {
3650 if (len > table->maxlen)
3651 len = table->maxlen;
3652 if (copy_to_user(oldval, valp, len))
3653 return -EFAULT;
3654 if (put_user(len, oldlenp))
3655 return -EFAULT;
3656 }
3657 }
3658
3659 if (valp != &ipv6_devconf_dflt.forwarding) {
3660 if (valp != &ipv6_devconf.forwarding) {
3661 struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
3662 int changed;
3663 if (unlikely(idev == NULL))
3664 return -ENODEV;
3665 changed = (!*valp) ^ (!new);
3666 *valp = new;
3667 if (changed)
3668 dev_forward_change(idev);
3669 } else {
3670 *valp = new;
3671 addrconf_forward_change();
3672 }
3673
3674 if (*valp)
3675 rt6_purge_dflt_routers();
3676 } else
3677 *valp = new;
3678
3679 return 1;
3680}
3681
3682static struct addrconf_sysctl_table
3683{
3684 struct ctl_table_header *sysctl_header;
3685 ctl_table addrconf_vars[__NET_IPV6_MAX];
3686 ctl_table addrconf_dev[2];
3687 ctl_table addrconf_conf_dir[2];
3688 ctl_table addrconf_proto_dir[2];
3689 ctl_table addrconf_root_dir[2];
Brian Haleyab32ea52006-09-22 14:15:41 -07003690} addrconf_sysctl __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 .sysctl_header = NULL,
3692 .addrconf_vars = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003693 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 .ctl_name = NET_IPV6_FORWARDING,
3695 .procname = "forwarding",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003696 .data = &ipv6_devconf.forwarding,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 .maxlen = sizeof(int),
3698 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003699 .proc_handler = &addrconf_sysctl_forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 .strategy = &addrconf_sysctl_forward_strategy,
3701 },
3702 {
3703 .ctl_name = NET_IPV6_HOP_LIMIT,
3704 .procname = "hop_limit",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003705 .data = &ipv6_devconf.hop_limit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 .maxlen = sizeof(int),
3707 .mode = 0644,
3708 .proc_handler = proc_dointvec,
3709 },
3710 {
3711 .ctl_name = NET_IPV6_MTU,
3712 .procname = "mtu",
3713 .data = &ipv6_devconf.mtu6,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003714 .maxlen = sizeof(int),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003716 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 },
3718 {
3719 .ctl_name = NET_IPV6_ACCEPT_RA,
3720 .procname = "accept_ra",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003721 .data = &ipv6_devconf.accept_ra,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 .maxlen = sizeof(int),
3723 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003724 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 },
3726 {
3727 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3728 .procname = "accept_redirects",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003729 .data = &ipv6_devconf.accept_redirects,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 .maxlen = sizeof(int),
3731 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003732 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 },
3734 {
3735 .ctl_name = NET_IPV6_AUTOCONF,
3736 .procname = "autoconf",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003737 .data = &ipv6_devconf.autoconf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 .maxlen = sizeof(int),
3739 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003740 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 },
3742 {
3743 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3744 .procname = "dad_transmits",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003745 .data = &ipv6_devconf.dad_transmits,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 .maxlen = sizeof(int),
3747 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003748 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 },
3750 {
3751 .ctl_name = NET_IPV6_RTR_SOLICITS,
3752 .procname = "router_solicitations",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003753 .data = &ipv6_devconf.rtr_solicits,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 .maxlen = sizeof(int),
3755 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003756 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 },
3758 {
3759 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3760 .procname = "router_solicitation_interval",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003761 .data = &ipv6_devconf.rtr_solicit_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 .maxlen = sizeof(int),
3763 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003764 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 .strategy = &sysctl_jiffies,
3766 },
3767 {
3768 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3769 .procname = "router_solicitation_delay",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003770 .data = &ipv6_devconf.rtr_solicit_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 .maxlen = sizeof(int),
3772 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003773 .proc_handler = &proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 .strategy = &sysctl_jiffies,
3775 },
3776 {
3777 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3778 .procname = "force_mld_version",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003779 .data = &ipv6_devconf.force_mld_version,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 .maxlen = sizeof(int),
3781 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003782 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 },
3784#ifdef CONFIG_IPV6_PRIVACY
3785 {
3786 .ctl_name = NET_IPV6_USE_TEMPADDR,
3787 .procname = "use_tempaddr",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003788 .data = &ipv6_devconf.use_tempaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 .maxlen = sizeof(int),
3790 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003791 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 },
3793 {
3794 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3795 .procname = "temp_valid_lft",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003796 .data = &ipv6_devconf.temp_valid_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 .maxlen = sizeof(int),
3798 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003799 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 },
3801 {
3802 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3803 .procname = "temp_prefered_lft",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003804 .data = &ipv6_devconf.temp_prefered_lft,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 .maxlen = sizeof(int),
3806 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003807 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 },
3809 {
3810 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3811 .procname = "regen_max_retry",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003812 .data = &ipv6_devconf.regen_max_retry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 .maxlen = sizeof(int),
3814 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003815 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 },
3817 {
3818 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3819 .procname = "max_desync_factor",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003820 .data = &ipv6_devconf.max_desync_factor,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 .maxlen = sizeof(int),
3822 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003823 .proc_handler = &proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 },
3825#endif
3826 {
3827 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3828 .procname = "max_addresses",
3829 .data = &ipv6_devconf.max_addresses,
3830 .maxlen = sizeof(int),
3831 .mode = 0644,
3832 .proc_handler = &proc_dointvec,
3833 },
3834 {
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003835 .ctl_name = NET_IPV6_ACCEPT_RA_DEFRTR,
3836 .procname = "accept_ra_defrtr",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003837 .data = &ipv6_devconf.accept_ra_defrtr,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003838 .maxlen = sizeof(int),
3839 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003840 .proc_handler = &proc_dointvec,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003841 },
3842 {
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003843 .ctl_name = NET_IPV6_ACCEPT_RA_PINFO,
3844 .procname = "accept_ra_pinfo",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003845 .data = &ipv6_devconf.accept_ra_pinfo,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003846 .maxlen = sizeof(int),
3847 .mode = 0644,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003848 .proc_handler = &proc_dointvec,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003849 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003850#ifdef CONFIG_IPV6_ROUTER_PREF
3851 {
3852 .ctl_name = NET_IPV6_ACCEPT_RA_RTR_PREF,
3853 .procname = "accept_ra_rtr_pref",
3854 .data = &ipv6_devconf.accept_ra_rtr_pref,
3855 .maxlen = sizeof(int),
3856 .mode = 0644,
3857 .proc_handler = &proc_dointvec,
3858 },
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08003859 {
3860 .ctl_name = NET_IPV6_RTR_PROBE_INTERVAL,
3861 .procname = "router_probe_interval",
3862 .data = &ipv6_devconf.rtr_probe_interval,
3863 .maxlen = sizeof(int),
3864 .mode = 0644,
3865 .proc_handler = &proc_dointvec_jiffies,
3866 .strategy = &sysctl_jiffies,
3867 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08003868#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08003869 {
3870 .ctl_name = NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN,
3871 .procname = "accept_ra_rt_info_max_plen",
3872 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
3873 .maxlen = sizeof(int),
3874 .mode = 0644,
3875 .proc_handler = &proc_dointvec,
3876 },
3877#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003878#endif
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003879 {
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07003880 .ctl_name = NET_IPV6_PROXY_NDP,
3881 .procname = "proxy_ndp",
3882 .data = &ipv6_devconf.proxy_ndp,
3883 .maxlen = sizeof(int),
3884 .mode = 0644,
3885 .proc_handler = &proc_dointvec,
3886 },
3887 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 .ctl_name = 0, /* sentinel */
3889 }
3890 },
3891 .addrconf_dev = {
3892 {
3893 .ctl_name = NET_PROTO_CONF_ALL,
3894 .procname = "all",
3895 .mode = 0555,
3896 .child = addrconf_sysctl.addrconf_vars,
3897 },
3898 {
3899 .ctl_name = 0, /* sentinel */
3900 }
3901 },
3902 .addrconf_conf_dir = {
3903 {
3904 .ctl_name = NET_IPV6_CONF,
3905 .procname = "conf",
3906 .mode = 0555,
3907 .child = addrconf_sysctl.addrconf_dev,
3908 },
3909 {
3910 .ctl_name = 0, /* sentinel */
3911 }
3912 },
3913 .addrconf_proto_dir = {
3914 {
3915 .ctl_name = NET_IPV6,
3916 .procname = "ipv6",
3917 .mode = 0555,
3918 .child = addrconf_sysctl.addrconf_conf_dir,
3919 },
3920 {
3921 .ctl_name = 0, /* sentinel */
3922 }
3923 },
3924 .addrconf_root_dir = {
3925 {
3926 .ctl_name = CTL_NET,
3927 .procname = "net",
3928 .mode = 0555,
3929 .child = addrconf_sysctl.addrconf_proto_dir,
3930 },
3931 {
3932 .ctl_name = 0, /* sentinel */
3933 }
3934 },
3935};
3936
3937static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
3938{
3939 int i;
3940 struct net_device *dev = idev ? idev->dev : NULL;
3941 struct addrconf_sysctl_table *t;
3942 char *dev_name = NULL;
3943
Arnaldo Carvalho de Meloaf879cc2006-11-17 12:14:37 -02003944 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 if (t == NULL)
3946 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 for (i=0; t->addrconf_vars[i].data; i++) {
3948 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
3950 }
3951 if (dev) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003952 dev_name = dev->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 t->addrconf_dev[0].ctl_name = dev->ifindex;
3954 } else {
3955 dev_name = "default";
3956 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
3957 }
3958
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003959 /*
3960 * Make a copy of dev_name, because '.procname' is regarded as const
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 * by sysctl and we wouldn't want anyone to change it under our feet
3962 * (see SIOCSIFNAME).
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003963 */
Paulo Marques543537b2005-06-23 00:09:02 -07003964 dev_name = kstrdup(dev_name, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 if (!dev_name)
3966 goto free;
3967
3968 t->addrconf_dev[0].procname = dev_name;
3969
3970 t->addrconf_dev[0].child = t->addrconf_vars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 t->addrconf_conf_dir[0].child = t->addrconf_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08003975 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 if (t->sysctl_header == NULL)
3977 goto free_procname;
3978 else
3979 p->sysctl = t;
3980 return;
3981
3982 /* error path */
3983 free_procname:
3984 kfree(dev_name);
3985 free:
3986 kfree(t);
3987
3988 return;
3989}
3990
3991static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
3992{
3993 if (p->sysctl) {
3994 struct addrconf_sysctl_table *t = p->sysctl;
3995 p->sysctl = NULL;
3996 unregister_sysctl_table(t->sysctl_header);
3997 kfree(t->addrconf_dev[0].procname);
3998 kfree(t);
3999 }
4000}
4001
4002
4003#endif
4004
4005/*
4006 * Device notifier
4007 */
4008
4009int register_inet6addr_notifier(struct notifier_block *nb)
4010{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004011 return atomic_notifier_chain_register(&inet6addr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012}
4013
4014int unregister_inet6addr_notifier(struct notifier_block *nb)
4015{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004016 return atomic_notifier_chain_unregister(&inet6addr_chain,nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017}
4018
4019/*
4020 * Init / cleanup code
4021 */
4022
4023int __init addrconf_init(void)
4024{
4025 int err = 0;
4026
4027 /* The addrconf netdev notifier requires that loopback_dev
4028 * has it's ipv6 private information allocated and setup
4029 * before it can bring up and give link-local addresses
4030 * to other devices which are up.
4031 *
4032 * Unfortunately, loopback_dev is not necessarily the first
4033 * entry in the global dev_base list of net devices. In fact,
4034 * it is likely to be the very last entry on that list.
4035 * So this causes the notifier registry below to try and
4036 * give link-local addresses to all devices besides loopback_dev
4037 * first, then loopback_dev, which cases all the non-loopback_dev
4038 * devices to fail to get a link-local address.
4039 *
4040 * So, as a temporary fix, allocate the ipv6 structure for
4041 * loopback_dev first by hand.
4042 * Longer term, all of the dependencies ipv6 has upon the loopback
4043 * device and it being up should be removed.
4044 */
4045 rtnl_lock();
4046 if (!ipv6_add_dev(&loopback_dev))
4047 err = -ENOMEM;
4048 rtnl_unlock();
4049 if (err)
4050 return err;
4051
Herbert Xuc62dba92005-09-26 15:10:16 -07004052 ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
4053
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 register_netdevice_notifier(&ipv6_dev_notf);
4055
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 addrconf_verify(0);
4057 rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
4058#ifdef CONFIG_SYSCTL
4059 addrconf_sysctl.sysctl_header =
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08004060 register_sysctl_table(addrconf_sysctl.addrconf_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
4062#endif
4063
4064 return 0;
4065}
4066
4067void __exit addrconf_cleanup(void)
4068{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004069 struct net_device *dev;
4070 struct inet6_dev *idev;
4071 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 int i;
4073
4074 unregister_netdevice_notifier(&ipv6_dev_notf);
4075
4076 rtnetlink_links[PF_INET6] = NULL;
4077#ifdef CONFIG_SYSCTL
4078 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
4079 addrconf_sysctl_unregister(&ipv6_devconf);
4080#endif
4081
4082 rtnl_lock();
4083
4084 /*
4085 * clean dev list.
4086 */
4087
4088 for (dev=dev_base; dev; dev=dev->next) {
4089 if ((idev = __in6_dev_get(dev)) == NULL)
4090 continue;
4091 addrconf_ifdown(dev, 1);
4092 }
4093 addrconf_ifdown(&loopback_dev, 2);
4094
4095 /*
4096 * Check hash table.
4097 */
4098
4099 write_lock_bh(&addrconf_hash_lock);
4100 for (i=0; i < IN6_ADDR_HSIZE; i++) {
4101 for (ifa=inet6_addr_lst[i]; ifa; ) {
4102 struct inet6_ifaddr *bifa;
4103
4104 bifa = ifa;
4105 ifa = ifa->lst_next;
4106 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
4107 /* Do not free it; something is wrong.
4108 Now we can investigate it with debugger.
4109 */
4110 }
4111 }
4112 write_unlock_bh(&addrconf_hash_lock);
4113
4114 del_timer(&addr_chk_timer);
4115
4116 rtnl_unlock();
4117
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118#ifdef CONFIG_PROC_FS
4119 proc_net_remove("if_inet6");
4120#endif
4121}