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 real_dev
= __dev_get_by_index(src_net
, nla_get_u32(tb
[IFLA_LINK
]));
139 if (!real_dev
|| !dev
)
142 if (!data
[IFLA_RMNET_MUX_ID
])
145 ep
= kzalloc(sizeof(*ep
), GFP_ATOMIC
);
149 mux_id
= nla_get_u16(data
[IFLA_RMNET_MUX_ID
]);
151 err
= rmnet_register_real_device(real_dev
);
155 port
= rmnet_get_port_rtnl(real_dev
);
156 err
= rmnet_vnd_newlink(mux_id
, dev
, port
, real_dev
, ep
);
160 port
->rmnet_mode
= mode
;
162 hlist_add_head_rcu(&ep
->hlnode
, &port
->muxed_ep
[mux_id
]);
164 if (data
[IFLA_RMNET_FLAGS
]) {
165 struct ifla_rmnet_flags
*flags
;
167 flags
= nla_data(data
[IFLA_RMNET_FLAGS
]);
168 data_format
= flags
->flags
& flags
->mask
;
171 netdev_dbg(dev
, "data format [0x%08X]\n", data_format
);
172 port
->data_format
= data_format
;
177 rmnet_unregister_real_device(real_dev
, port
);
183 static void rmnet_dellink(struct net_device
*dev
, struct list_head
*head
)
185 struct rmnet_priv
*priv
= netdev_priv(dev
);
186 struct net_device
*real_dev
;
187 struct rmnet_endpoint
*ep
;
188 struct rmnet_port
*port
;
191 real_dev
= priv
->real_dev
;
193 if (!real_dev
|| !rmnet_is_real_dev_registered(real_dev
))
196 port
= rmnet_get_port_rtnl(real_dev
);
198 mux_id
= rmnet_vnd_get_mux(dev
);
200 ep
= rmnet_get_endpoint(port
, mux_id
);
202 hlist_del_init_rcu(&ep
->hlnode
);
203 rmnet_unregister_bridge(dev
, port
);
204 rmnet_vnd_dellink(mux_id
, port
, ep
);
207 rmnet_unregister_real_device(real_dev
, port
);
209 unregister_netdevice_queue(dev
, head
);
212 static void rmnet_force_unassociate_device(struct net_device
*dev
)
214 struct net_device
*real_dev
= dev
;
215 struct hlist_node
*tmp_ep
;
216 struct rmnet_endpoint
*ep
;
217 struct rmnet_port
*port
;
218 unsigned long bkt_ep
;
221 if (!rmnet_is_real_dev_registered(real_dev
))
226 port
= rmnet_get_port_rtnl(dev
);
229 rmnet_unregister_bridge(dev
, port
);
231 hash_for_each_safe(port
->muxed_ep
, bkt_ep
, tmp_ep
, ep
, hlnode
) {
232 unregister_netdevice_queue(ep
->egress_dev
, &list
);
233 rmnet_vnd_dellink(ep
->mux_id
, port
, ep
);
235 hlist_del_init_rcu(&ep
->hlnode
);
240 unregister_netdevice_many(&list
);
242 rmnet_unregister_real_device(real_dev
, port
);
245 static int rmnet_config_notify_cb(struct notifier_block
*nb
,
246 unsigned long event
, void *data
)
248 struct net_device
*dev
= netdev_notifier_info_to_dev(data
);
254 case NETDEV_UNREGISTER
:
255 netdev_dbg(dev
, "Kernel unregister\n");
256 rmnet_force_unassociate_device(dev
);
266 static struct notifier_block rmnet_dev_notifier __read_mostly
= {
267 .notifier_call
= rmnet_config_notify_cb
,
270 static int rmnet_rtnl_validate(struct nlattr
*tb
[], struct nlattr
*data
[],
271 struct netlink_ext_ack
*extack
)
275 if (!data
|| !data
[IFLA_RMNET_MUX_ID
])
278 mux_id
= nla_get_u16(data
[IFLA_RMNET_MUX_ID
]);
279 if (mux_id
> (RMNET_MAX_LOGICAL_EP
- 1))
285 static int rmnet_changelink(struct net_device
*dev
, struct nlattr
*tb
[],
286 struct nlattr
*data
[],
287 struct netlink_ext_ack
*extack
)
289 struct rmnet_priv
*priv
= netdev_priv(dev
);
290 struct net_device
*real_dev
;
291 struct rmnet_endpoint
*ep
;
292 struct rmnet_port
*port
;
298 real_dev
= __dev_get_by_index(dev_net(dev
),
299 nla_get_u32(tb
[IFLA_LINK
]));
301 if (!real_dev
|| !rmnet_is_real_dev_registered(real_dev
))
304 port
= rmnet_get_port_rtnl(real_dev
);
306 if (data
[IFLA_RMNET_MUX_ID
]) {
307 mux_id
= nla_get_u16(data
[IFLA_RMNET_MUX_ID
]);
308 ep
= rmnet_get_endpoint(port
, priv
->mux_id
);
312 hlist_del_init_rcu(&ep
->hlnode
);
313 hlist_add_head_rcu(&ep
->hlnode
, &port
->muxed_ep
[mux_id
]);
316 priv
->mux_id
= mux_id
;
319 if (data
[IFLA_RMNET_FLAGS
]) {
320 struct ifla_rmnet_flags
*flags
;
322 flags
= nla_data(data
[IFLA_RMNET_FLAGS
]);
323 port
->data_format
= flags
->flags
& flags
->mask
;
329 static size_t rmnet_get_size(const struct net_device
*dev
)
332 /* IFLA_RMNET_MUX_ID */
334 /* IFLA_RMNET_FLAGS */
335 nla_total_size(sizeof(struct ifla_rmnet_flags
));
338 static int rmnet_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
340 struct rmnet_priv
*priv
= netdev_priv(dev
);
341 struct net_device
*real_dev
;
342 struct ifla_rmnet_flags f
;
343 struct rmnet_port
*port
;
345 real_dev
= priv
->real_dev
;
347 if (nla_put_u16(skb
, IFLA_RMNET_MUX_ID
, priv
->mux_id
))
348 goto nla_put_failure
;
350 if (rmnet_is_real_dev_registered(real_dev
)) {
351 port
= rmnet_get_port_rtnl(real_dev
);
352 f
.flags
= port
->data_format
;
359 if (nla_put(skb
, IFLA_RMNET_FLAGS
, sizeof(f
), &f
))
360 goto nla_put_failure
;
368 struct rtnl_link_ops rmnet_link_ops __read_mostly
= {
370 .maxtype
= __IFLA_RMNET_MAX
,
371 .priv_size
= sizeof(struct rmnet_priv
),
372 .setup
= rmnet_vnd_setup
,
373 .validate
= rmnet_rtnl_validate
,
374 .newlink
= rmnet_newlink
,
375 .dellink
= rmnet_dellink
,
376 .get_size
= rmnet_get_size
,
377 .changelink
= rmnet_changelink
,
378 .policy
= rmnet_policy
,
379 .fill_info
= rmnet_fill_info
,
382 /* Needs either rcu_read_lock() or rtnl lock */
383 struct rmnet_port
*rmnet_get_port(struct net_device
*real_dev
)
385 if (rmnet_is_real_dev_registered(real_dev
))
386 return rcu_dereference_rtnl(real_dev
->rx_handler_data
);
391 struct rmnet_endpoint
*rmnet_get_endpoint(struct rmnet_port
*port
, u8 mux_id
)
393 struct rmnet_endpoint
*ep
;
395 hlist_for_each_entry_rcu(ep
, &port
->muxed_ep
[mux_id
], hlnode
) {
396 if (ep
->mux_id
== mux_id
)
403 int rmnet_add_bridge(struct net_device
*rmnet_dev
,
404 struct net_device
*slave_dev
,
405 struct netlink_ext_ack
*extack
)
407 struct rmnet_priv
*priv
= netdev_priv(rmnet_dev
);
408 struct net_device
*real_dev
= priv
->real_dev
;
409 struct rmnet_port
*port
, *slave_port
;
412 port
= rmnet_get_port(real_dev
);
414 /* If there is more than one rmnet dev attached, its probably being
415 * used for muxing. Skip the briding in that case
417 if (port
->nr_rmnet_devs
> 1)
420 if (rmnet_is_real_dev_registered(slave_dev
))
423 err
= rmnet_register_real_device(slave_dev
);
427 slave_port
= rmnet_get_port(slave_dev
);
428 slave_port
->rmnet_mode
= RMNET_EPMODE_BRIDGE
;
429 slave_port
->bridge_ep
= real_dev
;
431 port
->rmnet_mode
= RMNET_EPMODE_BRIDGE
;
432 port
->bridge_ep
= slave_dev
;
434 netdev_dbg(slave_dev
, "registered with rmnet as slave\n");
438 int rmnet_del_bridge(struct net_device
*rmnet_dev
,
439 struct net_device
*slave_dev
)
441 struct rmnet_priv
*priv
= netdev_priv(rmnet_dev
);
442 struct net_device
*real_dev
= priv
->real_dev
;
443 struct rmnet_port
*port
, *slave_port
;
445 port
= rmnet_get_port(real_dev
);
446 port
->rmnet_mode
= RMNET_EPMODE_VND
;
447 port
->bridge_ep
= NULL
;
449 slave_port
= rmnet_get_port(slave_dev
);
450 rmnet_unregister_real_device(slave_dev
, slave_port
);
452 netdev_dbg(slave_dev
, "removed from rmnet as slave\n");
456 /* Startup/Shutdown */
458 static int __init
rmnet_init(void)
462 rc
= register_netdevice_notifier(&rmnet_dev_notifier
);
466 rc
= rtnl_link_register(&rmnet_link_ops
);
468 unregister_netdevice_notifier(&rmnet_dev_notifier
);
474 static void __exit
rmnet_exit(void)
476 unregister_netdevice_notifier(&rmnet_dev_notifier
);
477 rtnl_link_unregister(&rmnet_link_ops
);
480 module_init(rmnet_init
)
481 module_exit(rmnet_exit
)
482 MODULE_LICENSE("GPL v2");