addrconf.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:47k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * IPv6 Address [auto]configuration
  3.  * Linux INET6 implementation
  4.  *
  5.  * Authors:
  6.  * Pedro Roque <roque@di.fc.ul.pt>
  7.  * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  8.  *
  9.  * $Id: addrconf.c,v 1.69 2001/10/31 21:55:54 davem Exp $
  10.  *
  11.  * This program is free software; you can redistribute it and/or
  12.  *      modify it under the terms of the GNU General Public License
  13.  *      as published by the Free Software Foundation; either version
  14.  *      2 of the License, or (at your option) any later version.
  15.  */
  16. /*
  17.  * Changes:
  18.  *
  19.  * Janos Farkas : delete timer on ifdown
  20.  * <chexum@bankinf.banki.hu>
  21.  * Andi Kleen : kill doube kfree on module
  22.  * unload.
  23.  * Maciej W. Rozycki : FDDI support
  24.  * sekiya@USAGI : Don't send too many RS
  25.  * packets.
  26.  * yoshfuji@USAGI :       Fixed interval between DAD
  27.  * packets.
  28.  * YOSHIFUJI Hideaki @USAGI : improved accuracy of
  29.  * address validation timer.
  30.  */
  31. #include <linux/config.h>
  32. #include <linux/errno.h>
  33. #include <linux/types.h>
  34. #include <linux/socket.h>
  35. #include <linux/sockios.h>
  36. #include <linux/sched.h>
  37. #include <linux/net.h>
  38. #include <linux/in6.h>
  39. #include <linux/netdevice.h>
  40. #include <linux/if_arp.h>
  41. #include <linux/route.h>
  42. #include <linux/inetdevice.h>
  43. #include <linux/init.h>
  44. #ifdef CONFIG_SYSCTL
  45. #include <linux/sysctl.h>
  46. #endif
  47. #include <linux/delay.h>
  48. #include <linux/notifier.h>
  49. #include <linux/proc_fs.h>
  50. #include <net/sock.h>
  51. #include <net/snmp.h>
  52. #include <net/ipv6.h>
  53. #include <net/protocol.h>
  54. #include <net/ndisc.h>
  55. #include <net/ip6_route.h>
  56. #include <net/addrconf.h>
  57. #include <net/ip.h>
  58. #include <linux/if_tunnel.h>
  59. #include <linux/rtnetlink.h>
  60. #include <asm/uaccess.h>
  61. #define IPV6_MAX_ADDRESSES 16
  62. /* Set to 3 to get tracing... */
  63. #define ACONF_DEBUG 2
  64. #if ACONF_DEBUG >= 3
  65. #define ADBG(x) printk x
  66. #else
  67. #define ADBG(x)
  68. #endif
  69. #ifdef CONFIG_SYSCTL
  70. static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
  71. static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
  72. #endif
  73. int inet6_dev_count;
  74. int inet6_ifa_count;
  75. /*
  76.  * Configured unicast address hash table
  77.  */
  78. static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];
  79. static rwlock_t addrconf_hash_lock = RW_LOCK_UNLOCKED;
  80. /* Protects inet6 devices */
  81. rwlock_t addrconf_lock = RW_LOCK_UNLOCKED;
  82. void addrconf_verify(unsigned long);
  83. static struct timer_list addr_chk_timer = { function: addrconf_verify };
  84. static spinlock_t addrconf_verify_lock = SPIN_LOCK_UNLOCKED;
  85. static int addrconf_ifdown(struct net_device *dev, int how);
  86. static void addrconf_dad_start(struct inet6_ifaddr *ifp);
  87. static void addrconf_dad_timer(unsigned long data);
  88. static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
  89. static void addrconf_rs_timer(unsigned long data);
  90. static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
  91. static struct notifier_block *inet6addr_chain;
  92. struct ipv6_devconf ipv6_devconf =
  93. {
  94. 0, /* forwarding */
  95. IPV6_DEFAULT_HOPLIMIT, /* hop limit */
  96. IPV6_MIN_MTU, /* mtu */
  97. 1, /* accept RAs */
  98. 1, /* accept redirects */
  99. 1, /* autoconfiguration */
  100. 1, /* dad transmits */
  101. MAX_RTR_SOLICITATIONS, /* router solicits */
  102. RTR_SOLICITATION_INTERVAL, /* rtr solicit interval */
  103. MAX_RTR_SOLICITATION_DELAY, /* rtr solicit delay */
  104. };
  105. static struct ipv6_devconf ipv6_devconf_dflt =
  106. {
  107. 0, /* forwarding */
  108. IPV6_DEFAULT_HOPLIMIT, /* hop limit */
  109. IPV6_MIN_MTU, /* mtu */
  110. 1, /* accept RAs */
  111. 1, /* accept redirects */
  112. 1, /* autoconfiguration */
  113. 1, /* dad transmits */
  114. MAX_RTR_SOLICITATIONS, /* router solicits */
  115. RTR_SOLICITATION_INTERVAL, /* rtr solicit interval */
  116. MAX_RTR_SOLICITATION_DELAY, /* rtr solicit delay */
  117. };
  118. int ipv6_addr_type(struct in6_addr *addr)
  119. {
  120. u32 st;
  121. st = addr->s6_addr32[0];
  122. /* Consider all addresses with the first three bits different of
  123.    000 and 111 as unicasts.
  124.  */
  125. if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
  126.     (st & htonl(0xE0000000)) != htonl(0xE0000000))
  127. return IPV6_ADDR_UNICAST;
  128. if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
  129. int type = IPV6_ADDR_MULTICAST;
  130. switch((st & htonl(0x00FF0000))) {
  131. case __constant_htonl(0x00010000):
  132. type |= IPV6_ADDR_LOOPBACK;
  133. break;
  134. case __constant_htonl(0x00020000):
  135. type |= IPV6_ADDR_LINKLOCAL;
  136. break;
  137. case __constant_htonl(0x00050000):
  138. type |= IPV6_ADDR_SITELOCAL;
  139. break;
  140. };
  141. return type;
  142. }
  143. if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
  144. return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST);
  145. if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
  146. return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST);
  147. if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
  148. if (addr->s6_addr32[2] == 0) {
  149. if (addr->s6_addr32[3] == 0)
  150. return IPV6_ADDR_ANY;
  151. if (addr->s6_addr32[3] == htonl(0x00000001))
  152. return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST);
  153. return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST);
  154. }
  155. if (addr->s6_addr32[2] == htonl(0x0000ffff))
  156. return IPV6_ADDR_MAPPED;
  157. }
  158. return IPV6_ADDR_RESERVED;
  159. }
  160. static void addrconf_del_timer(struct inet6_ifaddr *ifp)
  161. {
  162. if (del_timer(&ifp->timer))
  163. __in6_ifa_put(ifp);
  164. }
  165. enum addrconf_timer_t
  166. {
  167. AC_NONE,
  168. AC_DAD,
  169. AC_RS,
  170. };
  171. static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
  172.        enum addrconf_timer_t what,
  173.        unsigned long when)
  174. {
  175. if (!del_timer(&ifp->timer))
  176. in6_ifa_hold(ifp);
  177. switch (what) {
  178. case AC_DAD:
  179. ifp->timer.function = addrconf_dad_timer;
  180. break;
  181. case AC_RS:
  182. ifp->timer.function = addrconf_rs_timer;
  183. break;
  184. default:;
  185. }
  186. ifp->timer.expires = jiffies + when;
  187. add_timer(&ifp->timer);
  188. }
  189. /* Nobody refers to this device, we may destroy it. */
  190. void in6_dev_finish_destroy(struct inet6_dev *idev)
  191. {
  192. struct net_device *dev = idev->dev;
  193. BUG_TRAP(idev->addr_list==NULL);
  194. BUG_TRAP(idev->mc_list==NULL);
  195. #ifdef NET_REFCNT_DEBUG
  196. printk(KERN_DEBUG "in6_dev_finish_destroy: %sn", dev ? dev->name : "NIL");
  197. #endif
  198. dev_put(dev);
  199. if (!idev->dead) {
  200. printk("Freeing alive inet6 device %pn", idev);
  201. return;
  202. }
  203. inet6_dev_count--;
  204. kfree(idev);
  205. }
  206. static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
  207. {
  208. struct inet6_dev *ndev;
  209. ASSERT_RTNL();
  210. if (dev->mtu < IPV6_MIN_MTU)
  211. return NULL;
  212. ndev = kmalloc(sizeof(struct inet6_dev), GFP_KERNEL);
  213. if (ndev) {
  214. memset(ndev, 0, sizeof(struct inet6_dev));
  215. ndev->lock = RW_LOCK_UNLOCKED;
  216. ndev->dev = dev;
  217. memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
  218. ndev->cnf.mtu6 = dev->mtu;
  219. ndev->cnf.sysctl = NULL;
  220. ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
  221. if (ndev->nd_parms == NULL) {
  222. kfree(ndev);
  223. return NULL;
  224. }
  225. inet6_dev_count++;
  226. /* We refer to the device */
  227. dev_hold(dev);
  228. write_lock_bh(&addrconf_lock);
  229. dev->ip6_ptr = ndev;
  230. /* One reference from device */
  231. in6_dev_hold(ndev);
  232. write_unlock_bh(&addrconf_lock);
  233. ipv6_mc_init_dev(ndev);
  234. #ifdef CONFIG_SYSCTL
  235. neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6, NET_IPV6_NEIGH, "ipv6");
  236. addrconf_sysctl_register(ndev, &ndev->cnf);
  237. #endif
  238. }
  239. return ndev;
  240. }
  241. static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
  242. {
  243. struct inet6_dev *idev;
  244. ASSERT_RTNL();
  245. if ((idev = __in6_dev_get(dev)) == NULL) {
  246. if ((idev = ipv6_add_dev(dev)) == NULL)
  247. return NULL;
  248. }
  249. if (dev->flags&IFF_UP)
  250. ipv6_mc_up(idev);
  251. return idev;
  252. }
  253. static void addrconf_forward_change(struct inet6_dev *idev)
  254. {
  255. struct net_device *dev;
  256. if (idev)
  257. return;
  258. read_lock(&dev_base_lock);
  259. for (dev=dev_base; dev; dev=dev->next) {
  260. read_lock(&addrconf_lock);
  261. idev = __in6_dev_get(dev);
  262. if (idev)
  263. idev->cnf.forwarding = ipv6_devconf.forwarding;
  264. read_unlock(&addrconf_lock);
  265. }
  266. read_unlock(&dev_base_lock);
  267. }
  268. /* Nobody refers to this ifaddr, destroy it */
  269. void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
  270. {
  271. BUG_TRAP(ifp->if_next==NULL);
  272. BUG_TRAP(ifp->lst_next==NULL);
  273. #ifdef NET_REFCNT_DEBUG
  274. printk(KERN_DEBUG "inet6_ifa_finish_destroyn");
  275. #endif
  276. in6_dev_put(ifp->idev);
  277. if (del_timer(&ifp->timer))
  278. printk("Timer is still running, when freeing ifa=%pn", ifp);
  279. if (!ifp->dead) {
  280. printk("Freeing alive inet6 address %pn", ifp);
  281. return;
  282. }
  283. inet6_ifa_count--;
  284. kfree(ifp);
  285. }
  286. /* On success it returns ifp with increased reference count */
  287. static struct inet6_ifaddr *
  288. ipv6_add_addr(struct inet6_dev *idev, struct in6_addr *addr, int pfxlen,
  289.       int scope, unsigned flags)
  290. {
  291. struct inet6_ifaddr *ifa;
  292. int hash;
  293. ifa = kmalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
  294. if (ifa == NULL) {
  295. ADBG(("ipv6_add_addr: malloc failedn"));
  296. return NULL;
  297. }
  298. memset(ifa, 0, sizeof(struct inet6_ifaddr));
  299. ipv6_addr_copy(&ifa->addr, addr);
  300. spin_lock_init(&ifa->lock);
  301. init_timer(&ifa->timer);
  302. ifa->timer.data = (unsigned long) ifa;
  303. ifa->scope = scope;
  304. ifa->prefix_len = pfxlen;
  305. ifa->flags = flags | IFA_F_TENTATIVE;
  306. read_lock(&addrconf_lock);
  307. if (idev->dead) {
  308. read_unlock(&addrconf_lock);
  309. kfree(ifa);
  310. return NULL;
  311. }
  312. inet6_ifa_count++;
  313. ifa->idev = idev;
  314. in6_dev_hold(idev);
  315. /* For caller */
  316. in6_ifa_hold(ifa);
  317. /* Add to big hash table */
  318. hash = ipv6_addr_hash(addr);
  319. write_lock_bh(&addrconf_hash_lock);
  320. ifa->lst_next = inet6_addr_lst[hash];
  321. inet6_addr_lst[hash] = ifa;
  322. in6_ifa_hold(ifa);
  323. write_unlock_bh(&addrconf_hash_lock);
  324. write_lock_bh(&idev->lock);
  325. /* Add to inet6_dev unicast addr list. */
  326. ifa->if_next = idev->addr_list;
  327. idev->addr_list = ifa;
  328. in6_ifa_hold(ifa);
  329. write_unlock_bh(&idev->lock);
  330. read_unlock(&addrconf_lock);
  331. notifier_call_chain(&inet6addr_chain,NETDEV_UP,ifa);
  332. return ifa;
  333. }
  334. /* This function wants to get referenced ifp and releases it before return */
  335. static void ipv6_del_addr(struct inet6_ifaddr *ifp)
  336. {
  337. struct inet6_ifaddr *ifa, **ifap;
  338. struct inet6_dev *idev = ifp->idev;
  339. int hash;
  340. hash = ipv6_addr_hash(&ifp->addr);
  341. ifp->dead = 1;
  342. write_lock_bh(&addrconf_hash_lock);
  343. for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
  344.      ifap = &ifa->lst_next) {
  345. if (ifa == ifp) {
  346. *ifap = ifa->lst_next;
  347. __in6_ifa_put(ifp);
  348. ifa->lst_next = NULL;
  349. break;
  350. }
  351. }
  352. write_unlock_bh(&addrconf_hash_lock);
  353. write_lock_bh(&idev->lock);
  354. for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;
  355.      ifap = &ifa->if_next) {
  356. if (ifa == ifp) {
  357. *ifap = ifa->if_next;
  358. __in6_ifa_put(ifp);
  359. ifa->if_next = NULL;
  360. break;
  361. }
  362. }
  363. write_unlock_bh(&idev->lock);
  364. ipv6_ifa_notify(RTM_DELADDR, ifp);
  365. notifier_call_chain(&inet6addr_chain,NETDEV_DOWN,ifp);
  366. addrconf_del_timer(ifp);
  367. in6_ifa_put(ifp);
  368. }
  369. /*
  370.  * Choose an apropriate source address
  371.  * should do:
  372.  * i) get an address with an apropriate scope
  373.  * ii) see if there is a specific route for the destination and use
  374.  * an address of the attached interface 
  375.  * iii) don't use deprecated addresses
  376.  */
  377. int ipv6_get_saddr(struct dst_entry *dst,
  378.    struct in6_addr *daddr, struct in6_addr *saddr)
  379. {
  380. int scope;
  381. struct inet6_ifaddr *ifp = NULL;
  382. struct inet6_ifaddr *match = NULL;
  383. struct net_device *dev = NULL;
  384. struct inet6_dev *idev;
  385. struct rt6_info *rt;
  386. int err;
  387. rt = (struct rt6_info *) dst;
  388. if (rt)
  389. dev = rt->rt6i_dev;
  390. scope = ipv6_addr_scope(daddr);
  391. if (rt && (rt->rt6i_flags & RTF_ALLONLINK)) {
  392. /*
  393.  * route for the "all destinations on link" rule
  394.  * when no routers are present
  395.  */
  396. scope = IFA_LINK;
  397. }
  398. /*
  399.  * known dev
  400.  * search dev and walk through dev addresses
  401.  */
  402. if (dev) {
  403. if (dev->flags & IFF_LOOPBACK)
  404. scope = IFA_HOST;
  405. read_lock(&addrconf_lock);
  406. idev = __in6_dev_get(dev);
  407. if (idev) {
  408. read_lock_bh(&idev->lock);
  409. for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
  410. if (ifp->scope == scope) {
  411. if (!(ifp->flags & (IFA_F_DEPRECATED|IFA_F_TENTATIVE))) {
  412. in6_ifa_hold(ifp);
  413. read_unlock_bh(&idev->lock);
  414. read_unlock(&addrconf_lock);
  415. goto out;
  416. }
  417. if (!match && !(ifp->flags & IFA_F_TENTATIVE)) {
  418. match = ifp;
  419. in6_ifa_hold(ifp);
  420. }
  421. }
  422. }
  423. read_unlock_bh(&idev->lock);
  424. }
  425. read_unlock(&addrconf_lock);
  426. }
  427. if (scope == IFA_LINK)
  428. goto out;
  429. /*
  430.  * dev == NULL or search failed for specified dev
  431.  */
  432. read_lock(&dev_base_lock);
  433. read_lock(&addrconf_lock);
  434. for (dev = dev_base; dev; dev=dev->next) {
  435. idev = __in6_dev_get(dev);
  436. if (idev) {
  437. read_lock_bh(&idev->lock);
  438. for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
  439. if (ifp->scope == scope) {
  440. if (!(ifp->flags&(IFA_F_DEPRECATED|IFA_F_TENTATIVE))) {
  441. in6_ifa_hold(ifp);
  442. read_unlock_bh(&idev->lock);
  443. goto out_unlock_base;
  444. }
  445. if (!match && !(ifp->flags&IFA_F_TENTATIVE)) {
  446. match = ifp;
  447. in6_ifa_hold(ifp);
  448. }
  449. }
  450. }
  451. read_unlock_bh(&idev->lock);
  452. }
  453. }
  454. out_unlock_base:
  455. read_unlock(&addrconf_lock);
  456. read_unlock(&dev_base_lock);
  457. out:
  458. if (ifp == NULL) {
  459. ifp = match;
  460. match = NULL;
  461. }
  462. err = -EADDRNOTAVAIL;
  463. if (ifp) {
  464. ipv6_addr_copy(saddr, &ifp->addr);
  465. err = 0;
  466. in6_ifa_put(ifp);
  467. }
  468. if (match)
  469. in6_ifa_put(match);
  470. return err;
  471. }
  472. int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
  473. {
  474. struct inet6_dev *idev;
  475. int err = -EADDRNOTAVAIL;
  476. read_lock(&addrconf_lock);
  477. if ((idev = __in6_dev_get(dev)) != NULL) {
  478. struct inet6_ifaddr *ifp;
  479. read_lock_bh(&idev->lock);
  480. for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
  481. if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
  482. ipv6_addr_copy(addr, &ifp->addr);
  483. err = 0;
  484. break;
  485. }
  486. }
  487. read_unlock_bh(&idev->lock);
  488. }
  489. read_unlock(&addrconf_lock);
  490. return err;
  491. }
  492. int ipv6_count_addresses(struct inet6_dev *idev)
  493. {
  494. int cnt = 0;
  495. struct inet6_ifaddr *ifp;
  496. read_lock_bh(&idev->lock);
  497. for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
  498. cnt++;
  499. read_unlock_bh(&idev->lock);
  500. return cnt;
  501. }
  502. int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev)
  503. {
  504. struct inet6_ifaddr * ifp;
  505. u8 hash = ipv6_addr_hash(addr);
  506. read_lock_bh(&addrconf_hash_lock);
  507. for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
  508. if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
  509.     !(ifp->flags&IFA_F_TENTATIVE)) {
  510. if (dev == NULL || ifp->idev->dev == dev ||
  511.     !(ifp->scope&(IFA_LINK|IFA_HOST)))
  512. break;
  513. }
  514. }
  515. read_unlock_bh(&addrconf_hash_lock);
  516. return ifp != NULL;
  517. }
  518. struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev)
  519. {
  520. struct inet6_ifaddr * ifp;
  521. u8 hash = ipv6_addr_hash(addr);
  522. read_lock_bh(&addrconf_hash_lock);
  523. for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
  524. if (ipv6_addr_cmp(&ifp->addr, addr) == 0) {
  525. if (dev == NULL || ifp->idev->dev == dev ||
  526.     !(ifp->scope&(IFA_LINK|IFA_HOST))) {
  527. in6_ifa_hold(ifp);
  528. break;
  529. }
  530. }
  531. }
  532. read_unlock_bh(&addrconf_hash_lock);
  533. return ifp;
  534. }
  535. /* Gets referenced address, destroys ifaddr */
  536. void addrconf_dad_failure(struct inet6_ifaddr *ifp)
  537. {
  538. if (net_ratelimit())
  539. printk(KERN_INFO "%s: duplicate address detected!n", ifp->idev->dev->name);
  540. if (ifp->flags&IFA_F_PERMANENT) {
  541. spin_lock_bh(&ifp->lock);
  542. addrconf_del_timer(ifp);
  543. ifp->flags |= IFA_F_TENTATIVE;
  544. spin_unlock_bh(&ifp->lock);
  545. in6_ifa_put(ifp);
  546. } else
  547. ipv6_del_addr(ifp);
  548. }
  549. /* Join to solicited addr multicast group. */
  550. static void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
  551. {
  552. struct in6_addr maddr;
  553. if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
  554. return;
  555. addrconf_addr_solict_mult(addr, &maddr);
  556. ipv6_dev_mc_inc(dev, &maddr);
  557. }
  558. static void addrconf_leave_solict(struct net_device *dev, struct in6_addr *addr)
  559. {
  560. struct in6_addr maddr;
  561. if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
  562. return;
  563. addrconf_addr_solict_mult(addr, &maddr);
  564. ipv6_dev_mc_dec(dev, &maddr);
  565. }
  566. static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
  567. {
  568. switch (dev->type) {
  569. case ARPHRD_ETHER:
  570. case ARPHRD_FDDI:
  571. case ARPHRD_IEEE802_TR:
  572. if (dev->addr_len != ETH_ALEN)
  573. return -1;
  574. memcpy(eui, dev->dev_addr, 3);
  575. memcpy(eui + 5, dev->dev_addr+3, 3);
  576. eui[3] = 0xFF;
  577. eui[4] = 0xFE;
  578. eui[0] ^= 2;
  579. return 0;
  580. }
  581. return -1;
  582. }
  583. static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
  584. {
  585. int err = -1;
  586. struct inet6_ifaddr *ifp;
  587. read_lock_bh(&idev->lock);
  588. for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
  589. if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
  590. memcpy(eui, ifp->addr.s6_addr+8, 8);
  591. err = 0;
  592. break;
  593. }
  594. }
  595. read_unlock_bh(&idev->lock);
  596. return err;
  597. }
  598. /*
  599.  * Add prefix route.
  600.  */
  601. static void
  602. addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
  603.       unsigned long expires, unsigned flags)
  604. {
  605. struct in6_rtmsg rtmsg;
  606. memset(&rtmsg, 0, sizeof(rtmsg));
  607. memcpy(&rtmsg.rtmsg_dst, pfx, sizeof(struct in6_addr));
  608. rtmsg.rtmsg_dst_len = plen;
  609. rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
  610. rtmsg.rtmsg_ifindex = dev->ifindex;
  611. rtmsg.rtmsg_info = expires;
  612. rtmsg.rtmsg_flags = RTF_UP|flags;
  613. rtmsg.rtmsg_type = RTMSG_NEWROUTE;
  614. /* Prevent useless cloning on PtP SIT.
  615.    This thing is done here expecting that the whole
  616.    class of non-broadcast devices need not cloning.
  617.  */
  618. if (dev->type == ARPHRD_SIT && (dev->flags&IFF_POINTOPOINT))
  619. rtmsg.rtmsg_flags |= RTF_NONEXTHOP;
  620. ip6_route_add(&rtmsg);
  621. }
  622. /* Create "default" multicast route to the interface */
  623. static void addrconf_add_mroute(struct net_device *dev)
  624. {
  625. struct in6_rtmsg rtmsg;
  626. memset(&rtmsg, 0, sizeof(rtmsg));
  627. ipv6_addr_set(&rtmsg.rtmsg_dst,
  628.       htonl(0xFF000000), 0, 0, 0);
  629. rtmsg.rtmsg_dst_len = 8;
  630. rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
  631. rtmsg.rtmsg_ifindex = dev->ifindex;
  632. rtmsg.rtmsg_flags = RTF_UP|RTF_ADDRCONF;
  633. rtmsg.rtmsg_type = RTMSG_NEWROUTE;
  634. ip6_route_add(&rtmsg);
  635. }
  636. static void sit_route_add(struct net_device *dev)
  637. {
  638. struct in6_rtmsg rtmsg;
  639. memset(&rtmsg, 0, sizeof(rtmsg));
  640. rtmsg.rtmsg_type = RTMSG_NEWROUTE;
  641. rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
  642. /* prefix length - 96 bytes "::d.d.d.d" */
  643. rtmsg.rtmsg_dst_len = 96;
  644. rtmsg.rtmsg_flags = RTF_UP|RTF_NONEXTHOP;
  645. rtmsg.rtmsg_ifindex = dev->ifindex;
  646. ip6_route_add(&rtmsg);
  647. }
  648. static void addrconf_add_lroute(struct net_device *dev)
  649. {
  650. struct in6_addr addr;
  651. ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
  652. addrconf_prefix_route(&addr, 10, dev, 0, RTF_ADDRCONF);
  653. }
  654. static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
  655. {
  656. struct inet6_dev *idev;
  657. ASSERT_RTNL();
  658. if ((idev = ipv6_find_idev(dev)) == NULL)
  659. return NULL;
  660. /* Add default multicast route */
  661. addrconf_add_mroute(dev);
  662. /* Add link local route */
  663. addrconf_add_lroute(dev);
  664. return idev;
  665. }
  666. void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
  667. {
  668. struct prefix_info *pinfo;
  669. struct rt6_info *rt;
  670. __u32 valid_lft;
  671. __u32 prefered_lft;
  672. int addr_type;
  673. unsigned long rt_expires;
  674. struct inet6_dev *in6_dev;
  675. pinfo = (struct prefix_info *) opt;
  676. if (len < sizeof(struct prefix_info)) {
  677. ADBG(("addrconf: prefix option too shortn"));
  678. return;
  679. }
  680. /*
  681.  * Validation checks ([ADDRCONF], page 19)
  682.  */
  683. addr_type = ipv6_addr_type(&pinfo->prefix);
  684. if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
  685. return;
  686. valid_lft = ntohl(pinfo->valid);
  687. prefered_lft = ntohl(pinfo->prefered);
  688. if (prefered_lft > valid_lft) {
  689. if (net_ratelimit())
  690. printk(KERN_WARNING "addrconf: prefix option has invalid lifetimen");
  691. return;
  692. }
  693. in6_dev = in6_dev_get(dev);
  694. if (in6_dev == NULL) {
  695. if (net_ratelimit())
  696. printk(KERN_DEBUG "addrconf: device %s not configuredn", dev->name);
  697. return;
  698. }
  699. /*
  700.  * Two things going on here:
  701.  * 1) Add routes for on-link prefixes
  702.  * 2) Configure prefixes with the auto flag set
  703.  */
  704. /* Avoid arithemtic overflow. Really, we could
  705.    save rt_expires in seconds, likely valid_lft,
  706.    but it would require division in fib gc, that it
  707.    not good.
  708.  */
  709. if (valid_lft >= 0x7FFFFFFF/HZ)
  710. rt_expires = 0;
  711. else
  712. rt_expires = jiffies + valid_lft * HZ;
  713. rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
  714. if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
  715. if (rt->rt6i_flags&RTF_EXPIRES) {
  716. if (pinfo->onlink == 0 || valid_lft == 0) {
  717. ip6_del_rt(rt);
  718. rt = NULL;
  719. } else {
  720. rt->rt6i_expires = rt_expires;
  721. }
  722. }
  723. } else if (pinfo->onlink && valid_lft) {
  724. addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
  725.       dev, rt_expires, RTF_ADDRCONF|RTF_EXPIRES);
  726. }
  727. if (rt)
  728. dst_release(&rt->u.dst);
  729. /* Try to figure out our local address for this prefix */
  730. if (pinfo->autoconf && in6_dev->cnf.autoconf) {
  731. struct inet6_ifaddr * ifp;
  732. struct in6_addr addr;
  733. int plen;
  734. plen = pinfo->prefix_len >> 3;
  735. if (pinfo->prefix_len == 64) {
  736. memcpy(&addr, &pinfo->prefix, 8);
  737. if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
  738.     ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
  739. in6_dev_put(in6_dev);
  740. return;
  741. }
  742. goto ok;
  743. }
  744. if (net_ratelimit())
  745. printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %dn",
  746.        pinfo->prefix_len);
  747. in6_dev_put(in6_dev);
  748. return;
  749. ok:
  750. ifp = ipv6_get_ifaddr(&addr, dev);
  751. if (ifp == NULL && valid_lft) {
  752. /* Do not allow to create too much of autoconfigured
  753.  * addresses; this would be too easy way to crash kernel.
  754.  */
  755. if (ipv6_count_addresses(in6_dev) < IPV6_MAX_ADDRESSES)
  756. ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
  757.     addr_type&IPV6_ADDR_SCOPE_MASK, 0);
  758. if (ifp == NULL) {
  759. in6_dev_put(in6_dev);
  760. return;
  761. }
  762. addrconf_dad_start(ifp);
  763. }
  764. if (ifp && valid_lft == 0) {
  765. ipv6_del_addr(ifp);
  766. ifp = NULL;
  767. }
  768. if (ifp) {
  769. int flags;
  770. spin_lock(&ifp->lock);
  771. ifp->valid_lft = valid_lft;
  772. ifp->prefered_lft = prefered_lft;
  773. ifp->tstamp = jiffies;
  774. flags = ifp->flags;
  775. ifp->flags &= ~IFA_F_DEPRECATED;
  776. spin_unlock(&ifp->lock);
  777. if (!(flags&IFA_F_TENTATIVE))
  778. ipv6_ifa_notify((flags&IFA_F_DEPRECATED) ?
  779. 0 : RTM_NEWADDR, ifp);
  780. in6_ifa_put(ifp);
  781. }
  782. }
  783. in6_dev_put(in6_dev);
  784. }
  785. /*
  786.  * Set destination address.
  787.  * Special case for SIT interfaces where we create a new "virtual"
  788.  * device.
  789.  */
  790. int addrconf_set_dstaddr(void *arg)
  791. {
  792. struct in6_ifreq ireq;
  793. struct net_device *dev;
  794. int err = -EINVAL;
  795. rtnl_lock();
  796. err = -EFAULT;
  797. if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
  798. goto err_exit;
  799. dev = __dev_get_by_index(ireq.ifr6_ifindex);
  800. err = -ENODEV;
  801. if (dev == NULL)
  802. goto err_exit;
  803. if (dev->type == ARPHRD_SIT) {
  804. struct ifreq ifr;
  805. mm_segment_t oldfs;
  806. struct ip_tunnel_parm p;
  807. err = -EADDRNOTAVAIL;
  808. if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
  809. goto err_exit;
  810. memset(&p, 0, sizeof(p));
  811. p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
  812. p.iph.saddr = 0;
  813. p.iph.version = 4;
  814. p.iph.ihl = 5;
  815. p.iph.protocol = IPPROTO_IPV6;
  816. p.iph.ttl = 64;
  817. ifr.ifr_ifru.ifru_data = (void*)&p;
  818. oldfs = get_fs(); set_fs(KERNEL_DS);
  819. err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
  820. set_fs(oldfs);
  821. if (err == 0) {
  822. err = -ENOBUFS;
  823. if ((dev = __dev_get_by_name(p.name)) == NULL)
  824. goto err_exit;
  825. err = dev_open(dev);
  826. }
  827. }
  828. err_exit:
  829. rtnl_unlock();
  830. return err;
  831. }
  832. /*
  833.  * Manual configuration of address on an interface
  834.  */
  835. static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen)
  836. {
  837. struct inet6_ifaddr *ifp;
  838. struct inet6_dev *idev;
  839. struct net_device *dev;
  840. int scope;
  841. ASSERT_RTNL();
  842. if ((dev = __dev_get_by_index(ifindex)) == NULL)
  843. return -ENODEV;
  844. if (!(dev->flags&IFF_UP))
  845. return -ENETDOWN;
  846. if ((idev = addrconf_add_dev(dev)) == NULL)
  847. return -ENOBUFS;
  848. scope = ipv6_addr_scope(pfx);
  849. if ((ifp = ipv6_add_addr(idev, pfx, plen, scope, IFA_F_PERMANENT)) != NULL) {
  850. addrconf_dad_start(ifp);
  851. in6_ifa_put(ifp);
  852. return 0;
  853. }
  854. return -ENOBUFS;
  855. }
  856. static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
  857. {
  858. struct inet6_ifaddr *ifp;
  859. struct inet6_dev *idev;
  860. struct net_device *dev;
  861. if ((dev = __dev_get_by_index(ifindex)) == NULL)
  862. return -ENODEV;
  863. if ((idev = __in6_dev_get(dev)) == NULL)
  864. return -ENXIO;
  865. read_lock_bh(&idev->lock);
  866. for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
  867. if (ifp->prefix_len == plen &&
  868.     (!memcmp(pfx, &ifp->addr, sizeof(struct in6_addr)))) {
  869. in6_ifa_hold(ifp);
  870. read_unlock_bh(&idev->lock);
  871. ipv6_del_addr(ifp);
  872. /* If the last address is deleted administratively,
  873.    disable IPv6 on this interface.
  874.  */
  875. if (idev->addr_list == NULL)
  876. addrconf_ifdown(idev->dev, 1);
  877. return 0;
  878. }
  879. }
  880. read_unlock_bh(&idev->lock);
  881. return -EADDRNOTAVAIL;
  882. }
  883. int addrconf_add_ifaddr(void *arg)
  884. {
  885. struct in6_ifreq ireq;
  886. int err;
  887. if (!capable(CAP_NET_ADMIN))
  888. return -EPERM;
  889. if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
  890. return -EFAULT;
  891. rtnl_lock();
  892. err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
  893. rtnl_unlock();
  894. return err;
  895. }
  896. int addrconf_del_ifaddr(void *arg)
  897. {
  898. struct in6_ifreq ireq;
  899. int err;
  900. if (!capable(CAP_NET_ADMIN))
  901. return -EPERM;
  902. if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
  903. return -EFAULT;
  904. rtnl_lock();
  905. err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
  906. rtnl_unlock();
  907. return err;
  908. }
  909. static void sit_add_v4_addrs(struct inet6_dev *idev)
  910. {
  911. struct inet6_ifaddr * ifp;
  912. struct in6_addr addr;
  913. struct net_device *dev;
  914. int scope;
  915. ASSERT_RTNL();
  916. memset(&addr, 0, sizeof(struct in6_addr));
  917. memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
  918. if (idev->dev->flags&IFF_POINTOPOINT) {
  919. addr.s6_addr32[0] = htonl(0xfe800000);
  920. scope = IFA_LINK;
  921. } else {
  922. scope = IPV6_ADDR_COMPATv4;
  923. }
  924. if (addr.s6_addr32[3]) {
  925. ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
  926. if (ifp) {
  927. spin_lock_bh(&ifp->lock);
  928. ifp->flags &= ~IFA_F_TENTATIVE;
  929. spin_unlock_bh(&ifp->lock);
  930. ipv6_ifa_notify(RTM_NEWADDR, ifp);
  931. in6_ifa_put(ifp);
  932. }
  933. return;
  934. }
  935.         for (dev = dev_base; dev != NULL; dev = dev->next) {
  936. struct in_device * in_dev = __in_dev_get(dev);
  937. if (in_dev && (dev->flags & IFF_UP)) {
  938. struct in_ifaddr * ifa;
  939. int flag = scope;
  940. for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
  941. int plen;
  942. addr.s6_addr32[3] = ifa->ifa_local;
  943. if (ifa->ifa_scope == RT_SCOPE_LINK)
  944. continue;
  945. if (ifa->ifa_scope >= RT_SCOPE_HOST) {
  946. if (idev->dev->flags&IFF_POINTOPOINT)
  947. continue;
  948. flag |= IFA_HOST;
  949. }
  950. if (idev->dev->flags&IFF_POINTOPOINT)
  951. plen = 10;
  952. else
  953. plen = 96;
  954. ifp = ipv6_add_addr(idev, &addr, plen, flag,
  955.     IFA_F_PERMANENT);
  956. if (ifp) {
  957. spin_lock_bh(&ifp->lock);
  958. ifp->flags &= ~IFA_F_TENTATIVE;
  959. spin_unlock_bh(&ifp->lock);
  960. ipv6_ifa_notify(RTM_NEWADDR, ifp);
  961. in6_ifa_put(ifp);
  962. }
  963. }
  964. }
  965.         }
  966. }
  967. static void init_loopback(struct net_device *dev)
  968. {
  969. struct in6_addr addr;
  970. struct inet6_dev  *idev;
  971. struct inet6_ifaddr * ifp;
  972. /* ::1 */
  973. ASSERT_RTNL();
  974. memset(&addr, 0, sizeof(struct in6_addr));
  975. addr.s6_addr32[3] = htonl(0x00000001);
  976. if ((idev = ipv6_find_idev(dev)) == NULL) {
  977. printk(KERN_DEBUG "init loopback: add_dev failedn");
  978. return;
  979. }
  980. ifp = ipv6_add_addr(idev, &addr, 128, IFA_HOST, IFA_F_PERMANENT);
  981. if (ifp) {
  982. spin_lock_bh(&ifp->lock);
  983. ifp->flags &= ~IFA_F_TENTATIVE;
  984. spin_unlock_bh(&ifp->lock);
  985. ipv6_ifa_notify(RTM_NEWADDR, ifp);
  986. in6_ifa_put(ifp);
  987. }
  988. }
  989. static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
  990. {
  991. struct inet6_ifaddr * ifp;
  992. ifp = ipv6_add_addr(idev, addr, 10, IFA_LINK, IFA_F_PERMANENT);
  993. if (ifp) {
  994. addrconf_dad_start(ifp);
  995. in6_ifa_put(ifp);
  996. }
  997. }
  998. static void addrconf_dev_config(struct net_device *dev)
  999. {
  1000. struct in6_addr addr;
  1001. struct inet6_dev    * idev;
  1002. ASSERT_RTNL();
  1003. if ((dev->type != ARPHRD_ETHER) && 
  1004.     (dev->type != ARPHRD_FDDI) &&
  1005.     (dev->type != ARPHRD_IEEE802_TR)) {
  1006. /* Alas, we support only Ethernet autoconfiguration. */
  1007. return;
  1008. }
  1009. idev = addrconf_add_dev(dev);
  1010. if (idev == NULL)
  1011. return;
  1012. memset(&addr, 0, sizeof(struct in6_addr));
  1013. addr.s6_addr32[0] = htonl(0xFE800000);
  1014. if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
  1015. addrconf_add_linklocal(idev, &addr);
  1016. }
  1017. static void addrconf_sit_config(struct net_device *dev)
  1018. {
  1019. struct inet6_dev *idev;
  1020. ASSERT_RTNL();
  1021. /* 
  1022.  * Configure the tunnel with one of our IPv4 
  1023.  * addresses... we should configure all of 
  1024.  * our v4 addrs in the tunnel
  1025.  */
  1026. if ((idev = ipv6_find_idev(dev)) == NULL) {
  1027. printk(KERN_DEBUG "init sit: add_dev failedn");
  1028. return;
  1029. }
  1030. sit_add_v4_addrs(idev);
  1031. if (dev->flags&IFF_POINTOPOINT) {
  1032. addrconf_add_mroute(dev);
  1033. addrconf_add_lroute(dev);
  1034. } else
  1035. sit_route_add(dev);
  1036. }
  1037. int addrconf_notify(struct notifier_block *this, unsigned long event, 
  1038.     void * data)
  1039. {
  1040. struct net_device *dev = (struct net_device *) data;
  1041. struct inet6_dev *idev = __in6_dev_get(dev);
  1042. switch(event) {
  1043. case NETDEV_UP:
  1044. switch(dev->type) {
  1045. case ARPHRD_SIT:
  1046. addrconf_sit_config(dev);
  1047. break;
  1048. case ARPHRD_LOOPBACK:
  1049. init_loopback(dev);
  1050. break;
  1051. default:
  1052. addrconf_dev_config(dev);
  1053. break;
  1054. };
  1055. if (idev) {
  1056. /* If the MTU changed during the interface down, when the 
  1057.    interface up, the changed MTU must be reflected in the 
  1058.    idev as well as routers.
  1059.  */
  1060. if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
  1061. rt6_mtu_change(dev, dev->mtu);
  1062. idev->cnf.mtu6 = dev->mtu;
  1063. }
  1064. /* If the changed mtu during down is lower than IPV6_MIN_MTU
  1065.    stop IPv6 on this interface.
  1066.  */
  1067. if (dev->mtu < IPV6_MIN_MTU)
  1068. addrconf_ifdown(dev, event != NETDEV_DOWN);
  1069. }
  1070. break;
  1071. case NETDEV_CHANGEMTU:
  1072. if ( idev && dev->mtu >= IPV6_MIN_MTU) {
  1073. rt6_mtu_change(dev, dev->mtu);
  1074. idev->cnf.mtu6 = dev->mtu;
  1075. break;
  1076. }
  1077. /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
  1078. case NETDEV_DOWN:
  1079. case NETDEV_UNREGISTER:
  1080. /*
  1081.  * Remove all addresses from this interface.
  1082.  */
  1083. addrconf_ifdown(dev, event != NETDEV_DOWN);
  1084. break;
  1085. case NETDEV_CHANGE:
  1086. break;
  1087. };
  1088. return NOTIFY_OK;
  1089. }
  1090. static int addrconf_ifdown(struct net_device *dev, int how)
  1091. {
  1092. struct inet6_dev *idev;
  1093. struct inet6_ifaddr *ifa, **bifa;
  1094. int i;
  1095. ASSERT_RTNL();
  1096. rt6_ifdown(dev);
  1097. neigh_ifdown(&nd_tbl, dev);
  1098. idev = __in6_dev_get(dev);
  1099. if (idev == NULL)
  1100. return -ENODEV;
  1101. /* Step 1: remove reference to ipv6 device from parent device.
  1102.            Do not dev_put!
  1103.  */
  1104. if (how == 1) {
  1105. write_lock_bh(&addrconf_lock);
  1106. dev->ip6_ptr = NULL;
  1107. idev->dead = 1;
  1108. write_unlock_bh(&addrconf_lock);
  1109. }
  1110. /* Step 2: clear hash table */
  1111. for (i=0; i<IN6_ADDR_HSIZE; i++) {
  1112. bifa = &inet6_addr_lst[i];
  1113. write_lock_bh(&addrconf_hash_lock);
  1114. while ((ifa = *bifa) != NULL) {
  1115. if (ifa->idev == idev) {
  1116. *bifa = ifa->lst_next;
  1117. ifa->lst_next = NULL;
  1118. addrconf_del_timer(ifa);
  1119. in6_ifa_put(ifa);
  1120. continue;
  1121. }
  1122. bifa = &ifa->lst_next;
  1123. }
  1124. write_unlock_bh(&addrconf_hash_lock);
  1125. }
  1126. /* Step 3: clear address list */
  1127. write_lock_bh(&idev->lock);
  1128. while ((ifa = idev->addr_list) != NULL) {
  1129. idev->addr_list = ifa->if_next;
  1130. ifa->if_next = NULL;
  1131. ifa->dead = 1;
  1132. addrconf_del_timer(ifa);
  1133. write_unlock_bh(&idev->lock);
  1134. ipv6_ifa_notify(RTM_DELADDR, ifa);
  1135. in6_ifa_put(ifa);
  1136. write_lock_bh(&idev->lock);
  1137. }
  1138. write_unlock_bh(&idev->lock);
  1139. /* Step 4: Discard multicast list */
  1140. if (how == 1)
  1141. ipv6_mc_destroy_dev(idev);
  1142. else
  1143. ipv6_mc_down(idev);
  1144. /* Shot the device (if unregistered) */
  1145. if (how == 1) {
  1146. neigh_parms_release(&nd_tbl, idev->nd_parms);
  1147. #ifdef CONFIG_SYSCTL
  1148. addrconf_sysctl_unregister(&idev->cnf);
  1149. #endif
  1150. in6_dev_put(idev);
  1151. }
  1152. return 0;
  1153. }
  1154. static void addrconf_rs_timer(unsigned long data)
  1155. {
  1156. struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
  1157. if (ifp->idev->cnf.forwarding)
  1158. goto out;
  1159. if (ifp->idev->if_flags & IF_RA_RCVD) {
  1160. /*
  1161.  * Announcement received after solicitation
  1162.  * was sent
  1163.  */
  1164. goto out;
  1165. }
  1166. spin_lock(&ifp->lock);
  1167. if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
  1168. struct in6_addr all_routers;
  1169. /* The wait after the last probe can be shorter */
  1170. addrconf_mod_timer(ifp, AC_RS,
  1171.    (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
  1172.    ifp->idev->cnf.rtr_solicit_delay :
  1173.    ifp->idev->cnf.rtr_solicit_interval);
  1174. spin_unlock(&ifp->lock);
  1175. ipv6_addr_all_routers(&all_routers);
  1176. ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
  1177. } else {
  1178. struct in6_rtmsg rtmsg;
  1179. spin_unlock(&ifp->lock);
  1180. printk(KERN_DEBUG "%s: no IPv6 routers presentn",
  1181.        ifp->idev->dev->name);
  1182. memset(&rtmsg, 0, sizeof(struct in6_rtmsg));
  1183. rtmsg.rtmsg_type = RTMSG_NEWROUTE;
  1184. rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
  1185. rtmsg.rtmsg_flags = (RTF_ALLONLINK | RTF_ADDRCONF | 
  1186.      RTF_DEFAULT | RTF_UP);
  1187. rtmsg.rtmsg_ifindex = ifp->idev->dev->ifindex;
  1188. ip6_route_add(&rtmsg);
  1189. }
  1190. out:
  1191. in6_ifa_put(ifp);
  1192. }
  1193. /*
  1194.  * Duplicate Address Detection
  1195.  */
  1196. static void addrconf_dad_start(struct inet6_ifaddr *ifp)
  1197. {
  1198. struct net_device *dev;
  1199. unsigned long rand_num;
  1200. dev = ifp->idev->dev;
  1201. addrconf_join_solict(dev, &ifp->addr);
  1202. if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
  1203. addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0, RTF_ADDRCONF);
  1204. net_srandom(ifp->addr.s6_addr32[3]);
  1205. rand_num = net_random() % (ifp->idev->cnf.rtr_solicit_delay ? : 1);
  1206. spin_lock_bh(&ifp->lock);
  1207. if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
  1208.     !(ifp->flags&IFA_F_TENTATIVE)) {
  1209. ifp->flags &= ~IFA_F_TENTATIVE;
  1210. spin_unlock_bh(&ifp->lock);
  1211. addrconf_dad_completed(ifp);
  1212. return;
  1213. }
  1214. ifp->probes = ifp->idev->cnf.dad_transmits;
  1215. addrconf_mod_timer(ifp, AC_DAD, rand_num);
  1216. spin_unlock_bh(&ifp->lock);
  1217. }
  1218. static void addrconf_dad_timer(unsigned long data)
  1219. {
  1220. struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
  1221. struct in6_addr unspec;
  1222. struct in6_addr mcaddr;
  1223. spin_lock_bh(&ifp->lock);
  1224. if (ifp->probes == 0) {
  1225. /*
  1226.  * DAD was successful
  1227.  */
  1228. ifp->flags &= ~IFA_F_TENTATIVE;
  1229. spin_unlock_bh(&ifp->lock);
  1230. addrconf_dad_completed(ifp);
  1231. in6_ifa_put(ifp);
  1232. return;
  1233. }
  1234. ifp->probes--;
  1235. addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
  1236. spin_unlock_bh(&ifp->lock);
  1237. /* send a neighbour solicitation for our addr */
  1238. memset(&unspec, 0, sizeof(unspec));
  1239. addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
  1240. ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
  1241. in6_ifa_put(ifp);
  1242. }
  1243. static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
  1244. {
  1245. struct net_device * dev = ifp->idev->dev;
  1246. /*
  1247.  * Configure the address for reception. Now it is valid.
  1248.  */
  1249. ipv6_ifa_notify(RTM_NEWADDR, ifp);
  1250. /* If added prefix is link local and forwarding is off,
  1251.    start sending router solicitations.
  1252.  */
  1253. if (ifp->idev->cnf.forwarding == 0 &&
  1254.     (dev->flags&IFF_LOOPBACK) == 0 &&
  1255.     (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
  1256. struct in6_addr all_routers;
  1257. ipv6_addr_all_routers(&all_routers);
  1258. /*
  1259.  * If a host as already performed a random delay
  1260.  * [...] as part of DAD [...] there is no need
  1261.  * to delay again before sending the first RS
  1262.  */
  1263. ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
  1264. spin_lock_bh(&ifp->lock);
  1265. ifp->probes = 1;
  1266. ifp->idev->if_flags |= IF_RS_SENT;
  1267. addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
  1268. spin_unlock_bh(&ifp->lock);
  1269. }
  1270. }
  1271. #ifdef CONFIG_PROC_FS
  1272. static int iface_proc_info(char *buffer, char **start, off_t offset,
  1273.    int length)
  1274. {
  1275. struct inet6_ifaddr *ifp;
  1276. int i;
  1277. int len = 0;
  1278. off_t pos=0;
  1279. off_t begin=0;
  1280. for (i=0; i < IN6_ADDR_HSIZE; i++) {
  1281. read_lock_bh(&addrconf_hash_lock);
  1282. for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
  1283. int j;
  1284. for (j=0; j<16; j++) {
  1285. sprintf(buffer + len, "%02x",
  1286. ifp->addr.s6_addr[j]);
  1287. len += 2;
  1288. }
  1289. len += sprintf(buffer + len,
  1290.        " %02x %02x %02x %02x %8sn",
  1291.        ifp->idev->dev->ifindex,
  1292.        ifp->prefix_len,
  1293.        ifp->scope,
  1294.        ifp->flags,
  1295.        ifp->idev->dev->name);
  1296. pos=begin+len;
  1297. if(pos<offset) {
  1298. len=0;
  1299. begin=pos;
  1300. }
  1301. if(pos>offset+length) {
  1302. read_unlock_bh(&addrconf_hash_lock);
  1303. goto done;
  1304. }
  1305. }
  1306. read_unlock_bh(&addrconf_hash_lock);
  1307. }
  1308. done:
  1309. *start=buffer+(offset-begin);
  1310. len-=(offset-begin);
  1311. if(len>length)
  1312. len=length;
  1313. if(len<0)
  1314. len=0;
  1315. return len;
  1316. }
  1317. #endif /* CONFIG_PROC_FS */
  1318. /*
  1319.  * Periodic address status verification
  1320.  */
  1321. void addrconf_verify(unsigned long foo)
  1322. {
  1323. struct inet6_ifaddr *ifp;
  1324. unsigned long now, next;
  1325. int i;
  1326. spin_lock_bh(&addrconf_verify_lock);
  1327. now = jiffies;
  1328. next = now + ADDR_CHECK_FREQUENCY;
  1329. del_timer(&addr_chk_timer);
  1330. for (i=0; i < IN6_ADDR_HSIZE; i++) {
  1331. restart:
  1332. write_lock(&addrconf_hash_lock);
  1333. for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
  1334. unsigned long age;
  1335. if (ifp->flags & IFA_F_PERMANENT)
  1336. continue;
  1337. spin_lock(&ifp->lock);
  1338. age = (now - ifp->tstamp) / HZ;
  1339. if (age >= ifp->valid_lft) {
  1340. spin_unlock(&ifp->lock);
  1341. in6_ifa_hold(ifp);
  1342. write_unlock(&addrconf_hash_lock);
  1343. ipv6_del_addr(ifp);
  1344. goto restart;
  1345. } else if (age >= ifp->prefered_lft) {
  1346. /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
  1347. int deprecate = 0;
  1348. if (!(ifp->flags&IFA_F_DEPRECATED)) {
  1349. deprecate = 1;
  1350. ifp->flags |= IFA_F_DEPRECATED;
  1351. }
  1352. if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
  1353. next = ifp->tstamp + ifp->valid_lft * HZ;
  1354. spin_unlock(&ifp->lock);
  1355. if (deprecate) {
  1356. in6_ifa_hold(ifp);
  1357. write_unlock(&addrconf_hash_lock);
  1358. ipv6_ifa_notify(0, ifp);
  1359. in6_ifa_put(ifp);
  1360. goto restart;
  1361. }
  1362. } else {
  1363. /* ifp->prefered_lft <= ifp->valid_lft */
  1364. if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
  1365. next = ifp->tstamp + ifp->prefered_lft * HZ;
  1366. spin_unlock(&ifp->lock);
  1367. }
  1368. }
  1369. write_unlock(&addrconf_hash_lock);
  1370. }
  1371. addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
  1372. add_timer(&addr_chk_timer);
  1373. spin_unlock_bh(&addrconf_verify_lock);
  1374. }
  1375. static int
  1376. inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  1377. {
  1378. struct rtattr **rta = arg;
  1379. struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
  1380. struct in6_addr *pfx;
  1381. pfx = NULL;
  1382. if (rta[IFA_ADDRESS-1]) {
  1383. if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
  1384. return -EINVAL;
  1385. pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
  1386. }
  1387. if (rta[IFA_LOCAL-1]) {
  1388. if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
  1389. return -EINVAL;
  1390. pfx = RTA_DATA(rta[IFA_LOCAL-1]);
  1391. }
  1392. if (pfx == NULL)
  1393. return -EINVAL;
  1394. return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
  1395. }
  1396. static int
  1397. inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  1398. {
  1399. struct rtattr  **rta = arg;
  1400. struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
  1401. struct in6_addr *pfx;
  1402. pfx = NULL;
  1403. if (rta[IFA_ADDRESS-1]) {
  1404. if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
  1405. return -EINVAL;
  1406. pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
  1407. }
  1408. if (rta[IFA_LOCAL-1]) {
  1409. if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
  1410. return -EINVAL;
  1411. pfx = RTA_DATA(rta[IFA_LOCAL-1]);
  1412. }
  1413. if (pfx == NULL)
  1414. return -EINVAL;
  1415. return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
  1416. }
  1417. static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
  1418.      u32 pid, u32 seq, int event)
  1419. {
  1420. struct ifaddrmsg *ifm;
  1421. struct nlmsghdr  *nlh;
  1422. struct ifa_cacheinfo ci;
  1423. unsigned char  *b = skb->tail;
  1424. nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
  1425. ifm = NLMSG_DATA(nlh);
  1426. ifm->ifa_family = AF_INET6;
  1427. ifm->ifa_prefixlen = ifa->prefix_len;
  1428. ifm->ifa_flags = ifa->flags;
  1429. ifm->ifa_scope = RT_SCOPE_UNIVERSE;
  1430. if (ifa->scope&IFA_HOST)
  1431. ifm->ifa_scope = RT_SCOPE_HOST;
  1432. else if (ifa->scope&IFA_LINK)
  1433. ifm->ifa_scope = RT_SCOPE_LINK;
  1434. else if (ifa->scope&IFA_SITE)
  1435. ifm->ifa_scope = RT_SCOPE_SITE;
  1436. ifm->ifa_index = ifa->idev->dev->ifindex;
  1437. RTA_PUT(skb, IFA_ADDRESS, 16, &ifa->addr);
  1438. if (!(ifa->flags&IFA_F_PERMANENT)) {
  1439. ci.ifa_prefered = ifa->prefered_lft;
  1440. ci.ifa_valid = ifa->valid_lft;
  1441. if (ci.ifa_prefered != 0xFFFFFFFF) {
  1442. long tval = (jiffies - ifa->tstamp)/HZ;
  1443. ci.ifa_prefered -= tval;
  1444. if (ci.ifa_valid != 0xFFFFFFFF)
  1445. ci.ifa_valid -= tval;
  1446. }
  1447. RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
  1448. }
  1449. nlh->nlmsg_len = skb->tail - b;
  1450. return skb->len;
  1451. nlmsg_failure:
  1452. rtattr_failure:
  1453. skb_trim(skb, b - skb->data);
  1454. return -1;
  1455. }
  1456. static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
  1457. {
  1458. int idx, ip_idx;
  1459. int s_idx, s_ip_idx;
  1460.   struct inet6_ifaddr *ifa;
  1461. s_idx = cb->args[0];
  1462. s_ip_idx = ip_idx = cb->args[1];
  1463. for (idx=0; idx < IN6_ADDR_HSIZE; idx++) {
  1464. if (idx < s_idx)
  1465. continue;
  1466. if (idx > s_idx)
  1467. s_ip_idx = 0;
  1468. read_lock_bh(&addrconf_hash_lock);
  1469. for (ifa=inet6_addr_lst[idx], ip_idx = 0; ifa;
  1470.      ifa = ifa->lst_next, ip_idx++) {
  1471. if (ip_idx < s_ip_idx)
  1472. continue;
  1473. if (inet6_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
  1474.       cb->nlh->nlmsg_seq, RTM_NEWADDR) <= 0) {
  1475. read_unlock_bh(&addrconf_hash_lock);
  1476. goto done;
  1477. }
  1478. }
  1479. read_unlock_bh(&addrconf_hash_lock);
  1480. }
  1481. done:
  1482. cb->args[0] = idx;
  1483. cb->args[1] = ip_idx;
  1484. return skb->len;
  1485. }
  1486. static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
  1487. {
  1488. struct sk_buff *skb;
  1489. int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
  1490. skb = alloc_skb(size, GFP_ATOMIC);
  1491. if (!skb) {
  1492. netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFADDR, ENOBUFS);
  1493. return;
  1494. }
  1495. if (inet6_fill_ifaddr(skb, ifa, 0, 0, event) < 0) {
  1496. kfree_skb(skb);
  1497. netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFADDR, EINVAL);
  1498. return;
  1499. }
  1500. NETLINK_CB(skb).dst_groups = RTMGRP_IPV6_IFADDR;
  1501. netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV6_IFADDR, GFP_ATOMIC);
  1502. }
  1503. static struct rtnetlink_link inet6_rtnetlink_table[RTM_MAX-RTM_BASE+1] =
  1504. {
  1505. { NULL, NULL, },
  1506. { NULL, NULL, },
  1507. { NULL, NULL, },
  1508. { NULL, NULL, },
  1509. { inet6_rtm_newaddr, NULL, },
  1510. { inet6_rtm_deladdr, NULL, },
  1511. { NULL, inet6_dump_ifaddr, },
  1512. { NULL, NULL, },
  1513. { inet6_rtm_newroute, NULL, },
  1514. { inet6_rtm_delroute, NULL, },
  1515. { inet6_rtm_getroute, inet6_dump_fib, },
  1516. { NULL, NULL, },
  1517. };
  1518. static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
  1519. {
  1520. inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
  1521. switch (event) {
  1522. case RTM_NEWADDR:
  1523. ip6_rt_addr_add(&ifp->addr, ifp->idev->dev);
  1524. break;
  1525. case RTM_DELADDR:
  1526. addrconf_leave_solict(ifp->idev->dev, &ifp->addr);
  1527. if (!ipv6_chk_addr(&ifp->addr, NULL))
  1528. ip6_rt_addr_del(&ifp->addr, ifp->idev->dev);
  1529. break;
  1530. }
  1531. }
  1532. #ifdef CONFIG_SYSCTL
  1533. static
  1534. int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
  1535.    void *buffer, size_t *lenp)
  1536. {
  1537. int *valp = ctl->data;
  1538. int val = *valp;
  1539. int ret;
  1540. ret = proc_dointvec(ctl, write, filp, buffer, lenp);
  1541. if (write && *valp != val && valp != &ipv6_devconf_dflt.forwarding) {
  1542. struct inet6_dev *idev = NULL;
  1543. if (valp != &ipv6_devconf.forwarding) {
  1544. struct net_device *dev = dev_get_by_index(ctl->ctl_name);
  1545. if (dev) {
  1546. idev = in6_dev_get(dev);
  1547. dev_put(dev);
  1548. }
  1549. if (idev == NULL)
  1550. return ret;
  1551. } else
  1552. ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
  1553. addrconf_forward_change(idev);
  1554. if (*valp)
  1555. rt6_purge_dflt_routers(0);
  1556. if (idev)
  1557. in6_dev_put(idev);
  1558. }
  1559.         return ret;
  1560. }
  1561. static struct addrconf_sysctl_table
  1562. {
  1563. struct ctl_table_header *sysctl_header;
  1564. ctl_table addrconf_vars[11];
  1565. ctl_table addrconf_dev[2];
  1566. ctl_table addrconf_conf_dir[2];
  1567. ctl_table addrconf_proto_dir[2];
  1568. ctl_table addrconf_root_dir[2];
  1569. } addrconf_sysctl = {
  1570. NULL,
  1571.         {{NET_IPV6_FORWARDING, "forwarding",
  1572.          &ipv6_devconf.forwarding, sizeof(int), 0644, NULL,
  1573.          &addrconf_sysctl_forward},
  1574. {NET_IPV6_HOP_LIMIT, "hop_limit",
  1575.          &ipv6_devconf.hop_limit, sizeof(int), 0644, NULL,
  1576.          &proc_dointvec},
  1577. {NET_IPV6_MTU, "mtu",
  1578.          &ipv6_devconf.mtu6, sizeof(int), 0644, NULL,
  1579.          &proc_dointvec},
  1580. {NET_IPV6_ACCEPT_RA, "accept_ra",
  1581.          &ipv6_devconf.accept_ra, sizeof(int), 0644, NULL,
  1582.          &proc_dointvec},
  1583. {NET_IPV6_ACCEPT_REDIRECTS, "accept_redirects",
  1584.          &ipv6_devconf.accept_redirects, sizeof(int), 0644, NULL,
  1585.          &proc_dointvec},
  1586. {NET_IPV6_AUTOCONF, "autoconf",
  1587.          &ipv6_devconf.autoconf, sizeof(int), 0644, NULL,
  1588.          &proc_dointvec},
  1589. {NET_IPV6_DAD_TRANSMITS, "dad_transmits",
  1590.          &ipv6_devconf.dad_transmits, sizeof(int), 0644, NULL,
  1591.          &proc_dointvec},
  1592. {NET_IPV6_RTR_SOLICITS, "router_solicitations",
  1593.          &ipv6_devconf.rtr_solicits, sizeof(int), 0644, NULL,
  1594.          &proc_dointvec},
  1595. {NET_IPV6_RTR_SOLICIT_INTERVAL, "router_solicitation_interval",
  1596.          &ipv6_devconf.rtr_solicit_interval, sizeof(int), 0644, NULL,
  1597.          &proc_dointvec_jiffies},
  1598. {NET_IPV6_RTR_SOLICIT_DELAY, "router_solicitation_delay",
  1599.          &ipv6_devconf.rtr_solicit_delay, sizeof(int), 0644, NULL,
  1600.          &proc_dointvec_jiffies},
  1601. {0}},
  1602. {{NET_PROTO_CONF_ALL, "all", NULL, 0, 0555, addrconf_sysctl.addrconf_vars},{0}},
  1603. {{NET_IPV6_CONF, "conf", NULL, 0, 0555, addrconf_sysctl.addrconf_dev},{0}},
  1604. {{NET_IPV6, "ipv6", NULL, 0, 0555, addrconf_sysctl.addrconf_conf_dir},{0}},
  1605. {{CTL_NET, "net", NULL, 0, 0555, addrconf_sysctl.addrconf_proto_dir},{0}}
  1606. };
  1607. static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
  1608. {
  1609. int i;
  1610. struct net_device *dev = idev ? idev->dev : NULL;
  1611. struct addrconf_sysctl_table *t;
  1612. t = kmalloc(sizeof(*t), GFP_KERNEL);
  1613. if (t == NULL)
  1614. return;
  1615. memcpy(t, &addrconf_sysctl, sizeof(*t));
  1616. for (i=0; i<sizeof(t->addrconf_vars)/sizeof(t->addrconf_vars[0])-1; i++) {
  1617. t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
  1618. t->addrconf_vars[i].de = NULL;
  1619. }
  1620. if (dev) {
  1621. t->addrconf_dev[0].procname = dev->name;
  1622. t->addrconf_dev[0].ctl_name = dev->ifindex;
  1623. } else {
  1624. t->addrconf_dev[0].procname = "default";
  1625. t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
  1626. }
  1627. t->addrconf_dev[0].child = t->addrconf_vars;
  1628. t->addrconf_dev[0].de = NULL;
  1629. t->addrconf_conf_dir[0].child = t->addrconf_dev;
  1630. t->addrconf_conf_dir[0].de = NULL;
  1631. t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
  1632. t->addrconf_proto_dir[0].de = NULL;
  1633. t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
  1634. t->addrconf_root_dir[0].de = NULL;
  1635. t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
  1636. if (t->sysctl_header == NULL)
  1637. kfree(t);
  1638. else
  1639. p->sysctl = t;
  1640. }
  1641. static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
  1642. {
  1643. if (p->sysctl) {
  1644. struct addrconf_sysctl_table *t = p->sysctl;
  1645. p->sysctl = NULL;
  1646. unregister_sysctl_table(t->sysctl_header);
  1647. kfree(t);
  1648. }
  1649. }
  1650. #endif
  1651. /*
  1652.  *      Device notifier
  1653.  */
  1654. int register_inet6addr_notifier(struct notifier_block *nb)
  1655. {
  1656.         return notifier_chain_register(&inet6addr_chain, nb);
  1657. }
  1658. int unregister_inet6addr_notifier(struct notifier_block *nb)
  1659. {
  1660.         return notifier_chain_unregister(&inet6addr_chain,nb);
  1661. }
  1662. /*
  1663.  * Init / cleanup code
  1664.  */
  1665. void __init addrconf_init(void)
  1666. {
  1667. #ifdef MODULE
  1668. struct net_device *dev;
  1669. /* This takes sense only during module load. */
  1670. rtnl_lock();
  1671. for (dev = dev_base; dev; dev = dev->next) {
  1672. if (!(dev->flags&IFF_UP))
  1673. continue;
  1674. switch (dev->type) {
  1675. case ARPHRD_LOOPBACK:
  1676. init_loopback(dev);
  1677. break;
  1678. case ARPHRD_ETHER:
  1679. case ARPHRD_FDDI:
  1680. case ARPHRD_IEEE802_TR:
  1681. addrconf_dev_config(dev);
  1682. break;
  1683. default:;
  1684. /* Ignore all other */
  1685. }
  1686. }
  1687. rtnl_unlock();
  1688. #endif
  1689. #ifdef CONFIG_PROC_FS
  1690. proc_net_create("if_inet6", 0, iface_proc_info);
  1691. #endif
  1692. addrconf_verify(0);
  1693. rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
  1694. #ifdef CONFIG_SYSCTL
  1695. addrconf_sysctl.sysctl_header =
  1696. register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
  1697. addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
  1698. #endif
  1699. }
  1700. #ifdef MODULE
  1701. void addrconf_cleanup(void)
  1702. {
  1703.   struct net_device *dev;
  1704.   struct inet6_dev *idev;
  1705.   struct inet6_ifaddr *ifa;
  1706. int i;
  1707. rtnetlink_links[PF_INET6] = NULL;
  1708. #ifdef CONFIG_SYSCTL
  1709. addrconf_sysctl_unregister(&ipv6_devconf_dflt);
  1710. addrconf_sysctl_unregister(&ipv6_devconf);
  1711. #endif
  1712. rtnl_lock();
  1713. /*
  1714.  * clean dev list.
  1715.  */
  1716. for (dev=dev_base; dev; dev=dev->next) {
  1717. if ((idev = __in6_dev_get(dev)) == NULL)
  1718. continue;
  1719. addrconf_ifdown(dev, 1);
  1720. }
  1721. /*
  1722.  * Check hash table.
  1723.  */
  1724. write_lock_bh(&addrconf_hash_lock);
  1725. for (i=0; i < IN6_ADDR_HSIZE; i++) {
  1726. for (ifa=inet6_addr_lst[i]; ifa; ) {
  1727. struct inet6_ifaddr *bifa;
  1728. bifa = ifa;
  1729. ifa = ifa->lst_next;
  1730. printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%pn", bifa);
  1731. /* Do not free it; something is wrong.
  1732.    Now we can investigate it with debugger.
  1733.  */
  1734. }
  1735. }
  1736. write_unlock_bh(&addrconf_hash_lock);
  1737. del_timer(&addr_chk_timer);
  1738. rtnl_unlock();
  1739. #ifdef CONFIG_PROC_FS
  1740. proc_net_remove("if_inet6");
  1741. #endif
  1742. }
  1743. #endif /* MODULE */