1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2013-2018, The Linux Foundation. All rights reserved.
4 * RMNET configuration engine
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"
17 * The shared resource which needs to be protected is realdev->rx_handler_data.
18 * For the writer path, this is using rtnl_lock(). The writer paths are
19 * rmnet_newlink(), rmnet_dellink() and rmnet_force_unassociate_device(). These
20 * paths are already called with rtnl_lock() acquired in. There is also an
21 * ASSERT_RTNL() to ensure that we are calling with rtnl acquired. For
22 * dereference here, we will need to use rtnl_dereference(). Dev list writing
23 * needs to happen with rtnl_lock() acquired for netdev_master_upper_dev_link().
24 * For the reader path, the real_dev->rx_handler_data is called in the TX / RX
25 * path. We only need rcu_read_lock() for these scenarios. In these cases,
26 * the rcu_read_lock() is held in __dev_queue_xmit() and
27 * netif_receive_skb_internal(), so readers need to use rcu_dereference_rtnl()
28 * to get the relevant information. For dev list reading, we again acquire
29 * rcu_read_lock() in rmnet_dellink() for netdev_master_upper_dev_get_rcu().
30 * We also use unregister_netdevice_many() to free all rmnet devices in
31 * rmnet_force_unassociate_device() so we dont lose the rtnl_lock() and free in
35 /* Local Definitions and Declarations */
37 static const struct nla_policy rmnet_policy[IFLA_RMNET_MAX + 1] = {
38 [IFLA_RMNET_MUX_ID] = { .type = NLA_U16 },
39 [IFLA_RMNET_FLAGS] = { .len = sizeof(struct ifla_rmnet_flags) },
42 static int rmnet_is_real_dev_registered(const struct net_device *real_dev)
44 return rcu_access_pointer(real_dev->rx_handler) == rmnet_rx_handler;
48 static struct rmnet_port*
49 rmnet_get_port_rtnl(const struct net_device *real_dev)
51 return rtnl_dereference(real_dev->rx_handler_data);
54 static int rmnet_unregister_real_device(struct net_device *real_dev,
55 struct rmnet_port *port)
57 if (port->nr_rmnet_devs)
60 netdev_rx_handler_unregister(real_dev);
64 /* release reference on real_dev */
67 netdev_dbg(real_dev, "Removed from rmnet\n");
71 static int rmnet_register_real_device(struct net_device *real_dev)
73 struct rmnet_port *port;
78 if (rmnet_is_real_dev_registered(real_dev))
81 port = kzalloc(sizeof(*port), GFP_ATOMIC);
86 rc = netdev_rx_handler_register(real_dev, rmnet_rx_handler, port);
92 /* hold on to real dev for MAP data */
95 for (entry = 0; entry < RMNET_MAX_LOGICAL_EP; entry++)
96 INIT_HLIST_HEAD(&port->muxed_ep[entry]);
98 netdev_dbg(real_dev, "registered with rmnet\n");
102 static void rmnet_unregister_bridge(struct net_device *dev,
103 struct rmnet_port *port)
105 struct rmnet_port *bridge_port;
106 struct net_device *bridge_dev;
108 if (port->rmnet_mode != RMNET_EPMODE_BRIDGE)
111 /* bridge slave handling */
112 if (!port->nr_rmnet_devs) {
113 bridge_dev = port->bridge_ep;
115 bridge_port = rmnet_get_port_rtnl(bridge_dev);
116 bridge_port->bridge_ep = NULL;
117 bridge_port->rmnet_mode = RMNET_EPMODE_VND;
119 bridge_dev = port->bridge_ep;
121 bridge_port = rmnet_get_port_rtnl(bridge_dev);
122 rmnet_unregister_real_device(bridge_dev, bridge_port);
126 static int rmnet_newlink(struct net *src_net, struct net_device *dev,
127 struct nlattr *tb[], struct nlattr *data[],
128 struct netlink_ext_ack *extack)
130 u32 data_format = RMNET_FLAGS_INGRESS_DEAGGREGATION;
131 struct net_device *real_dev;
132 int mode = RMNET_EPMODE_VND;
133 struct rmnet_endpoint *ep;
134 struct rmnet_port *port;
138 if (!tb[IFLA_LINK]) {
139 NL_SET_ERR_MSG_MOD(extack, "link not specified");
143 real_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
144 if (!real_dev || !dev)
147 if (!data[IFLA_RMNET_MUX_ID])
150 ep = kzalloc(sizeof(*ep), GFP_ATOMIC);
154 mux_id = nla_get_u16(data[IFLA_RMNET_MUX_ID]);
156 err = rmnet_register_real_device(real_dev);
160 port = rmnet_get_port_rtnl(real_dev);
161 err = rmnet_vnd_newlink(mux_id, dev, port, real_dev, ep);
165 port->rmnet_mode = mode;
167 hlist_add_head_rcu(&ep->hlnode, &port->muxed_ep[mux_id]);
169 if (data[IFLA_RMNET_FLAGS]) {
170 struct ifla_rmnet_flags *flags;
172 flags = nla_data(data[IFLA_RMNET_FLAGS]);
173 data_format = flags->flags & flags->mask;
176 netdev_dbg(dev, "data format [0x%08X]\n", data_format);
177 port->data_format = data_format;
182 rmnet_unregister_real_device(real_dev, port);
188 static void rmnet_dellink(struct net_device *dev, struct list_head *head)
190 struct rmnet_priv *priv = netdev_priv(dev);
191 struct net_device *real_dev;
192 struct rmnet_endpoint *ep;
193 struct rmnet_port *port;
196 real_dev = priv->real_dev;
198 if (!real_dev || !rmnet_is_real_dev_registered(real_dev))
201 port = rmnet_get_port_rtnl(real_dev);
203 mux_id = rmnet_vnd_get_mux(dev);
205 ep = rmnet_get_endpoint(port, mux_id);
207 hlist_del_init_rcu(&ep->hlnode);
208 rmnet_unregister_bridge(dev, port);
209 rmnet_vnd_dellink(mux_id, port, ep);
212 rmnet_unregister_real_device(real_dev, port);
214 unregister_netdevice_queue(dev, head);
217 static void rmnet_force_unassociate_device(struct net_device *dev)
219 struct net_device *real_dev = dev;
220 struct hlist_node *tmp_ep;
221 struct rmnet_endpoint *ep;
222 struct rmnet_port *port;
223 unsigned long bkt_ep;
226 if (!rmnet_is_real_dev_registered(real_dev))
231 port = rmnet_get_port_rtnl(dev);
234 rmnet_unregister_bridge(dev, port);
236 hash_for_each_safe(port->muxed_ep, bkt_ep, tmp_ep, ep, hlnode) {
237 unregister_netdevice_queue(ep->egress_dev, &list);
238 rmnet_vnd_dellink(ep->mux_id, port, ep);
240 hlist_del_init_rcu(&ep->hlnode);
245 unregister_netdevice_many(&list);
247 rmnet_unregister_real_device(real_dev, port);
250 static int rmnet_config_notify_cb(struct notifier_block *nb,
251 unsigned long event, void *data)
253 struct net_device *dev = netdev_notifier_info_to_dev(data);
259 case NETDEV_UNREGISTER:
260 netdev_dbg(dev, "Kernel unregister\n");
261 rmnet_force_unassociate_device(dev);
271 static struct notifier_block rmnet_dev_notifier __read_mostly = {
272 .notifier_call = rmnet_config_notify_cb,
275 static int rmnet_rtnl_validate(struct nlattr *tb[], struct nlattr *data[],
276 struct netlink_ext_ack *extack)
280 if (!data || !data[IFLA_RMNET_MUX_ID])
283 mux_id = nla_get_u16(data[IFLA_RMNET_MUX_ID]);
284 if (mux_id > (RMNET_MAX_LOGICAL_EP - 1))
290 static int rmnet_changelink(struct net_device *dev, struct nlattr *tb[],
291 struct nlattr *data[],
292 struct netlink_ext_ack *extack)
294 struct rmnet_priv *priv = netdev_priv(dev);
295 struct net_device *real_dev;
296 struct rmnet_endpoint *ep;
297 struct rmnet_port *port;
303 real_dev = __dev_get_by_index(dev_net(dev),
304 nla_get_u32(tb[IFLA_LINK]));
306 if (!real_dev || !rmnet_is_real_dev_registered(real_dev))
309 port = rmnet_get_port_rtnl(real_dev);
311 if (data[IFLA_RMNET_MUX_ID]) {
312 mux_id = nla_get_u16(data[IFLA_RMNET_MUX_ID]);
313 ep = rmnet_get_endpoint(port, priv->mux_id);
317 hlist_del_init_rcu(&ep->hlnode);
318 hlist_add_head_rcu(&ep->hlnode, &port->muxed_ep[mux_id]);
321 priv->mux_id = mux_id;
324 if (data[IFLA_RMNET_FLAGS]) {
325 struct ifla_rmnet_flags *flags;
327 flags = nla_data(data[IFLA_RMNET_FLAGS]);
328 port->data_format = flags->flags & flags->mask;
334 static size_t rmnet_get_size(const struct net_device *dev)
337 /* IFLA_RMNET_MUX_ID */
339 /* IFLA_RMNET_FLAGS */
340 nla_total_size(sizeof(struct ifla_rmnet_flags));
343 static int rmnet_fill_info(struct sk_buff *skb, const struct net_device *dev)
345 struct rmnet_priv *priv = netdev_priv(dev);
346 struct net_device *real_dev;
347 struct ifla_rmnet_flags f;
348 struct rmnet_port *port;
350 real_dev = priv->real_dev;
352 if (nla_put_u16(skb, IFLA_RMNET_MUX_ID, priv->mux_id))
353 goto nla_put_failure;
355 if (rmnet_is_real_dev_registered(real_dev)) {
356 port = rmnet_get_port_rtnl(real_dev);
357 f.flags = port->data_format;
364 if (nla_put(skb, IFLA_RMNET_FLAGS, sizeof(f), &f))
365 goto nla_put_failure;
373 struct rtnl_link_ops rmnet_link_ops __read_mostly = {
375 .maxtype = __IFLA_RMNET_MAX,
376 .priv_size = sizeof(struct rmnet_priv),
377 .setup = rmnet_vnd_setup,
378 .validate = rmnet_rtnl_validate,
379 .newlink = rmnet_newlink,
380 .dellink = rmnet_dellink,
381 .get_size = rmnet_get_size,
382 .changelink = rmnet_changelink,
383 .policy = rmnet_policy,
384 .fill_info = rmnet_fill_info,
387 /* Needs either rcu_read_lock() or rtnl lock */
388 struct rmnet_port *rmnet_get_port(struct net_device *real_dev)
390 if (rmnet_is_real_dev_registered(real_dev))
391 return rcu_dereference_rtnl(real_dev->rx_handler_data);
396 struct rmnet_endpoint *rmnet_get_endpoint(struct rmnet_port *port, u8 mux_id)
398 struct rmnet_endpoint *ep;
400 hlist_for_each_entry_rcu(ep, &port->muxed_ep[mux_id], hlnode) {
401 if (ep->mux_id == mux_id)
408 int rmnet_add_bridge(struct net_device *rmnet_dev,
409 struct net_device *slave_dev,
410 struct netlink_ext_ack *extack)
412 struct rmnet_priv *priv = netdev_priv(rmnet_dev);
413 struct net_device *real_dev = priv->real_dev;
414 struct rmnet_port *port, *slave_port;
417 port = rmnet_get_port(real_dev);
419 /* If there is more than one rmnet dev attached, its probably being
420 * used for muxing. Skip the briding in that case
422 if (port->nr_rmnet_devs > 1)
425 if (rmnet_is_real_dev_registered(slave_dev))
428 err = rmnet_register_real_device(slave_dev);
432 slave_port = rmnet_get_port(slave_dev);
433 slave_port->rmnet_mode = RMNET_EPMODE_BRIDGE;
434 slave_port->bridge_ep = real_dev;
436 port->rmnet_mode = RMNET_EPMODE_BRIDGE;
437 port->bridge_ep = slave_dev;
439 netdev_dbg(slave_dev, "registered with rmnet as slave\n");
443 int rmnet_del_bridge(struct net_device *rmnet_dev,
444 struct net_device *slave_dev)
446 struct rmnet_priv *priv = netdev_priv(rmnet_dev);
447 struct net_device *real_dev = priv->real_dev;
448 struct rmnet_port *port, *slave_port;
450 port = rmnet_get_port(real_dev);
451 port->rmnet_mode = RMNET_EPMODE_VND;
452 port->bridge_ep = NULL;
454 slave_port = rmnet_get_port(slave_dev);
455 rmnet_unregister_real_device(slave_dev, slave_port);
457 netdev_dbg(slave_dev, "removed from rmnet as slave\n");
461 /* Startup/Shutdown */
463 static int __init rmnet_init(void)
467 rc = register_netdevice_notifier(&rmnet_dev_notifier);
471 rc = rtnl_link_register(&rmnet_link_ops);
473 unregister_netdevice_notifier(&rmnet_dev_notifier);
479 static void __exit rmnet_exit(void)
481 unregister_netdevice_notifier(&rmnet_dev_notifier);
482 rtnl_link_unregister(&rmnet_link_ops);
485 module_init(rmnet_init)
486 module_exit(rmnet_exit)
487 MODULE_LICENSE("GPL v2");