bond_ipv6.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright(c) 2008 Hewlett-Packard Development Company, L.P.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called LICENSE.
  20. *
  21. */
  22. #include <linux/types.h>
  23. #include <linux/if_vlan.h>
  24. #include <net/ipv6.h>
  25. #include <net/ndisc.h>
  26. #include <net/addrconf.h>
  27. #include "bonding.h"
  28. /*
  29. * Assign bond->master_ipv6 to the next IPv6 address in the list, or
  30. * zero it out if there are none.
  31. */
  32. static void bond_glean_dev_ipv6(struct net_device *dev, struct in6_addr *addr)
  33. {
  34. struct inet6_dev *idev;
  35. struct inet6_ifaddr *ifa;
  36. if (!dev)
  37. return;
  38. idev = in6_dev_get(dev);
  39. if (!idev)
  40. return;
  41. read_lock_bh(&idev->lock);
  42. ifa = idev->addr_list;
  43. if (ifa)
  44. ipv6_addr_copy(addr, &ifa->addr);
  45. else
  46. ipv6_addr_set(addr, 0, 0, 0, 0);
  47. read_unlock_bh(&idev->lock);
  48. in6_dev_put(idev);
  49. }
  50. static void bond_na_send(struct net_device *slave_dev,
  51. struct in6_addr *daddr,
  52. int router,
  53. unsigned short vlan_id)
  54. {
  55. struct in6_addr mcaddr;
  56. struct icmp6hdr icmp6h = {
  57. .icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT,
  58. };
  59. struct sk_buff *skb;
  60. icmp6h.icmp6_router = router;
  61. icmp6h.icmp6_solicited = 0;
  62. icmp6h.icmp6_override = 1;
  63. addrconf_addr_solict_mult(daddr, &mcaddr);
  64. pr_debug("ipv6 na on slave %s: dest %pI6, src %pI6\n",
  65. slave_dev->name, &mcaddr, daddr);
  66. skb = ndisc_build_skb(slave_dev, &mcaddr, daddr, &icmp6h, daddr,
  67. ND_OPT_TARGET_LL_ADDR);
  68. if (!skb) {
  69. printk(KERN_ERR DRV_NAME ": NA packet allocation failed\n");
  70. return;
  71. }
  72. if (vlan_id) {
  73. skb = vlan_put_tag(skb, vlan_id);
  74. if (!skb) {
  75. printk(KERN_ERR DRV_NAME ": failed to insert VLAN tag\n");
  76. return;
  77. }
  78. }
  79. ndisc_send_skb(skb, slave_dev, NULL, &mcaddr, daddr, &icmp6h);
  80. }
  81. /*
  82. * Kick out an unsolicited Neighbor Advertisement for an IPv6 address on
  83. * the bonding master. This will help the switch learn our address
  84. * if in active-backup mode.
  85. *
  86. * Caller must hold curr_slave_lock for read or better
  87. */
  88. void bond_send_unsolicited_na(struct bonding *bond)
  89. {
  90. struct slave *slave = bond->curr_active_slave;
  91. struct vlan_entry *vlan;
  92. struct inet6_dev *idev;
  93. int is_router;
  94. pr_debug("bond_send_unsol_na: bond %s slave %s\n", bond->dev->name,
  95. slave ? slave->dev->name : "NULL");
  96. if (!slave || !bond->send_unsol_na ||
  97. test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
  98. return;
  99. bond->send_unsol_na--;
  100. idev = in6_dev_get(bond->dev);
  101. if (!idev)
  102. return;
  103. is_router = !!idev->cnf.forwarding;
  104. in6_dev_put(idev);
  105. if (!ipv6_addr_any(&bond->master_ipv6))
  106. bond_na_send(slave->dev, &bond->master_ipv6, is_router, 0);
  107. list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
  108. if (!ipv6_addr_any(&vlan->vlan_ipv6)) {
  109. bond_na_send(slave->dev, &vlan->vlan_ipv6, is_router,
  110. vlan->vlan_id);
  111. }
  112. }
  113. }
  114. /*
  115. * bond_inet6addr_event: handle inet6addr notifier chain events.
  116. *
  117. * We keep track of device IPv6 addresses primarily to use as source
  118. * addresses in NS probes.
  119. *
  120. * We track one IPv6 for the main device (if it has one).
  121. */
  122. static int bond_inet6addr_event(struct notifier_block *this,
  123. unsigned long event,
  124. void *ptr)
  125. {
  126. struct inet6_ifaddr *ifa = ptr;
  127. struct net_device *vlan_dev, *event_dev = ifa->idev->dev;
  128. struct bonding *bond;
  129. struct vlan_entry *vlan;
  130. if (dev_net(event_dev) != &init_net)
  131. return NOTIFY_DONE;
  132. list_for_each_entry(bond, &bond_dev_list, bond_list) {
  133. if (bond->dev == event_dev) {
  134. switch (event) {
  135. case NETDEV_UP:
  136. if (ipv6_addr_any(&bond->master_ipv6))
  137. ipv6_addr_copy(&bond->master_ipv6,
  138. &ifa->addr);
  139. return NOTIFY_OK;
  140. case NETDEV_DOWN:
  141. if (ipv6_addr_equal(&bond->master_ipv6,
  142. &ifa->addr))
  143. bond_glean_dev_ipv6(bond->dev,
  144. &bond->master_ipv6);
  145. return NOTIFY_OK;
  146. default:
  147. return NOTIFY_DONE;
  148. }
  149. }
  150. list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
  151. vlan_dev = vlan_group_get_device(bond->vlgrp,
  152. vlan->vlan_id);
  153. if (vlan_dev == event_dev) {
  154. switch (event) {
  155. case NETDEV_UP:
  156. if (ipv6_addr_any(&vlan->vlan_ipv6))
  157. ipv6_addr_copy(&vlan->vlan_ipv6,
  158. &ifa->addr);
  159. return NOTIFY_OK;
  160. case NETDEV_DOWN:
  161. if (ipv6_addr_equal(&vlan->vlan_ipv6,
  162. &ifa->addr))
  163. bond_glean_dev_ipv6(vlan_dev,
  164. &vlan->vlan_ipv6);
  165. return NOTIFY_OK;
  166. default:
  167. return NOTIFY_DONE;
  168. }
  169. }
  170. }
  171. }
  172. return NOTIFY_DONE;
  173. }
  174. static struct notifier_block bond_inet6addr_notifier = {
  175. .notifier_call = bond_inet6addr_event,
  176. };
  177. void bond_register_ipv6_notifier(void)
  178. {
  179. register_inet6addr_notifier(&bond_inet6addr_notifier);
  180. }
  181. void bond_unregister_ipv6_notifier(void)
  182. {
  183. unregister_inet6addr_notifier(&bond_inet6addr_notifier);
  184. }