addrconf_core.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * IPv6 library code, needed by static components when full IPv6 support is
  3. * not configured or static.
  4. */
  5. #include <linux/export.h>
  6. #include <net/ipv6.h>
  7. #include <net/addrconf.h>
  8. #include <net/ip.h>
  9. /* if ipv6 module registers this function is used by xfrm to force all
  10. * sockets to relookup their nodes - this is fairly expensive, be
  11. * careful
  12. */
  13. void (*__fib6_flush_trees)(struct net *);
  14. EXPORT_SYMBOL(__fib6_flush_trees);
  15. #define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16)
  16. static inline unsigned int ipv6_addr_scope2type(unsigned int scope)
  17. {
  18. switch (scope) {
  19. case IPV6_ADDR_SCOPE_NODELOCAL:
  20. return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL) |
  21. IPV6_ADDR_LOOPBACK);
  22. case IPV6_ADDR_SCOPE_LINKLOCAL:
  23. return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL) |
  24. IPV6_ADDR_LINKLOCAL);
  25. case IPV6_ADDR_SCOPE_SITELOCAL:
  26. return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL) |
  27. IPV6_ADDR_SITELOCAL);
  28. }
  29. return IPV6_ADDR_SCOPE_TYPE(scope);
  30. }
  31. int __ipv6_addr_type(const struct in6_addr *addr)
  32. {
  33. __be32 st;
  34. st = addr->s6_addr32[0];
  35. /* Consider all addresses with the first three bits different of
  36. 000 and 111 as unicasts.
  37. */
  38. if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
  39. (st & htonl(0xE0000000)) != htonl(0xE0000000))
  40. return (IPV6_ADDR_UNICAST |
  41. IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));
  42. if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
  43. /* multicast */
  44. /* addr-select 3.1 */
  45. return (IPV6_ADDR_MULTICAST |
  46. ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr)));
  47. }
  48. if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
  49. return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST |
  50. IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.1 */
  51. if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
  52. return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST |
  53. IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL)); /* addr-select 3.1 */
  54. if ((st & htonl(0xFE000000)) == htonl(0xFC000000))
  55. return (IPV6_ADDR_UNICAST |
  56. IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* RFC 4193 */
  57. if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
  58. if (addr->s6_addr32[2] == 0) {
  59. if (addr->s6_addr32[3] == 0)
  60. return IPV6_ADDR_ANY;
  61. if (addr->s6_addr32[3] == htonl(0x00000001))
  62. return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST |
  63. IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.4 */
  64. return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST |
  65. IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
  66. }
  67. if (addr->s6_addr32[2] == htonl(0x0000ffff))
  68. return (IPV6_ADDR_MAPPED |
  69. IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
  70. }
  71. return (IPV6_ADDR_UNICAST |
  72. IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */
  73. }
  74. EXPORT_SYMBOL(__ipv6_addr_type);
  75. static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
  76. static BLOCKING_NOTIFIER_HEAD(inet6addr_validator_chain);
  77. int register_inet6addr_notifier(struct notifier_block *nb)
  78. {
  79. return atomic_notifier_chain_register(&inet6addr_chain, nb);
  80. }
  81. EXPORT_SYMBOL(register_inet6addr_notifier);
  82. int unregister_inet6addr_notifier(struct notifier_block *nb)
  83. {
  84. return atomic_notifier_chain_unregister(&inet6addr_chain, nb);
  85. }
  86. EXPORT_SYMBOL(unregister_inet6addr_notifier);
  87. int inet6addr_notifier_call_chain(unsigned long val, void *v)
  88. {
  89. return atomic_notifier_call_chain(&inet6addr_chain, val, v);
  90. }
  91. EXPORT_SYMBOL(inet6addr_notifier_call_chain);
  92. int register_inet6addr_validator_notifier(struct notifier_block *nb)
  93. {
  94. return blocking_notifier_chain_register(&inet6addr_validator_chain, nb);
  95. }
  96. EXPORT_SYMBOL(register_inet6addr_validator_notifier);
  97. int unregister_inet6addr_validator_notifier(struct notifier_block *nb)
  98. {
  99. return blocking_notifier_chain_unregister(&inet6addr_validator_chain,
  100. nb);
  101. }
  102. EXPORT_SYMBOL(unregister_inet6addr_validator_notifier);
  103. int inet6addr_validator_notifier_call_chain(unsigned long val, void *v)
  104. {
  105. return blocking_notifier_call_chain(&inet6addr_validator_chain, val, v);
  106. }
  107. EXPORT_SYMBOL(inet6addr_validator_notifier_call_chain);
  108. static int eafnosupport_ipv6_dst_lookup(struct net *net, struct sock *u1,
  109. struct dst_entry **u2,
  110. struct flowi6 *u3)
  111. {
  112. return -EAFNOSUPPORT;
  113. }
  114. static struct fib6_table *eafnosupport_fib6_get_table(struct net *net, u32 id)
  115. {
  116. return NULL;
  117. }
  118. static struct fib6_info *
  119. eafnosupport_fib6_table_lookup(struct net *net, struct fib6_table *table,
  120. int oif, struct flowi6 *fl6, int flags)
  121. {
  122. return NULL;
  123. }
  124. static struct fib6_info *
  125. eafnosupport_fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
  126. int flags)
  127. {
  128. return NULL;
  129. }
  130. static struct fib6_info *
  131. eafnosupport_fib6_multipath_select(const struct net *net, struct fib6_info *f6i,
  132. struct flowi6 *fl6, int oif,
  133. const struct sk_buff *skb, int strict)
  134. {
  135. return f6i;
  136. }
  137. static u32
  138. eafnosupport_ip6_mtu_from_fib6(struct fib6_info *f6i, struct in6_addr *daddr,
  139. struct in6_addr *saddr)
  140. {
  141. return 0;
  142. }
  143. const struct ipv6_stub *ipv6_stub __read_mostly = &(struct ipv6_stub) {
  144. .ipv6_dst_lookup = eafnosupport_ipv6_dst_lookup,
  145. .fib6_get_table = eafnosupport_fib6_get_table,
  146. .fib6_table_lookup = eafnosupport_fib6_table_lookup,
  147. .fib6_lookup = eafnosupport_fib6_lookup,
  148. .fib6_multipath_select = eafnosupport_fib6_multipath_select,
  149. .ip6_mtu_from_fib6 = eafnosupport_ip6_mtu_from_fib6,
  150. };
  151. EXPORT_SYMBOL_GPL(ipv6_stub);
  152. /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
  153. const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
  154. EXPORT_SYMBOL(in6addr_loopback);
  155. const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
  156. EXPORT_SYMBOL(in6addr_any);
  157. const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
  158. EXPORT_SYMBOL(in6addr_linklocal_allnodes);
  159. const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
  160. EXPORT_SYMBOL(in6addr_linklocal_allrouters);
  161. const struct in6_addr in6addr_interfacelocal_allnodes = IN6ADDR_INTERFACELOCAL_ALLNODES_INIT;
  162. EXPORT_SYMBOL(in6addr_interfacelocal_allnodes);
  163. const struct in6_addr in6addr_interfacelocal_allrouters = IN6ADDR_INTERFACELOCAL_ALLROUTERS_INIT;
  164. EXPORT_SYMBOL(in6addr_interfacelocal_allrouters);
  165. const struct in6_addr in6addr_sitelocal_allrouters = IN6ADDR_SITELOCAL_ALLROUTERS_INIT;
  166. EXPORT_SYMBOL(in6addr_sitelocal_allrouters);
  167. static void snmp6_free_dev(struct inet6_dev *idev)
  168. {
  169. kfree(idev->stats.icmpv6msgdev);
  170. kfree(idev->stats.icmpv6dev);
  171. free_percpu(idev->stats.ipv6);
  172. }
  173. static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
  174. {
  175. struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
  176. snmp6_free_dev(idev);
  177. kfree(idev);
  178. }
  179. /* Nobody refers to this device, we may destroy it. */
  180. void in6_dev_finish_destroy(struct inet6_dev *idev)
  181. {
  182. struct net_device *dev = idev->dev;
  183. WARN_ON(!list_empty(&idev->addr_list));
  184. WARN_ON(idev->mc_list);
  185. WARN_ON(timer_pending(&idev->rs_timer));
  186. #ifdef NET_REFCNT_DEBUG
  187. pr_debug("%s: %s\n", __func__, dev ? dev->name : "NIL");
  188. #endif
  189. dev_put(dev);
  190. if (!idev->dead) {
  191. pr_warn("Freeing alive inet6 device %p\n", idev);
  192. return;
  193. }
  194. call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
  195. }
  196. EXPORT_SYMBOL(in6_dev_finish_destroy);