bond_ipv6.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/types.h>
  24. #include <linux/if_vlan.h>
  25. #include <net/ipv6.h>
  26. #include <net/ndisc.h>
  27. #include <net/addrconf.h>
  28. #include <net/netns/generic.h>
  29. #include "bonding.h"
  30. /*
  31. * Assign bond->master_ipv6 to the next IPv6 address in the list, or
  32. * zero it out if there are none.
  33. */
  34. static void bond_glean_dev_ipv6(struct net_device *dev, struct in6_addr *addr)
  35. {
  36. struct inet6_dev *idev;
  37. if (!dev)
  38. return;
  39. idev = in6_dev_get(dev);
  40. if (!idev)
  41. return;
  42. read_lock_bh(&idev->lock);
  43. if (!list_empty(&idev->addr_list)) {
  44. struct inet6_ifaddr *ifa
  45. = list_first_entry(&idev->addr_list,
  46. struct inet6_ifaddr, if_list);
  47. ipv6_addr_copy(addr, &ifa->addr);
  48. } else
  49. ipv6_addr_set(addr, 0, 0, 0, 0);
  50. read_unlock_bh(&idev->lock);
  51. in6_dev_put(idev);
  52. }
  53. static void bond_na_send(struct net_device *slave_dev,
  54. struct in6_addr *daddr,
  55. int router,
  56. unsigned short vlan_id)
  57. {
  58. struct in6_addr mcaddr;
  59. struct icmp6hdr icmp6h = {
  60. .icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT,
  61. };
  62. struct sk_buff *skb;
  63. icmp6h.icmp6_router = router;
  64. icmp6h.icmp6_solicited = 0;
  65. icmp6h.icmp6_override = 1;
  66. addrconf_addr_solict_mult(daddr, &mcaddr);
  67. pr_debug("ipv6 na on slave %s: dest %pI6, src %pI6\n",
  68. slave_dev->name, &mcaddr, daddr);
  69. skb = ndisc_build_skb(slave_dev, &mcaddr, daddr, &icmp6h, daddr,
  70. ND_OPT_TARGET_LL_ADDR);
  71. if (!skb) {
  72. pr_err("NA packet allocation failed\n");
  73. return;
  74. }
  75. if (vlan_id) {
  76. /* The Ethernet header is not present yet, so it is
  77. * too early to insert a VLAN tag. Force use of an
  78. * out-of-line tag here and let dev_hard_start_xmit()
  79. * insert it if the slave hardware can't.
  80. */
  81. skb = __vlan_hwaccel_put_tag(skb, vlan_id);
  82. if (!skb) {
  83. pr_err("failed to insert VLAN tag\n");
  84. return;
  85. }
  86. }
  87. ndisc_send_skb(skb, slave_dev, NULL, &mcaddr, daddr, &icmp6h);
  88. }
  89. /*
  90. * Kick out an unsolicited Neighbor Advertisement for an IPv6 address on
  91. * the bonding master. This will help the switch learn our address
  92. * if in active-backup mode.
  93. *
  94. * Caller must hold curr_slave_lock for read or better
  95. */
  96. void bond_send_unsolicited_na(struct bonding *bond)
  97. {
  98. struct slave *slave = bond->curr_active_slave;
  99. struct vlan_entry *vlan;
  100. struct inet6_dev *idev;
  101. int is_router;
  102. pr_debug("%s: bond %s slave %s\n", bond->dev->name,
  103. __func__, slave ? slave->dev->name : "NULL");
  104. if (!slave || !bond->send_unsol_na ||
  105. test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
  106. return;
  107. bond->send_unsol_na--;
  108. idev = in6_dev_get(bond->dev);
  109. if (!idev)
  110. return;
  111. is_router = !!idev->cnf.forwarding;
  112. in6_dev_put(idev);
  113. if (!ipv6_addr_any(&bond->master_ipv6))
  114. bond_na_send(slave->dev, &bond->master_ipv6, is_router, 0);
  115. list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
  116. if (!ipv6_addr_any(&vlan->vlan_ipv6)) {
  117. bond_na_send(slave->dev, &vlan->vlan_ipv6, is_router,
  118. vlan->vlan_id);
  119. }
  120. }
  121. }
  122. /*
  123. * bond_inet6addr_event: handle inet6addr notifier chain events.
  124. *
  125. * We keep track of device IPv6 addresses primarily to use as source
  126. * addresses in NS probes.
  127. *
  128. * We track one IPv6 for the main device (if it has one).
  129. */
  130. static int bond_inet6addr_event(struct notifier_block *this,
  131. unsigned long event,
  132. void *ptr)
  133. {
  134. struct inet6_ifaddr *ifa = ptr;
  135. struct net_device *vlan_dev, *event_dev = ifa->idev->dev;
  136. struct bonding *bond;
  137. struct vlan_entry *vlan;
  138. struct bond_net *bn = net_generic(dev_net(event_dev), bond_net_id);
  139. list_for_each_entry(bond, &bn->dev_list, bond_list) {
  140. if (bond->dev == event_dev) {
  141. switch (event) {
  142. case NETDEV_UP:
  143. if (ipv6_addr_any(&bond->master_ipv6))
  144. ipv6_addr_copy(&bond->master_ipv6,
  145. &ifa->addr);
  146. return NOTIFY_OK;
  147. case NETDEV_DOWN:
  148. if (ipv6_addr_equal(&bond->master_ipv6,
  149. &ifa->addr))
  150. bond_glean_dev_ipv6(bond->dev,
  151. &bond->master_ipv6);
  152. return NOTIFY_OK;
  153. default:
  154. return NOTIFY_DONE;
  155. }
  156. }
  157. list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
  158. if (!bond->vlgrp)
  159. continue;
  160. vlan_dev = vlan_group_get_device(bond->vlgrp,
  161. vlan->vlan_id);
  162. if (vlan_dev == event_dev) {
  163. switch (event) {
  164. case NETDEV_UP:
  165. if (ipv6_addr_any(&vlan->vlan_ipv6))
  166. ipv6_addr_copy(&vlan->vlan_ipv6,
  167. &ifa->addr);
  168. return NOTIFY_OK;
  169. case NETDEV_DOWN:
  170. if (ipv6_addr_equal(&vlan->vlan_ipv6,
  171. &ifa->addr))
  172. bond_glean_dev_ipv6(vlan_dev,
  173. &vlan->vlan_ipv6);
  174. return NOTIFY_OK;
  175. default:
  176. return NOTIFY_DONE;
  177. }
  178. }
  179. }
  180. }
  181. return NOTIFY_DONE;
  182. }
  183. static struct notifier_block bond_inet6addr_notifier = {
  184. .notifier_call = bond_inet6addr_event,
  185. };
  186. void bond_register_ipv6_notifier(void)
  187. {
  188. register_inet6addr_notifier(&bond_inet6addr_notifier);
  189. }
  190. void bond_unregister_ipv6_notifier(void)
  191. {
  192. unregister_inet6addr_notifier(&bond_inet6addr_notifier);
  193. }