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