addrconf_core.c 7.1 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 struct dst_entry *eafnosupport_ipv6_dst_lookup_flow(struct net *net,
  109. const struct sock *sk,
  110. struct flowi6 *fl6,
  111. const struct in6_addr *final_dst)
  112. {
  113. return ERR_PTR(-EAFNOSUPPORT);
  114. }
  115. static struct fib6_table *eafnosupport_fib6_get_table(struct net *net, u32 id)
  116. {
  117. return NULL;
  118. }
  119. static struct fib6_info *
  120. eafnosupport_fib6_table_lookup(struct net *net, struct fib6_table *table,
  121. int oif, struct flowi6 *fl6, int flags)
  122. {
  123. return NULL;
  124. }
  125. static struct fib6_info *
  126. eafnosupport_fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
  127. int flags)
  128. {
  129. return NULL;
  130. }
  131. static struct fib6_info *
  132. eafnosupport_fib6_multipath_select(const struct net *net, struct fib6_info *f6i,
  133. struct flowi6 *fl6, int oif,
  134. const struct sk_buff *skb, int strict)
  135. {
  136. return f6i;
  137. }
  138. static u32
  139. eafnosupport_ip6_mtu_from_fib6(struct fib6_info *f6i, struct in6_addr *daddr,
  140. struct in6_addr *saddr)
  141. {
  142. return 0;
  143. }
  144. const struct ipv6_stub *ipv6_stub __read_mostly = &(struct ipv6_stub) {
  145. .ipv6_dst_lookup_flow = eafnosupport_ipv6_dst_lookup_flow,
  146. .fib6_get_table = eafnosupport_fib6_get_table,
  147. .fib6_table_lookup = eafnosupport_fib6_table_lookup,
  148. .fib6_lookup = eafnosupport_fib6_lookup,
  149. .fib6_multipath_select = eafnosupport_fib6_multipath_select,
  150. .ip6_mtu_from_fib6 = eafnosupport_ip6_mtu_from_fib6,
  151. };
  152. EXPORT_SYMBOL_GPL(ipv6_stub);
  153. /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
  154. const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
  155. EXPORT_SYMBOL(in6addr_loopback);
  156. const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
  157. EXPORT_SYMBOL(in6addr_any);
  158. const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
  159. EXPORT_SYMBOL(in6addr_linklocal_allnodes);
  160. const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
  161. EXPORT_SYMBOL(in6addr_linklocal_allrouters);
  162. const struct in6_addr in6addr_interfacelocal_allnodes = IN6ADDR_INTERFACELOCAL_ALLNODES_INIT;
  163. EXPORT_SYMBOL(in6addr_interfacelocal_allnodes);
  164. const struct in6_addr in6addr_interfacelocal_allrouters = IN6ADDR_INTERFACELOCAL_ALLROUTERS_INIT;
  165. EXPORT_SYMBOL(in6addr_interfacelocal_allrouters);
  166. const struct in6_addr in6addr_sitelocal_allrouters = IN6ADDR_SITELOCAL_ALLROUTERS_INIT;
  167. EXPORT_SYMBOL(in6addr_sitelocal_allrouters);
  168. static void snmp6_free_dev(struct inet6_dev *idev)
  169. {
  170. kfree(idev->stats.icmpv6msgdev);
  171. kfree(idev->stats.icmpv6dev);
  172. free_percpu(idev->stats.ipv6);
  173. }
  174. static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
  175. {
  176. struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
  177. snmp6_free_dev(idev);
  178. kfree(idev);
  179. }
  180. /* Nobody refers to this device, we may destroy it. */
  181. void in6_dev_finish_destroy(struct inet6_dev *idev)
  182. {
  183. struct net_device *dev = idev->dev;
  184. WARN_ON(!list_empty(&idev->addr_list));
  185. WARN_ON(idev->mc_list);
  186. WARN_ON(timer_pending(&idev->rs_timer));
  187. #ifdef NET_REFCNT_DEBUG
  188. pr_debug("%s: %s\n", __func__, dev ? dev->name : "NIL");
  189. #endif
  190. dev_put(dev);
  191. if (!idev->dead) {
  192. pr_warn("Freeing alive inet6 device %p\n", idev);
  193. return;
  194. }
  195. call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
  196. }
  197. EXPORT_SYMBOL(in6_dev_finish_destroy);