2 * L2TPv3 ethernet pseudowire driver
4 * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
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.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
20 #include <linux/etherdevice.h>
21 #include <linux/spinlock.h>
26 #include <net/inet_common.h>
27 #include <net/inet_hashtables.h>
28 #include <net/tcp_states.h>
29 #include <net/protocol.h>
31 #include <net/net_namespace.h>
32 #include <net/netns/generic.h>
34 #include <linux/ipv6.h>
35 #include <linux/udp.h>
37 #include "l2tp_core.h"
39 /* Default device name. May be overridden by name specified by user */
40 #define L2TP_ETH_DEV_NAME "l2tpeth%d"
42 /* via netdev_priv() */
44 struct l2tp_session
*session
;
45 atomic_long_t tx_bytes
;
46 atomic_long_t tx_packets
;
47 atomic_long_t tx_dropped
;
48 atomic_long_t rx_bytes
;
49 atomic_long_t rx_packets
;
50 atomic_long_t rx_errors
;
53 /* via l2tp_session_priv() */
54 struct l2tp_eth_sess
{
55 struct net_device __rcu
*dev
;
59 static int l2tp_eth_dev_init(struct net_device
*dev
)
61 eth_hw_addr_random(dev
);
62 eth_broadcast_addr(dev
->broadcast
);
63 netdev_lockdep_set_classes(dev
);
68 static void l2tp_eth_dev_uninit(struct net_device
*dev
)
70 struct l2tp_eth
*priv
= netdev_priv(dev
);
71 struct l2tp_eth_sess
*spriv
;
73 spriv
= l2tp_session_priv(priv
->session
);
74 RCU_INIT_POINTER(spriv
->dev
, NULL
);
75 /* No need for synchronize_net() here. We're called by
76 * unregister_netdev*(), which does the synchronisation for us.
80 static int l2tp_eth_dev_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
82 struct l2tp_eth
*priv
= netdev_priv(dev
);
83 struct l2tp_session
*session
= priv
->session
;
84 unsigned int len
= skb
->len
;
85 int ret
= l2tp_xmit_skb(session
, skb
, session
->hdr_len
);
87 if (likely(ret
== NET_XMIT_SUCCESS
)) {
88 atomic_long_add(len
, &priv
->tx_bytes
);
89 atomic_long_inc(&priv
->tx_packets
);
91 atomic_long_inc(&priv
->tx_dropped
);
96 static void l2tp_eth_get_stats64(struct net_device
*dev
,
97 struct rtnl_link_stats64
*stats
)
99 struct l2tp_eth
*priv
= netdev_priv(dev
);
101 stats
->tx_bytes
= (unsigned long) atomic_long_read(&priv
->tx_bytes
);
102 stats
->tx_packets
= (unsigned long) atomic_long_read(&priv
->tx_packets
);
103 stats
->tx_dropped
= (unsigned long) atomic_long_read(&priv
->tx_dropped
);
104 stats
->rx_bytes
= (unsigned long) atomic_long_read(&priv
->rx_bytes
);
105 stats
->rx_packets
= (unsigned long) atomic_long_read(&priv
->rx_packets
);
106 stats
->rx_errors
= (unsigned long) atomic_long_read(&priv
->rx_errors
);
110 static const struct net_device_ops l2tp_eth_netdev_ops
= {
111 .ndo_init
= l2tp_eth_dev_init
,
112 .ndo_uninit
= l2tp_eth_dev_uninit
,
113 .ndo_start_xmit
= l2tp_eth_dev_xmit
,
114 .ndo_get_stats64
= l2tp_eth_get_stats64
,
115 .ndo_set_mac_address
= eth_mac_addr
,
118 static struct device_type l2tpeth_type
= {
122 static void l2tp_eth_dev_setup(struct net_device
*dev
)
124 SET_NETDEV_DEVTYPE(dev
, &l2tpeth_type
);
126 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
127 dev
->features
|= NETIF_F_LLTX
;
128 dev
->netdev_ops
= &l2tp_eth_netdev_ops
;
129 dev
->needs_free_netdev
= true;
132 static void l2tp_eth_dev_recv(struct l2tp_session
*session
, struct sk_buff
*skb
, int data_len
)
134 struct l2tp_eth_sess
*spriv
= l2tp_session_priv(session
);
135 struct net_device
*dev
;
136 struct l2tp_eth
*priv
;
138 if (session
->debug
& L2TP_MSG_DATA
) {
141 length
= min(32u, skb
->len
);
142 if (!pskb_may_pull(skb
, length
))
145 pr_debug("%s: eth recv\n", session
->name
);
146 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
, skb
->data
, length
);
149 if (!pskb_may_pull(skb
, ETH_HLEN
))
154 /* checksums verified by L2TP */
155 skb
->ip_summed
= CHECKSUM_NONE
;
161 dev
= rcu_dereference(spriv
->dev
);
165 priv
= netdev_priv(dev
);
166 if (dev_forward_skb(dev
, skb
) == NET_RX_SUCCESS
) {
167 atomic_long_inc(&priv
->rx_packets
);
168 atomic_long_add(data_len
, &priv
->rx_bytes
);
170 atomic_long_inc(&priv
->rx_errors
);
182 static void l2tp_eth_delete(struct l2tp_session
*session
)
184 struct l2tp_eth_sess
*spriv
;
185 struct net_device
*dev
;
188 spriv
= l2tp_session_priv(session
);
191 dev
= rtnl_dereference(spriv
->dev
);
193 unregister_netdevice(dev
);
195 module_put(THIS_MODULE
);
202 #if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
203 static void l2tp_eth_show(struct seq_file
*m
, void *arg
)
205 struct l2tp_session
*session
= arg
;
206 struct l2tp_eth_sess
*spriv
= l2tp_session_priv(session
);
207 struct net_device
*dev
;
210 dev
= rcu_dereference(spriv
->dev
);
218 seq_printf(m
, " interface %s\n", dev
->name
);
224 static void l2tp_eth_adjust_mtu(struct l2tp_tunnel
*tunnel
,
225 struct l2tp_session
*session
,
226 struct net_device
*dev
)
228 unsigned int overhead
= 0;
229 struct dst_entry
*dst
;
232 /* if the encap is UDP, account for UDP header size */
233 if (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) {
234 overhead
+= sizeof(struct udphdr
);
235 dev
->needed_headroom
+= sizeof(struct udphdr
);
237 if (session
->mtu
!= 0) {
238 dev
->mtu
= session
->mtu
;
239 dev
->needed_headroom
+= session
->hdr_len
;
242 lock_sock(tunnel
->sock
);
243 l3_overhead
= kernel_sock_ip_overhead(tunnel
->sock
);
244 release_sock(tunnel
->sock
);
245 if (l3_overhead
== 0) {
246 /* L3 Overhead couldn't be identified, this could be
247 * because tunnel->sock was NULL or the socket's
248 * address family was not IPv4 or IPv6,
249 * dev mtu stays at 1500.
253 /* Adjust MTU, factor overhead - underlay L3, overlay L2 hdr
254 * UDP overhead, if any, was already factored in above.
256 overhead
+= session
->hdr_len
+ ETH_HLEN
+ l3_overhead
;
258 /* If PMTU discovery was enabled, use discovered MTU on L2TP device */
259 dst
= sk_dst_get(tunnel
->sock
);
261 /* dst_mtu will use PMTU if found, else fallback to intf MTU */
262 u32 pmtu
= dst_mtu(dst
);
268 session
->mtu
= dev
->mtu
- overhead
;
269 dev
->mtu
= session
->mtu
;
270 dev
->needed_headroom
+= session
->hdr_len
;
273 static int l2tp_eth_create(struct net
*net
, struct l2tp_tunnel
*tunnel
,
274 u32 session_id
, u32 peer_session_id
,
275 struct l2tp_session_cfg
*cfg
)
277 unsigned char name_assign_type
;
278 struct net_device
*dev
;
280 struct l2tp_session
*session
;
281 struct l2tp_eth
*priv
;
282 struct l2tp_eth_sess
*spriv
;
286 strlcpy(name
, cfg
->ifname
, IFNAMSIZ
);
287 name_assign_type
= NET_NAME_USER
;
289 strcpy(name
, L2TP_ETH_DEV_NAME
);
290 name_assign_type
= NET_NAME_ENUM
;
293 session
= l2tp_session_create(sizeof(*spriv
), tunnel
, session_id
,
294 peer_session_id
, cfg
);
295 if (IS_ERR(session
)) {
296 rc
= PTR_ERR(session
);
300 dev
= alloc_netdev(sizeof(*priv
), name
, name_assign_type
,
307 dev_net_set(dev
, net
);
309 dev
->max_mtu
= ETH_MAX_MTU
;
310 l2tp_eth_adjust_mtu(tunnel
, session
, dev
);
312 priv
= netdev_priv(dev
);
313 priv
->session
= session
;
315 session
->recv_skb
= l2tp_eth_dev_recv
;
316 session
->session_close
= l2tp_eth_delete
;
317 #if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
318 session
->show
= l2tp_eth_show
;
321 spriv
= l2tp_session_priv(session
);
323 l2tp_session_inc_refcount(session
);
327 /* Register both device and session while holding the rtnl lock. This
328 * ensures that l2tp_eth_delete() will see that there's a device to
329 * unregister, even if it happened to run before we assign spriv->dev.
331 rc
= l2tp_session_register(session
, tunnel
);
337 rc
= register_netdevice(dev
);
340 l2tp_session_delete(session
);
341 l2tp_session_dec_refcount(session
);
347 strlcpy(session
->ifname
, dev
->name
, IFNAMSIZ
);
348 rcu_assign_pointer(spriv
->dev
, dev
);
352 l2tp_session_dec_refcount(session
);
354 __module_get(THIS_MODULE
);
359 l2tp_session_dec_refcount(session
);
368 static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops
= {
369 .session_create
= l2tp_eth_create
,
370 .session_delete
= l2tp_session_delete
,
374 static int __init
l2tp_eth_init(void)
378 err
= l2tp_nl_register_ops(L2TP_PWTYPE_ETH
, &l2tp_eth_nl_cmd_ops
);
382 pr_info("L2TP ethernet pseudowire support (L2TPv3)\n");
390 static void __exit
l2tp_eth_exit(void)
392 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH
);
395 module_init(l2tp_eth_init
);
396 module_exit(l2tp_eth_exit
);
398 MODULE_LICENSE("GPL");
399 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
400 MODULE_DESCRIPTION("L2TP ethernet pseudowire driver");
401 MODULE_VERSION("1.0");
402 MODULE_ALIAS_L2TP_PWTYPE(5);