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