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