wl1251: fix error handling in wl1251_op_config()
[safe/jmp/linux-2.6] / drivers / net / bonding / bond_sysfs.c
1 /*
2  * Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
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/kernel.h>
23 #include <linux/module.h>
24 #include <linux/device.h>
25 #include <linux/sched.h>
26 #include <linux/sysdev.h>
27 #include <linux/fs.h>
28 #include <linux/types.h>
29 #include <linux/string.h>
30 #include <linux/netdevice.h>
31 #include <linux/inetdevice.h>
32 #include <linux/in.h>
33 #include <linux/sysfs.h>
34 #include <linux/ctype.h>
35 #include <linux/inet.h>
36 #include <linux/rtnetlink.h>
37 #include <linux/etherdevice.h>
38 #include <net/net_namespace.h>
39 #include <net/netns/generic.h>
40 #include <linux/nsproxy.h>
41
42 #include "bonding.h"
43
44 #define to_dev(obj)     container_of(obj, struct device, kobj)
45 #define to_bond(cd)     ((struct bonding *)(netdev_priv(to_net_dev(cd))))
46
47 /*
48  * "show" function for the bond_masters attribute.
49  * The class parameter is ignored.
50  */
51 static ssize_t bonding_show_bonds(struct class *cls, char *buf)
52 {
53         struct net *net = current->nsproxy->net_ns;
54         struct bond_net *bn = net_generic(net, bond_net_id);
55         int res = 0;
56         struct bonding *bond;
57
58         rtnl_lock();
59
60         list_for_each_entry(bond, &bn->dev_list, bond_list) {
61                 if (res > (PAGE_SIZE - IFNAMSIZ)) {
62                         /* not enough space for another interface name */
63                         if ((PAGE_SIZE - res) > 10)
64                                 res = PAGE_SIZE - 10;
65                         res += sprintf(buf + res, "++more++ ");
66                         break;
67                 }
68                 res += sprintf(buf + res, "%s ", bond->dev->name);
69         }
70         if (res)
71                 buf[res-1] = '\n'; /* eat the leftover space */
72
73         rtnl_unlock();
74         return res;
75 }
76
77 static struct net_device *bond_get_by_name(struct net *net, const char *ifname)
78 {
79         struct bond_net *bn = net_generic(net, bond_net_id);
80         struct bonding *bond;
81
82         list_for_each_entry(bond, &bn->dev_list, bond_list) {
83                 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
84                         return bond->dev;
85         }
86         return NULL;
87 }
88
89 /*
90  * "store" function for the bond_masters attribute.  This is what
91  * creates and deletes entire bonds.
92  *
93  * The class parameter is ignored.
94  *
95  */
96
97 static ssize_t bonding_store_bonds(struct class *cls,
98                                    const char *buffer, size_t count)
99 {
100         struct net *net = current->nsproxy->net_ns;
101         char command[IFNAMSIZ + 1] = {0, };
102         char *ifname;
103         int rv, res = count;
104
105         sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
106         ifname = command + 1;
107         if ((strlen(command) <= 1) ||
108             !dev_valid_name(ifname))
109                 goto err_no_cmd;
110
111         if (command[0] == '+') {
112                 pr_info(DRV_NAME
113                         ": %s is being created...\n", ifname);
114                 rv = bond_create(net, ifname);
115                 if (rv) {
116                         pr_info(DRV_NAME ": Bond creation failed.\n");
117                         res = rv;
118                 }
119         } else if (command[0] == '-') {
120                 struct net_device *bond_dev;
121
122                 rtnl_lock();
123                 bond_dev = bond_get_by_name(net, ifname);
124                 if (bond_dev) {
125                         pr_info(DRV_NAME ": %s is being deleted...\n",
126                                 ifname);
127                         unregister_netdevice(bond_dev);
128                 } else {
129                         pr_err(DRV_NAME ": unable to delete non-existent %s\n",
130                                ifname);
131                         res = -ENODEV;
132                 }
133                 rtnl_unlock();
134         } else
135                 goto err_no_cmd;
136
137         /* Always return either count or an error.  If you return 0, you'll
138          * get called forever, which is bad.
139          */
140         return res;
141
142 err_no_cmd:
143         pr_err(DRV_NAME ": no command found in bonding_masters."
144                " Use +ifname or -ifname.\n");
145         return -EPERM;
146 }
147
148 /* class attribute for bond_masters file.  This ends up in /sys/class/net */
149 static CLASS_ATTR(bonding_masters,  S_IWUSR | S_IRUGO,
150                   bonding_show_bonds, bonding_store_bonds);
151
152 int bond_create_slave_symlinks(struct net_device *master,
153                                struct net_device *slave)
154 {
155         char linkname[IFNAMSIZ+7];
156         int ret = 0;
157
158         /* first, create a link from the slave back to the master */
159         ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
160                                 "master");
161         if (ret)
162                 return ret;
163         /* next, create a link from the master to the slave */
164         sprintf(linkname, "slave_%s", slave->name);
165         ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
166                                 linkname);
167         return ret;
168
169 }
170
171 void bond_destroy_slave_symlinks(struct net_device *master,
172                                  struct net_device *slave)
173 {
174         char linkname[IFNAMSIZ+7];
175
176         sysfs_remove_link(&(slave->dev.kobj), "master");
177         sprintf(linkname, "slave_%s", slave->name);
178         sysfs_remove_link(&(master->dev.kobj), linkname);
179 }
180
181
182 /*
183  * Show the slaves in the current bond.
184  */
185 static ssize_t bonding_show_slaves(struct device *d,
186                                    struct device_attribute *attr, char *buf)
187 {
188         struct slave *slave;
189         int i, res = 0;
190         struct bonding *bond = to_bond(d);
191
192         read_lock(&bond->lock);
193         bond_for_each_slave(bond, slave, i) {
194                 if (res > (PAGE_SIZE - IFNAMSIZ)) {
195                         /* not enough space for another interface name */
196                         if ((PAGE_SIZE - res) > 10)
197                                 res = PAGE_SIZE - 10;
198                         res += sprintf(buf + res, "++more++ ");
199                         break;
200                 }
201                 res += sprintf(buf + res, "%s ", slave->dev->name);
202         }
203         read_unlock(&bond->lock);
204         if (res)
205                 buf[res-1] = '\n'; /* eat the leftover space */
206         return res;
207 }
208
209 /*
210  * Set the slaves in the current bond.  The bond interface must be
211  * up for this to succeed.
212  * This function is largely the same flow as bonding_update_bonds().
213  */
214 static ssize_t bonding_store_slaves(struct device *d,
215                                     struct device_attribute *attr,
216                                     const char *buffer, size_t count)
217 {
218         char command[IFNAMSIZ + 1] = { 0, };
219         char *ifname;
220         int i, res, found, ret = count;
221         u32 original_mtu;
222         struct slave *slave;
223         struct net_device *dev = NULL;
224         struct bonding *bond = to_bond(d);
225
226         /* Quick sanity check -- is the bond interface up? */
227         if (!(bond->dev->flags & IFF_UP)) {
228                 pr_warning(DRV_NAME ": %s: doing slave updates when "
229                            "interface is down.\n", bond->dev->name);
230         }
231
232         /* Note:  We can't hold bond->lock here, as bond_create grabs it. */
233
234         if (!rtnl_trylock())
235                 return restart_syscall();
236
237         sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
238         ifname = command + 1;
239         if ((strlen(command) <= 1) ||
240             !dev_valid_name(ifname))
241                 goto err_no_cmd;
242
243         if (command[0] == '+') {
244
245                 /* Got a slave name in ifname.  Is it already in the list? */
246                 found = 0;
247
248                 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
249                 if (!dev) {
250                         pr_info(DRV_NAME
251                                ": %s: Interface %s does not exist!\n",
252                                bond->dev->name, ifname);
253                         ret = -ENODEV;
254                         goto out;
255                 }
256
257                 if (dev->flags & IFF_UP) {
258                         pr_err(DRV_NAME
259                                ": %s: Error: Unable to enslave %s "
260                                "because it is already up.\n",
261                                bond->dev->name, dev->name);
262                         ret = -EPERM;
263                         goto out;
264                 }
265
266                 read_lock(&bond->lock);
267                 bond_for_each_slave(bond, slave, i)
268                         if (slave->dev == dev) {
269                                 pr_err(DRV_NAME
270                                        ": %s: Interface %s is already enslaved!\n",
271                                        bond->dev->name, ifname);
272                                 ret = -EPERM;
273                                 read_unlock(&bond->lock);
274                                 goto out;
275                         }
276                 read_unlock(&bond->lock);
277
278                 pr_info(DRV_NAME ": %s: Adding slave %s.\n",
279                         bond->dev->name, ifname);
280
281                 /* If this is the first slave, then we need to set
282                    the master's hardware address to be the same as the
283                    slave's. */
284                 if (is_zero_ether_addr(bond->dev->dev_addr))
285                         memcpy(bond->dev->dev_addr, dev->dev_addr,
286                                dev->addr_len);
287
288                 /* Set the slave's MTU to match the bond */
289                 original_mtu = dev->mtu;
290                 res = dev_set_mtu(dev, bond->dev->mtu);
291                 if (res) {
292                         ret = res;
293                         goto out;
294                 }
295
296                 res = bond_enslave(bond->dev, dev);
297                 bond_for_each_slave(bond, slave, i)
298                         if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0)
299                                 slave->original_mtu = original_mtu;
300                 if (res)
301                         ret = res;
302
303                 goto out;
304         }
305
306         if (command[0] == '-') {
307                 dev = NULL;
308                 original_mtu = 0;
309                 bond_for_each_slave(bond, slave, i)
310                         if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
311                                 dev = slave->dev;
312                                 original_mtu = slave->original_mtu;
313                                 break;
314                         }
315                 if (dev) {
316                         pr_info(DRV_NAME ": %s: Removing slave %s\n",
317                                 bond->dev->name, dev->name);
318                                 res = bond_release(bond->dev, dev);
319                         if (res) {
320                                 ret = res;
321                                 goto out;
322                         }
323                         /* set the slave MTU to the default */
324                         dev_set_mtu(dev, original_mtu);
325                 } else {
326                         pr_err(DRV_NAME ": unable to remove non-existent"
327                                " slave %s for bond %s.\n",
328                                 ifname, bond->dev->name);
329                         ret = -ENODEV;
330                 }
331                 goto out;
332         }
333
334 err_no_cmd:
335         pr_err(DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
336         ret = -EPERM;
337
338 out:
339         rtnl_unlock();
340         return ret;
341 }
342
343 static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
344                    bonding_store_slaves);
345
346 /*
347  * Show and set the bonding mode.  The bond interface must be down to
348  * change the mode.
349  */
350 static ssize_t bonding_show_mode(struct device *d,
351                                  struct device_attribute *attr, char *buf)
352 {
353         struct bonding *bond = to_bond(d);
354
355         return sprintf(buf, "%s %d\n",
356                         bond_mode_tbl[bond->params.mode].modename,
357                         bond->params.mode);
358 }
359
360 static ssize_t bonding_store_mode(struct device *d,
361                                   struct device_attribute *attr,
362                                   const char *buf, size_t count)
363 {
364         int new_value, ret = count;
365         struct bonding *bond = to_bond(d);
366
367         if (bond->dev->flags & IFF_UP) {
368                 pr_err(DRV_NAME ": unable to update mode of %s"
369                        " because interface is up.\n", bond->dev->name);
370                 ret = -EPERM;
371                 goto out;
372         }
373
374         new_value = bond_parse_parm(buf, bond_mode_tbl);
375         if (new_value < 0)  {
376                 pr_err(DRV_NAME
377                        ": %s: Ignoring invalid mode value %.*s.\n",
378                        bond->dev->name,
379                        (int)strlen(buf) - 1, buf);
380                 ret = -EINVAL;
381                 goto out;
382         } else {
383                 if (bond->params.mode == BOND_MODE_8023AD)
384                         bond_unset_master_3ad_flags(bond);
385
386                 if (bond->params.mode == BOND_MODE_ALB)
387                         bond_unset_master_alb_flags(bond);
388
389                 bond->params.mode = new_value;
390                 bond_set_mode_ops(bond, bond->params.mode);
391                 pr_info(DRV_NAME ": %s: setting mode to %s (%d).\n",
392                        bond->dev->name, bond_mode_tbl[new_value].modename,
393                        new_value);
394         }
395 out:
396         return ret;
397 }
398 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
399                    bonding_show_mode, bonding_store_mode);
400
401 /*
402  * Show and set the bonding transmit hash method.
403  * The bond interface must be down to change the xmit hash policy.
404  */
405 static ssize_t bonding_show_xmit_hash(struct device *d,
406                                       struct device_attribute *attr,
407                                       char *buf)
408 {
409         struct bonding *bond = to_bond(d);
410
411         return sprintf(buf, "%s %d\n",
412                        xmit_hashtype_tbl[bond->params.xmit_policy].modename,
413                        bond->params.xmit_policy);
414 }
415
416 static ssize_t bonding_store_xmit_hash(struct device *d,
417                                        struct device_attribute *attr,
418                                        const char *buf, size_t count)
419 {
420         int new_value, ret = count;
421         struct bonding *bond = to_bond(d);
422
423         if (bond->dev->flags & IFF_UP) {
424                 pr_err(DRV_NAME
425                        "%s: Interface is up. Unable to update xmit policy.\n",
426                        bond->dev->name);
427                 ret = -EPERM;
428                 goto out;
429         }
430
431         new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
432         if (new_value < 0)  {
433                 pr_err(DRV_NAME
434                        ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
435                        bond->dev->name,
436                        (int)strlen(buf) - 1, buf);
437                 ret = -EINVAL;
438                 goto out;
439         } else {
440                 bond->params.xmit_policy = new_value;
441                 bond_set_mode_ops(bond, bond->params.mode);
442                 pr_info(DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
443                         bond->dev->name,
444                         xmit_hashtype_tbl[new_value].modename, new_value);
445         }
446 out:
447         return ret;
448 }
449 static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
450                    bonding_show_xmit_hash, bonding_store_xmit_hash);
451
452 /*
453  * Show and set arp_validate.
454  */
455 static ssize_t bonding_show_arp_validate(struct device *d,
456                                          struct device_attribute *attr,
457                                          char *buf)
458 {
459         struct bonding *bond = to_bond(d);
460
461         return sprintf(buf, "%s %d\n",
462                        arp_validate_tbl[bond->params.arp_validate].modename,
463                        bond->params.arp_validate);
464 }
465
466 static ssize_t bonding_store_arp_validate(struct device *d,
467                                           struct device_attribute *attr,
468                                           const char *buf, size_t count)
469 {
470         int new_value;
471         struct bonding *bond = to_bond(d);
472
473         new_value = bond_parse_parm(buf, arp_validate_tbl);
474         if (new_value < 0) {
475                 pr_err(DRV_NAME
476                        ": %s: Ignoring invalid arp_validate value %s\n",
477                        bond->dev->name, buf);
478                 return -EINVAL;
479         }
480         if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
481                 pr_err(DRV_NAME
482                        ": %s: arp_validate only supported in active-backup mode.\n",
483                        bond->dev->name);
484                 return -EINVAL;
485         }
486         pr_info(DRV_NAME ": %s: setting arp_validate to %s (%d).\n",
487                bond->dev->name, arp_validate_tbl[new_value].modename,
488                new_value);
489
490         if (!bond->params.arp_validate && new_value)
491                 bond_register_arp(bond);
492         else if (bond->params.arp_validate && !new_value)
493                 bond_unregister_arp(bond);
494
495         bond->params.arp_validate = new_value;
496
497         return count;
498 }
499
500 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
501                    bonding_store_arp_validate);
502
503 /*
504  * Show and store fail_over_mac.  User only allowed to change the
505  * value when there are no slaves.
506  */
507 static ssize_t bonding_show_fail_over_mac(struct device *d,
508                                           struct device_attribute *attr,
509                                           char *buf)
510 {
511         struct bonding *bond = to_bond(d);
512
513         return sprintf(buf, "%s %d\n",
514                        fail_over_mac_tbl[bond->params.fail_over_mac].modename,
515                        bond->params.fail_over_mac);
516 }
517
518 static ssize_t bonding_store_fail_over_mac(struct device *d,
519                                            struct device_attribute *attr,
520                                            const char *buf, size_t count)
521 {
522         int new_value;
523         struct bonding *bond = to_bond(d);
524
525         if (bond->slave_cnt != 0) {
526                 pr_err(DRV_NAME
527                        ": %s: Can't alter fail_over_mac with slaves in bond.\n",
528                        bond->dev->name);
529                 return -EPERM;
530         }
531
532         new_value = bond_parse_parm(buf, fail_over_mac_tbl);
533         if (new_value < 0) {
534                 pr_err(DRV_NAME
535                        ": %s: Ignoring invalid fail_over_mac value %s.\n",
536                        bond->dev->name, buf);
537                 return -EINVAL;
538         }
539
540         bond->params.fail_over_mac = new_value;
541         pr_info(DRV_NAME ": %s: Setting fail_over_mac to %s (%d).\n",
542                bond->dev->name, fail_over_mac_tbl[new_value].modename,
543                new_value);
544
545         return count;
546 }
547
548 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
549                    bonding_show_fail_over_mac, bonding_store_fail_over_mac);
550
551 /*
552  * Show and set the arp timer interval.  There are two tricky bits
553  * here.  First, if ARP monitoring is activated, then we must disable
554  * MII monitoring.  Second, if the ARP timer isn't running, we must
555  * start it.
556  */
557 static ssize_t bonding_show_arp_interval(struct device *d,
558                                          struct device_attribute *attr,
559                                          char *buf)
560 {
561         struct bonding *bond = to_bond(d);
562
563         return sprintf(buf, "%d\n", bond->params.arp_interval);
564 }
565
566 static ssize_t bonding_store_arp_interval(struct device *d,
567                                           struct device_attribute *attr,
568                                           const char *buf, size_t count)
569 {
570         int new_value, ret = count;
571         struct bonding *bond = to_bond(d);
572
573         if (sscanf(buf, "%d", &new_value) != 1) {
574                 pr_err(DRV_NAME
575                        ": %s: no arp_interval value specified.\n",
576                        bond->dev->name);
577                 ret = -EINVAL;
578                 goto out;
579         }
580         if (new_value < 0) {
581                 pr_err(DRV_NAME
582                        ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
583                        bond->dev->name, new_value, INT_MAX);
584                 ret = -EINVAL;
585                 goto out;
586         }
587
588         pr_info(DRV_NAME
589                ": %s: Setting ARP monitoring interval to %d.\n",
590                bond->dev->name, new_value);
591         bond->params.arp_interval = new_value;
592         if (bond->params.arp_interval)
593                 bond->dev->priv_flags |= IFF_MASTER_ARPMON;
594         if (bond->params.miimon) {
595                 pr_info(DRV_NAME
596                        ": %s: ARP monitoring cannot be used with MII monitoring. "
597                        "%s Disabling MII monitoring.\n",
598                        bond->dev->name, bond->dev->name);
599                 bond->params.miimon = 0;
600                 if (delayed_work_pending(&bond->mii_work)) {
601                         cancel_delayed_work(&bond->mii_work);
602                         flush_workqueue(bond->wq);
603                 }
604         }
605         if (!bond->params.arp_targets[0]) {
606                 pr_info(DRV_NAME
607                        ": %s: ARP monitoring has been set up, "
608                        "but no ARP targets have been specified.\n",
609                        bond->dev->name);
610         }
611         if (bond->dev->flags & IFF_UP) {
612                 /* If the interface is up, we may need to fire off
613                  * the ARP timer.  If the interface is down, the
614                  * timer will get fired off when the open function
615                  * is called.
616                  */
617                 if (!delayed_work_pending(&bond->arp_work)) {
618                         if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
619                                 INIT_DELAYED_WORK(&bond->arp_work,
620                                                   bond_activebackup_arp_mon);
621                         else
622                                 INIT_DELAYED_WORK(&bond->arp_work,
623                                                   bond_loadbalance_arp_mon);
624
625                         queue_delayed_work(bond->wq, &bond->arp_work, 0);
626                 }
627         }
628
629 out:
630         return ret;
631 }
632 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
633                    bonding_show_arp_interval, bonding_store_arp_interval);
634
635 /*
636  * Show and set the arp targets.
637  */
638 static ssize_t bonding_show_arp_targets(struct device *d,
639                                         struct device_attribute *attr,
640                                         char *buf)
641 {
642         int i, res = 0;
643         struct bonding *bond = to_bond(d);
644
645         for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
646                 if (bond->params.arp_targets[i])
647                         res += sprintf(buf + res, "%pI4 ",
648                                        &bond->params.arp_targets[i]);
649         }
650         if (res)
651                 buf[res-1] = '\n'; /* eat the leftover space */
652         return res;
653 }
654
655 static ssize_t bonding_store_arp_targets(struct device *d,
656                                          struct device_attribute *attr,
657                                          const char *buf, size_t count)
658 {
659         __be32 newtarget;
660         int i = 0, done = 0, ret = count;
661         struct bonding *bond = to_bond(d);
662         __be32 *targets;
663
664         targets = bond->params.arp_targets;
665         newtarget = in_aton(buf + 1);
666         /* look for adds */
667         if (buf[0] == '+') {
668                 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
669                         pr_err(DRV_NAME
670                                ": %s: invalid ARP target %pI4 specified for addition\n",
671                                bond->dev->name, &newtarget);
672                         ret = -EINVAL;
673                         goto out;
674                 }
675                 /* look for an empty slot to put the target in, and check for dupes */
676                 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
677                         if (targets[i] == newtarget) { /* duplicate */
678                                 pr_err(DRV_NAME
679                                        ": %s: ARP target %pI4 is already present\n",
680                                        bond->dev->name, &newtarget);
681                                 ret = -EINVAL;
682                                 goto out;
683                         }
684                         if (targets[i] == 0) {
685                                 pr_info(DRV_NAME
686                                        ": %s: adding ARP target %pI4.\n",
687                                        bond->dev->name, &newtarget);
688                                 done = 1;
689                                 targets[i] = newtarget;
690                         }
691                 }
692                 if (!done) {
693                         pr_err(DRV_NAME
694                                ": %s: ARP target table is full!\n",
695                                bond->dev->name);
696                         ret = -EINVAL;
697                         goto out;
698                 }
699
700         } else if (buf[0] == '-')       {
701                 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
702                         pr_err(DRV_NAME
703                                ": %s: invalid ARP target %pI4 specified for removal\n",
704                                bond->dev->name, &newtarget);
705                         ret = -EINVAL;
706                         goto out;
707                 }
708
709                 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
710                         if (targets[i] == newtarget) {
711                                 int j;
712                                 pr_info(DRV_NAME
713                                        ": %s: removing ARP target %pI4.\n",
714                                        bond->dev->name, &newtarget);
715                                 for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
716                                         targets[j] = targets[j+1];
717
718                                 targets[j] = 0;
719                                 done = 1;
720                         }
721                 }
722                 if (!done) {
723                         pr_info(DRV_NAME
724                                ": %s: unable to remove nonexistent ARP target %pI4.\n",
725                                bond->dev->name, &newtarget);
726                         ret = -EINVAL;
727                         goto out;
728                 }
729         } else {
730                 pr_err(DRV_NAME ": no command found in arp_ip_targets file"
731                        " for bond %s. Use +<addr> or -<addr>.\n",
732                         bond->dev->name);
733                 ret = -EPERM;
734                 goto out;
735         }
736
737 out:
738         return ret;
739 }
740 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
741
742 /*
743  * Show and set the up and down delays.  These must be multiples of the
744  * MII monitoring value, and are stored internally as the multiplier.
745  * Thus, we must translate to MS for the real world.
746  */
747 static ssize_t bonding_show_downdelay(struct device *d,
748                                       struct device_attribute *attr,
749                                       char *buf)
750 {
751         struct bonding *bond = to_bond(d);
752
753         return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
754 }
755
756 static ssize_t bonding_store_downdelay(struct device *d,
757                                        struct device_attribute *attr,
758                                        const char *buf, size_t count)
759 {
760         int new_value, ret = count;
761         struct bonding *bond = to_bond(d);
762
763         if (!(bond->params.miimon)) {
764                 pr_err(DRV_NAME
765                        ": %s: Unable to set down delay as MII monitoring is disabled\n",
766                        bond->dev->name);
767                 ret = -EPERM;
768                 goto out;
769         }
770
771         if (sscanf(buf, "%d", &new_value) != 1) {
772                 pr_err(DRV_NAME
773                        ": %s: no down delay value specified.\n",
774                        bond->dev->name);
775                 ret = -EINVAL;
776                 goto out;
777         }
778         if (new_value < 0) {
779                 pr_err(DRV_NAME
780                        ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
781                        bond->dev->name, new_value, 1, INT_MAX);
782                 ret = -EINVAL;
783                 goto out;
784         } else {
785                 if ((new_value % bond->params.miimon) != 0) {
786                         pr_warning(DRV_NAME
787                                    ": %s: Warning: down delay (%d) is not a "
788                                    "multiple of miimon (%d), delay rounded "
789                                    "to %d ms\n",
790                                    bond->dev->name, new_value,
791                                    bond->params.miimon,
792                                    (new_value / bond->params.miimon) *
793                                    bond->params.miimon);
794                 }
795                 bond->params.downdelay = new_value / bond->params.miimon;
796                 pr_info(DRV_NAME ": %s: Setting down delay to %d.\n",
797                        bond->dev->name,
798                        bond->params.downdelay * bond->params.miimon);
799
800         }
801
802 out:
803         return ret;
804 }
805 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
806                    bonding_show_downdelay, bonding_store_downdelay);
807
808 static ssize_t bonding_show_updelay(struct device *d,
809                                     struct device_attribute *attr,
810                                     char *buf)
811 {
812         struct bonding *bond = to_bond(d);
813
814         return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
815
816 }
817
818 static ssize_t bonding_store_updelay(struct device *d,
819                                      struct device_attribute *attr,
820                                      const char *buf, size_t count)
821 {
822         int new_value, ret = count;
823         struct bonding *bond = to_bond(d);
824
825         if (!(bond->params.miimon)) {
826                 pr_err(DRV_NAME
827                        ": %s: Unable to set up delay as MII monitoring is disabled\n",
828                        bond->dev->name);
829                 ret = -EPERM;
830                 goto out;
831         }
832
833         if (sscanf(buf, "%d", &new_value) != 1) {
834                 pr_err(DRV_NAME
835                        ": %s: no up delay value specified.\n",
836                        bond->dev->name);
837                 ret = -EINVAL;
838                 goto out;
839         }
840         if (new_value < 0) {
841                 pr_err(DRV_NAME
842                        ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
843                        bond->dev->name, new_value, 1, INT_MAX);
844                 ret = -EINVAL;
845                 goto out;
846         } else {
847                 if ((new_value % bond->params.miimon) != 0) {
848                         pr_warning(DRV_NAME
849                                    ": %s: Warning: up delay (%d) is not a "
850                                    "multiple of miimon (%d), updelay rounded "
851                                    "to %d ms\n",
852                                    bond->dev->name, new_value,
853                                    bond->params.miimon,
854                                    (new_value / bond->params.miimon) *
855                                    bond->params.miimon);
856                 }
857                 bond->params.updelay = new_value / bond->params.miimon;
858                 pr_info(DRV_NAME ": %s: Setting up delay to %d.\n",
859                        bond->dev->name, bond->params.updelay * bond->params.miimon);
860
861         }
862
863 out:
864         return ret;
865 }
866 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
867                    bonding_show_updelay, bonding_store_updelay);
868
869 /*
870  * Show and set the LACP interval.  Interface must be down, and the mode
871  * must be set to 802.3ad mode.
872  */
873 static ssize_t bonding_show_lacp(struct device *d,
874                                  struct device_attribute *attr,
875                                  char *buf)
876 {
877         struct bonding *bond = to_bond(d);
878
879         return sprintf(buf, "%s %d\n",
880                 bond_lacp_tbl[bond->params.lacp_fast].modename,
881                 bond->params.lacp_fast);
882 }
883
884 static ssize_t bonding_store_lacp(struct device *d,
885                                   struct device_attribute *attr,
886                                   const char *buf, size_t count)
887 {
888         int new_value, ret = count;
889         struct bonding *bond = to_bond(d);
890
891         if (bond->dev->flags & IFF_UP) {
892                 pr_err(DRV_NAME
893                        ": %s: Unable to update LACP rate because interface is up.\n",
894                        bond->dev->name);
895                 ret = -EPERM;
896                 goto out;
897         }
898
899         if (bond->params.mode != BOND_MODE_8023AD) {
900                 pr_err(DRV_NAME
901                        ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
902                        bond->dev->name);
903                 ret = -EPERM;
904                 goto out;
905         }
906
907         new_value = bond_parse_parm(buf, bond_lacp_tbl);
908
909         if ((new_value == 1) || (new_value == 0)) {
910                 bond->params.lacp_fast = new_value;
911                 pr_info(DRV_NAME ": %s: Setting LACP rate to %s (%d).\n",
912                         bond->dev->name, bond_lacp_tbl[new_value].modename,
913                         new_value);
914         } else {
915                 pr_err(DRV_NAME
916                        ": %s: Ignoring invalid LACP rate value %.*s.\n",
917                        bond->dev->name, (int)strlen(buf) - 1, buf);
918                 ret = -EINVAL;
919         }
920 out:
921         return ret;
922 }
923 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
924                    bonding_show_lacp, bonding_store_lacp);
925
926 static ssize_t bonding_show_ad_select(struct device *d,
927                                       struct device_attribute *attr,
928                                       char *buf)
929 {
930         struct bonding *bond = to_bond(d);
931
932         return sprintf(buf, "%s %d\n",
933                 ad_select_tbl[bond->params.ad_select].modename,
934                 bond->params.ad_select);
935 }
936
937
938 static ssize_t bonding_store_ad_select(struct device *d,
939                                        struct device_attribute *attr,
940                                        const char *buf, size_t count)
941 {
942         int new_value, ret = count;
943         struct bonding *bond = to_bond(d);
944
945         if (bond->dev->flags & IFF_UP) {
946                 pr_err(DRV_NAME
947                        ": %s: Unable to update ad_select because interface "
948                        "is up.\n", bond->dev->name);
949                 ret = -EPERM;
950                 goto out;
951         }
952
953         new_value = bond_parse_parm(buf, ad_select_tbl);
954
955         if (new_value != -1) {
956                 bond->params.ad_select = new_value;
957                 pr_info(DRV_NAME
958                        ": %s: Setting ad_select to %s (%d).\n",
959                        bond->dev->name, ad_select_tbl[new_value].modename,
960                        new_value);
961         } else {
962                 pr_err(DRV_NAME
963                        ": %s: Ignoring invalid ad_select value %.*s.\n",
964                        bond->dev->name, (int)strlen(buf) - 1, buf);
965                 ret = -EINVAL;
966         }
967 out:
968         return ret;
969 }
970 static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
971                    bonding_show_ad_select, bonding_store_ad_select);
972
973 /*
974  * Show and set the number of grat ARP to send after a failover event.
975  */
976 static ssize_t bonding_show_n_grat_arp(struct device *d,
977                                    struct device_attribute *attr,
978                                    char *buf)
979 {
980         struct bonding *bond = to_bond(d);
981
982         return sprintf(buf, "%d\n", bond->params.num_grat_arp);
983 }
984
985 static ssize_t bonding_store_n_grat_arp(struct device *d,
986                                     struct device_attribute *attr,
987                                     const char *buf, size_t count)
988 {
989         int new_value, ret = count;
990         struct bonding *bond = to_bond(d);
991
992         if (sscanf(buf, "%d", &new_value) != 1) {
993                 pr_err(DRV_NAME
994                        ": %s: no num_grat_arp value specified.\n",
995                        bond->dev->name);
996                 ret = -EINVAL;
997                 goto out;
998         }
999         if (new_value < 0 || new_value > 255) {
1000                 pr_err(DRV_NAME
1001                        ": %s: Invalid num_grat_arp value %d not in range 0-255; rejected.\n",
1002                        bond->dev->name, new_value);
1003                 ret = -EINVAL;
1004                 goto out;
1005         } else {
1006                 bond->params.num_grat_arp = new_value;
1007         }
1008 out:
1009         return ret;
1010 }
1011 static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
1012                    bonding_show_n_grat_arp, bonding_store_n_grat_arp);
1013
1014 /*
1015  * Show and set the number of unsolicited NA's to send after a failover event.
1016  */
1017 static ssize_t bonding_show_n_unsol_na(struct device *d,
1018                                        struct device_attribute *attr,
1019                                        char *buf)
1020 {
1021         struct bonding *bond = to_bond(d);
1022
1023         return sprintf(buf, "%d\n", bond->params.num_unsol_na);
1024 }
1025
1026 static ssize_t bonding_store_n_unsol_na(struct device *d,
1027                                         struct device_attribute *attr,
1028                                         const char *buf, size_t count)
1029 {
1030         int new_value, ret = count;
1031         struct bonding *bond = to_bond(d);
1032
1033         if (sscanf(buf, "%d", &new_value) != 1) {
1034                 pr_err(DRV_NAME
1035                        ": %s: no num_unsol_na value specified.\n",
1036                        bond->dev->name);
1037                 ret = -EINVAL;
1038                 goto out;
1039         }
1040
1041         if (new_value < 0 || new_value > 255) {
1042                 pr_err(DRV_NAME
1043                        ": %s: Invalid num_unsol_na value %d not in range 0-255; rejected.\n",
1044                        bond->dev->name, new_value);
1045                 ret = -EINVAL;
1046                 goto out;
1047         } else
1048                 bond->params.num_unsol_na = new_value;
1049 out:
1050         return ret;
1051 }
1052 static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
1053                    bonding_show_n_unsol_na, bonding_store_n_unsol_na);
1054
1055 /*
1056  * Show and set the MII monitor interval.  There are two tricky bits
1057  * here.  First, if MII monitoring is activated, then we must disable
1058  * ARP monitoring.  Second, if the timer isn't running, we must
1059  * start it.
1060  */
1061 static ssize_t bonding_show_miimon(struct device *d,
1062                                    struct device_attribute *attr,
1063                                    char *buf)
1064 {
1065         struct bonding *bond = to_bond(d);
1066
1067         return sprintf(buf, "%d\n", bond->params.miimon);
1068 }
1069
1070 static ssize_t bonding_store_miimon(struct device *d,
1071                                     struct device_attribute *attr,
1072                                     const char *buf, size_t count)
1073 {
1074         int new_value, ret = count;
1075         struct bonding *bond = to_bond(d);
1076
1077         if (sscanf(buf, "%d", &new_value) != 1) {
1078                 pr_err(DRV_NAME
1079                        ": %s: no miimon value specified.\n",
1080                        bond->dev->name);
1081                 ret = -EINVAL;
1082                 goto out;
1083         }
1084         if (new_value < 0) {
1085                 pr_err(DRV_NAME
1086                        ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
1087                        bond->dev->name, new_value, 1, INT_MAX);
1088                 ret = -EINVAL;
1089                 goto out;
1090         } else {
1091                 pr_info(DRV_NAME
1092                        ": %s: Setting MII monitoring interval to %d.\n",
1093                        bond->dev->name, new_value);
1094                 bond->params.miimon = new_value;
1095                 if (bond->params.updelay)
1096                         pr_info(DRV_NAME
1097                               ": %s: Note: Updating updelay (to %d) "
1098                               "since it is a multiple of the miimon value.\n",
1099                               bond->dev->name,
1100                               bond->params.updelay * bond->params.miimon);
1101                 if (bond->params.downdelay)
1102                         pr_info(DRV_NAME
1103                               ": %s: Note: Updating downdelay (to %d) "
1104                               "since it is a multiple of the miimon value.\n",
1105                               bond->dev->name,
1106                               bond->params.downdelay * bond->params.miimon);
1107                 if (bond->params.arp_interval) {
1108                         pr_info(DRV_NAME
1109                                ": %s: MII monitoring cannot be used with "
1110                                "ARP monitoring. Disabling ARP monitoring...\n",
1111                                bond->dev->name);
1112                         bond->params.arp_interval = 0;
1113                         bond->dev->priv_flags &= ~IFF_MASTER_ARPMON;
1114                         if (bond->params.arp_validate) {
1115                                 bond_unregister_arp(bond);
1116                                 bond->params.arp_validate =
1117                                         BOND_ARP_VALIDATE_NONE;
1118                         }
1119                         if (delayed_work_pending(&bond->arp_work)) {
1120                                 cancel_delayed_work(&bond->arp_work);
1121                                 flush_workqueue(bond->wq);
1122                         }
1123                 }
1124
1125                 if (bond->dev->flags & IFF_UP) {
1126                         /* If the interface is up, we may need to fire off
1127                          * the MII timer. If the interface is down, the
1128                          * timer will get fired off when the open function
1129                          * is called.
1130                          */
1131                         if (!delayed_work_pending(&bond->mii_work)) {
1132                                 INIT_DELAYED_WORK(&bond->mii_work,
1133                                                   bond_mii_monitor);
1134                                 queue_delayed_work(bond->wq,
1135                                                    &bond->mii_work, 0);
1136                         }
1137                 }
1138         }
1139 out:
1140         return ret;
1141 }
1142 static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1143                    bonding_show_miimon, bonding_store_miimon);
1144
1145 /*
1146  * Show and set the primary slave.  The store function is much
1147  * simpler than bonding_store_slaves function because it only needs to
1148  * handle one interface name.
1149  * The bond must be a mode that supports a primary for this be
1150  * set.
1151  */
1152 static ssize_t bonding_show_primary(struct device *d,
1153                                     struct device_attribute *attr,
1154                                     char *buf)
1155 {
1156         int count = 0;
1157         struct bonding *bond = to_bond(d);
1158
1159         if (bond->primary_slave)
1160                 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
1161
1162         return count;
1163 }
1164
1165 static ssize_t bonding_store_primary(struct device *d,
1166                                      struct device_attribute *attr,
1167                                      const char *buf, size_t count)
1168 {
1169         int i;
1170         struct slave *slave;
1171         struct bonding *bond = to_bond(d);
1172
1173         if (!rtnl_trylock())
1174                 return restart_syscall();
1175         read_lock(&bond->lock);
1176         write_lock_bh(&bond->curr_slave_lock);
1177
1178         if (!USES_PRIMARY(bond->params.mode)) {
1179                 pr_info(DRV_NAME
1180                        ": %s: Unable to set primary slave; %s is in mode %d\n",
1181                        bond->dev->name, bond->dev->name, bond->params.mode);
1182         } else {
1183                 bond_for_each_slave(bond, slave, i) {
1184                         if (strnicmp
1185                             (slave->dev->name, buf,
1186                              strlen(slave->dev->name)) == 0) {
1187                                 pr_info(DRV_NAME
1188                                        ": %s: Setting %s as primary slave.\n",
1189                                        bond->dev->name, slave->dev->name);
1190                                 bond->primary_slave = slave;
1191                                 strcpy(bond->params.primary, slave->dev->name);
1192                                 bond_select_active_slave(bond);
1193                                 goto out;
1194                         }
1195                 }
1196
1197                 /* if we got here, then we didn't match the name of any slave */
1198
1199                 if (strlen(buf) == 0 || buf[0] == '\n') {
1200                         pr_info(DRV_NAME
1201                                ": %s: Setting primary slave to None.\n",
1202                                bond->dev->name);
1203                         bond->primary_slave = NULL;
1204                                 bond_select_active_slave(bond);
1205                 } else {
1206                         pr_info(DRV_NAME
1207                                ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1208                                bond->dev->name, (int)strlen(buf) - 1, buf);
1209                 }
1210         }
1211 out:
1212         write_unlock_bh(&bond->curr_slave_lock);
1213         read_unlock(&bond->lock);
1214         rtnl_unlock();
1215
1216         return count;
1217 }
1218 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1219                    bonding_show_primary, bonding_store_primary);
1220
1221 /*
1222  * Show and set the primary_reselect flag.
1223  */
1224 static ssize_t bonding_show_primary_reselect(struct device *d,
1225                                              struct device_attribute *attr,
1226                                              char *buf)
1227 {
1228         struct bonding *bond = to_bond(d);
1229
1230         return sprintf(buf, "%s %d\n",
1231                        pri_reselect_tbl[bond->params.primary_reselect].modename,
1232                        bond->params.primary_reselect);
1233 }
1234
1235 static ssize_t bonding_store_primary_reselect(struct device *d,
1236                                               struct device_attribute *attr,
1237                                               const char *buf, size_t count)
1238 {
1239         int new_value, ret = count;
1240         struct bonding *bond = to_bond(d);
1241
1242         if (!rtnl_trylock())
1243                 return restart_syscall();
1244
1245         new_value = bond_parse_parm(buf, pri_reselect_tbl);
1246         if (new_value < 0)  {
1247                 pr_err(DRV_NAME
1248                        ": %s: Ignoring invalid primary_reselect value %.*s.\n",
1249                        bond->dev->name,
1250                        (int) strlen(buf) - 1, buf);
1251                 ret = -EINVAL;
1252                 goto out;
1253         }
1254
1255         bond->params.primary_reselect = new_value;
1256         pr_info(DRV_NAME ": %s: setting primary_reselect to %s (%d).\n",
1257                 bond->dev->name, pri_reselect_tbl[new_value].modename,
1258                 new_value);
1259
1260         read_lock(&bond->lock);
1261         write_lock_bh(&bond->curr_slave_lock);
1262         bond_select_active_slave(bond);
1263         write_unlock_bh(&bond->curr_slave_lock);
1264         read_unlock(&bond->lock);
1265 out:
1266         rtnl_unlock();
1267         return ret;
1268 }
1269 static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1270                    bonding_show_primary_reselect,
1271                    bonding_store_primary_reselect);
1272
1273 /*
1274  * Show and set the use_carrier flag.
1275  */
1276 static ssize_t bonding_show_carrier(struct device *d,
1277                                     struct device_attribute *attr,
1278                                     char *buf)
1279 {
1280         struct bonding *bond = to_bond(d);
1281
1282         return sprintf(buf, "%d\n", bond->params.use_carrier);
1283 }
1284
1285 static ssize_t bonding_store_carrier(struct device *d,
1286                                      struct device_attribute *attr,
1287                                      const char *buf, size_t count)
1288 {
1289         int new_value, ret = count;
1290         struct bonding *bond = to_bond(d);
1291
1292
1293         if (sscanf(buf, "%d", &new_value) != 1) {
1294                 pr_err(DRV_NAME
1295                        ": %s: no use_carrier value specified.\n",
1296                        bond->dev->name);
1297                 ret = -EINVAL;
1298                 goto out;
1299         }
1300         if ((new_value == 0) || (new_value == 1)) {
1301                 bond->params.use_carrier = new_value;
1302                 pr_info(DRV_NAME ": %s: Setting use_carrier to %d.\n",
1303                        bond->dev->name, new_value);
1304         } else {
1305                 pr_info(DRV_NAME
1306                        ": %s: Ignoring invalid use_carrier value %d.\n",
1307                        bond->dev->name, new_value);
1308         }
1309 out:
1310         return count;
1311 }
1312 static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1313                    bonding_show_carrier, bonding_store_carrier);
1314
1315
1316 /*
1317  * Show and set currently active_slave.
1318  */
1319 static ssize_t bonding_show_active_slave(struct device *d,
1320                                          struct device_attribute *attr,
1321                                          char *buf)
1322 {
1323         struct slave *curr;
1324         struct bonding *bond = to_bond(d);
1325         int count = 0;
1326
1327         read_lock(&bond->curr_slave_lock);
1328         curr = bond->curr_active_slave;
1329         read_unlock(&bond->curr_slave_lock);
1330
1331         if (USES_PRIMARY(bond->params.mode) && curr)
1332                 count = sprintf(buf, "%s\n", curr->dev->name);
1333         return count;
1334 }
1335
1336 static ssize_t bonding_store_active_slave(struct device *d,
1337                                           struct device_attribute *attr,
1338                                           const char *buf, size_t count)
1339 {
1340         int i;
1341         struct slave *slave;
1342         struct slave *old_active = NULL;
1343         struct slave *new_active = NULL;
1344         struct bonding *bond = to_bond(d);
1345
1346         if (!rtnl_trylock())
1347                 return restart_syscall();
1348         read_lock(&bond->lock);
1349         write_lock_bh(&bond->curr_slave_lock);
1350
1351         if (!USES_PRIMARY(bond->params.mode))
1352                 pr_info(DRV_NAME ": %s: Unable to change active slave;"
1353                         " %s is in mode %d\n",
1354                         bond->dev->name, bond->dev->name, bond->params.mode);
1355         else {
1356                 bond_for_each_slave(bond, slave, i) {
1357                         if (strnicmp
1358                             (slave->dev->name, buf,
1359                              strlen(slave->dev->name)) == 0) {
1360                                 old_active = bond->curr_active_slave;
1361                                 new_active = slave;
1362                                 if (new_active == old_active) {
1363                                         /* do nothing */
1364                                         pr_info(DRV_NAME
1365                                                 ": %s: %s is already the current active slave.\n",
1366                                                 bond->dev->name, slave->dev->name);
1367                                         goto out;
1368                                 }
1369                                 else {
1370                                         if ((new_active) &&
1371                                             (old_active) &&
1372                                             (new_active->link == BOND_LINK_UP) &&
1373                                             IS_UP(new_active->dev)) {
1374                                                 pr_info(DRV_NAME
1375                                                         ": %s: Setting %s as active slave.\n",
1376                                                         bond->dev->name, slave->dev->name);
1377                                                         bond_change_active_slave(bond, new_active);
1378                                         }
1379                                         else {
1380                                                 pr_info(DRV_NAME
1381                                                         ": %s: Could not set %s as active slave; "
1382                                                         "either %s is down or the link is down.\n",
1383                                                         bond->dev->name, slave->dev->name,
1384                                                         slave->dev->name);
1385                                         }
1386                                         goto out;
1387                                 }
1388                         }
1389                 }
1390
1391                 /* if we got here, then we didn't match the name of any slave */
1392
1393                 if (strlen(buf) == 0 || buf[0] == '\n') {
1394                         pr_info(DRV_NAME
1395                                 ": %s: Setting active slave to None.\n",
1396                                 bond->dev->name);
1397                         bond->primary_slave = NULL;
1398                         bond_select_active_slave(bond);
1399                 } else {
1400                         pr_info(DRV_NAME ": %s: Unable to set %.*s"
1401                                 " as active slave as it is not a slave.\n",
1402                                 bond->dev->name, (int)strlen(buf) - 1, buf);
1403                 }
1404         }
1405  out:
1406         write_unlock_bh(&bond->curr_slave_lock);
1407         read_unlock(&bond->lock);
1408         rtnl_unlock();
1409
1410         return count;
1411
1412 }
1413 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1414                    bonding_show_active_slave, bonding_store_active_slave);
1415
1416
1417 /*
1418  * Show link status of the bond interface.
1419  */
1420 static ssize_t bonding_show_mii_status(struct device *d,
1421                                        struct device_attribute *attr,
1422                                        char *buf)
1423 {
1424         struct slave *curr;
1425         struct bonding *bond = to_bond(d);
1426
1427         read_lock(&bond->curr_slave_lock);
1428         curr = bond->curr_active_slave;
1429         read_unlock(&bond->curr_slave_lock);
1430
1431         return sprintf(buf, "%s\n", curr ? "up" : "down");
1432 }
1433 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1434
1435
1436 /*
1437  * Show current 802.3ad aggregator ID.
1438  */
1439 static ssize_t bonding_show_ad_aggregator(struct device *d,
1440                                           struct device_attribute *attr,
1441                                           char *buf)
1442 {
1443         int count = 0;
1444         struct bonding *bond = to_bond(d);
1445
1446         if (bond->params.mode == BOND_MODE_8023AD) {
1447                 struct ad_info ad_info;
1448                 count = sprintf(buf, "%d\n",
1449                                 (bond_3ad_get_active_agg_info(bond, &ad_info))
1450                                 ?  0 : ad_info.aggregator_id);
1451         }
1452
1453         return count;
1454 }
1455 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1456
1457
1458 /*
1459  * Show number of active 802.3ad ports.
1460  */
1461 static ssize_t bonding_show_ad_num_ports(struct device *d,
1462                                          struct device_attribute *attr,
1463                                          char *buf)
1464 {
1465         int count = 0;
1466         struct bonding *bond = to_bond(d);
1467
1468         if (bond->params.mode == BOND_MODE_8023AD) {
1469                 struct ad_info ad_info;
1470                 count = sprintf(buf, "%d\n",
1471                                 (bond_3ad_get_active_agg_info(bond, &ad_info))
1472                                 ?  0 : ad_info.ports);
1473         }
1474
1475         return count;
1476 }
1477 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1478
1479
1480 /*
1481  * Show current 802.3ad actor key.
1482  */
1483 static ssize_t bonding_show_ad_actor_key(struct device *d,
1484                                          struct device_attribute *attr,
1485                                          char *buf)
1486 {
1487         int count = 0;
1488         struct bonding *bond = to_bond(d);
1489
1490         if (bond->params.mode == BOND_MODE_8023AD) {
1491                 struct ad_info ad_info;
1492                 count = sprintf(buf, "%d\n",
1493                                 (bond_3ad_get_active_agg_info(bond, &ad_info))
1494                                 ?  0 : ad_info.actor_key);
1495         }
1496
1497         return count;
1498 }
1499 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1500
1501
1502 /*
1503  * Show current 802.3ad partner key.
1504  */
1505 static ssize_t bonding_show_ad_partner_key(struct device *d,
1506                                            struct device_attribute *attr,
1507                                            char *buf)
1508 {
1509         int count = 0;
1510         struct bonding *bond = to_bond(d);
1511
1512         if (bond->params.mode == BOND_MODE_8023AD) {
1513                 struct ad_info ad_info;
1514                 count = sprintf(buf, "%d\n",
1515                                 (bond_3ad_get_active_agg_info(bond, &ad_info))
1516                                 ?  0 : ad_info.partner_key);
1517         }
1518
1519         return count;
1520 }
1521 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1522
1523
1524 /*
1525  * Show current 802.3ad partner mac.
1526  */
1527 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1528                                            struct device_attribute *attr,
1529                                            char *buf)
1530 {
1531         int count = 0;
1532         struct bonding *bond = to_bond(d);
1533
1534         if (bond->params.mode == BOND_MODE_8023AD) {
1535                 struct ad_info ad_info;
1536                 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
1537                         count = sprintf(buf, "%pM\n", ad_info.partner_system);
1538         }
1539
1540         return count;
1541 }
1542 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1543
1544
1545
1546 static struct attribute *per_bond_attrs[] = {
1547         &dev_attr_slaves.attr,
1548         &dev_attr_mode.attr,
1549         &dev_attr_fail_over_mac.attr,
1550         &dev_attr_arp_validate.attr,
1551         &dev_attr_arp_interval.attr,
1552         &dev_attr_arp_ip_target.attr,
1553         &dev_attr_downdelay.attr,
1554         &dev_attr_updelay.attr,
1555         &dev_attr_lacp_rate.attr,
1556         &dev_attr_ad_select.attr,
1557         &dev_attr_xmit_hash_policy.attr,
1558         &dev_attr_num_grat_arp.attr,
1559         &dev_attr_num_unsol_na.attr,
1560         &dev_attr_miimon.attr,
1561         &dev_attr_primary.attr,
1562         &dev_attr_primary_reselect.attr,
1563         &dev_attr_use_carrier.attr,
1564         &dev_attr_active_slave.attr,
1565         &dev_attr_mii_status.attr,
1566         &dev_attr_ad_aggregator.attr,
1567         &dev_attr_ad_num_ports.attr,
1568         &dev_attr_ad_actor_key.attr,
1569         &dev_attr_ad_partner_key.attr,
1570         &dev_attr_ad_partner_mac.attr,
1571         NULL,
1572 };
1573
1574 static struct attribute_group bonding_group = {
1575         .name = "bonding",
1576         .attrs = per_bond_attrs,
1577 };
1578
1579 /*
1580  * Initialize sysfs.  This sets up the bonding_masters file in
1581  * /sys/class/net.
1582  */
1583 int bond_create_sysfs(void)
1584 {
1585         int ret;
1586
1587         ret = netdev_class_create_file(&class_attr_bonding_masters);
1588         /*
1589          * Permit multiple loads of the module by ignoring failures to
1590          * create the bonding_masters sysfs file.  Bonding devices
1591          * created by second or subsequent loads of the module will
1592          * not be listed in, or controllable by, bonding_masters, but
1593          * will have the usual "bonding" sysfs directory.
1594          *
1595          * This is done to preserve backwards compatibility for
1596          * initscripts/sysconfig, which load bonding multiple times to
1597          * configure multiple bonding devices.
1598          */
1599         if (ret == -EEXIST) {
1600                 /* Is someone being kinky and naming a device bonding_master? */
1601                 if (__dev_get_by_name(&init_net,
1602                                       class_attr_bonding_masters.attr.name))
1603                         pr_err("network device named %s already "
1604                                "exists in sysfs",
1605                                class_attr_bonding_masters.attr.name);
1606                 ret = 0;
1607         }
1608
1609         return ret;
1610
1611 }
1612
1613 /*
1614  * Remove /sys/class/net/bonding_masters.
1615  */
1616 void bond_destroy_sysfs(void)
1617 {
1618         netdev_class_remove_file(&class_attr_bonding_masters);
1619 }
1620
1621 /*
1622  * Initialize sysfs for each bond.  This sets up and registers
1623  * the 'bondctl' directory for each individual bond under /sys/class/net.
1624  */
1625 void bond_prepare_sysfs_group(struct bonding *bond)
1626 {
1627         bond->dev->sysfs_groups[0] = &bonding_group;
1628 }
1629