blob: 121b073a6c3fae1985c164071f0a8f285dba0c14 [file] [log] [blame]
Brian Haley305d5522008-11-04 17:51:14 -08001/*
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
Joe Perchesa4aee5c2009-12-13 20:06:07 -080023#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
Brian Haley305d5522008-11-04 17:51:14 -080025#include <linux/types.h>
26#include <linux/if_vlan.h>
27#include <net/ipv6.h>
28#include <net/ndisc.h>
29#include <net/addrconf.h>
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000030#include <net/netns/generic.h>
Brian Haley305d5522008-11-04 17:51:14 -080031#include "bonding.h"
32
33/*
34 * Assign bond->master_ipv6 to the next IPv6 address in the list, or
35 * zero it out if there are none.
36 */
37static void bond_glean_dev_ipv6(struct net_device *dev, struct in6_addr *addr)
38{
39 struct inet6_dev *idev;
Brian Haley305d5522008-11-04 17:51:14 -080040
41 if (!dev)
42 return;
43
44 idev = in6_dev_get(dev);
45 if (!idev)
46 return;
47
48 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +000049 if (!list_empty(&idev->addr_list)) {
50 struct inet6_ifaddr *ifa
51 = list_first_entry(&idev->addr_list,
52 struct inet6_ifaddr, if_list);
Brian Haley305d5522008-11-04 17:51:14 -080053 ipv6_addr_copy(addr, &ifa->addr);
stephen hemminger502a2ff2010-03-17 20:31:13 +000054 } else
Brian Haley305d5522008-11-04 17:51:14 -080055 ipv6_addr_set(addr, 0, 0, 0, 0);
56
57 read_unlock_bh(&idev->lock);
58
59 in6_dev_put(idev);
60}
61
62static void bond_na_send(struct net_device *slave_dev,
63 struct in6_addr *daddr,
64 int router,
65 unsigned short vlan_id)
66{
67 struct in6_addr mcaddr;
68 struct icmp6hdr icmp6h = {
69 .icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT,
70 };
71 struct sk_buff *skb;
72
73 icmp6h.icmp6_router = router;
74 icmp6h.icmp6_solicited = 0;
75 icmp6h.icmp6_override = 1;
76
77 addrconf_addr_solict_mult(daddr, &mcaddr);
78
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -080079 pr_debug("ipv6 na on slave %s: dest %pI6, src %pI6\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -080080 slave_dev->name, &mcaddr, daddr);
Brian Haley305d5522008-11-04 17:51:14 -080081
82 skb = ndisc_build_skb(slave_dev, &mcaddr, daddr, &icmp6h, daddr,
83 ND_OPT_TARGET_LL_ADDR);
84
85 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -080086 pr_err("NA packet allocation failed\n");
Brian Haley305d5522008-11-04 17:51:14 -080087 return;
88 }
89
90 if (vlan_id) {
91 skb = vlan_put_tag(skb, vlan_id);
92 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -080093 pr_err("failed to insert VLAN tag\n");
Brian Haley305d5522008-11-04 17:51:14 -080094 return;
95 }
96 }
97
98 ndisc_send_skb(skb, slave_dev, NULL, &mcaddr, daddr, &icmp6h);
99}
100
101/*
102 * Kick out an unsolicited Neighbor Advertisement for an IPv6 address on
103 * the bonding master. This will help the switch learn our address
104 * if in active-backup mode.
105 *
106 * Caller must hold curr_slave_lock for read or better
107 */
108void bond_send_unsolicited_na(struct bonding *bond)
109{
110 struct slave *slave = bond->curr_active_slave;
111 struct vlan_entry *vlan;
112 struct inet6_dev *idev;
113 int is_router;
114
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800115 pr_debug("%s: bond %s slave %s\n", bond->dev->name,
116 __func__, slave ? slave->dev->name : "NULL");
Brian Haley305d5522008-11-04 17:51:14 -0800117
118 if (!slave || !bond->send_unsol_na ||
119 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
120 return;
121
122 bond->send_unsol_na--;
123
124 idev = in6_dev_get(bond->dev);
125 if (!idev)
126 return;
127
128 is_router = !!idev->cnf.forwarding;
129
130 in6_dev_put(idev);
131
132 if (!ipv6_addr_any(&bond->master_ipv6))
133 bond_na_send(slave->dev, &bond->master_ipv6, is_router, 0);
134
135 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
136 if (!ipv6_addr_any(&vlan->vlan_ipv6)) {
137 bond_na_send(slave->dev, &vlan->vlan_ipv6, is_router,
138 vlan->vlan_id);
139 }
140 }
141}
142
143/*
144 * bond_inet6addr_event: handle inet6addr notifier chain events.
145 *
146 * We keep track of device IPv6 addresses primarily to use as source
147 * addresses in NS probes.
148 *
149 * We track one IPv6 for the main device (if it has one).
150 */
151static int bond_inet6addr_event(struct notifier_block *this,
152 unsigned long event,
153 void *ptr)
154{
155 struct inet6_ifaddr *ifa = ptr;
156 struct net_device *vlan_dev, *event_dev = ifa->idev->dev;
157 struct bonding *bond;
158 struct vlan_entry *vlan;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +0000159 struct bond_net *bn = net_generic(dev_net(event_dev), bond_net_id);
Brian Haley305d5522008-11-04 17:51:14 -0800160
Eric W. Biedermanec87fd32009-10-29 14:18:26 +0000161 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Brian Haley305d5522008-11-04 17:51:14 -0800162 if (bond->dev == event_dev) {
163 switch (event) {
164 case NETDEV_UP:
165 if (ipv6_addr_any(&bond->master_ipv6))
166 ipv6_addr_copy(&bond->master_ipv6,
167 &ifa->addr);
168 return NOTIFY_OK;
169 case NETDEV_DOWN:
170 if (ipv6_addr_equal(&bond->master_ipv6,
171 &ifa->addr))
172 bond_glean_dev_ipv6(bond->dev,
173 &bond->master_ipv6);
174 return NOTIFY_OK;
175 default:
176 return NOTIFY_DONE;
177 }
178 }
179
180 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Eric Dumazetdb5dda92010-07-19 03:24:14 +0000181 if (!bond->vlgrp)
182 continue;
Brian Haley305d5522008-11-04 17:51:14 -0800183 vlan_dev = vlan_group_get_device(bond->vlgrp,
184 vlan->vlan_id);
185 if (vlan_dev == event_dev) {
186 switch (event) {
187 case NETDEV_UP:
188 if (ipv6_addr_any(&vlan->vlan_ipv6))
189 ipv6_addr_copy(&vlan->vlan_ipv6,
190 &ifa->addr);
191 return NOTIFY_OK;
192 case NETDEV_DOWN:
193 if (ipv6_addr_equal(&vlan->vlan_ipv6,
194 &ifa->addr))
195 bond_glean_dev_ipv6(vlan_dev,
196 &vlan->vlan_ipv6);
197 return NOTIFY_OK;
198 default:
199 return NOTIFY_DONE;
200 }
201 }
202 }
203 }
204 return NOTIFY_DONE;
205}
206
207static struct notifier_block bond_inet6addr_notifier = {
208 .notifier_call = bond_inet6addr_event,
209};
210
211void bond_register_ipv6_notifier(void)
212{
213 register_inet6addr_notifier(&bond_inet6addr_notifier);
214}
215
216void bond_unregister_ipv6_notifier(void)
217{
218 unregister_inet6addr_notifier(&bond_inet6addr_notifier);
219}
220