vcan.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* vcan.c - Virtual CAN interface
  2. *
  3. * Copyright (c) 2002-2017 Volkswagen Group Electronic Research
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of Volkswagen nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * Alternatively, provided that this notice is retained in full, this
  19. * software may be distributed under the terms of the GNU General
  20. * Public License ("GPL") version 2, in which case the provisions of the
  21. * GPL apply INSTEAD OF those given above.
  22. *
  23. * The provided data structures and external interfaces from this code
  24. * are not restricted to be used by modules with a GPL compatible license.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  29. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  30. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  31. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  32. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  33. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  34. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  36. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  37. * DAMAGE.
  38. *
  39. */
  40. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  41. #include <linux/module.h>
  42. #include <linux/init.h>
  43. #include <linux/netdevice.h>
  44. #include <linux/if_arp.h>
  45. #include <linux/if_ether.h>
  46. #include <linux/can.h>
  47. #include <linux/can/can-ml.h>
  48. #include <linux/can/dev.h>
  49. #include <linux/can/skb.h>
  50. #include <linux/slab.h>
  51. #include <net/rtnetlink.h>
  52. #define DRV_NAME "vcan"
  53. MODULE_DESCRIPTION("virtual CAN interface");
  54. MODULE_LICENSE("Dual BSD/GPL");
  55. MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
  56. MODULE_ALIAS_RTNL_LINK(DRV_NAME);
  57. /* CAN test feature:
  58. * Enable the echo on driver level for testing the CAN core echo modes.
  59. * See Documentation/networking/can.rst for details.
  60. */
  61. static bool echo; /* echo testing. Default: 0 (Off) */
  62. module_param(echo, bool, 0444);
  63. MODULE_PARM_DESC(echo, "Echo sent frames (for testing). Default: 0 (Off)");
  64. static void vcan_rx(struct sk_buff *skb, struct net_device *dev)
  65. {
  66. struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
  67. struct net_device_stats *stats = &dev->stats;
  68. stats->rx_packets++;
  69. stats->rx_bytes += cfd->len;
  70. skb->pkt_type = PACKET_BROADCAST;
  71. skb->dev = dev;
  72. skb->ip_summed = CHECKSUM_UNNECESSARY;
  73. netif_rx_ni(skb);
  74. }
  75. static netdev_tx_t vcan_tx(struct sk_buff *skb, struct net_device *dev)
  76. {
  77. struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
  78. struct net_device_stats *stats = &dev->stats;
  79. int loop;
  80. if (can_dropped_invalid_skb(dev, skb))
  81. return NETDEV_TX_OK;
  82. stats->tx_packets++;
  83. stats->tx_bytes += cfd->len;
  84. /* set flag whether this packet has to be looped back */
  85. loop = skb->pkt_type == PACKET_LOOPBACK;
  86. if (!echo) {
  87. /* no echo handling available inside this driver */
  88. if (loop) {
  89. /* only count the packets here, because the
  90. * CAN core already did the echo for us
  91. */
  92. stats->rx_packets++;
  93. stats->rx_bytes += cfd->len;
  94. }
  95. consume_skb(skb);
  96. return NETDEV_TX_OK;
  97. }
  98. /* perform standard echo handling for CAN network interfaces */
  99. if (loop) {
  100. skb = can_create_echo_skb(skb);
  101. if (!skb)
  102. return NETDEV_TX_OK;
  103. /* receive with packet counting */
  104. vcan_rx(skb, dev);
  105. } else {
  106. /* no looped packets => no counting */
  107. consume_skb(skb);
  108. }
  109. return NETDEV_TX_OK;
  110. }
  111. static int vcan_change_mtu(struct net_device *dev, int new_mtu)
  112. {
  113. /* Do not allow changing the MTU while running */
  114. if (dev->flags & IFF_UP)
  115. return -EBUSY;
  116. if (new_mtu != CAN_MTU && new_mtu != CANFD_MTU)
  117. return -EINVAL;
  118. dev->mtu = new_mtu;
  119. return 0;
  120. }
  121. static const struct net_device_ops vcan_netdev_ops = {
  122. .ndo_start_xmit = vcan_tx,
  123. .ndo_change_mtu = vcan_change_mtu,
  124. };
  125. static void vcan_setup(struct net_device *dev)
  126. {
  127. dev->type = ARPHRD_CAN;
  128. dev->mtu = CANFD_MTU;
  129. dev->hard_header_len = 0;
  130. dev->addr_len = 0;
  131. dev->tx_queue_len = 0;
  132. dev->flags = IFF_NOARP;
  133. can_set_ml_priv(dev, netdev_priv(dev));
  134. /* set flags according to driver capabilities */
  135. if (echo)
  136. dev->flags |= IFF_ECHO;
  137. dev->netdev_ops = &vcan_netdev_ops;
  138. dev->needs_free_netdev = true;
  139. }
  140. static struct rtnl_link_ops vcan_link_ops __read_mostly = {
  141. .kind = DRV_NAME,
  142. .priv_size = sizeof(struct can_ml_priv),
  143. .setup = vcan_setup,
  144. };
  145. static __init int vcan_init_module(void)
  146. {
  147. pr_info("Virtual CAN interface driver\n");
  148. if (echo)
  149. pr_info("enabled echo on driver level.\n");
  150. return rtnl_link_register(&vcan_link_ops);
  151. }
  152. static __exit void vcan_cleanup_module(void)
  153. {
  154. rtnl_link_unregister(&vcan_link_ops);
  155. }
  156. module_init(vcan_init_module);
  157. module_exit(vcan_cleanup_module);