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 [IFLA_BOND_ACTIVE_SLAVE] = { .type = NLA_U32 }, 26 }; 27 28 static int bond_validate(struct nlattr *tb[], struct nlattr *data[]) 29 { 30 if (tb[IFLA_ADDRESS]) { 31 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) 32 return -EINVAL; 33 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) 34 return -EADDRNOTAVAIL; 35 } 36 return 0; 37 } 38 39 static int bond_changelink(struct net_device *bond_dev, 40 struct nlattr *tb[], struct nlattr *data[]) 41 { 42 struct bonding *bond = netdev_priv(bond_dev); 43 int err; 44 45 if (data && data[IFLA_BOND_MODE]) { 46 int mode = nla_get_u8(data[IFLA_BOND_MODE]); 47 48 err = bond_option_mode_set(bond, mode); 49 if (err) 50 return err; 51 } 52 if (data && data[IFLA_BOND_ACTIVE_SLAVE]) { 53 int ifindex = nla_get_u32(data[IFLA_BOND_ACTIVE_SLAVE]); 54 struct net_device *slave_dev; 55 56 if (ifindex == 0) { 57 slave_dev = NULL; 58 } else { 59 slave_dev = __dev_get_by_index(dev_net(bond_dev), 60 ifindex); 61 if (!slave_dev) 62 return -ENODEV; 63 } 64 err = bond_option_active_slave_set(bond, slave_dev); 65 if (err) 66 return err; 67 } 68 return 0; 69 } 70 71 static int bond_newlink(struct net *src_net, struct net_device *bond_dev, 72 struct nlattr *tb[], struct nlattr *data[]) 73 { 74 int err; 75 76 err = bond_changelink(bond_dev, tb, data); 77 if (err < 0) 78 return err; 79 80 return register_netdevice(bond_dev); 81 } 82 83 static size_t bond_get_size(const struct net_device *bond_dev) 84 { 85 return nla_total_size(sizeof(u8)) + /* IFLA_BOND_MODE */ 86 nla_total_size(sizeof(u32)); /* IFLA_BOND_ACTIVE_SLAVE */ 87 } 88 89 static int bond_fill_info(struct sk_buff *skb, 90 const struct net_device *bond_dev) 91 { 92 struct bonding *bond = netdev_priv(bond_dev); 93 struct net_device *slave_dev = bond_option_active_slave_get(bond); 94 95 if (nla_put_u8(skb, IFLA_BOND_MODE, bond->params.mode) || 96 (slave_dev && 97 nla_put_u32(skb, IFLA_BOND_ACTIVE_SLAVE, slave_dev->ifindex))) 98 goto nla_put_failure; 99 return 0; 100 101 nla_put_failure: 102 return -EMSGSIZE; 103 } 104 105 struct rtnl_link_ops bond_link_ops __read_mostly = { 106 .kind = "bond", 107 .priv_size = sizeof(struct bonding), 108 .setup = bond_setup, 109 .maxtype = IFLA_BOND_MAX, 110 .policy = bond_policy, 111 .validate = bond_validate, 112 .newlink = bond_newlink, 113 .changelink = bond_changelink, 114 .get_size = bond_get_size, 115 .fill_info = bond_fill_info, 116 .get_num_tx_queues = bond_get_num_tx_queues, 117 .get_num_rx_queues = bond_get_num_tx_queues, /* Use the same number 118 as for TX queues */ 119 }; 120 121 int __init bond_netlink_init(void) 122 { 123 return rtnl_link_register(&bond_link_ops); 124 } 125 126 void bond_netlink_fini(void) 127 { 128 rtnl_link_unregister(&bond_link_ops); 129 } 130 131 MODULE_ALIAS_RTNL_LINK("bond"); 132