vxcan.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * vxcan.c - Virtual CAN Tunnel for cross namespace communication
  4. *
  5. * This code is derived from drivers/net/can/vcan.c for the virtual CAN
  6. * specific parts and from drivers/net/veth.c to implement the netlink API
  7. * for network interface pairs in a common and established way.
  8. *
  9. * Copyright (c) 2017 Oliver Hartkopp <socketcan@hartkopp.net>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/if_arp.h>
  15. #include <linux/if_ether.h>
  16. #include <linux/can.h>
  17. #include <linux/can/dev.h>
  18. #include <linux/can/skb.h>
  19. #include <linux/can/vxcan.h>
  20. #include <linux/can/can-ml.h>
  21. #include <linux/slab.h>
  22. #include <net/rtnetlink.h>
  23. #define DRV_NAME "vxcan"
  24. MODULE_DESCRIPTION("Virtual CAN Tunnel");
  25. MODULE_LICENSE("GPL");
  26. MODULE_AUTHOR("Oliver Hartkopp <socketcan@hartkopp.net>");
  27. MODULE_ALIAS_RTNL_LINK(DRV_NAME);
  28. struct vxcan_priv {
  29. struct net_device __rcu *peer;
  30. };
  31. static netdev_tx_t vxcan_xmit(struct sk_buff *skb, struct net_device *dev)
  32. {
  33. struct vxcan_priv *priv = netdev_priv(dev);
  34. struct net_device *peer;
  35. struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
  36. struct net_device_stats *peerstats, *srcstats = &dev->stats;
  37. u8 len;
  38. if (can_dropped_invalid_skb(dev, skb))
  39. return NETDEV_TX_OK;
  40. rcu_read_lock();
  41. peer = rcu_dereference(priv->peer);
  42. if (unlikely(!peer)) {
  43. kfree_skb(skb);
  44. dev->stats.tx_dropped++;
  45. goto out_unlock;
  46. }
  47. skb = can_create_echo_skb(skb);
  48. if (!skb)
  49. goto out_unlock;
  50. /* reset CAN GW hop counter */
  51. skb->csum_start = 0;
  52. skb->pkt_type = PACKET_BROADCAST;
  53. skb->dev = peer;
  54. skb->ip_summed = CHECKSUM_UNNECESSARY;
  55. len = cfd->len;
  56. if (netif_rx_ni(skb) == NET_RX_SUCCESS) {
  57. srcstats->tx_packets++;
  58. srcstats->tx_bytes += len;
  59. peerstats = &peer->stats;
  60. peerstats->rx_packets++;
  61. peerstats->rx_bytes += len;
  62. }
  63. out_unlock:
  64. rcu_read_unlock();
  65. return NETDEV_TX_OK;
  66. }
  67. static int vxcan_open(struct net_device *dev)
  68. {
  69. struct vxcan_priv *priv = netdev_priv(dev);
  70. struct net_device *peer = rtnl_dereference(priv->peer);
  71. if (!peer)
  72. return -ENOTCONN;
  73. if (peer->flags & IFF_UP) {
  74. netif_carrier_on(dev);
  75. netif_carrier_on(peer);
  76. }
  77. return 0;
  78. }
  79. static int vxcan_close(struct net_device *dev)
  80. {
  81. struct vxcan_priv *priv = netdev_priv(dev);
  82. struct net_device *peer = rtnl_dereference(priv->peer);
  83. netif_carrier_off(dev);
  84. if (peer)
  85. netif_carrier_off(peer);
  86. return 0;
  87. }
  88. static int vxcan_get_iflink(const struct net_device *dev)
  89. {
  90. struct vxcan_priv *priv = netdev_priv(dev);
  91. struct net_device *peer;
  92. int iflink;
  93. rcu_read_lock();
  94. peer = rcu_dereference(priv->peer);
  95. iflink = peer ? peer->ifindex : 0;
  96. rcu_read_unlock();
  97. return iflink;
  98. }
  99. static int vxcan_change_mtu(struct net_device *dev, int new_mtu)
  100. {
  101. /* Do not allow changing the MTU while running */
  102. if (dev->flags & IFF_UP)
  103. return -EBUSY;
  104. if (new_mtu != CAN_MTU && new_mtu != CANFD_MTU)
  105. return -EINVAL;
  106. dev->mtu = new_mtu;
  107. return 0;
  108. }
  109. static const struct net_device_ops vxcan_netdev_ops = {
  110. .ndo_open = vxcan_open,
  111. .ndo_stop = vxcan_close,
  112. .ndo_start_xmit = vxcan_xmit,
  113. .ndo_get_iflink = vxcan_get_iflink,
  114. .ndo_change_mtu = vxcan_change_mtu,
  115. };
  116. static void vxcan_setup(struct net_device *dev)
  117. {
  118. struct can_ml_priv *can_ml;
  119. dev->type = ARPHRD_CAN;
  120. dev->mtu = CANFD_MTU;
  121. dev->hard_header_len = 0;
  122. dev->addr_len = 0;
  123. dev->tx_queue_len = 0;
  124. dev->flags = (IFF_NOARP|IFF_ECHO);
  125. dev->netdev_ops = &vxcan_netdev_ops;
  126. dev->needs_free_netdev = true;
  127. can_ml = netdev_priv(dev) + ALIGN(sizeof(struct vxcan_priv), NETDEV_ALIGN);
  128. can_set_ml_priv(dev, can_ml);
  129. }
  130. /* forward declaration for rtnl_create_link() */
  131. static struct rtnl_link_ops vxcan_link_ops;
  132. static int vxcan_newlink(struct net *net, struct net_device *dev,
  133. struct nlattr *tb[], struct nlattr *data[],
  134. struct netlink_ext_ack *extack)
  135. {
  136. struct vxcan_priv *priv;
  137. struct net_device *peer;
  138. struct net *peer_net;
  139. struct nlattr *peer_tb[IFLA_MAX + 1], **tbp = tb;
  140. char ifname[IFNAMSIZ];
  141. unsigned char name_assign_type;
  142. struct ifinfomsg *ifmp = NULL;
  143. int err;
  144. /* register peer device */
  145. if (data && data[VXCAN_INFO_PEER]) {
  146. struct nlattr *nla_peer;
  147. nla_peer = data[VXCAN_INFO_PEER];
  148. ifmp = nla_data(nla_peer);
  149. err = rtnl_nla_parse_ifla(peer_tb,
  150. nla_data(nla_peer) +
  151. sizeof(struct ifinfomsg),
  152. nla_len(nla_peer) -
  153. sizeof(struct ifinfomsg),
  154. NULL);
  155. if (err < 0)
  156. return err;
  157. tbp = peer_tb;
  158. }
  159. if (ifmp && tbp[IFLA_IFNAME]) {
  160. nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
  161. name_assign_type = NET_NAME_USER;
  162. } else {
  163. snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
  164. name_assign_type = NET_NAME_ENUM;
  165. }
  166. peer_net = rtnl_link_get_net(net, tbp);
  167. if (IS_ERR(peer_net))
  168. return PTR_ERR(peer_net);
  169. peer = rtnl_create_link(peer_net, ifname, name_assign_type,
  170. &vxcan_link_ops, tbp, extack);
  171. if (IS_ERR(peer)) {
  172. put_net(peer_net);
  173. return PTR_ERR(peer);
  174. }
  175. if (ifmp && dev->ifindex)
  176. peer->ifindex = ifmp->ifi_index;
  177. err = register_netdevice(peer);
  178. put_net(peer_net);
  179. peer_net = NULL;
  180. if (err < 0) {
  181. free_netdev(peer);
  182. return err;
  183. }
  184. netif_carrier_off(peer);
  185. err = rtnl_configure_link(peer, ifmp);
  186. if (err < 0)
  187. goto unregister_network_device;
  188. /* register first device */
  189. if (tb[IFLA_IFNAME])
  190. nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
  191. else
  192. snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
  193. err = register_netdevice(dev);
  194. if (err < 0)
  195. goto unregister_network_device;
  196. netif_carrier_off(dev);
  197. /* cross link the device pair */
  198. priv = netdev_priv(dev);
  199. rcu_assign_pointer(priv->peer, peer);
  200. priv = netdev_priv(peer);
  201. rcu_assign_pointer(priv->peer, dev);
  202. return 0;
  203. unregister_network_device:
  204. unregister_netdevice(peer);
  205. return err;
  206. }
  207. static void vxcan_dellink(struct net_device *dev, struct list_head *head)
  208. {
  209. struct vxcan_priv *priv;
  210. struct net_device *peer;
  211. priv = netdev_priv(dev);
  212. peer = rtnl_dereference(priv->peer);
  213. /* Note : dellink() is called from default_device_exit_batch(),
  214. * before a rcu_synchronize() point. The devices are guaranteed
  215. * not being freed before one RCU grace period.
  216. */
  217. RCU_INIT_POINTER(priv->peer, NULL);
  218. unregister_netdevice_queue(dev, head);
  219. if (peer) {
  220. priv = netdev_priv(peer);
  221. RCU_INIT_POINTER(priv->peer, NULL);
  222. unregister_netdevice_queue(peer, head);
  223. }
  224. }
  225. static const struct nla_policy vxcan_policy[VXCAN_INFO_MAX + 1] = {
  226. [VXCAN_INFO_PEER] = { .len = sizeof(struct ifinfomsg) },
  227. };
  228. static struct net *vxcan_get_link_net(const struct net_device *dev)
  229. {
  230. struct vxcan_priv *priv = netdev_priv(dev);
  231. struct net_device *peer = rtnl_dereference(priv->peer);
  232. return peer ? dev_net(peer) : dev_net(dev);
  233. }
  234. static struct rtnl_link_ops vxcan_link_ops = {
  235. .kind = DRV_NAME,
  236. .priv_size = ALIGN(sizeof(struct vxcan_priv), NETDEV_ALIGN) + sizeof(struct can_ml_priv),
  237. .setup = vxcan_setup,
  238. .newlink = vxcan_newlink,
  239. .dellink = vxcan_dellink,
  240. .policy = vxcan_policy,
  241. .maxtype = VXCAN_INFO_MAX,
  242. .get_link_net = vxcan_get_link_net,
  243. };
  244. static __init int vxcan_init(void)
  245. {
  246. pr_info("vxcan: Virtual CAN Tunnel driver\n");
  247. return rtnl_link_register(&vxcan_link_ops);
  248. }
  249. static __exit void vxcan_exit(void)
  250. {
  251. rtnl_link_unregister(&vxcan_link_ops);
  252. }
  253. module_init(vxcan_init);
  254. module_exit(vxcan_exit);