1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* L2TPv3 ethernet pseudowire driver 3 * 4 * Copyright (c) 2008,2009,2010 Katalix Systems Ltd 5 */ 6 7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 8 9 #include <linux/module.h> 10 #include <linux/skbuff.h> 11 #include <linux/socket.h> 12 #include <linux/hash.h> 13 #include <linux/l2tp.h> 14 #include <linux/in.h> 15 #include <linux/etherdevice.h> 16 #include <linux/spinlock.h> 17 #include <net/sock.h> 18 #include <net/ip.h> 19 #include <net/icmp.h> 20 #include <net/udp.h> 21 #include <net/inet_common.h> 22 #include <net/inet_hashtables.h> 23 #include <net/tcp_states.h> 24 #include <net/protocol.h> 25 #include <net/xfrm.h> 26 #include <net/net_namespace.h> 27 #include <net/netns/generic.h> 28 #include <linux/ip.h> 29 #include <linux/ipv6.h> 30 #include <linux/udp.h> 31 32 #include "l2tp_core.h" 33 34 /* Default device name. May be overridden by name specified by user */ 35 #define L2TP_ETH_DEV_NAME "l2tpeth%d" 36 37 /* via netdev_priv() */ 38 struct l2tp_eth { 39 struct l2tp_session *session; 40 }; 41 42 /* via l2tp_session_priv() */ 43 struct l2tp_eth_sess { 44 struct net_device __rcu *dev; 45 }; 46 47 static int l2tp_eth_dev_init(struct net_device *dev) 48 { 49 eth_hw_addr_random(dev); 50 eth_broadcast_addr(dev->broadcast); 51 netdev_lockdep_set_classes(dev); 52 53 return 0; 54 } 55 56 static void l2tp_eth_dev_uninit(struct net_device *dev) 57 { 58 struct l2tp_eth *priv = netdev_priv(dev); 59 struct l2tp_eth_sess *spriv; 60 61 spriv = l2tp_session_priv(priv->session); 62 RCU_INIT_POINTER(spriv->dev, NULL); 63 /* No need for synchronize_net() here. We're called by 64 * unregister_netdev*(), which does the synchronisation for us. 65 */ 66 } 67 68 static netdev_tx_t l2tp_eth_dev_xmit(struct sk_buff *skb, struct net_device *dev) 69 { 70 struct l2tp_eth *priv = netdev_priv(dev); 71 struct l2tp_session *session = priv->session; 72 unsigned int len = skb->len; 73 int ret = l2tp_xmit_skb(session, skb); 74 75 if (likely(ret == NET_XMIT_SUCCESS)) 76 dev_sw_netstats_tx_add(dev, 1, len); 77 else 78 DEV_STATS_INC(dev, tx_dropped); 79 80 return NETDEV_TX_OK; 81 } 82 83 static const struct net_device_ops l2tp_eth_netdev_ops = { 84 .ndo_init = l2tp_eth_dev_init, 85 .ndo_uninit = l2tp_eth_dev_uninit, 86 .ndo_start_xmit = l2tp_eth_dev_xmit, 87 .ndo_get_stats64 = dev_get_tstats64, 88 .ndo_set_mac_address = eth_mac_addr, 89 }; 90 91 static const struct device_type l2tpeth_type = { 92 .name = "l2tpeth", 93 }; 94 95 static void l2tp_eth_dev_setup(struct net_device *dev) 96 { 97 SET_NETDEV_DEVTYPE(dev, &l2tpeth_type); 98 ether_setup(dev); 99 dev->priv_flags &= ~IFF_TX_SKB_SHARING; 100 dev->lltx = true; 101 dev->netdev_ops = &l2tp_eth_netdev_ops; 102 dev->needs_free_netdev = true; 103 dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS; 104 } 105 106 static void l2tp_eth_dev_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len) 107 { 108 struct l2tp_eth_sess *spriv = l2tp_session_priv(session); 109 struct net_device *dev; 110 111 if (!pskb_may_pull(skb, ETH_HLEN)) 112 goto error; 113 114 secpath_reset(skb); 115 116 /* checksums verified by L2TP */ 117 skb->ip_summed = CHECKSUM_NONE; 118 119 /* drop outer flow-hash */ 120 skb_clear_hash(skb); 121 122 skb_dst_drop(skb); 123 nf_reset_ct(skb); 124 125 rcu_read_lock(); 126 dev = rcu_dereference(spriv->dev); 127 if (!dev) 128 goto error_rcu; 129 130 if (dev_forward_skb(dev, skb) == NET_RX_SUCCESS) 131 dev_sw_netstats_rx_add(dev, data_len); 132 else 133 DEV_STATS_INC(dev, rx_errors); 134 135 rcu_read_unlock(); 136 137 return; 138 139 error_rcu: 140 rcu_read_unlock(); 141 error: 142 kfree_skb(skb); 143 } 144 145 static void l2tp_eth_delete(struct l2tp_session *session) 146 { 147 struct l2tp_eth_sess *spriv; 148 struct net_device *dev; 149 150 if (session) { 151 spriv = l2tp_session_priv(session); 152 153 rtnl_lock(); 154 dev = rtnl_dereference(spriv->dev); 155 if (dev) { 156 unregister_netdevice(dev); 157 rtnl_unlock(); 158 module_put(THIS_MODULE); 159 } else { 160 rtnl_unlock(); 161 } 162 } 163 } 164 165 static void l2tp_eth_show(struct seq_file *m, void *arg) 166 { 167 struct l2tp_session *session = arg; 168 struct l2tp_eth_sess *spriv = l2tp_session_priv(session); 169 struct net_device *dev; 170 171 rcu_read_lock(); 172 dev = rcu_dereference(spriv->dev); 173 if (!dev) { 174 rcu_read_unlock(); 175 return; 176 } 177 dev_hold(dev); 178 rcu_read_unlock(); 179 180 seq_printf(m, " interface %s\n", dev->name); 181 182 dev_put(dev); 183 } 184 185 static void l2tp_eth_adjust_mtu(struct l2tp_tunnel *tunnel, 186 struct l2tp_session *session, 187 struct net_device *dev) 188 { 189 unsigned int overhead = 0; 190 u32 l3_overhead = 0; 191 u32 mtu; 192 193 /* if the encap is UDP, account for UDP header size */ 194 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) { 195 overhead += sizeof(struct udphdr); 196 dev->needed_headroom += sizeof(struct udphdr); 197 } 198 199 lock_sock(tunnel->sock); 200 l3_overhead = kernel_sock_ip_overhead(tunnel->sock); 201 release_sock(tunnel->sock); 202 203 if (l3_overhead == 0) { 204 /* L3 Overhead couldn't be identified, this could be 205 * because tunnel->sock was NULL or the socket's 206 * address family was not IPv4 or IPv6, 207 * dev mtu stays at 1500. 208 */ 209 return; 210 } 211 /* Adjust MTU, factor overhead - underlay L3, overlay L2 hdr 212 * UDP overhead, if any, was already factored in above. 213 */ 214 overhead += session->hdr_len + ETH_HLEN + l3_overhead; 215 216 mtu = l2tp_tunnel_dst_mtu(tunnel) - overhead; 217 if (mtu < dev->min_mtu || mtu > dev->max_mtu) 218 dev->mtu = ETH_DATA_LEN - overhead; 219 else 220 dev->mtu = mtu; 221 222 dev->needed_headroom += session->hdr_len; 223 } 224 225 static int l2tp_eth_create(struct net *net, struct l2tp_tunnel *tunnel, 226 u32 session_id, u32 peer_session_id, 227 struct l2tp_session_cfg *cfg) 228 { 229 unsigned char name_assign_type; 230 struct net_device *dev; 231 char name[IFNAMSIZ]; 232 struct l2tp_session *session; 233 struct l2tp_eth *priv; 234 struct l2tp_eth_sess *spriv; 235 int rc; 236 237 if (cfg->ifname) { 238 strscpy(name, cfg->ifname, IFNAMSIZ); 239 name_assign_type = NET_NAME_USER; 240 } else { 241 strcpy(name, L2TP_ETH_DEV_NAME); 242 name_assign_type = NET_NAME_ENUM; 243 } 244 245 session = l2tp_session_create(sizeof(*spriv), tunnel, session_id, 246 peer_session_id, cfg); 247 if (IS_ERR(session)) { 248 rc = PTR_ERR(session); 249 goto err; 250 } 251 252 dev = alloc_netdev(sizeof(*priv), name, name_assign_type, 253 l2tp_eth_dev_setup); 254 if (!dev) { 255 rc = -ENOMEM; 256 goto err_sess; 257 } 258 259 dev_net_set(dev, net); 260 dev->min_mtu = 0; 261 dev->max_mtu = ETH_MAX_MTU; 262 l2tp_eth_adjust_mtu(tunnel, session, dev); 263 264 priv = netdev_priv(dev); 265 priv->session = session; 266 267 session->recv_skb = l2tp_eth_dev_recv; 268 session->session_close = l2tp_eth_delete; 269 if (IS_ENABLED(CONFIG_L2TP_DEBUGFS)) 270 session->show = l2tp_eth_show; 271 272 spriv = l2tp_session_priv(session); 273 274 refcount_inc(&session->ref_count); 275 276 rtnl_lock(); 277 278 /* Register both device and session while holding the rtnl lock. This 279 * ensures that l2tp_eth_delete() will see that there's a device to 280 * unregister, even if it happened to run before we assign spriv->dev. 281 */ 282 rc = l2tp_session_register(session, tunnel); 283 if (rc < 0) { 284 rtnl_unlock(); 285 goto err_sess_dev; 286 } 287 288 rc = register_netdevice(dev); 289 if (rc < 0) { 290 rtnl_unlock(); 291 l2tp_session_delete(session); 292 l2tp_session_put(session); 293 free_netdev(dev); 294 295 return rc; 296 } 297 298 strscpy(session->ifname, dev->name, IFNAMSIZ); 299 rcu_assign_pointer(spriv->dev, dev); 300 301 rtnl_unlock(); 302 303 l2tp_session_put(session); 304 305 __module_get(THIS_MODULE); 306 307 return 0; 308 309 err_sess_dev: 310 l2tp_session_put(session); 311 free_netdev(dev); 312 err_sess: 313 l2tp_session_put(session); 314 err: 315 return rc; 316 } 317 318 static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops = { 319 .session_create = l2tp_eth_create, 320 .session_delete = l2tp_session_delete, 321 }; 322 323 static int __init l2tp_eth_init(void) 324 { 325 int err = 0; 326 327 err = l2tp_nl_register_ops(L2TP_PWTYPE_ETH, &l2tp_eth_nl_cmd_ops); 328 if (err) 329 goto err; 330 331 pr_info("L2TP ethernet pseudowire support (L2TPv3)\n"); 332 333 return 0; 334 335 err: 336 return err; 337 } 338 339 static void __exit l2tp_eth_exit(void) 340 { 341 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH); 342 } 343 344 module_init(l2tp_eth_init); 345 module_exit(l2tp_eth_exit); 346 347 MODULE_LICENSE("GPL"); 348 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); 349 MODULE_DESCRIPTION("L2TP ethernet pseudowire driver"); 350 MODULE_VERSION("1.0"); 351 MODULE_ALIAS_L2TP_PWTYPE(5); 352