1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2013-2018, The Linux Foundation. All rights reserved.
3  *
4  * RMNET configuration engine
5  */
6 
7 #include <net/sock.h>
8 #include <linux/module.h>
9 #include <linux/netlink.h>
10 #include <linux/netdevice.h>
11 #include "rmnet_config.h"
12 #include "rmnet_handlers.h"
13 #include "rmnet_vnd.h"
14 #include "rmnet_private.h"
15 
16 /* Local Definitions and Declarations */
17 
18 static const struct nla_policy rmnet_policy[IFLA_RMNET_MAX + 1] = {
19 	[IFLA_RMNET_MUX_ID]	= { .type = NLA_U16 },
20 	[IFLA_RMNET_FLAGS]	= { .len = sizeof(struct ifla_rmnet_flags) },
21 };
22 
23 static int rmnet_is_real_dev_registered(const struct net_device *real_dev)
24 {
25 	return rcu_access_pointer(real_dev->rx_handler) == rmnet_rx_handler;
26 }
27 
28 /* Needs rtnl lock */
29 static struct rmnet_port*
30 rmnet_get_port_rtnl(const struct net_device *real_dev)
31 {
32 	return rtnl_dereference(real_dev->rx_handler_data);
33 }
34 
35 static int rmnet_unregister_real_device(struct net_device *real_dev)
36 {
37 	struct rmnet_port *port = rmnet_get_port_rtnl(real_dev);
38 
39 	if (port->nr_rmnet_devs)
40 		return -EINVAL;
41 
42 	netdev_rx_handler_unregister(real_dev);
43 
44 	kfree(port);
45 
46 	netdev_dbg(real_dev, "Removed from rmnet\n");
47 	return 0;
48 }
49 
50 static int rmnet_register_real_device(struct net_device *real_dev,
51 				      struct netlink_ext_ack *extack)
52 {
53 	struct rmnet_port *port;
54 	int rc, entry;
55 
56 	ASSERT_RTNL();
57 
58 	if (rmnet_is_real_dev_registered(real_dev)) {
59 		port = rmnet_get_port_rtnl(real_dev);
60 		if (port->rmnet_mode != RMNET_EPMODE_VND) {
61 			NL_SET_ERR_MSG_MOD(extack, "bridge device already exists");
62 			return -EINVAL;
63 		}
64 
65 		return 0;
66 	}
67 
68 	port = kzalloc(sizeof(*port), GFP_KERNEL);
69 	if (!port)
70 		return -ENOMEM;
71 
72 	port->dev = real_dev;
73 	rc = netdev_rx_handler_register(real_dev, rmnet_rx_handler, port);
74 	if (rc) {
75 		kfree(port);
76 		return -EBUSY;
77 	}
78 
79 	for (entry = 0; entry < RMNET_MAX_LOGICAL_EP; entry++)
80 		INIT_HLIST_HEAD(&port->muxed_ep[entry]);
81 
82 	netdev_dbg(real_dev, "registered with rmnet\n");
83 	return 0;
84 }
85 
86 static void rmnet_unregister_bridge(struct rmnet_port *port)
87 {
88 	struct net_device *bridge_dev, *real_dev, *rmnet_dev;
89 	struct rmnet_port *real_port;
90 
91 	if (port->rmnet_mode != RMNET_EPMODE_BRIDGE)
92 		return;
93 
94 	rmnet_dev = port->rmnet_dev;
95 	if (!port->nr_rmnet_devs) {
96 		/* bridge device */
97 		real_dev = port->bridge_ep;
98 		bridge_dev = port->dev;
99 
100 		real_port = rmnet_get_port_rtnl(real_dev);
101 		real_port->bridge_ep = NULL;
102 		real_port->rmnet_mode = RMNET_EPMODE_VND;
103 	} else {
104 		/* real device */
105 		bridge_dev = port->bridge_ep;
106 
107 		port->bridge_ep = NULL;
108 		port->rmnet_mode = RMNET_EPMODE_VND;
109 	}
110 
111 	netdev_upper_dev_unlink(bridge_dev, rmnet_dev);
112 	rmnet_unregister_real_device(bridge_dev);
113 }
114 
115 static int rmnet_newlink(struct net *src_net, struct net_device *dev,
116 			 struct nlattr *tb[], struct nlattr *data[],
117 			 struct netlink_ext_ack *extack)
118 {
119 	u32 data_format = RMNET_FLAGS_INGRESS_DEAGGREGATION;
120 	struct net_device *real_dev;
121 	int mode = RMNET_EPMODE_VND;
122 	struct rmnet_endpoint *ep;
123 	struct rmnet_port *port;
124 	int err = 0;
125 	u16 mux_id;
126 
127 	if (!tb[IFLA_LINK]) {
128 		NL_SET_ERR_MSG_MOD(extack, "link not specified");
129 		return -EINVAL;
130 	}
131 
132 	real_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
133 	if (!real_dev) {
134 		NL_SET_ERR_MSG_MOD(extack, "link does not exist");
135 		return -ENODEV;
136 	}
137 
138 	ep = kzalloc(sizeof(*ep), GFP_KERNEL);
139 	if (!ep)
140 		return -ENOMEM;
141 
142 	mux_id = nla_get_u16(data[IFLA_RMNET_MUX_ID]);
143 
144 	err = rmnet_register_real_device(real_dev, extack);
145 	if (err)
146 		goto err0;
147 
148 	port = rmnet_get_port_rtnl(real_dev);
149 	err = rmnet_vnd_newlink(mux_id, dev, port, real_dev, ep, extack);
150 	if (err)
151 		goto err1;
152 
153 	err = netdev_upper_dev_link(real_dev, dev, extack);
154 	if (err < 0)
155 		goto err2;
156 
157 	port->rmnet_mode = mode;
158 	port->rmnet_dev = dev;
159 
160 	hlist_add_head_rcu(&ep->hlnode, &port->muxed_ep[mux_id]);
161 
162 	if (data[IFLA_RMNET_FLAGS]) {
163 		struct ifla_rmnet_flags *flags;
164 
165 		flags = nla_data(data[IFLA_RMNET_FLAGS]);
166 		data_format = flags->flags & flags->mask;
167 	}
168 
169 	netdev_dbg(dev, "data format [0x%08X]\n", data_format);
170 	port->data_format = data_format;
171 
172 	return 0;
173 
174 err2:
175 	unregister_netdevice(dev);
176 	rmnet_vnd_dellink(mux_id, port, ep);
177 err1:
178 	rmnet_unregister_real_device(real_dev);
179 err0:
180 	kfree(ep);
181 	return err;
182 }
183 
184 static void rmnet_dellink(struct net_device *dev, struct list_head *head)
185 {
186 	struct rmnet_priv *priv = netdev_priv(dev);
187 	struct net_device *real_dev, *bridge_dev;
188 	struct rmnet_port *real_port, *bridge_port;
189 	struct rmnet_endpoint *ep;
190 	u8 mux_id = priv->mux_id;
191 
192 	real_dev = priv->real_dev;
193 
194 	if (!rmnet_is_real_dev_registered(real_dev))
195 		return;
196 
197 	real_port = rmnet_get_port_rtnl(real_dev);
198 	bridge_dev = real_port->bridge_ep;
199 	if (bridge_dev) {
200 		bridge_port = rmnet_get_port_rtnl(bridge_dev);
201 		rmnet_unregister_bridge(bridge_port);
202 	}
203 
204 	ep = rmnet_get_endpoint(real_port, mux_id);
205 	if (ep) {
206 		hlist_del_init_rcu(&ep->hlnode);
207 		rmnet_vnd_dellink(mux_id, real_port, ep);
208 		kfree(ep);
209 	}
210 
211 	netdev_upper_dev_unlink(real_dev, dev);
212 	rmnet_unregister_real_device(real_dev);
213 	unregister_netdevice_queue(dev, head);
214 }
215 
216 static void rmnet_force_unassociate_device(struct net_device *real_dev)
217 {
218 	struct hlist_node *tmp_ep;
219 	struct rmnet_endpoint *ep;
220 	struct rmnet_port *port;
221 	unsigned long bkt_ep;
222 	LIST_HEAD(list);
223 
224 	port = rmnet_get_port_rtnl(real_dev);
225 
226 	if (port->nr_rmnet_devs) {
227 		/* real device */
228 		rmnet_unregister_bridge(port);
229 		hash_for_each_safe(port->muxed_ep, bkt_ep, tmp_ep, ep, hlnode) {
230 			unregister_netdevice_queue(ep->egress_dev, &list);
231 			netdev_upper_dev_unlink(real_dev, ep->egress_dev);
232 			rmnet_vnd_dellink(ep->mux_id, port, ep);
233 			hlist_del_init_rcu(&ep->hlnode);
234 			kfree(ep);
235 		}
236 		rmnet_unregister_real_device(real_dev);
237 		unregister_netdevice_many(&list);
238 	} else {
239 		rmnet_unregister_bridge(port);
240 	}
241 }
242 
243 static int rmnet_config_notify_cb(struct notifier_block *nb,
244 				  unsigned long event, void *data)
245 {
246 	struct net_device *real_dev = netdev_notifier_info_to_dev(data);
247 
248 	if (!rmnet_is_real_dev_registered(real_dev))
249 		return NOTIFY_DONE;
250 
251 	switch (event) {
252 	case NETDEV_UNREGISTER:
253 		netdev_dbg(real_dev, "Kernel unregister\n");
254 		rmnet_force_unassociate_device(real_dev);
255 		break;
256 
257 	default:
258 		break;
259 	}
260 
261 	return NOTIFY_DONE;
262 }
263 
264 static struct notifier_block rmnet_dev_notifier __read_mostly = {
265 	.notifier_call = rmnet_config_notify_cb,
266 };
267 
268 static int rmnet_rtnl_validate(struct nlattr *tb[], struct nlattr *data[],
269 			       struct netlink_ext_ack *extack)
270 {
271 	u16 mux_id;
272 
273 	if (!data || !data[IFLA_RMNET_MUX_ID]) {
274 		NL_SET_ERR_MSG_MOD(extack, "MUX ID not specified");
275 		return -EINVAL;
276 	}
277 
278 	mux_id = nla_get_u16(data[IFLA_RMNET_MUX_ID]);
279 	if (mux_id > (RMNET_MAX_LOGICAL_EP - 1)) {
280 		NL_SET_ERR_MSG_MOD(extack, "invalid MUX ID");
281 		return -ERANGE;
282 	}
283 
284 	return 0;
285 }
286 
287 static int rmnet_changelink(struct net_device *dev, struct nlattr *tb[],
288 			    struct nlattr *data[],
289 			    struct netlink_ext_ack *extack)
290 {
291 	struct rmnet_priv *priv = netdev_priv(dev);
292 	struct net_device *real_dev;
293 	struct rmnet_port *port;
294 	u16 mux_id;
295 
296 	if (!dev)
297 		return -ENODEV;
298 
299 	real_dev = priv->real_dev;
300 	if (!rmnet_is_real_dev_registered(real_dev))
301 		return -ENODEV;
302 
303 	port = rmnet_get_port_rtnl(real_dev);
304 
305 	if (data[IFLA_RMNET_MUX_ID]) {
306 		mux_id = nla_get_u16(data[IFLA_RMNET_MUX_ID]);
307 
308 		if (mux_id != priv->mux_id) {
309 			struct rmnet_endpoint *ep;
310 
311 			ep = rmnet_get_endpoint(port, priv->mux_id);
312 			if (!ep)
313 				return -ENODEV;
314 
315 			if (rmnet_get_endpoint(port, mux_id)) {
316 				NL_SET_ERR_MSG_MOD(extack,
317 						   "MUX ID already exists");
318 				return -EINVAL;
319 			}
320 
321 			hlist_del_init_rcu(&ep->hlnode);
322 			hlist_add_head_rcu(&ep->hlnode,
323 					   &port->muxed_ep[mux_id]);
324 
325 			ep->mux_id = mux_id;
326 			priv->mux_id = mux_id;
327 		}
328 	}
329 
330 	if (data[IFLA_RMNET_FLAGS]) {
331 		struct ifla_rmnet_flags *flags;
332 
333 		flags = nla_data(data[IFLA_RMNET_FLAGS]);
334 		port->data_format = flags->flags & flags->mask;
335 	}
336 
337 	return 0;
338 }
339 
340 static size_t rmnet_get_size(const struct net_device *dev)
341 {
342 	return
343 		/* IFLA_RMNET_MUX_ID */
344 		nla_total_size(2) +
345 		/* IFLA_RMNET_FLAGS */
346 		nla_total_size(sizeof(struct ifla_rmnet_flags));
347 }
348 
349 static int rmnet_fill_info(struct sk_buff *skb, const struct net_device *dev)
350 {
351 	struct rmnet_priv *priv = netdev_priv(dev);
352 	struct net_device *real_dev;
353 	struct ifla_rmnet_flags f;
354 	struct rmnet_port *port;
355 
356 	real_dev = priv->real_dev;
357 
358 	if (nla_put_u16(skb, IFLA_RMNET_MUX_ID, priv->mux_id))
359 		goto nla_put_failure;
360 
361 	if (rmnet_is_real_dev_registered(real_dev)) {
362 		port = rmnet_get_port_rtnl(real_dev);
363 		f.flags = port->data_format;
364 	} else {
365 		f.flags = 0;
366 	}
367 
368 	f.mask  = ~0;
369 
370 	if (nla_put(skb, IFLA_RMNET_FLAGS, sizeof(f), &f))
371 		goto nla_put_failure;
372 
373 	return 0;
374 
375 nla_put_failure:
376 	return -EMSGSIZE;
377 }
378 
379 struct rtnl_link_ops rmnet_link_ops __read_mostly = {
380 	.kind		= "rmnet",
381 	.maxtype	= __IFLA_RMNET_MAX,
382 	.priv_size	= sizeof(struct rmnet_priv),
383 	.setup		= rmnet_vnd_setup,
384 	.validate	= rmnet_rtnl_validate,
385 	.newlink	= rmnet_newlink,
386 	.dellink	= rmnet_dellink,
387 	.get_size	= rmnet_get_size,
388 	.changelink     = rmnet_changelink,
389 	.policy		= rmnet_policy,
390 	.fill_info	= rmnet_fill_info,
391 };
392 
393 struct rmnet_port *rmnet_get_port_rcu(struct net_device *real_dev)
394 {
395 	if (rmnet_is_real_dev_registered(real_dev))
396 		return rcu_dereference_bh(real_dev->rx_handler_data);
397 	else
398 		return NULL;
399 }
400 
401 struct rmnet_endpoint *rmnet_get_endpoint(struct rmnet_port *port, u8 mux_id)
402 {
403 	struct rmnet_endpoint *ep;
404 
405 	hlist_for_each_entry_rcu(ep, &port->muxed_ep[mux_id], hlnode) {
406 		if (ep->mux_id == mux_id)
407 			return ep;
408 	}
409 
410 	return NULL;
411 }
412 
413 int rmnet_add_bridge(struct net_device *rmnet_dev,
414 		     struct net_device *slave_dev,
415 		     struct netlink_ext_ack *extack)
416 {
417 	struct rmnet_priv *priv = netdev_priv(rmnet_dev);
418 	struct net_device *real_dev = priv->real_dev;
419 	struct rmnet_port *port, *slave_port;
420 	int err;
421 
422 	port = rmnet_get_port_rtnl(real_dev);
423 
424 	/* If there is more than one rmnet dev attached, its probably being
425 	 * used for muxing. Skip the briding in that case
426 	 */
427 	if (port->nr_rmnet_devs > 1) {
428 		NL_SET_ERR_MSG_MOD(extack, "more than one rmnet dev attached");
429 		return -EINVAL;
430 	}
431 
432 	if (port->rmnet_mode != RMNET_EPMODE_VND) {
433 		NL_SET_ERR_MSG_MOD(extack, "more than one bridge dev attached");
434 		return -EINVAL;
435 	}
436 
437 	if (rmnet_is_real_dev_registered(slave_dev)) {
438 		NL_SET_ERR_MSG_MOD(extack,
439 				   "slave cannot be another rmnet dev");
440 
441 		return -EBUSY;
442 	}
443 
444 	err = rmnet_register_real_device(slave_dev, extack);
445 	if (err)
446 		return -EBUSY;
447 
448 	err = netdev_master_upper_dev_link(slave_dev, rmnet_dev, NULL, NULL,
449 					   extack);
450 	if (err) {
451 		rmnet_unregister_real_device(slave_dev);
452 		return err;
453 	}
454 
455 	slave_port = rmnet_get_port_rtnl(slave_dev);
456 	slave_port->rmnet_mode = RMNET_EPMODE_BRIDGE;
457 	slave_port->bridge_ep = real_dev;
458 	slave_port->rmnet_dev = rmnet_dev;
459 
460 	port->rmnet_mode = RMNET_EPMODE_BRIDGE;
461 	port->bridge_ep = slave_dev;
462 
463 	netdev_dbg(slave_dev, "registered with rmnet as slave\n");
464 	return 0;
465 }
466 
467 int rmnet_del_bridge(struct net_device *rmnet_dev,
468 		     struct net_device *slave_dev)
469 {
470 	struct rmnet_port *port = rmnet_get_port_rtnl(slave_dev);
471 
472 	rmnet_unregister_bridge(port);
473 
474 	netdev_dbg(slave_dev, "removed from rmnet as slave\n");
475 	return 0;
476 }
477 
478 /* Startup/Shutdown */
479 
480 static int __init rmnet_init(void)
481 {
482 	int rc;
483 
484 	rc = register_netdevice_notifier(&rmnet_dev_notifier);
485 	if (rc != 0)
486 		return rc;
487 
488 	rc = rtnl_link_register(&rmnet_link_ops);
489 	if (rc != 0) {
490 		unregister_netdevice_notifier(&rmnet_dev_notifier);
491 		return rc;
492 	}
493 	return rc;
494 }
495 
496 static void __exit rmnet_exit(void)
497 {
498 	rtnl_link_unregister(&rmnet_link_ops);
499 	unregister_netdevice_notifier(&rmnet_dev_notifier);
500 }
501 
502 module_init(rmnet_init)
503 module_exit(rmnet_exit)
504 MODULE_ALIAS_RTNL_LINK("rmnet");
505 MODULE_LICENSE("GPL v2");
506