blob: d3b520b9b2c9d226f90b579b7b40e8baf459f73a [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
James Chapmand9e31d12010-04-02 06:19:26 +00002/*
3 * L2TPv3 ethernet pseudowire driver
4 *
5 * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
James Chapmand9e31d12010-04-02 06:19:26 +00006 */
7
Joe Perchesa4ca44f2012-05-16 09:55:56 +00008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
James Chapmand9e31d12010-04-02 06:19:26 +000010#include <linux/module.h>
11#include <linux/skbuff.h>
12#include <linux/socket.h>
13#include <linux/hash.h>
14#include <linux/l2tp.h>
15#include <linux/in.h>
16#include <linux/etherdevice.h>
17#include <linux/spinlock.h>
18#include <net/sock.h>
19#include <net/ip.h>
20#include <net/icmp.h>
21#include <net/udp.h>
22#include <net/inet_common.h>
23#include <net/inet_hashtables.h>
24#include <net/tcp_states.h>
25#include <net/protocol.h>
26#include <net/xfrm.h>
27#include <net/net_namespace.h>
28#include <net/netns/generic.h>
R. Parameswaranb784e7e2017-04-05 17:00:07 -070029#include <linux/ip.h>
30#include <linux/ipv6.h>
31#include <linux/udp.h>
James Chapmand9e31d12010-04-02 06:19:26 +000032
33#include "l2tp_core.h"
34
35/* Default device name. May be overridden by name specified by user */
36#define L2TP_ETH_DEV_NAME "l2tpeth%d"
37
38/* via netdev_priv() */
39struct l2tp_eth {
James Chapmand9e31d12010-04-02 06:19:26 +000040 struct l2tp_session *session;
Eric Dumazeta2842a12012-06-25 05:35:45 +000041 atomic_long_t tx_bytes;
42 atomic_long_t tx_packets;
Eric Dumazetb8c84302012-06-28 20:15:13 +000043 atomic_long_t tx_dropped;
Eric Dumazeta2842a12012-06-25 05:35:45 +000044 atomic_long_t rx_bytes;
45 atomic_long_t rx_packets;
46 atomic_long_t rx_errors;
James Chapmand9e31d12010-04-02 06:19:26 +000047};
48
49/* via l2tp_session_priv() */
50struct l2tp_eth_sess {
Guillaume Naultee28de62017-10-27 16:51:51 +020051 struct net_device __rcu *dev;
James Chapmand9e31d12010-04-02 06:19:26 +000052};
53
James Chapmand9e31d12010-04-02 06:19:26 +000054
55static int l2tp_eth_dev_init(struct net_device *dev)
56{
Danny Kukawkaf2cedb62012-02-15 06:45:39 +000057 eth_hw_addr_random(dev);
Joe Perches1cea7e22015-03-02 19:54:59 -080058 eth_broadcast_addr(dev->broadcast);
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -070059
James Chapmand9e31d12010-04-02 06:19:26 +000060 return 0;
61}
62
63static void l2tp_eth_dev_uninit(struct net_device *dev)
64{
Guillaume Naultee28de62017-10-27 16:51:51 +020065 struct l2tp_eth *priv = netdev_priv(dev);
66 struct l2tp_eth_sess *spriv;
67
68 spriv = l2tp_session_priv(priv->session);
69 RCU_INIT_POINTER(spriv->dev, NULL);
70 /* No need for synchronize_net() here. We're called by
71 * unregister_netdev*(), which does the synchronisation for us.
72 */
James Chapmand9e31d12010-04-02 06:19:26 +000073}
74
75static int l2tp_eth_dev_xmit(struct sk_buff *skb, struct net_device *dev)
76{
77 struct l2tp_eth *priv = netdev_priv(dev);
78 struct l2tp_session *session = priv->session;
Eric Dumazetb8c84302012-06-28 20:15:13 +000079 unsigned int len = skb->len;
80 int ret = l2tp_xmit_skb(session, skb, session->hdr_len);
James Chapmand9e31d12010-04-02 06:19:26 +000081
WANG Conga4cd0272016-11-21 23:24:43 -080082 if (likely(ret == NET_XMIT_SUCCESS)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +000083 atomic_long_add(len, &priv->tx_bytes);
84 atomic_long_inc(&priv->tx_packets);
85 } else {
86 atomic_long_inc(&priv->tx_dropped);
87 }
Eric Dumazetaa214de02012-06-25 00:45:14 +000088 return NETDEV_TX_OK;
James Chapmand9e31d12010-04-02 06:19:26 +000089}
90
stephen hemmingerbc1f4472017-01-06 19:12:52 -080091static void l2tp_eth_get_stats64(struct net_device *dev,
92 struct rtnl_link_stats64 *stats)
Eric Dumazeta2842a12012-06-25 05:35:45 +000093{
94 struct l2tp_eth *priv = netdev_priv(dev);
95
Dominik Heidler9b3dc0a2017-06-09 16:29:47 +020096 stats->tx_bytes = (unsigned long) atomic_long_read(&priv->tx_bytes);
97 stats->tx_packets = (unsigned long) atomic_long_read(&priv->tx_packets);
98 stats->tx_dropped = (unsigned long) atomic_long_read(&priv->tx_dropped);
99 stats->rx_bytes = (unsigned long) atomic_long_read(&priv->rx_bytes);
100 stats->rx_packets = (unsigned long) atomic_long_read(&priv->rx_packets);
101 stats->rx_errors = (unsigned long) atomic_long_read(&priv->rx_errors);
102
Eric Dumazeta2842a12012-06-25 05:35:45 +0000103}
104
Julia Lawalleb947372016-09-15 22:23:26 +0200105static const struct net_device_ops l2tp_eth_netdev_ops = {
James Chapmand9e31d12010-04-02 06:19:26 +0000106 .ndo_init = l2tp_eth_dev_init,
107 .ndo_uninit = l2tp_eth_dev_uninit,
108 .ndo_start_xmit = l2tp_eth_dev_xmit,
Eric Dumazeta2842a12012-06-25 05:35:45 +0000109 .ndo_get_stats64 = l2tp_eth_get_stats64,
Alexander Couzensfe159122014-11-19 13:24:39 +0100110 .ndo_set_mac_address = eth_mac_addr,
James Chapmand9e31d12010-04-02 06:19:26 +0000111};
112
Guillaume Naulta485c2b2017-04-24 14:16:07 +0200113static struct device_type l2tpeth_type = {
114 .name = "l2tpeth",
115};
116
James Chapmand9e31d12010-04-02 06:19:26 +0000117static void l2tp_eth_dev_setup(struct net_device *dev)
118{
Guillaume Naulta485c2b2017-04-24 14:16:07 +0200119 SET_NETDEV_DEVTYPE(dev, &l2tpeth_type);
James Chapmand9e31d12010-04-02 06:19:26 +0000120 ether_setup(dev);
Eric Dumazeta2842a12012-06-25 05:35:45 +0000121 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
122 dev->features |= NETIF_F_LLTX;
James Chapmand9e31d12010-04-02 06:19:26 +0000123 dev->netdev_ops = &l2tp_eth_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -0400124 dev->needs_free_netdev = true;
James Chapmand9e31d12010-04-02 06:19:26 +0000125}
126
127static void l2tp_eth_dev_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
128{
129 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
Guillaume Naultee28de62017-10-27 16:51:51 +0200130 struct net_device *dev;
131 struct l2tp_eth *priv;
James Chapmand9e31d12010-04-02 06:19:26 +0000132
133 if (session->debug & L2TP_MSG_DATA) {
134 unsigned int length;
James Chapmand9e31d12010-04-02 06:19:26 +0000135
136 length = min(32u, skb->len);
137 if (!pskb_may_pull(skb, length))
138 goto error;
139
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000140 pr_debug("%s: eth recv\n", session->name);
Eric Dumazeta2842a12012-06-25 05:35:45 +0000141 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmand9e31d12010-04-02 06:19:26 +0000142 }
143
Eric Dumazetc0cc88a2012-09-04 15:54:55 -0400144 if (!pskb_may_pull(skb, ETH_HLEN))
James Chapmand9e31d12010-04-02 06:19:26 +0000145 goto error;
146
147 secpath_reset(skb);
148
149 /* checksums verified by L2TP */
150 skb->ip_summed = CHECKSUM_NONE;
151
152 skb_dst_drop(skb);
Florian Westphal895b5c92019-09-29 20:54:03 +0200153 nf_reset_ct(skb);
James Chapmand9e31d12010-04-02 06:19:26 +0000154
Guillaume Naultee28de62017-10-27 16:51:51 +0200155 rcu_read_lock();
156 dev = rcu_dereference(spriv->dev);
157 if (!dev)
158 goto error_rcu;
159
160 priv = netdev_priv(dev);
James Chapmand9e31d12010-04-02 06:19:26 +0000161 if (dev_forward_skb(dev, skb) == NET_RX_SUCCESS) {
Eric Dumazeta2842a12012-06-25 05:35:45 +0000162 atomic_long_inc(&priv->rx_packets);
163 atomic_long_add(data_len, &priv->rx_bytes);
164 } else {
165 atomic_long_inc(&priv->rx_errors);
166 }
Guillaume Naultee28de62017-10-27 16:51:51 +0200167 rcu_read_unlock();
168
James Chapmand9e31d12010-04-02 06:19:26 +0000169 return;
170
Guillaume Naultee28de62017-10-27 16:51:51 +0200171error_rcu:
172 rcu_read_unlock();
James Chapmand9e31d12010-04-02 06:19:26 +0000173error:
James Chapmand9e31d12010-04-02 06:19:26 +0000174 kfree_skb(skb);
175}
176
177static void l2tp_eth_delete(struct l2tp_session *session)
178{
179 struct l2tp_eth_sess *spriv;
180 struct net_device *dev;
181
182 if (session) {
183 spriv = l2tp_session_priv(session);
Guillaume Naultee28de62017-10-27 16:51:51 +0200184
185 rtnl_lock();
186 dev = rtnl_dereference(spriv->dev);
James Chapmand9e31d12010-04-02 06:19:26 +0000187 if (dev) {
Guillaume Naultee28de62017-10-27 16:51:51 +0200188 unregister_netdevice(dev);
189 rtnl_unlock();
Eric Dumazeta06998b2012-06-07 00:07:20 +0000190 module_put(THIS_MODULE);
Guillaume Naultee28de62017-10-27 16:51:51 +0200191 } else {
192 rtnl_unlock();
James Chapmand9e31d12010-04-02 06:19:26 +0000193 }
194 }
195}
196
James Chapman0ad66142010-04-02 06:19:33 +0000197static void l2tp_eth_show(struct seq_file *m, void *arg)
198{
199 struct l2tp_session *session = arg;
200 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
Guillaume Naultee28de62017-10-27 16:51:51 +0200201 struct net_device *dev;
202
203 rcu_read_lock();
204 dev = rcu_dereference(spriv->dev);
205 if (!dev) {
206 rcu_read_unlock();
207 return;
208 }
209 dev_hold(dev);
210 rcu_read_unlock();
James Chapman0ad66142010-04-02 06:19:33 +0000211
212 seq_printf(m, " interface %s\n", dev->name);
Guillaume Naultee28de62017-10-27 16:51:51 +0200213
214 dev_put(dev);
James Chapman0ad66142010-04-02 06:19:33 +0000215}
James Chapman0ad66142010-04-02 06:19:33 +0000216
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700217static void l2tp_eth_adjust_mtu(struct l2tp_tunnel *tunnel,
218 struct l2tp_session *session,
219 struct net_device *dev)
220{
221 unsigned int overhead = 0;
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700222 u32 l3_overhead = 0;
Guillaume Nault1f5cd2a2018-08-03 12:38:34 +0200223 u32 mtu;
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700224
225 /* if the encap is UDP, account for UDP header size */
226 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
227 overhead += sizeof(struct udphdr);
228 dev->needed_headroom += sizeof(struct udphdr);
229 }
Guillaume Naulte9697e22018-08-03 12:38:39 +0200230
R. Parameswaran57240d02017-04-12 18:31:04 -0700231 lock_sock(tunnel->sock);
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700232 l3_overhead = kernel_sock_ip_overhead(tunnel->sock);
R. Parameswaran57240d02017-04-12 18:31:04 -0700233 release_sock(tunnel->sock);
Guillaume Naulte9697e22018-08-03 12:38:39 +0200234
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700235 if (l3_overhead == 0) {
236 /* L3 Overhead couldn't be identified, this could be
237 * because tunnel->sock was NULL or the socket's
238 * address family was not IPv4 or IPv6,
239 * dev mtu stays at 1500.
240 */
241 return;
242 }
243 /* Adjust MTU, factor overhead - underlay L3, overlay L2 hdr
244 * UDP overhead, if any, was already factored in above.
245 */
246 overhead += session->hdr_len + ETH_HLEN + l3_overhead;
247
Guillaume Naulte9697e22018-08-03 12:38:39 +0200248 mtu = l2tp_tunnel_dst_mtu(tunnel) - overhead;
249 if (mtu < dev->min_mtu || mtu > dev->max_mtu)
250 dev->mtu = ETH_DATA_LEN - overhead;
251 else
Guillaume Nault1f5cd2a2018-08-03 12:38:34 +0200252 dev->mtu = mtu;
Guillaume Naulte9697e22018-08-03 12:38:39 +0200253
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700254 dev->needed_headroom += session->hdr_len;
255}
256
Guillaume Naultf026bc22017-09-01 17:58:51 +0200257static int l2tp_eth_create(struct net *net, struct l2tp_tunnel *tunnel,
258 u32 session_id, u32 peer_session_id,
259 struct l2tp_session_cfg *cfg)
James Chapmand9e31d12010-04-02 06:19:26 +0000260{
Guillaume Naultc39855f2017-04-24 14:16:06 +0200261 unsigned char name_assign_type;
James Chapmand9e31d12010-04-02 06:19:26 +0000262 struct net_device *dev;
263 char name[IFNAMSIZ];
James Chapmand9e31d12010-04-02 06:19:26 +0000264 struct l2tp_session *session;
265 struct l2tp_eth *priv;
266 struct l2tp_eth_sess *spriv;
267 int rc;
James Chapmand9e31d12010-04-02 06:19:26 +0000268
James Chapmand9e31d12010-04-02 06:19:26 +0000269 if (cfg->ifname) {
James Chapmand9e31d12010-04-02 06:19:26 +0000270 strlcpy(name, cfg->ifname, IFNAMSIZ);
Guillaume Naultc39855f2017-04-24 14:16:06 +0200271 name_assign_type = NET_NAME_USER;
272 } else {
James Chapmand9e31d12010-04-02 06:19:26 +0000273 strcpy(name, L2TP_ETH_DEV_NAME);
Guillaume Naultc39855f2017-04-24 14:16:06 +0200274 name_assign_type = NET_NAME_ENUM;
275 }
James Chapmand9e31d12010-04-02 06:19:26 +0000276
277 session = l2tp_session_create(sizeof(*spriv), tunnel, session_id,
278 peer_session_id, cfg);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200279 if (IS_ERR(session)) {
280 rc = PTR_ERR(session);
Guillaume Naultee28de62017-10-27 16:51:51 +0200281 goto err;
Guillaume Nault3953ae72017-10-27 16:51:50 +0200282 }
283
Guillaume Naultc39855f2017-04-24 14:16:06 +0200284 dev = alloc_netdev(sizeof(*priv), name, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +0200285 l2tp_eth_dev_setup);
James Chapmand9e31d12010-04-02 06:19:26 +0000286 if (!dev) {
287 rc = -ENOMEM;
Guillaume Naultee28de62017-10-27 16:51:51 +0200288 goto err_sess;
James Chapmand9e31d12010-04-02 06:19:26 +0000289 }
290
291 dev_net_set(dev, net);
Jarod Wilson8b1efc02016-10-20 23:25:27 -0400292 dev->min_mtu = 0;
293 dev->max_mtu = ETH_MAX_MTU;
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700294 l2tp_eth_adjust_mtu(tunnel, session, dev);
James Chapmand9e31d12010-04-02 06:19:26 +0000295
296 priv = netdev_priv(dev);
James Chapmand9e31d12010-04-02 06:19:26 +0000297 priv->session = session;
James Chapmand9e31d12010-04-02 06:19:26 +0000298
James Chapmand9e31d12010-04-02 06:19:26 +0000299 session->recv_skb = l2tp_eth_dev_recv;
300 session->session_close = l2tp_eth_delete;
Arnd Bergmannc2ebc252018-08-13 23:43:05 +0200301 if (IS_ENABLED(CONFIG_L2TP_DEBUGFS))
302 session->show = l2tp_eth_show;
James Chapmand9e31d12010-04-02 06:19:26 +0000303
304 spriv = l2tp_session_priv(session);
James Chapmand9e31d12010-04-02 06:19:26 +0000305
Guillaume Naultee28de62017-10-27 16:51:51 +0200306 l2tp_session_inc_refcount(session);
James Chapmand9e31d12010-04-02 06:19:26 +0000307
Guillaume Naultee28de62017-10-27 16:51:51 +0200308 rtnl_lock();
309
310 /* Register both device and session while holding the rtnl lock. This
311 * ensures that l2tp_eth_delete() will see that there's a device to
312 * unregister, even if it happened to run before we assign spriv->dev.
313 */
314 rc = l2tp_session_register(session, tunnel);
315 if (rc < 0) {
316 rtnl_unlock();
317 goto err_sess_dev;
318 }
319
320 rc = register_netdevice(dev);
321 if (rc < 0) {
322 rtnl_unlock();
323 l2tp_session_delete(session);
324 l2tp_session_dec_refcount(session);
325 free_netdev(dev);
326
327 return rc;
328 }
329
James Chapmand9e31d12010-04-02 06:19:26 +0000330 strlcpy(session->ifname, dev->name, IFNAMSIZ);
Guillaume Naultee28de62017-10-27 16:51:51 +0200331 rcu_assign_pointer(spriv->dev, dev);
332
333 rtnl_unlock();
334
Guillaume Nault3953ae72017-10-27 16:51:50 +0200335 l2tp_session_dec_refcount(session);
James Chapmand9e31d12010-04-02 06:19:26 +0000336
Guillaume Naultee28de62017-10-27 16:51:51 +0200337 __module_get(THIS_MODULE);
James Chapmand9e31d12010-04-02 06:19:26 +0000338
339 return 0;
340
Guillaume Naultee28de62017-10-27 16:51:51 +0200341err_sess_dev:
Guillaume Nault3953ae72017-10-27 16:51:50 +0200342 l2tp_session_dec_refcount(session);
Guillaume Naultee28de62017-10-27 16:51:51 +0200343 free_netdev(dev);
344err_sess:
345 kfree(session);
346err:
James Chapmand9e31d12010-04-02 06:19:26 +0000347 return rc;
348}
349
James Chapmand9e31d12010-04-02 06:19:26 +0000350
351static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops = {
352 .session_create = l2tp_eth_create,
353 .session_delete = l2tp_session_delete,
354};
355
356
357static int __init l2tp_eth_init(void)
358{
359 int err = 0;
360
361 err = l2tp_nl_register_ops(L2TP_PWTYPE_ETH, &l2tp_eth_nl_cmd_ops);
362 if (err)
Guillaume Nault9f775ea2017-09-28 15:44:38 +0200363 goto err;
James Chapmand9e31d12010-04-02 06:19:26 +0000364
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000365 pr_info("L2TP ethernet pseudowire support (L2TPv3)\n");
James Chapmand9e31d12010-04-02 06:19:26 +0000366
367 return 0;
368
Guillaume Nault9f775ea2017-09-28 15:44:38 +0200369err:
James Chapmand9e31d12010-04-02 06:19:26 +0000370 return err;
371}
372
373static void __exit l2tp_eth_exit(void)
374{
James Chapmand9e31d12010-04-02 06:19:26 +0000375 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH);
376}
377
378module_init(l2tp_eth_init);
379module_exit(l2tp_eth_exit);
380
381MODULE_LICENSE("GPL");
382MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
383MODULE_DESCRIPTION("L2TP ethernet pseudowire driver");
384MODULE_VERSION("1.0");
stephen hemmingerf1f39f92015-09-23 21:33:34 -0700385MODULE_ALIAS_L2TP_PWTYPE(5);