e8526552790cdab3e1c6cfe5cfb6558af2776947
[firefly-linux-kernel-4.4.55.git] / drivers / net / bonding / bond_netlink.c
1 /*
2  * drivers/net/bond/bond_netlink.c - Netlink interface for bonding
3  * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
4  * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14 #include <linux/module.h>
15 #include <linux/errno.h>
16 #include <linux/netdevice.h>
17 #include <linux/etherdevice.h>
18 #include <linux/if_link.h>
19 #include <linux/if_ether.h>
20 #include <net/netlink.h>
21 #include <net/rtnetlink.h>
22 #include "bonding.h"
23
24 int bond_get_slave(struct net_device *slave_dev, struct sk_buff *skb)
25 {
26         struct slave *slave = bond_slave_get_rtnl(slave_dev);
27         const struct aggregator *agg;
28
29         if (nla_put_u8(skb, IFLA_SLAVE_STATE, bond_slave_state(slave)))
30                 goto nla_put_failure;
31
32         if (nla_put_u8(skb, IFLA_SLAVE_MII_STATUS, slave->link))
33                 goto nla_put_failure;
34
35         if (nla_put_u32(skb, IFLA_SLAVE_LINK_FAILURE_COUNT,
36                         slave->link_failure_count))
37                 goto nla_put_failure;
38
39         if (nla_put(skb, IFLA_SLAVE_PERM_HWADDR,
40                     slave_dev->addr_len, slave->perm_hwaddr))
41                 goto nla_put_failure;
42
43         if (nla_put_u16(skb, IFLA_SLAVE_QUEUE_ID, slave->queue_id))
44                 goto nla_put_failure;
45
46         if (slave->bond->params.mode == BOND_MODE_8023AD) {
47                 agg = SLAVE_AD_INFO(slave).port.aggregator;
48                 if (agg)
49                         if (nla_put_u16(skb, IFLA_SLAVE_AD_AGGREGATOR_ID,
50                                         agg->aggregator_identifier))
51                                 goto nla_put_failure;
52         }
53
54         return 0;
55
56 nla_put_failure:
57         return -EMSGSIZE;
58 }
59
60 static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
61         [IFLA_BOND_MODE]                = { .type = NLA_U8 },
62         [IFLA_BOND_ACTIVE_SLAVE]        = { .type = NLA_U32 },
63         [IFLA_BOND_MIIMON]              = { .type = NLA_U32 },
64         [IFLA_BOND_UPDELAY]             = { .type = NLA_U32 },
65         [IFLA_BOND_DOWNDELAY]           = { .type = NLA_U32 },
66         [IFLA_BOND_USE_CARRIER]         = { .type = NLA_U8 },
67         [IFLA_BOND_ARP_INTERVAL]        = { .type = NLA_U32 },
68         [IFLA_BOND_ARP_IP_TARGET]       = { .type = NLA_NESTED },
69         [IFLA_BOND_ARP_VALIDATE]        = { .type = NLA_U32 },
70         [IFLA_BOND_ARP_ALL_TARGETS]     = { .type = NLA_U32 },
71         [IFLA_BOND_PRIMARY]             = { .type = NLA_U32 },
72         [IFLA_BOND_PRIMARY_RESELECT]    = { .type = NLA_U8 },
73         [IFLA_BOND_FAIL_OVER_MAC]       = { .type = NLA_U8 },
74         [IFLA_BOND_XMIT_HASH_POLICY]    = { .type = NLA_U8 },
75         [IFLA_BOND_RESEND_IGMP]         = { .type = NLA_U32 },
76         [IFLA_BOND_NUM_PEER_NOTIF]      = { .type = NLA_U8 },
77         [IFLA_BOND_ALL_SLAVES_ACTIVE]   = { .type = NLA_U8 },
78         [IFLA_BOND_MIN_LINKS]           = { .type = NLA_U32 },
79         [IFLA_BOND_LP_INTERVAL]         = { .type = NLA_U32 },
80         [IFLA_BOND_PACKETS_PER_SLAVE]   = { .type = NLA_U32 },
81         [IFLA_BOND_AD_LACP_RATE]        = { .type = NLA_U8 },
82         [IFLA_BOND_AD_SELECT]           = { .type = NLA_U8 },
83         [IFLA_BOND_AD_INFO]             = { .type = NLA_NESTED },
84 };
85
86 static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
87 {
88         if (tb[IFLA_ADDRESS]) {
89                 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
90                         return -EINVAL;
91                 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
92                         return -EADDRNOTAVAIL;
93         }
94         return 0;
95 }
96
97 static int bond_changelink(struct net_device *bond_dev,
98                            struct nlattr *tb[], struct nlattr *data[])
99 {
100         struct bonding *bond = netdev_priv(bond_dev);
101         int miimon = 0;
102         int err;
103
104         if (!data)
105                 return 0;
106
107         if (data[IFLA_BOND_MODE]) {
108                 int mode = nla_get_u8(data[IFLA_BOND_MODE]);
109
110                 err = bond_option_mode_set(bond, mode);
111                 if (err)
112                         return err;
113         }
114         if (data[IFLA_BOND_ACTIVE_SLAVE]) {
115                 int ifindex = nla_get_u32(data[IFLA_BOND_ACTIVE_SLAVE]);
116                 struct net_device *slave_dev;
117
118                 if (ifindex == 0) {
119                         slave_dev = NULL;
120                 } else {
121                         slave_dev = __dev_get_by_index(dev_net(bond_dev),
122                                                        ifindex);
123                         if (!slave_dev)
124                                 return -ENODEV;
125                 }
126                 err = bond_option_active_slave_set(bond, slave_dev);
127                 if (err)
128                         return err;
129         }
130         if (data[IFLA_BOND_MIIMON]) {
131                 miimon = nla_get_u32(data[IFLA_BOND_MIIMON]);
132
133                 err = bond_option_miimon_set(bond, miimon);
134                 if (err)
135                         return err;
136         }
137         if (data[IFLA_BOND_UPDELAY]) {
138                 int updelay = nla_get_u32(data[IFLA_BOND_UPDELAY]);
139
140                 err = bond_option_updelay_set(bond, updelay);
141                 if (err)
142                         return err;
143         }
144         if (data[IFLA_BOND_DOWNDELAY]) {
145                 int downdelay = nla_get_u32(data[IFLA_BOND_DOWNDELAY]);
146
147                 err = bond_option_downdelay_set(bond, downdelay);
148                 if (err)
149                         return err;
150         }
151         if (data[IFLA_BOND_USE_CARRIER]) {
152                 int use_carrier = nla_get_u8(data[IFLA_BOND_USE_CARRIER]);
153
154                 err = bond_option_use_carrier_set(bond, use_carrier);
155                 if (err)
156                         return err;
157         }
158         if (data[IFLA_BOND_ARP_INTERVAL]) {
159                 int arp_interval = nla_get_u32(data[IFLA_BOND_ARP_INTERVAL]);
160
161                 if (arp_interval && miimon) {
162                         pr_err("%s: ARP monitoring cannot be used with MII monitoring.\n",
163                                bond->dev->name);
164                         return -EINVAL;
165                 }
166
167                 err = bond_option_arp_interval_set(bond, arp_interval);
168                 if (err)
169                         return err;
170         }
171         if (data[IFLA_BOND_ARP_IP_TARGET]) {
172                 __be32 targets[BOND_MAX_ARP_TARGETS] = { 0, };
173                 struct nlattr *attr;
174                 int i = 0, rem;
175
176                 nla_for_each_nested(attr, data[IFLA_BOND_ARP_IP_TARGET], rem) {
177                         __be32 target = nla_get_be32(attr);
178                         targets[i++] = target;
179                 }
180
181                 err = bond_option_arp_ip_targets_set(bond, targets, i);
182                 if (err)
183                         return err;
184         }
185         if (data[IFLA_BOND_ARP_VALIDATE]) {
186                 int arp_validate = nla_get_u32(data[IFLA_BOND_ARP_VALIDATE]);
187
188                 if (arp_validate && miimon) {
189                         pr_err("%s: ARP validating cannot be used with MII monitoring.\n",
190                                bond->dev->name);
191                         return -EINVAL;
192                 }
193
194                 err = bond_option_arp_validate_set(bond, arp_validate);
195                 if (err)
196                         return err;
197         }
198         if (data[IFLA_BOND_ARP_ALL_TARGETS]) {
199                 int arp_all_targets =
200                         nla_get_u32(data[IFLA_BOND_ARP_ALL_TARGETS]);
201
202                 err = bond_option_arp_all_targets_set(bond, arp_all_targets);
203                 if (err)
204                         return err;
205         }
206         if (data[IFLA_BOND_PRIMARY]) {
207                 int ifindex = nla_get_u32(data[IFLA_BOND_PRIMARY]);
208                 struct net_device *dev;
209                 char *primary = "";
210
211                 dev = __dev_get_by_index(dev_net(bond_dev), ifindex);
212                 if (dev)
213                         primary = dev->name;
214
215                 err = bond_option_primary_set(bond, primary);
216                 if (err)
217                         return err;
218         }
219         if (data[IFLA_BOND_PRIMARY_RESELECT]) {
220                 int primary_reselect =
221                         nla_get_u8(data[IFLA_BOND_PRIMARY_RESELECT]);
222
223                 err = bond_option_primary_reselect_set(bond, primary_reselect);
224                 if (err)
225                         return err;
226         }
227         if (data[IFLA_BOND_FAIL_OVER_MAC]) {
228                 int fail_over_mac =
229                         nla_get_u8(data[IFLA_BOND_FAIL_OVER_MAC]);
230
231                 err = bond_option_fail_over_mac_set(bond, fail_over_mac);
232                 if (err)
233                         return err;
234         }
235         if (data[IFLA_BOND_XMIT_HASH_POLICY]) {
236                 int xmit_hash_policy =
237                         nla_get_u8(data[IFLA_BOND_XMIT_HASH_POLICY]);
238
239                 err = bond_option_xmit_hash_policy_set(bond, xmit_hash_policy);
240                 if (err)
241                         return err;
242         }
243         if (data[IFLA_BOND_RESEND_IGMP]) {
244                 int resend_igmp =
245                         nla_get_u32(data[IFLA_BOND_RESEND_IGMP]);
246
247                 err = bond_option_resend_igmp_set(bond, resend_igmp);
248                 if (err)
249                         return err;
250         }
251         if (data[IFLA_BOND_NUM_PEER_NOTIF]) {
252                 int num_peer_notif =
253                         nla_get_u8(data[IFLA_BOND_NUM_PEER_NOTIF]);
254
255                 err = bond_option_num_peer_notif_set(bond, num_peer_notif);
256                 if (err)
257                         return err;
258         }
259         if (data[IFLA_BOND_ALL_SLAVES_ACTIVE]) {
260                 int all_slaves_active =
261                         nla_get_u8(data[IFLA_BOND_ALL_SLAVES_ACTIVE]);
262
263                 err = bond_option_all_slaves_active_set(bond,
264                                                         all_slaves_active);
265                 if (err)
266                         return err;
267         }
268         if (data[IFLA_BOND_MIN_LINKS]) {
269                 int min_links =
270                         nla_get_u32(data[IFLA_BOND_MIN_LINKS]);
271
272                 err = bond_option_min_links_set(bond, min_links);
273                 if (err)
274                         return err;
275         }
276         if (data[IFLA_BOND_LP_INTERVAL]) {
277                 int lp_interval =
278                         nla_get_u32(data[IFLA_BOND_LP_INTERVAL]);
279
280                 err = bond_option_lp_interval_set(bond, lp_interval);
281                 if (err)
282                         return err;
283         }
284         if (data[IFLA_BOND_PACKETS_PER_SLAVE]) {
285                 int packets_per_slave =
286                         nla_get_u32(data[IFLA_BOND_PACKETS_PER_SLAVE]);
287
288                 err = bond_option_packets_per_slave_set(bond,
289                                                         packets_per_slave);
290                 if (err)
291                         return err;
292         }
293         if (data[IFLA_BOND_AD_LACP_RATE]) {
294                 int lacp_rate =
295                         nla_get_u8(data[IFLA_BOND_AD_LACP_RATE]);
296
297                 err = bond_option_lacp_rate_set(bond, lacp_rate);
298                 if (err)
299                         return err;
300         }
301         if (data[IFLA_BOND_AD_SELECT]) {
302                 int ad_select =
303                         nla_get_u8(data[IFLA_BOND_AD_SELECT]);
304
305                 err = bond_option_ad_select_set(bond, ad_select);
306                 if (err)
307                         return err;
308         }
309         return 0;
310 }
311
312 static int bond_newlink(struct net *src_net, struct net_device *bond_dev,
313                         struct nlattr *tb[], struct nlattr *data[])
314 {
315         int err;
316
317         err = bond_changelink(bond_dev, tb, data);
318         if (err < 0)
319                 return err;
320
321         return register_netdevice(bond_dev);
322 }
323
324 static size_t bond_get_size(const struct net_device *bond_dev)
325 {
326         return nla_total_size(sizeof(u8)) +     /* IFLA_BOND_MODE */
327                 nla_total_size(sizeof(u32)) +   /* IFLA_BOND_ACTIVE_SLAVE */
328                 nla_total_size(sizeof(u32)) +   /* IFLA_BOND_MIIMON */
329                 nla_total_size(sizeof(u32)) +   /* IFLA_BOND_UPDELAY */
330                 nla_total_size(sizeof(u32)) +   /* IFLA_BOND_DOWNDELAY */
331                 nla_total_size(sizeof(u8)) +    /* IFLA_BOND_USE_CARRIER */
332                 nla_total_size(sizeof(u32)) +   /* IFLA_BOND_ARP_INTERVAL */
333                                                 /* IFLA_BOND_ARP_IP_TARGET */
334                 nla_total_size(sizeof(struct nlattr)) +
335                 nla_total_size(sizeof(u32)) * BOND_MAX_ARP_TARGETS +
336                 nla_total_size(sizeof(u32)) +   /* IFLA_BOND_ARP_VALIDATE */
337                 nla_total_size(sizeof(u32)) +   /* IFLA_BOND_ARP_ALL_TARGETS */
338                 nla_total_size(sizeof(u32)) +   /* IFLA_BOND_PRIMARY */
339                 nla_total_size(sizeof(u8)) +    /* IFLA_BOND_PRIMARY_RESELECT */
340                 nla_total_size(sizeof(u8)) +    /* IFLA_BOND_FAIL_OVER_MAC */
341                 nla_total_size(sizeof(u8)) +    /* IFLA_BOND_XMIT_HASH_POLICY */
342                 nla_total_size(sizeof(u32)) +   /* IFLA_BOND_RESEND_IGMP */
343                 nla_total_size(sizeof(u8)) +    /* IFLA_BOND_NUM_PEER_NOTIF */
344                 nla_total_size(sizeof(u8)) +   /* IFLA_BOND_ALL_SLAVES_ACTIVE */
345                 nla_total_size(sizeof(u32)) +   /* IFLA_BOND_MIN_LINKS */
346                 nla_total_size(sizeof(u32)) +   /* IFLA_BOND_LP_INTERVAL */
347                 nla_total_size(sizeof(u32)) +  /* IFLA_BOND_PACKETS_PER_SLAVE */
348                 nla_total_size(sizeof(u8)) +    /* IFLA_BOND_AD_LACP_RATE */
349                 nla_total_size(sizeof(u8)) +    /* IFLA_BOND_AD_SELECT */
350                 nla_total_size(sizeof(struct nlattr)) + /* IFLA_BOND_AD_INFO */
351                 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_AGGREGATOR */
352                 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_NUM_PORTS */
353                 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_ACTOR_KEY */
354                 nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_PARTNER_KEY*/
355                 nla_total_size(ETH_ALEN) +    /* IFLA_BOND_AD_INFO_PARTNER_MAC*/
356                 0;
357 }
358
359 static int bond_fill_info(struct sk_buff *skb,
360                           const struct net_device *bond_dev)
361 {
362         struct bonding *bond = netdev_priv(bond_dev);
363         struct net_device *slave_dev = bond_option_active_slave_get(bond);
364         struct nlattr *targets;
365         unsigned int packets_per_slave;
366         int i, targets_added;
367
368         if (nla_put_u8(skb, IFLA_BOND_MODE, bond->params.mode))
369                 goto nla_put_failure;
370
371         if (slave_dev &&
372             nla_put_u32(skb, IFLA_BOND_ACTIVE_SLAVE, slave_dev->ifindex))
373                 goto nla_put_failure;
374
375         if (nla_put_u32(skb, IFLA_BOND_MIIMON, bond->params.miimon))
376                 goto nla_put_failure;
377
378         if (nla_put_u32(skb, IFLA_BOND_UPDELAY,
379                         bond->params.updelay * bond->params.miimon))
380                 goto nla_put_failure;
381
382         if (nla_put_u32(skb, IFLA_BOND_DOWNDELAY,
383                         bond->params.downdelay * bond->params.miimon))
384                 goto nla_put_failure;
385
386         if (nla_put_u8(skb, IFLA_BOND_USE_CARRIER, bond->params.use_carrier))
387                 goto nla_put_failure;
388
389         if (nla_put_u32(skb, IFLA_BOND_ARP_INTERVAL, bond->params.arp_interval))
390                 goto nla_put_failure;
391
392         targets = nla_nest_start(skb, IFLA_BOND_ARP_IP_TARGET);
393         if (!targets)
394                 goto nla_put_failure;
395
396         targets_added = 0;
397         for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
398                 if (bond->params.arp_targets[i]) {
399                         nla_put_be32(skb, i, bond->params.arp_targets[i]);
400                         targets_added = 1;
401                 }
402         }
403
404         if (targets_added)
405                 nla_nest_end(skb, targets);
406         else
407                 nla_nest_cancel(skb, targets);
408
409         if (nla_put_u32(skb, IFLA_BOND_ARP_VALIDATE, bond->params.arp_validate))
410                 goto nla_put_failure;
411
412         if (nla_put_u32(skb, IFLA_BOND_ARP_ALL_TARGETS,
413                         bond->params.arp_all_targets))
414                 goto nla_put_failure;
415
416         if (bond->primary_slave &&
417             nla_put_u32(skb, IFLA_BOND_PRIMARY,
418                         bond->primary_slave->dev->ifindex))
419                 goto nla_put_failure;
420
421         if (nla_put_u8(skb, IFLA_BOND_PRIMARY_RESELECT,
422                        bond->params.primary_reselect))
423                 goto nla_put_failure;
424
425         if (nla_put_u8(skb, IFLA_BOND_FAIL_OVER_MAC,
426                        bond->params.fail_over_mac))
427                 goto nla_put_failure;
428
429         if (nla_put_u8(skb, IFLA_BOND_XMIT_HASH_POLICY,
430                        bond->params.xmit_policy))
431                 goto nla_put_failure;
432
433         if (nla_put_u32(skb, IFLA_BOND_RESEND_IGMP,
434                         bond->params.resend_igmp))
435                 goto nla_put_failure;
436
437         if (nla_put_u8(skb, IFLA_BOND_NUM_PEER_NOTIF,
438                        bond->params.num_peer_notif))
439                 goto nla_put_failure;
440
441         if (nla_put_u8(skb, IFLA_BOND_ALL_SLAVES_ACTIVE,
442                        bond->params.all_slaves_active))
443                 goto nla_put_failure;
444
445         if (nla_put_u32(skb, IFLA_BOND_MIN_LINKS,
446                         bond->params.min_links))
447                 goto nla_put_failure;
448
449         if (nla_put_u32(skb, IFLA_BOND_LP_INTERVAL,
450                         bond->params.lp_interval))
451                 goto nla_put_failure;
452
453         packets_per_slave = bond->params.packets_per_slave;
454         if (nla_put_u32(skb, IFLA_BOND_PACKETS_PER_SLAVE,
455                         packets_per_slave))
456                 goto nla_put_failure;
457
458         if (nla_put_u8(skb, IFLA_BOND_AD_LACP_RATE,
459                        bond->params.lacp_fast))
460                 goto nla_put_failure;
461
462         if (nla_put_u8(skb, IFLA_BOND_AD_SELECT,
463                        bond->params.ad_select))
464                 goto nla_put_failure;
465
466         if (bond->params.mode == BOND_MODE_8023AD) {
467                 struct ad_info info;
468
469                 if (!bond_3ad_get_active_agg_info(bond, &info)) {
470                         struct nlattr *nest;
471
472                         nest = nla_nest_start(skb, IFLA_BOND_AD_INFO);
473                         if (!nest)
474                                 goto nla_put_failure;
475
476                         if (nla_put_u16(skb, IFLA_BOND_AD_INFO_AGGREGATOR,
477                                         info.aggregator_id))
478                                 goto nla_put_failure;
479                         if (nla_put_u16(skb, IFLA_BOND_AD_INFO_NUM_PORTS,
480                                         info.ports))
481                                 goto nla_put_failure;
482                         if (nla_put_u16(skb, IFLA_BOND_AD_INFO_ACTOR_KEY,
483                                         info.actor_key))
484                                 goto nla_put_failure;
485                         if (nla_put_u16(skb, IFLA_BOND_AD_INFO_PARTNER_KEY,
486                                         info.partner_key))
487                                 goto nla_put_failure;
488                         if (nla_put(skb, IFLA_BOND_AD_INFO_PARTNER_MAC,
489                                     sizeof(info.partner_system),
490                                     &info.partner_system))
491                                 goto nla_put_failure;
492
493                         nla_nest_end(skb, nest);
494                 }
495         }
496
497         return 0;
498
499 nla_put_failure:
500         return -EMSGSIZE;
501 }
502
503 struct rtnl_link_ops bond_link_ops __read_mostly = {
504         .kind                   = "bond",
505         .priv_size              = sizeof(struct bonding),
506         .setup                  = bond_setup,
507         .maxtype                = IFLA_BOND_MAX,
508         .policy                 = bond_policy,
509         .validate               = bond_validate,
510         .newlink                = bond_newlink,
511         .changelink             = bond_changelink,
512         .get_size               = bond_get_size,
513         .fill_info              = bond_fill_info,
514         .get_num_tx_queues      = bond_get_num_tx_queues,
515         .get_num_rx_queues      = bond_get_num_tx_queues, /* Use the same number
516                                                              as for TX queues */
517 };
518
519 int __init bond_netlink_init(void)
520 {
521         return rtnl_link_register(&bond_link_ops);
522 }
523
524 void bond_netlink_fini(void)
525 {
526         rtnl_link_unregister(&bond_link_ops);
527 }
528
529 MODULE_ALIAS_RTNL_LINK("bond");