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