1 /*
2  * Copyright (c) 2004 Topspin Communications.  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/module.h>
34 #include <linux/sched/signal.h>
35 
36 #include <linux/init.h>
37 #include <linux/seq_file.h>
38 
39 #include <linux/uaccess.h>
40 
41 #include "ipoib.h"
42 
43 static ssize_t show_parent(struct device *d, struct device_attribute *attr,
44 			   char *buf)
45 {
46 	struct net_device *dev = to_net_dev(d);
47 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
48 
49 	return sprintf(buf, "%s\n", priv->parent->name);
50 }
51 static DEVICE_ATTR(parent, S_IRUGO, show_parent, NULL);
52 
53 int __ipoib_vlan_add(struct ipoib_dev_priv *ppriv, struct ipoib_dev_priv *priv,
54 		     u16 pkey, int type)
55 {
56 	int result;
57 
58 	priv->max_ib_mtu = ppriv->max_ib_mtu;
59 	/* MTU will be reset when mcast join happens */
60 	priv->dev->mtu   = IPOIB_UD_MTU(priv->max_ib_mtu);
61 	priv->mcast_mtu  = priv->admin_mtu = priv->dev->mtu;
62 	priv->parent = ppriv->dev;
63 	set_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags);
64 
65 	ipoib_set_dev_features(priv, ppriv->ca);
66 
67 	priv->pkey = pkey;
68 
69 	memcpy(priv->dev->dev_addr, ppriv->dev->dev_addr, INFINIBAND_ALEN);
70 	memcpy(&priv->local_gid, &ppriv->local_gid, sizeof(priv->local_gid));
71 	set_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
72 	priv->dev->broadcast[8] = pkey >> 8;
73 	priv->dev->broadcast[9] = pkey & 0xff;
74 
75 	result = ipoib_dev_init(priv->dev, ppriv->ca, ppriv->port);
76 	if (result < 0) {
77 		ipoib_warn(ppriv, "failed to initialize subinterface: "
78 			   "device %s, port %d",
79 			   ppriv->ca->name, ppriv->port);
80 		goto err;
81 	}
82 
83 	result = register_netdevice(priv->dev);
84 	if (result) {
85 		ipoib_warn(priv, "failed to initialize; error %i", result);
86 		goto register_failed;
87 	}
88 
89 	/* RTNL childs don't need proprietary sysfs entries */
90 	if (type == IPOIB_LEGACY_CHILD) {
91 		if (ipoib_cm_add_mode_attr(priv->dev))
92 			goto sysfs_failed;
93 		if (ipoib_add_pkey_attr(priv->dev))
94 			goto sysfs_failed;
95 		if (ipoib_add_umcast_attr(priv->dev))
96 			goto sysfs_failed;
97 
98 		if (device_create_file(&priv->dev->dev, &dev_attr_parent))
99 			goto sysfs_failed;
100 	}
101 
102 	priv->child_type  = type;
103 	list_add_tail(&priv->list, &ppriv->child_intfs);
104 
105 	return 0;
106 
107 sysfs_failed:
108 	result = -ENOMEM;
109 	unregister_netdevice(priv->dev);
110 
111 register_failed:
112 	ipoib_dev_cleanup(priv->dev);
113 
114 err:
115 	return result;
116 }
117 
118 int ipoib_vlan_add(struct net_device *pdev, unsigned short pkey)
119 {
120 	struct ipoib_dev_priv *ppriv, *priv;
121 	char intf_name[IFNAMSIZ];
122 	struct ipoib_dev_priv *tpriv;
123 	int result;
124 
125 	if (!capable(CAP_NET_ADMIN))
126 		return -EPERM;
127 
128 	ppriv = ipoib_priv(pdev);
129 
130 	if (test_bit(IPOIB_FLAG_GOING_DOWN, &ppriv->flags))
131 		return -EPERM;
132 
133 	snprintf(intf_name, sizeof intf_name, "%s.%04x",
134 		 ppriv->dev->name, pkey);
135 
136 	if (!mutex_trylock(&ppriv->sysfs_mutex))
137 		return restart_syscall();
138 
139 	if (!rtnl_trylock()) {
140 		mutex_unlock(&ppriv->sysfs_mutex);
141 		return restart_syscall();
142 	}
143 
144 	priv = ipoib_intf_alloc(ppriv->ca, ppriv->port, intf_name);
145 	if (!priv) {
146 		rtnl_unlock();
147 		mutex_unlock(&ppriv->sysfs_mutex);
148 		return -ENOMEM;
149 	}
150 
151 	down_write(&ppriv->vlan_rwsem);
152 
153 	/*
154 	 * First ensure this isn't a duplicate. We check the parent device and
155 	 * then all of the legacy child interfaces to make sure the Pkey
156 	 * doesn't match.
157 	 */
158 	if (ppriv->pkey == pkey) {
159 		result = -ENOTUNIQ;
160 		goto out;
161 	}
162 
163 	list_for_each_entry(tpriv, &ppriv->child_intfs, list) {
164 		if (tpriv->pkey == pkey &&
165 		    tpriv->child_type == IPOIB_LEGACY_CHILD) {
166 			result = -ENOTUNIQ;
167 			goto out;
168 		}
169 	}
170 
171 	result = __ipoib_vlan_add(ppriv, priv, pkey, IPOIB_LEGACY_CHILD);
172 
173 out:
174 	up_write(&ppriv->vlan_rwsem);
175 	rtnl_unlock();
176 	mutex_unlock(&ppriv->sysfs_mutex);
177 
178 	if (result) {
179 		free_netdev(priv->dev);
180 		kfree(priv);
181 	}
182 
183 	return result;
184 }
185 
186 int ipoib_vlan_delete(struct net_device *pdev, unsigned short pkey)
187 {
188 	struct ipoib_dev_priv *ppriv, *priv, *tpriv;
189 	struct net_device *dev = NULL;
190 
191 	if (!capable(CAP_NET_ADMIN))
192 		return -EPERM;
193 
194 	ppriv = ipoib_priv(pdev);
195 
196 	if (test_bit(IPOIB_FLAG_GOING_DOWN, &ppriv->flags))
197 		return -EPERM;
198 
199 	if (!mutex_trylock(&ppriv->sysfs_mutex))
200 		return restart_syscall();
201 
202 	if (!rtnl_trylock()) {
203 		mutex_unlock(&ppriv->sysfs_mutex);
204 		return restart_syscall();
205 	}
206 
207 	down_write(&ppriv->vlan_rwsem);
208 	list_for_each_entry_safe(priv, tpriv, &ppriv->child_intfs, list) {
209 		if (priv->pkey == pkey &&
210 		    priv->child_type == IPOIB_LEGACY_CHILD) {
211 			list_del(&priv->list);
212 			dev = priv->dev;
213 			break;
214 		}
215 	}
216 	up_write(&ppriv->vlan_rwsem);
217 
218 	if (dev) {
219 		ipoib_dbg(ppriv, "delete child vlan %s\n", dev->name);
220 		unregister_netdevice(dev);
221 	}
222 
223 	rtnl_unlock();
224 	mutex_unlock(&ppriv->sysfs_mutex);
225 
226 	if (dev) {
227 		free_netdev(dev);
228 		kfree(priv);
229 		return 0;
230 	}
231 
232 	return -ENODEV;
233 }
234