rmnet_config.c 11 KB

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