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