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 net_device
*dev
;
45 struct sock
*tunnel_sock
;
46 struct l2tp_session
*session
;
47 atomic_long_t tx_bytes
;
48 atomic_long_t tx_packets
;
49 atomic_long_t tx_dropped
;
50 atomic_long_t rx_bytes
;
51 atomic_long_t rx_packets
;
52 atomic_long_t rx_errors
;
55 /* via l2tp_session_priv() */
56 struct l2tp_eth_sess
{
57 struct net_device
*dev
;
61 static int l2tp_eth_dev_init(struct net_device
*dev
)
63 struct l2tp_eth
*priv
= netdev_priv(dev
);
66 eth_hw_addr_random(dev
);
67 eth_broadcast_addr(dev
->broadcast
);
68 netdev_lockdep_set_classes(dev
);
73 static void l2tp_eth_dev_uninit(struct net_device
*dev
)
78 static int l2tp_eth_dev_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
80 struct l2tp_eth
*priv
= netdev_priv(dev
);
81 struct l2tp_session
*session
= priv
->session
;
82 unsigned int len
= skb
->len
;
83 int ret
= l2tp_xmit_skb(session
, skb
, session
->hdr_len
);
85 if (likely(ret
== NET_XMIT_SUCCESS
)) {
86 atomic_long_add(len
, &priv
->tx_bytes
);
87 atomic_long_inc(&priv
->tx_packets
);
89 atomic_long_inc(&priv
->tx_dropped
);
94 static void l2tp_eth_get_stats64(struct net_device
*dev
,
95 struct rtnl_link_stats64
*stats
)
97 struct l2tp_eth
*priv
= netdev_priv(dev
);
99 stats
->tx_bytes
= (unsigned long) atomic_long_read(&priv
->tx_bytes
);
100 stats
->tx_packets
= (unsigned long) atomic_long_read(&priv
->tx_packets
);
101 stats
->tx_dropped
= (unsigned long) atomic_long_read(&priv
->tx_dropped
);
102 stats
->rx_bytes
= (unsigned long) atomic_long_read(&priv
->rx_bytes
);
103 stats
->rx_packets
= (unsigned long) atomic_long_read(&priv
->rx_packets
);
104 stats
->rx_errors
= (unsigned long) atomic_long_read(&priv
->rx_errors
);
108 static const struct net_device_ops l2tp_eth_netdev_ops
= {
109 .ndo_init
= l2tp_eth_dev_init
,
110 .ndo_uninit
= l2tp_eth_dev_uninit
,
111 .ndo_start_xmit
= l2tp_eth_dev_xmit
,
112 .ndo_get_stats64
= l2tp_eth_get_stats64
,
113 .ndo_set_mac_address
= eth_mac_addr
,
116 static struct device_type l2tpeth_type
= {
120 static void l2tp_eth_dev_setup(struct net_device
*dev
)
122 SET_NETDEV_DEVTYPE(dev
, &l2tpeth_type
);
124 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
125 dev
->features
|= NETIF_F_LLTX
;
126 dev
->netdev_ops
= &l2tp_eth_netdev_ops
;
127 dev
->needs_free_netdev
= true;
130 static void l2tp_eth_dev_recv(struct l2tp_session
*session
, struct sk_buff
*skb
, int data_len
)
132 struct l2tp_eth_sess
*spriv
= l2tp_session_priv(session
);
133 struct net_device
*dev
= spriv
->dev
;
134 struct l2tp_eth
*priv
= netdev_priv(dev
);
136 if (session
->debug
& L2TP_MSG_DATA
) {
139 length
= min(32u, skb
->len
);
140 if (!pskb_may_pull(skb
, length
))
143 pr_debug("%s: eth recv\n", session
->name
);
144 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
, skb
->data
, length
);
147 if (!pskb_may_pull(skb
, ETH_HLEN
))
152 /* checksums verified by L2TP */
153 skb
->ip_summed
= CHECKSUM_NONE
;
158 if (dev_forward_skb(dev
, skb
) == NET_RX_SUCCESS
) {
159 atomic_long_inc(&priv
->rx_packets
);
160 atomic_long_add(data_len
, &priv
->rx_bytes
);
162 atomic_long_inc(&priv
->rx_errors
);
167 atomic_long_inc(&priv
->rx_errors
);
171 static void l2tp_eth_delete(struct l2tp_session
*session
)
173 struct l2tp_eth_sess
*spriv
;
174 struct net_device
*dev
;
177 spriv
= l2tp_session_priv(session
);
180 unregister_netdev(dev
);
182 module_put(THIS_MODULE
);
187 #if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
188 static void l2tp_eth_show(struct seq_file
*m
, void *arg
)
190 struct l2tp_session
*session
= arg
;
191 struct l2tp_eth_sess
*spriv
= l2tp_session_priv(session
);
192 struct net_device
*dev
= spriv
->dev
;
194 seq_printf(m
, " interface %s\n", dev
->name
);
198 static void l2tp_eth_adjust_mtu(struct l2tp_tunnel
*tunnel
,
199 struct l2tp_session
*session
,
200 struct net_device
*dev
)
202 unsigned int overhead
= 0;
203 struct dst_entry
*dst
;
206 /* if the encap is UDP, account for UDP header size */
207 if (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) {
208 overhead
+= sizeof(struct udphdr
);
209 dev
->needed_headroom
+= sizeof(struct udphdr
);
211 if (session
->mtu
!= 0) {
212 dev
->mtu
= session
->mtu
;
213 dev
->needed_headroom
+= session
->hdr_len
;
216 lock_sock(tunnel
->sock
);
217 l3_overhead
= kernel_sock_ip_overhead(tunnel
->sock
);
218 release_sock(tunnel
->sock
);
219 if (l3_overhead
== 0) {
220 /* L3 Overhead couldn't be identified, this could be
221 * because tunnel->sock was NULL or the socket's
222 * address family was not IPv4 or IPv6,
223 * dev mtu stays at 1500.
227 /* Adjust MTU, factor overhead - underlay L3, overlay L2 hdr
228 * UDP overhead, if any, was already factored in above.
230 overhead
+= session
->hdr_len
+ ETH_HLEN
+ l3_overhead
;
232 /* If PMTU discovery was enabled, use discovered MTU on L2TP device */
233 dst
= sk_dst_get(tunnel
->sock
);
235 /* dst_mtu will use PMTU if found, else fallback to intf MTU */
236 u32 pmtu
= dst_mtu(dst
);
242 session
->mtu
= dev
->mtu
- overhead
;
243 dev
->mtu
= session
->mtu
;
244 dev
->needed_headroom
+= session
->hdr_len
;
247 static int l2tp_eth_create(struct net
*net
, struct l2tp_tunnel
*tunnel
,
248 u32 session_id
, u32 peer_session_id
,
249 struct l2tp_session_cfg
*cfg
)
251 unsigned char name_assign_type
;
252 struct net_device
*dev
;
254 struct l2tp_session
*session
;
255 struct l2tp_eth
*priv
;
256 struct l2tp_eth_sess
*spriv
;
260 strlcpy(name
, cfg
->ifname
, IFNAMSIZ
);
261 name_assign_type
= NET_NAME_USER
;
263 strcpy(name
, L2TP_ETH_DEV_NAME
);
264 name_assign_type
= NET_NAME_ENUM
;
267 session
= l2tp_session_create(sizeof(*spriv
), tunnel
, session_id
,
268 peer_session_id
, cfg
);
269 if (IS_ERR(session
)) {
270 rc
= PTR_ERR(session
);
274 dev
= alloc_netdev(sizeof(*priv
), name
, name_assign_type
,
278 goto out_del_session
;
281 dev_net_set(dev
, net
);
283 dev
->max_mtu
= ETH_MAX_MTU
;
284 l2tp_eth_adjust_mtu(tunnel
, session
, dev
);
286 priv
= netdev_priv(dev
);
288 priv
->session
= session
;
290 priv
->tunnel_sock
= tunnel
->sock
;
291 session
->recv_skb
= l2tp_eth_dev_recv
;
292 session
->session_close
= l2tp_eth_delete
;
293 #if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
294 session
->show
= l2tp_eth_show
;
297 spriv
= l2tp_session_priv(session
);
300 rc
= register_netdev(dev
);
304 __module_get(THIS_MODULE
);
305 /* Must be done after register_netdev() */
306 strlcpy(session
->ifname
, dev
->name
, IFNAMSIZ
);
316 l2tp_session_delete(session
);
322 static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops
= {
323 .session_create
= l2tp_eth_create
,
324 .session_delete
= l2tp_session_delete
,
328 static int __init
l2tp_eth_init(void)
332 err
= l2tp_nl_register_ops(L2TP_PWTYPE_ETH
, &l2tp_eth_nl_cmd_ops
);
336 pr_info("L2TP ethernet pseudowire support (L2TPv3)\n");
344 static void __exit
l2tp_eth_exit(void)
346 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH
);
349 module_init(l2tp_eth_init
);
350 module_exit(l2tp_eth_exit
);
352 MODULE_LICENSE("GPL");
353 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
354 MODULE_DESCRIPTION("L2TP ethernet pseudowire driver");
355 MODULE_VERSION("1.0");
356 MODULE_ALIAS_L2TP_PWTYPE(5);