1 /*
2  * drivers/net/bond/bond_netlink.c - Netlink interface for bonding
3  * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  */
10 
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 
13 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/netdevice.h>
16 #include <linux/etherdevice.h>
17 #include <linux/if_link.h>
18 #include <linux/if_ether.h>
19 #include <net/netlink.h>
20 #include <net/rtnetlink.h>
21 #include "bonding.h"
22 
23 static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
24 	[IFLA_BOND_MODE]		= { .type = NLA_U8 },
25 };
26 
27 static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
28 {
29 	if (tb[IFLA_ADDRESS]) {
30 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
31 			return -EINVAL;
32 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
33 			return -EADDRNOTAVAIL;
34 	}
35 	return 0;
36 }
37 
38 static int bond_changelink(struct net_device *bond_dev,
39 			   struct nlattr *tb[], struct nlattr *data[])
40 {
41 	struct bonding *bond = netdev_priv(bond_dev);
42 	int err;
43 
44 	if (data && data[IFLA_BOND_MODE]) {
45 		int mode = nla_get_u8(data[IFLA_BOND_MODE]);
46 
47 		err = bond_option_mode_set(bond, mode);
48 		if (err)
49 			return err;
50 	}
51 	return 0;
52 }
53 
54 static int bond_newlink(struct net *src_net, struct net_device *bond_dev,
55 			struct nlattr *tb[], struct nlattr *data[])
56 {
57 	int err;
58 
59 	err = bond_changelink(bond_dev, tb, data);
60 	if (err < 0)
61 		return err;
62 
63 	return register_netdevice(bond_dev);
64 }
65 
66 static size_t bond_get_size(const struct net_device *bond_dev)
67 {
68 	return nla_total_size(sizeof(u8));	/* IFLA_BOND_MODE */
69 }
70 
71 static int bond_fill_info(struct sk_buff *skb,
72 			  const struct net_device *bond_dev)
73 {
74 	struct bonding *bond = netdev_priv(bond_dev);
75 
76 	if (nla_put_u8(skb, IFLA_BOND_MODE, bond->params.mode))
77 		goto nla_put_failure;
78 	return 0;
79 
80 nla_put_failure:
81 	return -EMSGSIZE;
82 }
83 
84 struct rtnl_link_ops bond_link_ops __read_mostly = {
85 	.kind			= "bond",
86 	.priv_size		= sizeof(struct bonding),
87 	.setup			= bond_setup,
88 	.maxtype		= IFLA_BOND_MAX,
89 	.policy			= bond_policy,
90 	.validate		= bond_validate,
91 	.newlink		= bond_newlink,
92 	.changelink		= bond_changelink,
93 	.get_size		= bond_get_size,
94 	.fill_info		= bond_fill_info,
95 	.get_num_tx_queues	= bond_get_num_tx_queues,
96 	.get_num_rx_queues	= bond_get_num_tx_queues, /* Use the same number
97 							     as for TX queues */
98 };
99 
100 int __init bond_netlink_init(void)
101 {
102 	return rtnl_link_register(&bond_link_ops);
103 }
104 
105 void __exit bond_netlink_fini(void)
106 {
107 	rtnl_link_unregister(&bond_link_ops);
108 }
109 
110 MODULE_ALIAS_RTNL_LINK("bond");
111