1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* L2TPv3 ethernet pseudowire driver
4 * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
15 #include <linux/etherdevice.h>
16 #include <linux/spinlock.h>
21 #include <net/inet_common.h>
22 #include <net/inet_hashtables.h>
23 #include <net/tcp_states.h>
24 #include <net/protocol.h>
26 #include <net/net_namespace.h>
27 #include <net/netns/generic.h>
29 #include <linux/ipv6.h>
30 #include <linux/udp.h>
32 #include "l2tp_core.h"
34 /* Default device name. May be overridden by name specified by user */
35 #define L2TP_ETH_DEV_NAME "l2tpeth%d"
37 /* via netdev_priv() */
39 struct l2tp_session *session;
40 atomic_long_t tx_bytes;
41 atomic_long_t tx_packets;
42 atomic_long_t tx_dropped;
43 atomic_long_t rx_bytes;
44 atomic_long_t rx_packets;
45 atomic_long_t rx_errors;
48 /* via l2tp_session_priv() */
49 struct l2tp_eth_sess {
50 struct net_device __rcu *dev;
53 static int l2tp_eth_dev_init(struct net_device *dev)
55 eth_hw_addr_random(dev);
56 eth_broadcast_addr(dev->broadcast);
57 netdev_lockdep_set_classes(dev);
62 static void l2tp_eth_dev_uninit(struct net_device *dev)
64 struct l2tp_eth *priv = netdev_priv(dev);
65 struct l2tp_eth_sess *spriv;
67 spriv = l2tp_session_priv(priv->session);
68 RCU_INIT_POINTER(spriv->dev, NULL);
69 /* No need for synchronize_net() here. We're called by
70 * unregister_netdev*(), which does the synchronisation for us.
74 static netdev_tx_t l2tp_eth_dev_xmit(struct sk_buff *skb, struct net_device *dev)
76 struct l2tp_eth *priv = netdev_priv(dev);
77 struct l2tp_session *session = priv->session;
78 unsigned int len = skb->len;
79 int ret = l2tp_xmit_skb(session, skb);
81 if (likely(ret == NET_XMIT_SUCCESS)) {
82 atomic_long_add(len, &priv->tx_bytes);
83 atomic_long_inc(&priv->tx_packets);
85 atomic_long_inc(&priv->tx_dropped);
90 static void l2tp_eth_get_stats64(struct net_device *dev,
91 struct rtnl_link_stats64 *stats)
93 struct l2tp_eth *priv = netdev_priv(dev);
95 stats->tx_bytes = (unsigned long)atomic_long_read(&priv->tx_bytes);
96 stats->tx_packets = (unsigned long)atomic_long_read(&priv->tx_packets);
97 stats->tx_dropped = (unsigned long)atomic_long_read(&priv->tx_dropped);
98 stats->rx_bytes = (unsigned long)atomic_long_read(&priv->rx_bytes);
99 stats->rx_packets = (unsigned long)atomic_long_read(&priv->rx_packets);
100 stats->rx_errors = (unsigned long)atomic_long_read(&priv->rx_errors);
103 static const struct net_device_ops l2tp_eth_netdev_ops = {
104 .ndo_init = l2tp_eth_dev_init,
105 .ndo_uninit = l2tp_eth_dev_uninit,
106 .ndo_start_xmit = l2tp_eth_dev_xmit,
107 .ndo_get_stats64 = l2tp_eth_get_stats64,
108 .ndo_set_mac_address = eth_mac_addr,
111 static struct device_type l2tpeth_type = {
115 static void l2tp_eth_dev_setup(struct net_device *dev)
117 SET_NETDEV_DEVTYPE(dev, &l2tpeth_type);
119 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
120 dev->features |= NETIF_F_LLTX;
121 dev->netdev_ops = &l2tp_eth_netdev_ops;
122 dev->needs_free_netdev = true;
125 static void l2tp_eth_dev_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
127 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
128 struct net_device *dev;
129 struct l2tp_eth *priv;
131 if (!pskb_may_pull(skb, ETH_HLEN))
136 /* checksums verified by L2TP */
137 skb->ip_summed = CHECKSUM_NONE;
143 dev = rcu_dereference(spriv->dev);
147 priv = netdev_priv(dev);
148 if (dev_forward_skb(dev, skb) == NET_RX_SUCCESS) {
149 atomic_long_inc(&priv->rx_packets);
150 atomic_long_add(data_len, &priv->rx_bytes);
152 atomic_long_inc(&priv->rx_errors);
164 static void l2tp_eth_delete(struct l2tp_session *session)
166 struct l2tp_eth_sess *spriv;
167 struct net_device *dev;
170 spriv = l2tp_session_priv(session);
173 dev = rtnl_dereference(spriv->dev);
175 unregister_netdevice(dev);
177 module_put(THIS_MODULE);
184 static void l2tp_eth_show(struct seq_file *m, void *arg)
186 struct l2tp_session *session = arg;
187 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
188 struct net_device *dev;
191 dev = rcu_dereference(spriv->dev);
199 seq_printf(m, " interface %s\n", dev->name);
204 static void l2tp_eth_adjust_mtu(struct l2tp_tunnel *tunnel,
205 struct l2tp_session *session,
206 struct net_device *dev)
208 unsigned int overhead = 0;
212 /* if the encap is UDP, account for UDP header size */
213 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
214 overhead += sizeof(struct udphdr);
215 dev->needed_headroom += sizeof(struct udphdr);
218 lock_sock(tunnel->sock);
219 l3_overhead = kernel_sock_ip_overhead(tunnel->sock);
220 release_sock(tunnel->sock);
222 if (l3_overhead == 0) {
223 /* L3 Overhead couldn't be identified, this could be
224 * because tunnel->sock was NULL or the socket's
225 * address family was not IPv4 or IPv6,
226 * dev mtu stays at 1500.
230 /* Adjust MTU, factor overhead - underlay L3, overlay L2 hdr
231 * UDP overhead, if any, was already factored in above.
233 overhead += session->hdr_len + ETH_HLEN + l3_overhead;
235 mtu = l2tp_tunnel_dst_mtu(tunnel) - overhead;
236 if (mtu < dev->min_mtu || mtu > dev->max_mtu)
237 dev->mtu = ETH_DATA_LEN - overhead;
241 dev->needed_headroom += session->hdr_len;
244 static int l2tp_eth_create(struct net *net, struct l2tp_tunnel *tunnel,
245 u32 session_id, u32 peer_session_id,
246 struct l2tp_session_cfg *cfg)
248 unsigned char name_assign_type;
249 struct net_device *dev;
251 struct l2tp_session *session;
252 struct l2tp_eth *priv;
253 struct l2tp_eth_sess *spriv;
257 strlcpy(name, cfg->ifname, IFNAMSIZ);
258 name_assign_type = NET_NAME_USER;
260 strcpy(name, L2TP_ETH_DEV_NAME);
261 name_assign_type = NET_NAME_ENUM;
264 session = l2tp_session_create(sizeof(*spriv), tunnel, session_id,
265 peer_session_id, cfg);
266 if (IS_ERR(session)) {
267 rc = PTR_ERR(session);
271 dev = alloc_netdev(sizeof(*priv), name, name_assign_type,
278 dev_net_set(dev, net);
280 dev->max_mtu = ETH_MAX_MTU;
281 l2tp_eth_adjust_mtu(tunnel, session, dev);
283 priv = netdev_priv(dev);
284 priv->session = session;
286 session->recv_skb = l2tp_eth_dev_recv;
287 session->session_close = l2tp_eth_delete;
288 if (IS_ENABLED(CONFIG_L2TP_DEBUGFS))
289 session->show = l2tp_eth_show;
291 spriv = l2tp_session_priv(session);
293 l2tp_session_inc_refcount(session);
297 /* Register both device and session while holding the rtnl lock. This
298 * ensures that l2tp_eth_delete() will see that there's a device to
299 * unregister, even if it happened to run before we assign spriv->dev.
301 rc = l2tp_session_register(session, tunnel);
307 rc = register_netdevice(dev);
310 l2tp_session_delete(session);
311 l2tp_session_dec_refcount(session);
317 strlcpy(session->ifname, dev->name, IFNAMSIZ);
318 rcu_assign_pointer(spriv->dev, dev);
322 l2tp_session_dec_refcount(session);
324 __module_get(THIS_MODULE);
329 l2tp_session_dec_refcount(session);
337 static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops = {
338 .session_create = l2tp_eth_create,
339 .session_delete = l2tp_session_delete,
342 static int __init l2tp_eth_init(void)
346 err = l2tp_nl_register_ops(L2TP_PWTYPE_ETH, &l2tp_eth_nl_cmd_ops);
350 pr_info("L2TP ethernet pseudowire support (L2TPv3)\n");
358 static void __exit l2tp_eth_exit(void)
360 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH);
363 module_init(l2tp_eth_init);
364 module_exit(l2tp_eth_exit);
366 MODULE_LICENSE("GPL");
367 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
368 MODULE_DESCRIPTION("L2TP ethernet pseudowire driver");
369 MODULE_VERSION("1.0");
370 MODULE_ALIAS_L2TP_PWTYPE(5);