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"
16 /* Local Definitions and Declarations */
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
) },
23 static int rmnet_is_real_dev_registered(const struct net_device
*real_dev
)
25 return rcu_access_pointer(real_dev
->rx_handler
) == rmnet_rx_handler
;
30 rmnet_get_port_rtnl(const struct net_device
*real_dev
)
32 return rtnl_dereference(real_dev
->rx_handler_data
);
35 static int rmnet_unregister_real_device(struct net_device
*real_dev
)
37 struct rmnet_port
*port
= rmnet_get_port_rtnl(real_dev
);
39 if (port
->nr_rmnet_devs
)
42 netdev_rx_handler_unregister(real_dev
);
46 netdev_dbg(real_dev
, "Removed from rmnet\n");
50 static int rmnet_register_real_device(struct net_device
*real_dev
,
51 struct netlink_ext_ack
*extack
)
53 struct rmnet_port
*port
;
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");
68 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
73 rc
= netdev_rx_handler_register(real_dev
, rmnet_rx_handler
, port
);
79 for (entry
= 0; entry
< RMNET_MAX_LOGICAL_EP
; entry
++)
80 INIT_HLIST_HEAD(&port
->muxed_ep
[entry
]);
82 netdev_dbg(real_dev
, "registered with rmnet\n");
86 static void rmnet_unregister_bridge(struct rmnet_port
*port
)
88 struct net_device
*bridge_dev
, *real_dev
, *rmnet_dev
;
89 struct rmnet_port
*real_port
;
91 if (port
->rmnet_mode
!= RMNET_EPMODE_BRIDGE
)
94 rmnet_dev
= port
->rmnet_dev
;
95 if (!port
->nr_rmnet_devs
) {
97 real_dev
= port
->bridge_ep
;
98 bridge_dev
= port
->dev
;
100 real_port
= rmnet_get_port_rtnl(real_dev
);
101 real_port
->bridge_ep
= NULL
;
102 real_port
->rmnet_mode
= RMNET_EPMODE_VND
;
105 bridge_dev
= port
->bridge_ep
;
107 port
->bridge_ep
= NULL
;
108 port
->rmnet_mode
= RMNET_EPMODE_VND
;
111 netdev_upper_dev_unlink(bridge_dev
, rmnet_dev
);
112 rmnet_unregister_real_device(bridge_dev
);
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
)
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
;
127 if (!tb
[IFLA_LINK
]) {
128 NL_SET_ERR_MSG_MOD(extack
, "link not specified");
132 real_dev
= __dev_get_by_index(src_net
, nla_get_u32(tb
[IFLA_LINK
]));
134 NL_SET_ERR_MSG_MOD(extack
, "link does not exist");
138 ep
= kzalloc(sizeof(*ep
), GFP_KERNEL
);
142 mux_id
= nla_get_u16(data
[IFLA_RMNET_MUX_ID
]);
144 err
= rmnet_register_real_device(real_dev
, extack
);
148 port
= rmnet_get_port_rtnl(real_dev
);
149 err
= rmnet_vnd_newlink(mux_id
, dev
, port
, real_dev
, ep
, extack
);
153 err
= netdev_upper_dev_link(real_dev
, dev
, extack
);
157 port
->rmnet_mode
= mode
;
158 port
->rmnet_dev
= dev
;
160 hlist_add_head_rcu(&ep
->hlnode
, &port
->muxed_ep
[mux_id
]);
162 if (data
[IFLA_RMNET_FLAGS
]) {
163 struct ifla_rmnet_flags
*flags
;
165 flags
= nla_data(data
[IFLA_RMNET_FLAGS
]);
166 data_format
= flags
->flags
& flags
->mask
;
169 netdev_dbg(dev
, "data format [0x%08X]\n", data_format
);
170 port
->data_format
= data_format
;
175 unregister_netdevice(dev
);
176 rmnet_vnd_dellink(mux_id
, port
, ep
);
178 rmnet_unregister_real_device(real_dev
);
184 static void rmnet_dellink(struct net_device
*dev
, struct list_head
*head
)
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
;
192 real_dev
= priv
->real_dev
;
194 if (!rmnet_is_real_dev_registered(real_dev
))
197 real_port
= rmnet_get_port_rtnl(real_dev
);
198 bridge_dev
= real_port
->bridge_ep
;
200 bridge_port
= rmnet_get_port_rtnl(bridge_dev
);
201 rmnet_unregister_bridge(bridge_port
);
204 ep
= rmnet_get_endpoint(real_port
, mux_id
);
206 hlist_del_init_rcu(&ep
->hlnode
);
207 rmnet_vnd_dellink(mux_id
, real_port
, ep
);
211 netdev_upper_dev_unlink(real_dev
, dev
);
212 rmnet_unregister_real_device(real_dev
);
213 unregister_netdevice_queue(dev
, head
);
216 static void rmnet_force_unassociate_device(struct net_device
*real_dev
)
218 struct hlist_node
*tmp_ep
;
219 struct rmnet_endpoint
*ep
;
220 struct rmnet_port
*port
;
221 unsigned long bkt_ep
;
224 port
= rmnet_get_port_rtnl(real_dev
);
226 if (port
->nr_rmnet_devs
) {
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
);
236 rmnet_unregister_real_device(real_dev
);
237 unregister_netdevice_many(&list
);
239 rmnet_unregister_bridge(port
);
243 static int rmnet_config_notify_cb(struct notifier_block
*nb
,
244 unsigned long event
, void *data
)
246 struct net_device
*real_dev
= netdev_notifier_info_to_dev(data
);
248 if (!rmnet_is_real_dev_registered(real_dev
))
252 case NETDEV_UNREGISTER
:
253 netdev_dbg(real_dev
, "Kernel unregister\n");
254 rmnet_force_unassociate_device(real_dev
);
256 case NETDEV_CHANGEMTU
:
257 if (rmnet_vnd_validate_real_dev_mtu(real_dev
))
267 static struct notifier_block rmnet_dev_notifier __read_mostly
= {
268 .notifier_call
= rmnet_config_notify_cb
,
271 static int rmnet_rtnl_validate(struct nlattr
*tb
[], struct nlattr
*data
[],
272 struct netlink_ext_ack
*extack
)
276 if (!data
|| !data
[IFLA_RMNET_MUX_ID
]) {
277 NL_SET_ERR_MSG_MOD(extack
, "MUX ID not specified");
281 mux_id
= nla_get_u16(data
[IFLA_RMNET_MUX_ID
]);
282 if (mux_id
> (RMNET_MAX_LOGICAL_EP
- 1)) {
283 NL_SET_ERR_MSG_MOD(extack
, "invalid MUX ID");
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_port
*port
;
302 real_dev
= priv
->real_dev
;
303 if (!rmnet_is_real_dev_registered(real_dev
))
306 port
= rmnet_get_port_rtnl(real_dev
);
308 if (data
[IFLA_RMNET_MUX_ID
]) {
309 mux_id
= nla_get_u16(data
[IFLA_RMNET_MUX_ID
]);
311 if (mux_id
!= priv
->mux_id
) {
312 struct rmnet_endpoint
*ep
;
314 ep
= rmnet_get_endpoint(port
, priv
->mux_id
);
318 if (rmnet_get_endpoint(port
, mux_id
)) {
319 NL_SET_ERR_MSG_MOD(extack
,
320 "MUX ID already exists");
324 hlist_del_init_rcu(&ep
->hlnode
);
325 hlist_add_head_rcu(&ep
->hlnode
,
326 &port
->muxed_ep
[mux_id
]);
329 priv
->mux_id
= mux_id
;
333 if (data
[IFLA_RMNET_FLAGS
]) {
334 struct ifla_rmnet_flags
*flags
;
337 old_data_format
= port
->data_format
;
338 flags
= nla_data(data
[IFLA_RMNET_FLAGS
]);
339 port
->data_format
= flags
->flags
& flags
->mask
;
341 if (rmnet_vnd_update_dev_mtu(port
, real_dev
)) {
342 port
->data_format
= old_data_format
;
343 NL_SET_ERR_MSG_MOD(extack
, "Invalid MTU on real dev");
351 static size_t rmnet_get_size(const struct net_device
*dev
)
354 /* IFLA_RMNET_MUX_ID */
356 /* IFLA_RMNET_FLAGS */
357 nla_total_size(sizeof(struct ifla_rmnet_flags
));
360 static int rmnet_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
362 struct rmnet_priv
*priv
= netdev_priv(dev
);
363 struct net_device
*real_dev
;
364 struct ifla_rmnet_flags f
;
365 struct rmnet_port
*port
;
367 real_dev
= priv
->real_dev
;
369 if (nla_put_u16(skb
, IFLA_RMNET_MUX_ID
, priv
->mux_id
))
370 goto nla_put_failure
;
372 if (rmnet_is_real_dev_registered(real_dev
)) {
373 port
= rmnet_get_port_rtnl(real_dev
);
374 f
.flags
= port
->data_format
;
381 if (nla_put(skb
, IFLA_RMNET_FLAGS
, sizeof(f
), &f
))
382 goto nla_put_failure
;
390 struct rtnl_link_ops rmnet_link_ops __read_mostly
= {
392 .maxtype
= __IFLA_RMNET_MAX
,
393 .priv_size
= sizeof(struct rmnet_priv
),
394 .setup
= rmnet_vnd_setup
,
395 .validate
= rmnet_rtnl_validate
,
396 .newlink
= rmnet_newlink
,
397 .dellink
= rmnet_dellink
,
398 .get_size
= rmnet_get_size
,
399 .changelink
= rmnet_changelink
,
400 .policy
= rmnet_policy
,
401 .fill_info
= rmnet_fill_info
,
404 struct rmnet_port
*rmnet_get_port_rcu(struct net_device
*real_dev
)
406 if (rmnet_is_real_dev_registered(real_dev
))
407 return rcu_dereference_bh(real_dev
->rx_handler_data
);
412 struct rmnet_endpoint
*rmnet_get_endpoint(struct rmnet_port
*port
, u8 mux_id
)
414 struct rmnet_endpoint
*ep
;
416 hlist_for_each_entry_rcu(ep
, &port
->muxed_ep
[mux_id
], hlnode
) {
417 if (ep
->mux_id
== mux_id
)
424 int rmnet_add_bridge(struct net_device
*rmnet_dev
,
425 struct net_device
*slave_dev
,
426 struct netlink_ext_ack
*extack
)
428 struct rmnet_priv
*priv
= netdev_priv(rmnet_dev
);
429 struct net_device
*real_dev
= priv
->real_dev
;
430 struct rmnet_port
*port
, *slave_port
;
433 port
= rmnet_get_port_rtnl(real_dev
);
435 /* If there is more than one rmnet dev attached, its probably being
436 * used for muxing. Skip the briding in that case
438 if (port
->nr_rmnet_devs
> 1) {
439 NL_SET_ERR_MSG_MOD(extack
, "more than one rmnet dev attached");
443 if (port
->rmnet_mode
!= RMNET_EPMODE_VND
) {
444 NL_SET_ERR_MSG_MOD(extack
, "more than one bridge dev attached");
448 if (rmnet_is_real_dev_registered(slave_dev
)) {
449 NL_SET_ERR_MSG_MOD(extack
,
450 "slave cannot be another rmnet dev");
455 err
= rmnet_register_real_device(slave_dev
, extack
);
459 err
= netdev_master_upper_dev_link(slave_dev
, rmnet_dev
, NULL
, NULL
,
462 rmnet_unregister_real_device(slave_dev
);
466 slave_port
= rmnet_get_port_rtnl(slave_dev
);
467 slave_port
->rmnet_mode
= RMNET_EPMODE_BRIDGE
;
468 slave_port
->bridge_ep
= real_dev
;
469 slave_port
->rmnet_dev
= rmnet_dev
;
471 port
->rmnet_mode
= RMNET_EPMODE_BRIDGE
;
472 port
->bridge_ep
= slave_dev
;
474 netdev_dbg(slave_dev
, "registered with rmnet as slave\n");
478 int rmnet_del_bridge(struct net_device
*rmnet_dev
,
479 struct net_device
*slave_dev
)
481 struct rmnet_port
*port
= rmnet_get_port_rtnl(slave_dev
);
483 rmnet_unregister_bridge(port
);
485 netdev_dbg(slave_dev
, "removed from rmnet as slave\n");
489 /* Startup/Shutdown */
491 static int __init
rmnet_init(void)
495 rc
= register_netdevice_notifier(&rmnet_dev_notifier
);
499 rc
= rtnl_link_register(&rmnet_link_ops
);
501 unregister_netdevice_notifier(&rmnet_dev_notifier
);
507 static void __exit
rmnet_exit(void)
509 rtnl_link_unregister(&rmnet_link_ops
);
510 unregister_netdevice_notifier(&rmnet_dev_notifier
);
513 module_init(rmnet_init
)
514 module_exit(rmnet_exit
)
515 MODULE_ALIAS_RTNL_LINK("rmnet");
516 MODULE_LICENSE("GPL v2");