Or Gerlitz | 9baa0b0 | 2012-09-13 05:56:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 Mellanox Technologies. - All rights reserved. |
| 3 | * |
| 4 | * This software is available to you under a choice of one of two |
| 5 | * licenses. You may choose to be licensed under the terms of the GNU |
| 6 | * General Public License (GPL) Version 2, available from the file |
| 7 | * COPYING in the main directory of this source tree, or the |
| 8 | * OpenIB.org BSD license below: |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or |
| 11 | * without modification, are permitted provided that the following |
| 12 | * conditions are met: |
| 13 | * |
| 14 | * - Redistributions of source code must retain the above |
| 15 | * copyright notice, this list of conditions and the following |
| 16 | * disclaimer. |
| 17 | * |
| 18 | * - Redistributions in binary form must reproduce the above |
| 19 | * copyright notice, this list of conditions and the following |
| 20 | * disclaimer in the documentation and/or other materials |
| 21 | * provided with the distribution. |
| 22 | * |
| 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 25 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 27 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 28 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 29 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 30 | * SOFTWARE. |
| 31 | */ |
| 32 | |
| 33 | #include <linux/netdevice.h> |
Hangbin Liu | 0d68fc4 | 2014-01-03 11:33:45 +0800 | [diff] [blame] | 34 | #include <linux/if_arp.h> /* For ARPHRD_xxx */ |
Or Gerlitz | 9baa0b0 | 2012-09-13 05:56:36 +0000 | [diff] [blame] | 35 | #include <linux/module.h> |
| 36 | #include <net/rtnetlink.h> |
| 37 | #include "ipoib.h" |
| 38 | |
| 39 | static const struct nla_policy ipoib_policy[IFLA_IPOIB_MAX + 1] = { |
| 40 | [IFLA_IPOIB_PKEY] = { .type = NLA_U16 }, |
Or Gerlitz | 862096a | 2012-09-27 12:06:02 +0000 | [diff] [blame] | 41 | [IFLA_IPOIB_MODE] = { .type = NLA_U16 }, |
| 42 | [IFLA_IPOIB_UMCAST] = { .type = NLA_U16 }, |
Or Gerlitz | 9baa0b0 | 2012-09-13 05:56:36 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
Or Gerlitz | 862096a | 2012-09-27 12:06:02 +0000 | [diff] [blame] | 45 | static int ipoib_fill_info(struct sk_buff *skb, const struct net_device *dev) |
| 46 | { |
Erez Shitrit | c1048af | 2017-04-10 11:22:29 +0300 | [diff] [blame] | 47 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
Or Gerlitz | 862096a | 2012-09-27 12:06:02 +0000 | [diff] [blame] | 48 | u16 val; |
| 49 | |
| 50 | if (nla_put_u16(skb, IFLA_IPOIB_PKEY, priv->pkey)) |
| 51 | goto nla_put_failure; |
| 52 | |
| 53 | val = test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags); |
| 54 | if (nla_put_u16(skb, IFLA_IPOIB_MODE, val)) |
| 55 | goto nla_put_failure; |
| 56 | |
| 57 | val = test_bit(IPOIB_FLAG_UMCAST, &priv->flags); |
| 58 | if (nla_put_u16(skb, IFLA_IPOIB_UMCAST, val)) |
| 59 | goto nla_put_failure; |
| 60 | |
| 61 | return 0; |
| 62 | |
| 63 | nla_put_failure: |
| 64 | return -EMSGSIZE; |
| 65 | } |
| 66 | |
| 67 | static int ipoib_changelink(struct net_device *dev, |
| 68 | struct nlattr *tb[], struct nlattr *data[]) |
| 69 | { |
| 70 | u16 mode, umcast; |
| 71 | int ret = 0; |
| 72 | |
| 73 | if (data[IFLA_IPOIB_MODE]) { |
| 74 | mode = nla_get_u16(data[IFLA_IPOIB_MODE]); |
| 75 | if (mode == IPOIB_MODE_DATAGRAM) |
| 76 | ret = ipoib_set_mode(dev, "datagram\n"); |
| 77 | else if (mode == IPOIB_MODE_CONNECTED) |
| 78 | ret = ipoib_set_mode(dev, "connected\n"); |
| 79 | else |
| 80 | ret = -EINVAL; |
| 81 | |
| 82 | if (ret < 0) |
| 83 | goto out_err; |
| 84 | } |
| 85 | |
| 86 | if (data[IFLA_IPOIB_UMCAST]) { |
| 87 | umcast = nla_get_u16(data[IFLA_IPOIB_UMCAST]); |
| 88 | ipoib_set_umcast(dev, umcast); |
| 89 | } |
| 90 | |
| 91 | out_err: |
| 92 | return ret; |
| 93 | } |
| 94 | |
Or Gerlitz | 9baa0b0 | 2012-09-13 05:56:36 +0000 | [diff] [blame] | 95 | static int ipoib_new_child_link(struct net *src_net, struct net_device *dev, |
| 96 | struct nlattr *tb[], struct nlattr *data[]) |
| 97 | { |
| 98 | struct net_device *pdev; |
| 99 | struct ipoib_dev_priv *ppriv; |
| 100 | u16 child_pkey; |
| 101 | int err; |
| 102 | |
| 103 | if (!tb[IFLA_LINK]) |
| 104 | return -EINVAL; |
| 105 | |
| 106 | pdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); |
Hangbin Liu | 0d68fc4 | 2014-01-03 11:33:45 +0800 | [diff] [blame] | 107 | if (!pdev || pdev->type != ARPHRD_INFINIBAND) |
Or Gerlitz | 9baa0b0 | 2012-09-13 05:56:36 +0000 | [diff] [blame] | 108 | return -ENODEV; |
| 109 | |
Erez Shitrit | c1048af | 2017-04-10 11:22:29 +0300 | [diff] [blame] | 110 | ppriv = ipoib_priv(pdev); |
Or Gerlitz | 9baa0b0 | 2012-09-13 05:56:36 +0000 | [diff] [blame] | 111 | |
| 112 | if (test_bit(IPOIB_FLAG_SUBINTERFACE, &ppriv->flags)) { |
| 113 | ipoib_warn(ppriv, "child creation disallowed for child devices\n"); |
| 114 | return -EINVAL; |
| 115 | } |
| 116 | |
| 117 | if (!data || !data[IFLA_IPOIB_PKEY]) { |
| 118 | ipoib_dbg(ppriv, "no pkey specified, using parent pkey\n"); |
| 119 | child_pkey = ppriv->pkey; |
| 120 | } else |
| 121 | child_pkey = nla_get_u16(data[IFLA_IPOIB_PKEY]); |
| 122 | |
Or Gerlitz | 3d790a4 | 2013-07-18 14:02:31 +0300 | [diff] [blame] | 123 | if (child_pkey == 0 || child_pkey == 0x8000) |
| 124 | return -EINVAL; |
| 125 | |
| 126 | /* |
| 127 | * Set the full membership bit, so that we join the right |
| 128 | * broadcast group, etc. |
| 129 | */ |
| 130 | child_pkey |= 0x8000; |
| 131 | |
Erez Shitrit | c1048af | 2017-04-10 11:22:29 +0300 | [diff] [blame] | 132 | err = __ipoib_vlan_add(ppriv, ipoib_priv(dev), |
| 133 | child_pkey, IPOIB_RTNL_CHILD); |
Or Gerlitz | 9baa0b0 | 2012-09-13 05:56:36 +0000 | [diff] [blame] | 134 | |
Or Gerlitz | 862096a | 2012-09-27 12:06:02 +0000 | [diff] [blame] | 135 | if (!err && data) |
| 136 | err = ipoib_changelink(dev, tb, data); |
Or Gerlitz | 9baa0b0 | 2012-09-13 05:56:36 +0000 | [diff] [blame] | 137 | return err; |
| 138 | } |
| 139 | |
| 140 | static void ipoib_unregister_child_dev(struct net_device *dev, struct list_head *head) |
| 141 | { |
| 142 | struct ipoib_dev_priv *priv, *ppriv; |
| 143 | |
Erez Shitrit | c1048af | 2017-04-10 11:22:29 +0300 | [diff] [blame] | 144 | priv = ipoib_priv(dev); |
| 145 | ppriv = ipoib_priv(priv->parent); |
Or Gerlitz | 9baa0b0 | 2012-09-13 05:56:36 +0000 | [diff] [blame] | 146 | |
Erez Shitrit | f47944c | 2013-10-16 17:37:49 +0300 | [diff] [blame] | 147 | down_write(&ppriv->vlan_rwsem); |
Or Gerlitz | 9baa0b0 | 2012-09-13 05:56:36 +0000 | [diff] [blame] | 148 | unregister_netdevice_queue(dev, head); |
| 149 | list_del(&priv->list); |
Erez Shitrit | f47944c | 2013-10-16 17:37:49 +0300 | [diff] [blame] | 150 | up_write(&ppriv->vlan_rwsem); |
Or Gerlitz | 9baa0b0 | 2012-09-13 05:56:36 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static size_t ipoib_get_size(const struct net_device *dev) |
| 154 | { |
Or Gerlitz | 862096a | 2012-09-27 12:06:02 +0000 | [diff] [blame] | 155 | return nla_total_size(2) + /* IFLA_IPOIB_PKEY */ |
| 156 | nla_total_size(2) + /* IFLA_IPOIB_MODE */ |
| 157 | nla_total_size(2); /* IFLA_IPOIB_UMCAST */ |
Or Gerlitz | 9baa0b0 | 2012-09-13 05:56:36 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static struct rtnl_link_ops ipoib_link_ops __read_mostly = { |
| 161 | .kind = "ipoib", |
| 162 | .maxtype = IFLA_IPOIB_MAX, |
| 163 | .policy = ipoib_policy, |
| 164 | .priv_size = sizeof(struct ipoib_dev_priv), |
Erez Shitrit | cd565b4 | 2017-04-10 11:22:30 +0300 | [diff] [blame] | 165 | .setup = ipoib_setup_common, |
Or Gerlitz | 9baa0b0 | 2012-09-13 05:56:36 +0000 | [diff] [blame] | 166 | .newlink = ipoib_new_child_link, |
Or Gerlitz | 862096a | 2012-09-27 12:06:02 +0000 | [diff] [blame] | 167 | .changelink = ipoib_changelink, |
Or Gerlitz | 9baa0b0 | 2012-09-13 05:56:36 +0000 | [diff] [blame] | 168 | .dellink = ipoib_unregister_child_dev, |
| 169 | .get_size = ipoib_get_size, |
Or Gerlitz | 862096a | 2012-09-27 12:06:02 +0000 | [diff] [blame] | 170 | .fill_info = ipoib_fill_info, |
Or Gerlitz | 9baa0b0 | 2012-09-13 05:56:36 +0000 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | int __init ipoib_netlink_init(void) |
| 174 | { |
| 175 | return rtnl_link_register(&ipoib_link_ops); |
| 176 | } |
| 177 | |
| 178 | void __exit ipoib_netlink_fini(void) |
| 179 | { |
| 180 | rtnl_link_unregister(&ipoib_link_ops); |
| 181 | } |
| 182 | |
| 183 | MODULE_ALIAS_RTNL_LINK("ipoib"); |