vcan.c 5.3 KB

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