blob: 5c366ecfa1cb0e2918c54f30d80ab30780f4cfdb [file] [log] [blame]
James Chapmand9e31d12010-04-02 06:19:26 +00001/*
2 * L2TPv3 ethernet pseudowire driver
3 *
4 * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
5 *
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.
10 */
11
Joe Perchesa4ca44f2012-05-16 09:55:56 +000012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
James Chapmand9e31d12010-04-02 06:19:26 +000014#include <linux/module.h>
15#include <linux/skbuff.h>
16#include <linux/socket.h>
17#include <linux/hash.h>
18#include <linux/l2tp.h>
19#include <linux/in.h>
20#include <linux/etherdevice.h>
21#include <linux/spinlock.h>
22#include <net/sock.h>
23#include <net/ip.h>
24#include <net/icmp.h>
25#include <net/udp.h>
26#include <net/inet_common.h>
27#include <net/inet_hashtables.h>
28#include <net/tcp_states.h>
29#include <net/protocol.h>
30#include <net/xfrm.h>
31#include <net/net_namespace.h>
32#include <net/netns/generic.h>
R. Parameswaranb784e7e2017-04-05 17:00:07 -070033#include <linux/ip.h>
34#include <linux/ipv6.h>
35#include <linux/udp.h>
James Chapmand9e31d12010-04-02 06:19:26 +000036
37#include "l2tp_core.h"
38
39/* Default device name. May be overridden by name specified by user */
40#define L2TP_ETH_DEV_NAME "l2tpeth%d"
41
42/* via netdev_priv() */
43struct l2tp_eth {
James Chapmand9e31d12010-04-02 06:19:26 +000044 struct l2tp_session *session;
Eric Dumazeta2842a12012-06-25 05:35:45 +000045 atomic_long_t tx_bytes;
46 atomic_long_t tx_packets;
Eric Dumazetb8c84302012-06-28 20:15:13 +000047 atomic_long_t tx_dropped;
Eric Dumazeta2842a12012-06-25 05:35:45 +000048 atomic_long_t rx_bytes;
49 atomic_long_t rx_packets;
50 atomic_long_t rx_errors;
James Chapmand9e31d12010-04-02 06:19:26 +000051};
52
53/* via l2tp_session_priv() */
54struct l2tp_eth_sess {
Guillaume Naultee28de62017-10-27 16:51:51 +020055 struct net_device __rcu *dev;
James Chapmand9e31d12010-04-02 06:19:26 +000056};
57
James Chapmand9e31d12010-04-02 06:19:26 +000058
59static int l2tp_eth_dev_init(struct net_device *dev)
60{
Danny Kukawkaf2cedb62012-02-15 06:45:39 +000061 eth_hw_addr_random(dev);
Joe Perches1cea7e22015-03-02 19:54:59 -080062 eth_broadcast_addr(dev->broadcast);
Eric Dumazetd3fff6c2016-06-09 07:45:12 -070063 netdev_lockdep_set_classes(dev);
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -070064
James Chapmand9e31d12010-04-02 06:19:26 +000065 return 0;
66}
67
68static void l2tp_eth_dev_uninit(struct net_device *dev)
69{
Guillaume Naultee28de62017-10-27 16:51:51 +020070 struct l2tp_eth *priv = netdev_priv(dev);
71 struct l2tp_eth_sess *spriv;
72
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.
77 */
James Chapmand9e31d12010-04-02 06:19:26 +000078}
79
80static int l2tp_eth_dev_xmit(struct sk_buff *skb, struct net_device *dev)
81{
82 struct l2tp_eth *priv = netdev_priv(dev);
83 struct l2tp_session *session = priv->session;
Eric Dumazetb8c84302012-06-28 20:15:13 +000084 unsigned int len = skb->len;
85 int ret = l2tp_xmit_skb(session, skb, session->hdr_len);
James Chapmand9e31d12010-04-02 06:19:26 +000086
WANG Conga4cd0272016-11-21 23:24:43 -080087 if (likely(ret == NET_XMIT_SUCCESS)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +000088 atomic_long_add(len, &priv->tx_bytes);
89 atomic_long_inc(&priv->tx_packets);
90 } else {
91 atomic_long_inc(&priv->tx_dropped);
92 }
Eric Dumazetaa214de02012-06-25 00:45:14 +000093 return NETDEV_TX_OK;
James Chapmand9e31d12010-04-02 06:19:26 +000094}
95
stephen hemmingerbc1f4472017-01-06 19:12:52 -080096static void l2tp_eth_get_stats64(struct net_device *dev,
97 struct rtnl_link_stats64 *stats)
Eric Dumazeta2842a12012-06-25 05:35:45 +000098{
99 struct l2tp_eth *priv = netdev_priv(dev);
100
Dominik Heidler9b3dc0a2017-06-09 16:29:47 +0200101 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);
107
Eric Dumazeta2842a12012-06-25 05:35:45 +0000108}
109
Julia Lawalleb947372016-09-15 22:23:26 +0200110static const struct net_device_ops l2tp_eth_netdev_ops = {
James Chapmand9e31d12010-04-02 06:19:26 +0000111 .ndo_init = l2tp_eth_dev_init,
112 .ndo_uninit = l2tp_eth_dev_uninit,
113 .ndo_start_xmit = l2tp_eth_dev_xmit,
Eric Dumazeta2842a12012-06-25 05:35:45 +0000114 .ndo_get_stats64 = l2tp_eth_get_stats64,
Alexander Couzensfe159122014-11-19 13:24:39 +0100115 .ndo_set_mac_address = eth_mac_addr,
James Chapmand9e31d12010-04-02 06:19:26 +0000116};
117
Guillaume Naulta485c2b2017-04-24 14:16:07 +0200118static struct device_type l2tpeth_type = {
119 .name = "l2tpeth",
120};
121
James Chapmand9e31d12010-04-02 06:19:26 +0000122static void l2tp_eth_dev_setup(struct net_device *dev)
123{
Guillaume Naulta485c2b2017-04-24 14:16:07 +0200124 SET_NETDEV_DEVTYPE(dev, &l2tpeth_type);
James Chapmand9e31d12010-04-02 06:19:26 +0000125 ether_setup(dev);
Eric Dumazeta2842a12012-06-25 05:35:45 +0000126 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
127 dev->features |= NETIF_F_LLTX;
James Chapmand9e31d12010-04-02 06:19:26 +0000128 dev->netdev_ops = &l2tp_eth_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -0400129 dev->needs_free_netdev = true;
James Chapmand9e31d12010-04-02 06:19:26 +0000130}
131
132static void l2tp_eth_dev_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
133{
134 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
Guillaume Naultee28de62017-10-27 16:51:51 +0200135 struct net_device *dev;
136 struct l2tp_eth *priv;
James Chapmand9e31d12010-04-02 06:19:26 +0000137
138 if (session->debug & L2TP_MSG_DATA) {
139 unsigned int length;
James Chapmand9e31d12010-04-02 06:19:26 +0000140
141 length = min(32u, skb->len);
142 if (!pskb_may_pull(skb, length))
143 goto error;
144
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000145 pr_debug("%s: eth recv\n", session->name);
Eric Dumazeta2842a12012-06-25 05:35:45 +0000146 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmand9e31d12010-04-02 06:19:26 +0000147 }
148
Eric Dumazetc0cc88a2012-09-04 15:54:55 -0400149 if (!pskb_may_pull(skb, ETH_HLEN))
James Chapmand9e31d12010-04-02 06:19:26 +0000150 goto error;
151
152 secpath_reset(skb);
153
154 /* checksums verified by L2TP */
155 skb->ip_summed = CHECKSUM_NONE;
156
157 skb_dst_drop(skb);
158 nf_reset(skb);
159
Guillaume Naultee28de62017-10-27 16:51:51 +0200160 rcu_read_lock();
161 dev = rcu_dereference(spriv->dev);
162 if (!dev)
163 goto error_rcu;
164
165 priv = netdev_priv(dev);
James Chapmand9e31d12010-04-02 06:19:26 +0000166 if (dev_forward_skb(dev, skb) == NET_RX_SUCCESS) {
Eric Dumazeta2842a12012-06-25 05:35:45 +0000167 atomic_long_inc(&priv->rx_packets);
168 atomic_long_add(data_len, &priv->rx_bytes);
169 } else {
170 atomic_long_inc(&priv->rx_errors);
171 }
Guillaume Naultee28de62017-10-27 16:51:51 +0200172 rcu_read_unlock();
173
James Chapmand9e31d12010-04-02 06:19:26 +0000174 return;
175
Guillaume Naultee28de62017-10-27 16:51:51 +0200176error_rcu:
177 rcu_read_unlock();
James Chapmand9e31d12010-04-02 06:19:26 +0000178error:
James Chapmand9e31d12010-04-02 06:19:26 +0000179 kfree_skb(skb);
180}
181
182static void l2tp_eth_delete(struct l2tp_session *session)
183{
184 struct l2tp_eth_sess *spriv;
185 struct net_device *dev;
186
187 if (session) {
188 spriv = l2tp_session_priv(session);
Guillaume Naultee28de62017-10-27 16:51:51 +0200189
190 rtnl_lock();
191 dev = rtnl_dereference(spriv->dev);
James Chapmand9e31d12010-04-02 06:19:26 +0000192 if (dev) {
Guillaume Naultee28de62017-10-27 16:51:51 +0200193 unregister_netdevice(dev);
194 rtnl_unlock();
Eric Dumazeta06998b2012-06-07 00:07:20 +0000195 module_put(THIS_MODULE);
Guillaume Naultee28de62017-10-27 16:51:51 +0200196 } else {
197 rtnl_unlock();
James Chapmand9e31d12010-04-02 06:19:26 +0000198 }
199 }
200}
201
Javier Martinez Canillas9dd79942016-09-09 08:43:17 -0400202#if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
James Chapman0ad66142010-04-02 06:19:33 +0000203static void l2tp_eth_show(struct seq_file *m, void *arg)
204{
205 struct l2tp_session *session = arg;
206 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
Guillaume Naultee28de62017-10-27 16:51:51 +0200207 struct net_device *dev;
208
209 rcu_read_lock();
210 dev = rcu_dereference(spriv->dev);
211 if (!dev) {
212 rcu_read_unlock();
213 return;
214 }
215 dev_hold(dev);
216 rcu_read_unlock();
James Chapman0ad66142010-04-02 06:19:33 +0000217
218 seq_printf(m, " interface %s\n", dev->name);
Guillaume Naultee28de62017-10-27 16:51:51 +0200219
220 dev_put(dev);
James Chapman0ad66142010-04-02 06:19:33 +0000221}
222#endif
223
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700224static void l2tp_eth_adjust_mtu(struct l2tp_tunnel *tunnel,
225 struct l2tp_session *session,
226 struct net_device *dev)
227{
228 unsigned int overhead = 0;
229 struct dst_entry *dst;
230 u32 l3_overhead = 0;
231
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);
236 }
237 if (session->mtu != 0) {
238 dev->mtu = session->mtu;
239 dev->needed_headroom += session->hdr_len;
240 return;
241 }
R. Parameswaran57240d02017-04-12 18:31:04 -0700242 lock_sock(tunnel->sock);
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700243 l3_overhead = kernel_sock_ip_overhead(tunnel->sock);
R. Parameswaran57240d02017-04-12 18:31:04 -0700244 release_sock(tunnel->sock);
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700245 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.
250 */
251 return;
252 }
253 /* Adjust MTU, factor overhead - underlay L3, overlay L2 hdr
254 * UDP overhead, if any, was already factored in above.
255 */
256 overhead += session->hdr_len + ETH_HLEN + l3_overhead;
257
258 /* If PMTU discovery was enabled, use discovered MTU on L2TP device */
259 dst = sk_dst_get(tunnel->sock);
260 if (dst) {
261 /* dst_mtu will use PMTU if found, else fallback to intf MTU */
262 u32 pmtu = dst_mtu(dst);
263
264 if (pmtu != 0)
265 dev->mtu = pmtu;
266 dst_release(dst);
267 }
268 session->mtu = dev->mtu - overhead;
269 dev->mtu = session->mtu;
270 dev->needed_headroom += session->hdr_len;
271}
272
Guillaume Naultf026bc22017-09-01 17:58:51 +0200273static 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)
James Chapmand9e31d12010-04-02 06:19:26 +0000276{
Guillaume Naultc39855f2017-04-24 14:16:06 +0200277 unsigned char name_assign_type;
James Chapmand9e31d12010-04-02 06:19:26 +0000278 struct net_device *dev;
279 char name[IFNAMSIZ];
James Chapmand9e31d12010-04-02 06:19:26 +0000280 struct l2tp_session *session;
281 struct l2tp_eth *priv;
282 struct l2tp_eth_sess *spriv;
283 int rc;
James Chapmand9e31d12010-04-02 06:19:26 +0000284
James Chapmand9e31d12010-04-02 06:19:26 +0000285 if (cfg->ifname) {
James Chapmand9e31d12010-04-02 06:19:26 +0000286 strlcpy(name, cfg->ifname, IFNAMSIZ);
Guillaume Naultc39855f2017-04-24 14:16:06 +0200287 name_assign_type = NET_NAME_USER;
288 } else {
James Chapmand9e31d12010-04-02 06:19:26 +0000289 strcpy(name, L2TP_ETH_DEV_NAME);
Guillaume Naultc39855f2017-04-24 14:16:06 +0200290 name_assign_type = NET_NAME_ENUM;
291 }
James Chapmand9e31d12010-04-02 06:19:26 +0000292
293 session = l2tp_session_create(sizeof(*spriv), tunnel, session_id,
294 peer_session_id, cfg);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200295 if (IS_ERR(session)) {
296 rc = PTR_ERR(session);
Guillaume Naultee28de62017-10-27 16:51:51 +0200297 goto err;
Guillaume Nault3953ae72017-10-27 16:51:50 +0200298 }
299
Guillaume Naultc39855f2017-04-24 14:16:06 +0200300 dev = alloc_netdev(sizeof(*priv), name, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +0200301 l2tp_eth_dev_setup);
James Chapmand9e31d12010-04-02 06:19:26 +0000302 if (!dev) {
303 rc = -ENOMEM;
Guillaume Naultee28de62017-10-27 16:51:51 +0200304 goto err_sess;
James Chapmand9e31d12010-04-02 06:19:26 +0000305 }
306
307 dev_net_set(dev, net);
Jarod Wilson8b1efc02016-10-20 23:25:27 -0400308 dev->min_mtu = 0;
309 dev->max_mtu = ETH_MAX_MTU;
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700310 l2tp_eth_adjust_mtu(tunnel, session, dev);
James Chapmand9e31d12010-04-02 06:19:26 +0000311
312 priv = netdev_priv(dev);
James Chapmand9e31d12010-04-02 06:19:26 +0000313 priv->session = session;
James Chapmand9e31d12010-04-02 06:19:26 +0000314
James Chapmand9e31d12010-04-02 06:19:26 +0000315 session->recv_skb = l2tp_eth_dev_recv;
316 session->session_close = l2tp_eth_delete;
Javier Martinez Canillas9dd79942016-09-09 08:43:17 -0400317#if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
James Chapman0ad66142010-04-02 06:19:33 +0000318 session->show = l2tp_eth_show;
319#endif
James Chapmand9e31d12010-04-02 06:19:26 +0000320
321 spriv = l2tp_session_priv(session);
James Chapmand9e31d12010-04-02 06:19:26 +0000322
Guillaume Naultee28de62017-10-27 16:51:51 +0200323 l2tp_session_inc_refcount(session);
James Chapmand9e31d12010-04-02 06:19:26 +0000324
Guillaume Naultee28de62017-10-27 16:51:51 +0200325 rtnl_lock();
326
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.
330 */
331 rc = l2tp_session_register(session, tunnel);
332 if (rc < 0) {
333 rtnl_unlock();
334 goto err_sess_dev;
335 }
336
337 rc = register_netdevice(dev);
338 if (rc < 0) {
339 rtnl_unlock();
340 l2tp_session_delete(session);
341 l2tp_session_dec_refcount(session);
342 free_netdev(dev);
343
344 return rc;
345 }
346
James Chapmand9e31d12010-04-02 06:19:26 +0000347 strlcpy(session->ifname, dev->name, IFNAMSIZ);
Guillaume Naultee28de62017-10-27 16:51:51 +0200348 rcu_assign_pointer(spriv->dev, dev);
349
350 rtnl_unlock();
351
Guillaume Nault3953ae72017-10-27 16:51:50 +0200352 l2tp_session_dec_refcount(session);
James Chapmand9e31d12010-04-02 06:19:26 +0000353
Guillaume Naultee28de62017-10-27 16:51:51 +0200354 __module_get(THIS_MODULE);
James Chapmand9e31d12010-04-02 06:19:26 +0000355
356 return 0;
357
Guillaume Naultee28de62017-10-27 16:51:51 +0200358err_sess_dev:
Guillaume Nault3953ae72017-10-27 16:51:50 +0200359 l2tp_session_dec_refcount(session);
Guillaume Naultee28de62017-10-27 16:51:51 +0200360 free_netdev(dev);
361err_sess:
362 kfree(session);
363err:
James Chapmand9e31d12010-04-02 06:19:26 +0000364 return rc;
365}
366
James Chapmand9e31d12010-04-02 06:19:26 +0000367
368static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops = {
369 .session_create = l2tp_eth_create,
370 .session_delete = l2tp_session_delete,
371};
372
373
374static int __init l2tp_eth_init(void)
375{
376 int err = 0;
377
378 err = l2tp_nl_register_ops(L2TP_PWTYPE_ETH, &l2tp_eth_nl_cmd_ops);
379 if (err)
Guillaume Nault9f775ea2017-09-28 15:44:38 +0200380 goto err;
James Chapmand9e31d12010-04-02 06:19:26 +0000381
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000382 pr_info("L2TP ethernet pseudowire support (L2TPv3)\n");
James Chapmand9e31d12010-04-02 06:19:26 +0000383
384 return 0;
385
Guillaume Nault9f775ea2017-09-28 15:44:38 +0200386err:
James Chapmand9e31d12010-04-02 06:19:26 +0000387 return err;
388}
389
390static void __exit l2tp_eth_exit(void)
391{
James Chapmand9e31d12010-04-02 06:19:26 +0000392 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH);
393}
394
395module_init(l2tp_eth_init);
396module_exit(l2tp_eth_exit);
397
398MODULE_LICENSE("GPL");
399MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
400MODULE_DESCRIPTION("L2TP ethernet pseudowire driver");
401MODULE_VERSION("1.0");
stephen hemmingerf1f39f92015-09-23 21:33:34 -0700402MODULE_ALIAS_L2TP_PWTYPE(5);