1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * L2TPv3 ethernet pseudowire driver
5 * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/skbuff.h>
12 #include <linux/socket.h>
13 #include <linux/hash.h>
14 #include <linux/l2tp.h>
16 #include <linux/etherdevice.h>
17 #include <linux/spinlock.h>
22 #include <net/inet_common.h>
23 #include <net/inet_hashtables.h>
24 #include <net/tcp_states.h>
25 #include <net/protocol.h>
27 #include <net/net_namespace.h>
28 #include <net/netns/generic.h>
30 #include <linux/ipv6.h>
31 #include <linux/udp.h>
33 #include "l2tp_core.h"
35 /* Default device name. May be overridden by name specified by user */
36 #define L2TP_ETH_DEV_NAME "l2tpeth%d"
38 /* via netdev_priv() */
40 struct l2tp_session
*session
;
41 atomic_long_t tx_bytes
;
42 atomic_long_t tx_packets
;
43 atomic_long_t tx_dropped
;
44 atomic_long_t rx_bytes
;
45 atomic_long_t rx_packets
;
46 atomic_long_t rx_errors
;
49 /* via l2tp_session_priv() */
50 struct l2tp_eth_sess
{
51 struct net_device __rcu
*dev
;
55 static int l2tp_eth_dev_init(struct net_device
*dev
)
57 eth_hw_addr_random(dev
);
58 eth_broadcast_addr(dev
->broadcast
);
63 static void l2tp_eth_dev_uninit(struct net_device
*dev
)
65 struct l2tp_eth
*priv
= netdev_priv(dev
);
66 struct l2tp_eth_sess
*spriv
;
68 spriv
= l2tp_session_priv(priv
->session
);
69 RCU_INIT_POINTER(spriv
->dev
, NULL
);
70 /* No need for synchronize_net() here. We're called by
71 * unregister_netdev*(), which does the synchronisation for us.
75 static int l2tp_eth_dev_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
77 struct l2tp_eth
*priv
= netdev_priv(dev
);
78 struct l2tp_session
*session
= priv
->session
;
79 unsigned int len
= skb
->len
;
80 int ret
= l2tp_xmit_skb(session
, skb
, session
->hdr_len
);
82 if (likely(ret
== NET_XMIT_SUCCESS
)) {
83 atomic_long_add(len
, &priv
->tx_bytes
);
84 atomic_long_inc(&priv
->tx_packets
);
86 atomic_long_inc(&priv
->tx_dropped
);
91 static void l2tp_eth_get_stats64(struct net_device
*dev
,
92 struct rtnl_link_stats64
*stats
)
94 struct l2tp_eth
*priv
= netdev_priv(dev
);
96 stats
->tx_bytes
= (unsigned long) atomic_long_read(&priv
->tx_bytes
);
97 stats
->tx_packets
= (unsigned long) atomic_long_read(&priv
->tx_packets
);
98 stats
->tx_dropped
= (unsigned long) atomic_long_read(&priv
->tx_dropped
);
99 stats
->rx_bytes
= (unsigned long) atomic_long_read(&priv
->rx_bytes
);
100 stats
->rx_packets
= (unsigned long) atomic_long_read(&priv
->rx_packets
);
101 stats
->rx_errors
= (unsigned long) atomic_long_read(&priv
->rx_errors
);
105 static const struct net_device_ops l2tp_eth_netdev_ops
= {
106 .ndo_init
= l2tp_eth_dev_init
,
107 .ndo_uninit
= l2tp_eth_dev_uninit
,
108 .ndo_start_xmit
= l2tp_eth_dev_xmit
,
109 .ndo_get_stats64
= l2tp_eth_get_stats64
,
110 .ndo_set_mac_address
= eth_mac_addr
,
113 static struct device_type l2tpeth_type
= {
117 static void l2tp_eth_dev_setup(struct net_device
*dev
)
119 SET_NETDEV_DEVTYPE(dev
, &l2tpeth_type
);
121 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
122 dev
->features
|= NETIF_F_LLTX
;
123 dev
->netdev_ops
= &l2tp_eth_netdev_ops
;
124 dev
->needs_free_netdev
= true;
127 static void l2tp_eth_dev_recv(struct l2tp_session
*session
, struct sk_buff
*skb
, int data_len
)
129 struct l2tp_eth_sess
*spriv
= l2tp_session_priv(session
);
130 struct net_device
*dev
;
131 struct l2tp_eth
*priv
;
133 if (session
->debug
& L2TP_MSG_DATA
) {
136 length
= min(32u, skb
->len
);
137 if (!pskb_may_pull(skb
, length
))
140 pr_debug("%s: eth recv\n", session
->name
);
141 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
, skb
->data
, length
);
144 if (!pskb_may_pull(skb
, ETH_HLEN
))
149 /* checksums verified by L2TP */
150 skb
->ip_summed
= CHECKSUM_NONE
;
156 dev
= rcu_dereference(spriv
->dev
);
160 priv
= netdev_priv(dev
);
161 if (dev_forward_skb(dev
, skb
) == NET_RX_SUCCESS
) {
162 atomic_long_inc(&priv
->rx_packets
);
163 atomic_long_add(data_len
, &priv
->rx_bytes
);
165 atomic_long_inc(&priv
->rx_errors
);
177 static void l2tp_eth_delete(struct l2tp_session
*session
)
179 struct l2tp_eth_sess
*spriv
;
180 struct net_device
*dev
;
183 spriv
= l2tp_session_priv(session
);
186 dev
= rtnl_dereference(spriv
->dev
);
188 unregister_netdevice(dev
);
190 module_put(THIS_MODULE
);
197 static void l2tp_eth_show(struct seq_file
*m
, void *arg
)
199 struct l2tp_session
*session
= arg
;
200 struct l2tp_eth_sess
*spriv
= l2tp_session_priv(session
);
201 struct net_device
*dev
;
204 dev
= rcu_dereference(spriv
->dev
);
212 seq_printf(m
, " interface %s\n", dev
->name
);
217 static void l2tp_eth_adjust_mtu(struct l2tp_tunnel
*tunnel
,
218 struct l2tp_session
*session
,
219 struct net_device
*dev
)
221 unsigned int overhead
= 0;
225 /* if the encap is UDP, account for UDP header size */
226 if (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) {
227 overhead
+= sizeof(struct udphdr
);
228 dev
->needed_headroom
+= sizeof(struct udphdr
);
231 lock_sock(tunnel
->sock
);
232 l3_overhead
= kernel_sock_ip_overhead(tunnel
->sock
);
233 release_sock(tunnel
->sock
);
235 if (l3_overhead
== 0) {
236 /* L3 Overhead couldn't be identified, this could be
237 * because tunnel->sock was NULL or the socket's
238 * address family was not IPv4 or IPv6,
239 * dev mtu stays at 1500.
243 /* Adjust MTU, factor overhead - underlay L3, overlay L2 hdr
244 * UDP overhead, if any, was already factored in above.
246 overhead
+= session
->hdr_len
+ ETH_HLEN
+ l3_overhead
;
248 mtu
= l2tp_tunnel_dst_mtu(tunnel
) - overhead
;
249 if (mtu
< dev
->min_mtu
|| mtu
> dev
->max_mtu
)
250 dev
->mtu
= ETH_DATA_LEN
- overhead
;
254 dev
->needed_headroom
+= session
->hdr_len
;
257 static int l2tp_eth_create(struct net
*net
, struct l2tp_tunnel
*tunnel
,
258 u32 session_id
, u32 peer_session_id
,
259 struct l2tp_session_cfg
*cfg
)
261 unsigned char name_assign_type
;
262 struct net_device
*dev
;
264 struct l2tp_session
*session
;
265 struct l2tp_eth
*priv
;
266 struct l2tp_eth_sess
*spriv
;
270 strlcpy(name
, cfg
->ifname
, IFNAMSIZ
);
271 name_assign_type
= NET_NAME_USER
;
273 strcpy(name
, L2TP_ETH_DEV_NAME
);
274 name_assign_type
= NET_NAME_ENUM
;
277 session
= l2tp_session_create(sizeof(*spriv
), tunnel
, session_id
,
278 peer_session_id
, cfg
);
279 if (IS_ERR(session
)) {
280 rc
= PTR_ERR(session
);
284 dev
= alloc_netdev(sizeof(*priv
), name
, name_assign_type
,
291 dev_net_set(dev
, net
);
293 dev
->max_mtu
= ETH_MAX_MTU
;
294 l2tp_eth_adjust_mtu(tunnel
, session
, dev
);
296 priv
= netdev_priv(dev
);
297 priv
->session
= session
;
299 session
->recv_skb
= l2tp_eth_dev_recv
;
300 session
->session_close
= l2tp_eth_delete
;
301 if (IS_ENABLED(CONFIG_L2TP_DEBUGFS
))
302 session
->show
= l2tp_eth_show
;
304 spriv
= l2tp_session_priv(session
);
306 l2tp_session_inc_refcount(session
);
310 /* Register both device and session while holding the rtnl lock. This
311 * ensures that l2tp_eth_delete() will see that there's a device to
312 * unregister, even if it happened to run before we assign spriv->dev.
314 rc
= l2tp_session_register(session
, tunnel
);
320 rc
= register_netdevice(dev
);
323 l2tp_session_delete(session
);
324 l2tp_session_dec_refcount(session
);
330 strlcpy(session
->ifname
, dev
->name
, IFNAMSIZ
);
331 rcu_assign_pointer(spriv
->dev
, dev
);
335 l2tp_session_dec_refcount(session
);
337 __module_get(THIS_MODULE
);
342 l2tp_session_dec_refcount(session
);
351 static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops
= {
352 .session_create
= l2tp_eth_create
,
353 .session_delete
= l2tp_session_delete
,
357 static int __init
l2tp_eth_init(void)
361 err
= l2tp_nl_register_ops(L2TP_PWTYPE_ETH
, &l2tp_eth_nl_cmd_ops
);
365 pr_info("L2TP ethernet pseudowire support (L2TPv3)\n");
373 static void __exit
l2tp_eth_exit(void)
375 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH
);
378 module_init(l2tp_eth_init
);
379 module_exit(l2tp_eth_exit
);
381 MODULE_LICENSE("GPL");
382 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
383 MODULE_DESCRIPTION("L2TP ethernet pseudowire driver");
384 MODULE_VERSION("1.0");
385 MODULE_ALIAS_L2TP_PWTYPE(5);