blob: 8aadc4f3bb9e91a6f8c323b91c7ca8d188c58050 [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
James Chapman0ad66142010-04-02 06:19:33 +0000202static void l2tp_eth_show(struct seq_file *m, void *arg)
203{
204 struct l2tp_session *session = arg;
205 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
Guillaume Naultee28de62017-10-27 16:51:51 +0200206 struct net_device *dev;
207
208 rcu_read_lock();
209 dev = rcu_dereference(spriv->dev);
210 if (!dev) {
211 rcu_read_unlock();
212 return;
213 }
214 dev_hold(dev);
215 rcu_read_unlock();
James Chapman0ad66142010-04-02 06:19:33 +0000216
217 seq_printf(m, " interface %s\n", dev->name);
Guillaume Naultee28de62017-10-27 16:51:51 +0200218
219 dev_put(dev);
James Chapman0ad66142010-04-02 06:19:33 +0000220}
James Chapman0ad66142010-04-02 06:19:33 +0000221
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700222static void l2tp_eth_adjust_mtu(struct l2tp_tunnel *tunnel,
223 struct l2tp_session *session,
224 struct net_device *dev)
225{
226 unsigned int overhead = 0;
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700227 u32 l3_overhead = 0;
Guillaume Nault1f5cd2a2018-08-03 12:38:34 +0200228 u32 mtu;
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700229
230 /* if the encap is UDP, account for UDP header size */
231 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
232 overhead += sizeof(struct udphdr);
233 dev->needed_headroom += sizeof(struct udphdr);
234 }
Guillaume Naulte9697e22018-08-03 12:38:39 +0200235
R. Parameswaran57240d02017-04-12 18:31:04 -0700236 lock_sock(tunnel->sock);
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700237 l3_overhead = kernel_sock_ip_overhead(tunnel->sock);
R. Parameswaran57240d02017-04-12 18:31:04 -0700238 release_sock(tunnel->sock);
Guillaume Naulte9697e22018-08-03 12:38:39 +0200239
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700240 if (l3_overhead == 0) {
241 /* L3 Overhead couldn't be identified, this could be
242 * because tunnel->sock was NULL or the socket's
243 * address family was not IPv4 or IPv6,
244 * dev mtu stays at 1500.
245 */
246 return;
247 }
248 /* Adjust MTU, factor overhead - underlay L3, overlay L2 hdr
249 * UDP overhead, if any, was already factored in above.
250 */
251 overhead += session->hdr_len + ETH_HLEN + l3_overhead;
252
Guillaume Naulte9697e22018-08-03 12:38:39 +0200253 mtu = l2tp_tunnel_dst_mtu(tunnel) - overhead;
254 if (mtu < dev->min_mtu || mtu > dev->max_mtu)
255 dev->mtu = ETH_DATA_LEN - overhead;
256 else
Guillaume Nault1f5cd2a2018-08-03 12:38:34 +0200257 dev->mtu = mtu;
Guillaume Naulte9697e22018-08-03 12:38:39 +0200258
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700259 dev->needed_headroom += session->hdr_len;
260}
261
Guillaume Naultf026bc22017-09-01 17:58:51 +0200262static int l2tp_eth_create(struct net *net, struct l2tp_tunnel *tunnel,
263 u32 session_id, u32 peer_session_id,
264 struct l2tp_session_cfg *cfg)
James Chapmand9e31d12010-04-02 06:19:26 +0000265{
Guillaume Naultc39855f2017-04-24 14:16:06 +0200266 unsigned char name_assign_type;
James Chapmand9e31d12010-04-02 06:19:26 +0000267 struct net_device *dev;
268 char name[IFNAMSIZ];
James Chapmand9e31d12010-04-02 06:19:26 +0000269 struct l2tp_session *session;
270 struct l2tp_eth *priv;
271 struct l2tp_eth_sess *spriv;
272 int rc;
James Chapmand9e31d12010-04-02 06:19:26 +0000273
James Chapmand9e31d12010-04-02 06:19:26 +0000274 if (cfg->ifname) {
James Chapmand9e31d12010-04-02 06:19:26 +0000275 strlcpy(name, cfg->ifname, IFNAMSIZ);
Guillaume Naultc39855f2017-04-24 14:16:06 +0200276 name_assign_type = NET_NAME_USER;
277 } else {
James Chapmand9e31d12010-04-02 06:19:26 +0000278 strcpy(name, L2TP_ETH_DEV_NAME);
Guillaume Naultc39855f2017-04-24 14:16:06 +0200279 name_assign_type = NET_NAME_ENUM;
280 }
James Chapmand9e31d12010-04-02 06:19:26 +0000281
282 session = l2tp_session_create(sizeof(*spriv), tunnel, session_id,
283 peer_session_id, cfg);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200284 if (IS_ERR(session)) {
285 rc = PTR_ERR(session);
Guillaume Naultee28de62017-10-27 16:51:51 +0200286 goto err;
Guillaume Nault3953ae72017-10-27 16:51:50 +0200287 }
288
Guillaume Naultc39855f2017-04-24 14:16:06 +0200289 dev = alloc_netdev(sizeof(*priv), name, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +0200290 l2tp_eth_dev_setup);
James Chapmand9e31d12010-04-02 06:19:26 +0000291 if (!dev) {
292 rc = -ENOMEM;
Guillaume Naultee28de62017-10-27 16:51:51 +0200293 goto err_sess;
James Chapmand9e31d12010-04-02 06:19:26 +0000294 }
295
296 dev_net_set(dev, net);
Jarod Wilson8b1efc02016-10-20 23:25:27 -0400297 dev->min_mtu = 0;
298 dev->max_mtu = ETH_MAX_MTU;
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700299 l2tp_eth_adjust_mtu(tunnel, session, dev);
James Chapmand9e31d12010-04-02 06:19:26 +0000300
301 priv = netdev_priv(dev);
James Chapmand9e31d12010-04-02 06:19:26 +0000302 priv->session = session;
James Chapmand9e31d12010-04-02 06:19:26 +0000303
James Chapmand9e31d12010-04-02 06:19:26 +0000304 session->recv_skb = l2tp_eth_dev_recv;
305 session->session_close = l2tp_eth_delete;
Arnd Bergmannc2ebc252018-08-13 23:43:05 +0200306 if (IS_ENABLED(CONFIG_L2TP_DEBUGFS))
307 session->show = l2tp_eth_show;
James Chapmand9e31d12010-04-02 06:19:26 +0000308
309 spriv = l2tp_session_priv(session);
James Chapmand9e31d12010-04-02 06:19:26 +0000310
Guillaume Naultee28de62017-10-27 16:51:51 +0200311 l2tp_session_inc_refcount(session);
James Chapmand9e31d12010-04-02 06:19:26 +0000312
Guillaume Naultee28de62017-10-27 16:51:51 +0200313 rtnl_lock();
314
315 /* Register both device and session while holding the rtnl lock. This
316 * ensures that l2tp_eth_delete() will see that there's a device to
317 * unregister, even if it happened to run before we assign spriv->dev.
318 */
319 rc = l2tp_session_register(session, tunnel);
320 if (rc < 0) {
321 rtnl_unlock();
322 goto err_sess_dev;
323 }
324
325 rc = register_netdevice(dev);
326 if (rc < 0) {
327 rtnl_unlock();
328 l2tp_session_delete(session);
329 l2tp_session_dec_refcount(session);
330 free_netdev(dev);
331
332 return rc;
333 }
334
James Chapmand9e31d12010-04-02 06:19:26 +0000335 strlcpy(session->ifname, dev->name, IFNAMSIZ);
Guillaume Naultee28de62017-10-27 16:51:51 +0200336 rcu_assign_pointer(spriv->dev, dev);
337
338 rtnl_unlock();
339
Guillaume Nault3953ae72017-10-27 16:51:50 +0200340 l2tp_session_dec_refcount(session);
James Chapmand9e31d12010-04-02 06:19:26 +0000341
Guillaume Naultee28de62017-10-27 16:51:51 +0200342 __module_get(THIS_MODULE);
James Chapmand9e31d12010-04-02 06:19:26 +0000343
344 return 0;
345
Guillaume Naultee28de62017-10-27 16:51:51 +0200346err_sess_dev:
Guillaume Nault3953ae72017-10-27 16:51:50 +0200347 l2tp_session_dec_refcount(session);
Guillaume Naultee28de62017-10-27 16:51:51 +0200348 free_netdev(dev);
349err_sess:
350 kfree(session);
351err:
James Chapmand9e31d12010-04-02 06:19:26 +0000352 return rc;
353}
354
James Chapmand9e31d12010-04-02 06:19:26 +0000355
356static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops = {
357 .session_create = l2tp_eth_create,
358 .session_delete = l2tp_session_delete,
359};
360
361
362static int __init l2tp_eth_init(void)
363{
364 int err = 0;
365
366 err = l2tp_nl_register_ops(L2TP_PWTYPE_ETH, &l2tp_eth_nl_cmd_ops);
367 if (err)
Guillaume Nault9f775ea2017-09-28 15:44:38 +0200368 goto err;
James Chapmand9e31d12010-04-02 06:19:26 +0000369
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000370 pr_info("L2TP ethernet pseudowire support (L2TPv3)\n");
James Chapmand9e31d12010-04-02 06:19:26 +0000371
372 return 0;
373
Guillaume Nault9f775ea2017-09-28 15:44:38 +0200374err:
James Chapmand9e31d12010-04-02 06:19:26 +0000375 return err;
376}
377
378static void __exit l2tp_eth_exit(void)
379{
James Chapmand9e31d12010-04-02 06:19:26 +0000380 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH);
381}
382
383module_init(l2tp_eth_init);
384module_exit(l2tp_eth_exit);
385
386MODULE_LICENSE("GPL");
387MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
388MODULE_DESCRIPTION("L2TP ethernet pseudowire driver");
389MODULE_VERSION("1.0");
stephen hemmingerf1f39f92015-09-23 21:33:34 -0700390MODULE_ALIAS_L2TP_PWTYPE(5);