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
);
59 netdev_lockdep_set_classes(dev
);
64 static void l2tp_eth_dev_uninit(struct net_device
*dev
)
66 struct l2tp_eth
*priv
= netdev_priv(dev
);
67 struct l2tp_eth_sess
*spriv
;
69 spriv
= l2tp_session_priv(priv
->session
);
70 RCU_INIT_POINTER(spriv
->dev
, NULL
);
71 /* No need for synchronize_net() here. We're called by
72 * unregister_netdev*(), which does the synchronisation for us.
76 static int l2tp_eth_dev_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
78 struct l2tp_eth
*priv
= netdev_priv(dev
);
79 struct l2tp_session
*session
= priv
->session
;
80 unsigned int len
= skb
->len
;
81 int ret
= l2tp_xmit_skb(session
, skb
, session
->hdr_len
);
83 if (likely(ret
== NET_XMIT_SUCCESS
)) {
84 atomic_long_add(len
, &priv
->tx_bytes
);
85 atomic_long_inc(&priv
->tx_packets
);
87 atomic_long_inc(&priv
->tx_dropped
);
92 static void l2tp_eth_get_stats64(struct net_device
*dev
,
93 struct rtnl_link_stats64
*stats
)
95 struct l2tp_eth
*priv
= netdev_priv(dev
);
97 stats
->tx_bytes
= (unsigned long) atomic_long_read(&priv
->tx_bytes
);
98 stats
->tx_packets
= (unsigned long) atomic_long_read(&priv
->tx_packets
);
99 stats
->tx_dropped
= (unsigned long) atomic_long_read(&priv
->tx_dropped
);
100 stats
->rx_bytes
= (unsigned long) atomic_long_read(&priv
->rx_bytes
);
101 stats
->rx_packets
= (unsigned long) atomic_long_read(&priv
->rx_packets
);
102 stats
->rx_errors
= (unsigned long) atomic_long_read(&priv
->rx_errors
);
106 static const struct net_device_ops l2tp_eth_netdev_ops
= {
107 .ndo_init
= l2tp_eth_dev_init
,
108 .ndo_uninit
= l2tp_eth_dev_uninit
,
109 .ndo_start_xmit
= l2tp_eth_dev_xmit
,
110 .ndo_get_stats64
= l2tp_eth_get_stats64
,
111 .ndo_set_mac_address
= eth_mac_addr
,
114 static struct device_type l2tpeth_type
= {
118 static void l2tp_eth_dev_setup(struct net_device
*dev
)
120 SET_NETDEV_DEVTYPE(dev
, &l2tpeth_type
);
122 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
123 dev
->features
|= NETIF_F_LLTX
;
124 dev
->netdev_ops
= &l2tp_eth_netdev_ops
;
125 dev
->needs_free_netdev
= true;
128 static void l2tp_eth_dev_recv(struct l2tp_session
*session
, struct sk_buff
*skb
, int data_len
)
130 struct l2tp_eth_sess
*spriv
= l2tp_session_priv(session
);
131 struct net_device
*dev
;
132 struct l2tp_eth
*priv
;
134 if (session
->debug
& L2TP_MSG_DATA
) {
137 length
= min(32u, skb
->len
);
138 if (!pskb_may_pull(skb
, length
))
141 pr_debug("%s: eth recv\n", session
->name
);
142 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
, skb
->data
, length
);
145 if (!pskb_may_pull(skb
, ETH_HLEN
))
150 /* checksums verified by L2TP */
151 skb
->ip_summed
= CHECKSUM_NONE
;
157 dev
= rcu_dereference(spriv
->dev
);
161 priv
= netdev_priv(dev
);
162 if (dev_forward_skb(dev
, skb
) == NET_RX_SUCCESS
) {
163 atomic_long_inc(&priv
->rx_packets
);
164 atomic_long_add(data_len
, &priv
->rx_bytes
);
166 atomic_long_inc(&priv
->rx_errors
);
178 static void l2tp_eth_delete(struct l2tp_session
*session
)
180 struct l2tp_eth_sess
*spriv
;
181 struct net_device
*dev
;
184 spriv
= l2tp_session_priv(session
);
187 dev
= rtnl_dereference(spriv
->dev
);
189 unregister_netdevice(dev
);
191 module_put(THIS_MODULE
);
198 static void l2tp_eth_show(struct seq_file
*m
, void *arg
)
200 struct l2tp_session
*session
= arg
;
201 struct l2tp_eth_sess
*spriv
= l2tp_session_priv(session
);
202 struct net_device
*dev
;
205 dev
= rcu_dereference(spriv
->dev
);
213 seq_printf(m
, " interface %s\n", dev
->name
);
218 static void l2tp_eth_adjust_mtu(struct l2tp_tunnel
*tunnel
,
219 struct l2tp_session
*session
,
220 struct net_device
*dev
)
222 unsigned int overhead
= 0;
226 /* if the encap is UDP, account for UDP header size */
227 if (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) {
228 overhead
+= sizeof(struct udphdr
);
229 dev
->needed_headroom
+= sizeof(struct udphdr
);
232 lock_sock(tunnel
->sock
);
233 l3_overhead
= kernel_sock_ip_overhead(tunnel
->sock
);
234 release_sock(tunnel
->sock
);
236 if (l3_overhead
== 0) {
237 /* L3 Overhead couldn't be identified, this could be
238 * because tunnel->sock was NULL or the socket's
239 * address family was not IPv4 or IPv6,
240 * dev mtu stays at 1500.
244 /* Adjust MTU, factor overhead - underlay L3, overlay L2 hdr
245 * UDP overhead, if any, was already factored in above.
247 overhead
+= session
->hdr_len
+ ETH_HLEN
+ l3_overhead
;
249 mtu
= l2tp_tunnel_dst_mtu(tunnel
) - overhead
;
250 if (mtu
< dev
->min_mtu
|| mtu
> dev
->max_mtu
)
251 dev
->mtu
= ETH_DATA_LEN
- overhead
;
255 dev
->needed_headroom
+= session
->hdr_len
;
258 static int l2tp_eth_create(struct net
*net
, struct l2tp_tunnel
*tunnel
,
259 u32 session_id
, u32 peer_session_id
,
260 struct l2tp_session_cfg
*cfg
)
262 unsigned char name_assign_type
;
263 struct net_device
*dev
;
265 struct l2tp_session
*session
;
266 struct l2tp_eth
*priv
;
267 struct l2tp_eth_sess
*spriv
;
271 strlcpy(name
, cfg
->ifname
, IFNAMSIZ
);
272 name_assign_type
= NET_NAME_USER
;
274 strcpy(name
, L2TP_ETH_DEV_NAME
);
275 name_assign_type
= NET_NAME_ENUM
;
278 session
= l2tp_session_create(sizeof(*spriv
), tunnel
, session_id
,
279 peer_session_id
, cfg
);
280 if (IS_ERR(session
)) {
281 rc
= PTR_ERR(session
);
285 dev
= alloc_netdev(sizeof(*priv
), name
, name_assign_type
,
292 dev_net_set(dev
, net
);
294 dev
->max_mtu
= ETH_MAX_MTU
;
295 l2tp_eth_adjust_mtu(tunnel
, session
, dev
);
297 priv
= netdev_priv(dev
);
298 priv
->session
= session
;
300 session
->recv_skb
= l2tp_eth_dev_recv
;
301 session
->session_close
= l2tp_eth_delete
;
302 if (IS_ENABLED(CONFIG_L2TP_DEBUGFS
))
303 session
->show
= l2tp_eth_show
;
305 spriv
= l2tp_session_priv(session
);
307 l2tp_session_inc_refcount(session
);
311 /* Register both device and session while holding the rtnl lock. This
312 * ensures that l2tp_eth_delete() will see that there's a device to
313 * unregister, even if it happened to run before we assign spriv->dev.
315 rc
= l2tp_session_register(session
, tunnel
);
321 rc
= register_netdevice(dev
);
324 l2tp_session_delete(session
);
325 l2tp_session_dec_refcount(session
);
331 strlcpy(session
->ifname
, dev
->name
, IFNAMSIZ
);
332 rcu_assign_pointer(spriv
->dev
, dev
);
336 l2tp_session_dec_refcount(session
);
338 __module_get(THIS_MODULE
);
343 l2tp_session_dec_refcount(session
);
352 static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops
= {
353 .session_create
= l2tp_eth_create
,
354 .session_delete
= l2tp_session_delete
,
358 static int __init
l2tp_eth_init(void)
362 err
= l2tp_nl_register_ops(L2TP_PWTYPE_ETH
, &l2tp_eth_nl_cmd_ops
);
366 pr_info("L2TP ethernet pseudowire support (L2TPv3)\n");
374 static void __exit
l2tp_eth_exit(void)
376 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH
);
379 module_init(l2tp_eth_init
);
380 module_exit(l2tp_eth_exit
);
382 MODULE_LICENSE("GPL");
383 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
384 MODULE_DESCRIPTION("L2TP ethernet pseudowire driver");
385 MODULE_VERSION("1.0");
386 MODULE_ALIAS_L2TP_PWTYPE(5);