blob: bd3f39349d4084021d2bdc1bd385f877b4a845e3 [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 Dumazetd3fff6c2016-06-09 07:45:12 -070059 netdev_lockdep_set_classes(dev);
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -070060
James Chapmand9e31d12010-04-02 06:19:26 +000061 return 0;
62}
63
64static void l2tp_eth_dev_uninit(struct net_device *dev)
65{
Guillaume Naultee28de62017-10-27 16:51:51 +020066 struct l2tp_eth *priv = netdev_priv(dev);
67 struct l2tp_eth_sess *spriv;
68
69 spriv = l2tp_session_priv(priv->session);
70 RCU_INIT_POINTER(spriv->dev, NULL);
71 /* No need for synchronize_net() here. We're called by
72 * unregister_netdev*(), which does the synchronisation for us.
73 */
James Chapmand9e31d12010-04-02 06:19:26 +000074}
75
76static int l2tp_eth_dev_xmit(struct sk_buff *skb, struct net_device *dev)
77{
78 struct l2tp_eth *priv = netdev_priv(dev);
79 struct l2tp_session *session = priv->session;
Eric Dumazetb8c84302012-06-28 20:15:13 +000080 unsigned int len = skb->len;
81 int ret = l2tp_xmit_skb(session, skb, session->hdr_len);
James Chapmand9e31d12010-04-02 06:19:26 +000082
WANG Conga4cd0272016-11-21 23:24:43 -080083 if (likely(ret == NET_XMIT_SUCCESS)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +000084 atomic_long_add(len, &priv->tx_bytes);
85 atomic_long_inc(&priv->tx_packets);
86 } else {
87 atomic_long_inc(&priv->tx_dropped);
88 }
Eric Dumazetaa214de02012-06-25 00:45:14 +000089 return NETDEV_TX_OK;
James Chapmand9e31d12010-04-02 06:19:26 +000090}
91
stephen hemmingerbc1f4472017-01-06 19:12:52 -080092static void l2tp_eth_get_stats64(struct net_device *dev,
93 struct rtnl_link_stats64 *stats)
Eric Dumazeta2842a12012-06-25 05:35:45 +000094{
95 struct l2tp_eth *priv = netdev_priv(dev);
96
Dominik Heidler9b3dc0a2017-06-09 16:29:47 +020097 stats->tx_bytes = (unsigned long) atomic_long_read(&priv->tx_bytes);
98 stats->tx_packets = (unsigned long) atomic_long_read(&priv->tx_packets);
99 stats->tx_dropped = (unsigned long) atomic_long_read(&priv->tx_dropped);
100 stats->rx_bytes = (unsigned long) atomic_long_read(&priv->rx_bytes);
101 stats->rx_packets = (unsigned long) atomic_long_read(&priv->rx_packets);
102 stats->rx_errors = (unsigned long) atomic_long_read(&priv->rx_errors);
103
Eric Dumazeta2842a12012-06-25 05:35:45 +0000104}
105
Julia Lawalleb947372016-09-15 22:23:26 +0200106static const struct net_device_ops l2tp_eth_netdev_ops = {
James Chapmand9e31d12010-04-02 06:19:26 +0000107 .ndo_init = l2tp_eth_dev_init,
108 .ndo_uninit = l2tp_eth_dev_uninit,
109 .ndo_start_xmit = l2tp_eth_dev_xmit,
Eric Dumazeta2842a12012-06-25 05:35:45 +0000110 .ndo_get_stats64 = l2tp_eth_get_stats64,
Alexander Couzensfe159122014-11-19 13:24:39 +0100111 .ndo_set_mac_address = eth_mac_addr,
James Chapmand9e31d12010-04-02 06:19:26 +0000112};
113
Guillaume Naulta485c2b2017-04-24 14:16:07 +0200114static struct device_type l2tpeth_type = {
115 .name = "l2tpeth",
116};
117
James Chapmand9e31d12010-04-02 06:19:26 +0000118static void l2tp_eth_dev_setup(struct net_device *dev)
119{
Guillaume Naulta485c2b2017-04-24 14:16:07 +0200120 SET_NETDEV_DEVTYPE(dev, &l2tpeth_type);
James Chapmand9e31d12010-04-02 06:19:26 +0000121 ether_setup(dev);
Eric Dumazeta2842a12012-06-25 05:35:45 +0000122 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
123 dev->features |= NETIF_F_LLTX;
James Chapmand9e31d12010-04-02 06:19:26 +0000124 dev->netdev_ops = &l2tp_eth_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -0400125 dev->needs_free_netdev = true;
James Chapmand9e31d12010-04-02 06:19:26 +0000126}
127
128static void l2tp_eth_dev_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
129{
130 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
Guillaume Naultee28de62017-10-27 16:51:51 +0200131 struct net_device *dev;
132 struct l2tp_eth *priv;
James Chapmand9e31d12010-04-02 06:19:26 +0000133
134 if (session->debug & L2TP_MSG_DATA) {
135 unsigned int length;
James Chapmand9e31d12010-04-02 06:19:26 +0000136
137 length = min(32u, skb->len);
138 if (!pskb_may_pull(skb, length))
139 goto error;
140
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000141 pr_debug("%s: eth recv\n", session->name);
Eric Dumazeta2842a12012-06-25 05:35:45 +0000142 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmand9e31d12010-04-02 06:19:26 +0000143 }
144
Eric Dumazetc0cc88a2012-09-04 15:54:55 -0400145 if (!pskb_may_pull(skb, ETH_HLEN))
James Chapmand9e31d12010-04-02 06:19:26 +0000146 goto error;
147
148 secpath_reset(skb);
149
150 /* checksums verified by L2TP */
151 skb->ip_summed = CHECKSUM_NONE;
152
153 skb_dst_drop(skb);
154 nf_reset(skb);
155
Guillaume Naultee28de62017-10-27 16:51:51 +0200156 rcu_read_lock();
157 dev = rcu_dereference(spriv->dev);
158 if (!dev)
159 goto error_rcu;
160
161 priv = netdev_priv(dev);
James Chapmand9e31d12010-04-02 06:19:26 +0000162 if (dev_forward_skb(dev, skb) == NET_RX_SUCCESS) {
Eric Dumazeta2842a12012-06-25 05:35:45 +0000163 atomic_long_inc(&priv->rx_packets);
164 atomic_long_add(data_len, &priv->rx_bytes);
165 } else {
166 atomic_long_inc(&priv->rx_errors);
167 }
Guillaume Naultee28de62017-10-27 16:51:51 +0200168 rcu_read_unlock();
169
James Chapmand9e31d12010-04-02 06:19:26 +0000170 return;
171
Guillaume Naultee28de62017-10-27 16:51:51 +0200172error_rcu:
173 rcu_read_unlock();
James Chapmand9e31d12010-04-02 06:19:26 +0000174error:
James Chapmand9e31d12010-04-02 06:19:26 +0000175 kfree_skb(skb);
176}
177
178static void l2tp_eth_delete(struct l2tp_session *session)
179{
180 struct l2tp_eth_sess *spriv;
181 struct net_device *dev;
182
183 if (session) {
184 spriv = l2tp_session_priv(session);
Guillaume Naultee28de62017-10-27 16:51:51 +0200185
186 rtnl_lock();
187 dev = rtnl_dereference(spriv->dev);
James Chapmand9e31d12010-04-02 06:19:26 +0000188 if (dev) {
Guillaume Naultee28de62017-10-27 16:51:51 +0200189 unregister_netdevice(dev);
190 rtnl_unlock();
Eric Dumazeta06998b2012-06-07 00:07:20 +0000191 module_put(THIS_MODULE);
Guillaume Naultee28de62017-10-27 16:51:51 +0200192 } else {
193 rtnl_unlock();
James Chapmand9e31d12010-04-02 06:19:26 +0000194 }
195 }
196}
197
James Chapman0ad66142010-04-02 06:19:33 +0000198static void l2tp_eth_show(struct seq_file *m, void *arg)
199{
200 struct l2tp_session *session = arg;
201 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
Guillaume Naultee28de62017-10-27 16:51:51 +0200202 struct net_device *dev;
203
204 rcu_read_lock();
205 dev = rcu_dereference(spriv->dev);
206 if (!dev) {
207 rcu_read_unlock();
208 return;
209 }
210 dev_hold(dev);
211 rcu_read_unlock();
James Chapman0ad66142010-04-02 06:19:33 +0000212
213 seq_printf(m, " interface %s\n", dev->name);
Guillaume Naultee28de62017-10-27 16:51:51 +0200214
215 dev_put(dev);
James Chapman0ad66142010-04-02 06:19:33 +0000216}
James Chapman0ad66142010-04-02 06:19:33 +0000217
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700218static void l2tp_eth_adjust_mtu(struct l2tp_tunnel *tunnel,
219 struct l2tp_session *session,
220 struct net_device *dev)
221{
222 unsigned int overhead = 0;
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700223 u32 l3_overhead = 0;
Guillaume Nault1f5cd2a2018-08-03 12:38:34 +0200224 u32 mtu;
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700225
226 /* if the encap is UDP, account for UDP header size */
227 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
228 overhead += sizeof(struct udphdr);
229 dev->needed_headroom += sizeof(struct udphdr);
230 }
Guillaume Naulte9697e22018-08-03 12:38:39 +0200231
R. Parameswaran57240d02017-04-12 18:31:04 -0700232 lock_sock(tunnel->sock);
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700233 l3_overhead = kernel_sock_ip_overhead(tunnel->sock);
R. Parameswaran57240d02017-04-12 18:31:04 -0700234 release_sock(tunnel->sock);
Guillaume Naulte9697e22018-08-03 12:38:39 +0200235
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700236 if (l3_overhead == 0) {
237 /* L3 Overhead couldn't be identified, this could be
238 * because tunnel->sock was NULL or the socket's
239 * address family was not IPv4 or IPv6,
240 * dev mtu stays at 1500.
241 */
242 return;
243 }
244 /* Adjust MTU, factor overhead - underlay L3, overlay L2 hdr
245 * UDP overhead, if any, was already factored in above.
246 */
247 overhead += session->hdr_len + ETH_HLEN + l3_overhead;
248
Guillaume Naulte9697e22018-08-03 12:38:39 +0200249 mtu = l2tp_tunnel_dst_mtu(tunnel) - overhead;
250 if (mtu < dev->min_mtu || mtu > dev->max_mtu)
251 dev->mtu = ETH_DATA_LEN - overhead;
252 else
Guillaume Nault1f5cd2a2018-08-03 12:38:34 +0200253 dev->mtu = mtu;
Guillaume Naulte9697e22018-08-03 12:38:39 +0200254
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700255 dev->needed_headroom += session->hdr_len;
256}
257
Guillaume Naultf026bc22017-09-01 17:58:51 +0200258static int l2tp_eth_create(struct net *net, struct l2tp_tunnel *tunnel,
259 u32 session_id, u32 peer_session_id,
260 struct l2tp_session_cfg *cfg)
James Chapmand9e31d12010-04-02 06:19:26 +0000261{
Guillaume Naultc39855f2017-04-24 14:16:06 +0200262 unsigned char name_assign_type;
James Chapmand9e31d12010-04-02 06:19:26 +0000263 struct net_device *dev;
264 char name[IFNAMSIZ];
James Chapmand9e31d12010-04-02 06:19:26 +0000265 struct l2tp_session *session;
266 struct l2tp_eth *priv;
267 struct l2tp_eth_sess *spriv;
268 int rc;
James Chapmand9e31d12010-04-02 06:19:26 +0000269
James Chapmand9e31d12010-04-02 06:19:26 +0000270 if (cfg->ifname) {
James Chapmand9e31d12010-04-02 06:19:26 +0000271 strlcpy(name, cfg->ifname, IFNAMSIZ);
Guillaume Naultc39855f2017-04-24 14:16:06 +0200272 name_assign_type = NET_NAME_USER;
273 } else {
James Chapmand9e31d12010-04-02 06:19:26 +0000274 strcpy(name, L2TP_ETH_DEV_NAME);
Guillaume Naultc39855f2017-04-24 14:16:06 +0200275 name_assign_type = NET_NAME_ENUM;
276 }
James Chapmand9e31d12010-04-02 06:19:26 +0000277
278 session = l2tp_session_create(sizeof(*spriv), tunnel, session_id,
279 peer_session_id, cfg);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200280 if (IS_ERR(session)) {
281 rc = PTR_ERR(session);
Guillaume Naultee28de62017-10-27 16:51:51 +0200282 goto err;
Guillaume Nault3953ae72017-10-27 16:51:50 +0200283 }
284
Guillaume Naultc39855f2017-04-24 14:16:06 +0200285 dev = alloc_netdev(sizeof(*priv), name, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +0200286 l2tp_eth_dev_setup);
James Chapmand9e31d12010-04-02 06:19:26 +0000287 if (!dev) {
288 rc = -ENOMEM;
Guillaume Naultee28de62017-10-27 16:51:51 +0200289 goto err_sess;
James Chapmand9e31d12010-04-02 06:19:26 +0000290 }
291
292 dev_net_set(dev, net);
Jarod Wilson8b1efc02016-10-20 23:25:27 -0400293 dev->min_mtu = 0;
294 dev->max_mtu = ETH_MAX_MTU;
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700295 l2tp_eth_adjust_mtu(tunnel, session, dev);
James Chapmand9e31d12010-04-02 06:19:26 +0000296
297 priv = netdev_priv(dev);
James Chapmand9e31d12010-04-02 06:19:26 +0000298 priv->session = session;
James Chapmand9e31d12010-04-02 06:19:26 +0000299
James Chapmand9e31d12010-04-02 06:19:26 +0000300 session->recv_skb = l2tp_eth_dev_recv;
301 session->session_close = l2tp_eth_delete;
Arnd Bergmannc2ebc252018-08-13 23:43:05 +0200302 if (IS_ENABLED(CONFIG_L2TP_DEBUGFS))
303 session->show = l2tp_eth_show;
James Chapmand9e31d12010-04-02 06:19:26 +0000304
305 spriv = l2tp_session_priv(session);
James Chapmand9e31d12010-04-02 06:19:26 +0000306
Guillaume Naultee28de62017-10-27 16:51:51 +0200307 l2tp_session_inc_refcount(session);
James Chapmand9e31d12010-04-02 06:19:26 +0000308
Guillaume Naultee28de62017-10-27 16:51:51 +0200309 rtnl_lock();
310
311 /* Register both device and session while holding the rtnl lock. This
312 * ensures that l2tp_eth_delete() will see that there's a device to
313 * unregister, even if it happened to run before we assign spriv->dev.
314 */
315 rc = l2tp_session_register(session, tunnel);
316 if (rc < 0) {
317 rtnl_unlock();
318 goto err_sess_dev;
319 }
320
321 rc = register_netdevice(dev);
322 if (rc < 0) {
323 rtnl_unlock();
324 l2tp_session_delete(session);
325 l2tp_session_dec_refcount(session);
326 free_netdev(dev);
327
328 return rc;
329 }
330
James Chapmand9e31d12010-04-02 06:19:26 +0000331 strlcpy(session->ifname, dev->name, IFNAMSIZ);
Guillaume Naultee28de62017-10-27 16:51:51 +0200332 rcu_assign_pointer(spriv->dev, dev);
333
334 rtnl_unlock();
335
Guillaume Nault3953ae72017-10-27 16:51:50 +0200336 l2tp_session_dec_refcount(session);
James Chapmand9e31d12010-04-02 06:19:26 +0000337
Guillaume Naultee28de62017-10-27 16:51:51 +0200338 __module_get(THIS_MODULE);
James Chapmand9e31d12010-04-02 06:19:26 +0000339
340 return 0;
341
Guillaume Naultee28de62017-10-27 16:51:51 +0200342err_sess_dev:
Guillaume Nault3953ae72017-10-27 16:51:50 +0200343 l2tp_session_dec_refcount(session);
Guillaume Naultee28de62017-10-27 16:51:51 +0200344 free_netdev(dev);
345err_sess:
346 kfree(session);
347err:
James Chapmand9e31d12010-04-02 06:19:26 +0000348 return rc;
349}
350
James Chapmand9e31d12010-04-02 06:19:26 +0000351
352static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops = {
353 .session_create = l2tp_eth_create,
354 .session_delete = l2tp_session_delete,
355};
356
357
358static int __init l2tp_eth_init(void)
359{
360 int err = 0;
361
362 err = l2tp_nl_register_ops(L2TP_PWTYPE_ETH, &l2tp_eth_nl_cmd_ops);
363 if (err)
Guillaume Nault9f775ea2017-09-28 15:44:38 +0200364 goto err;
James Chapmand9e31d12010-04-02 06:19:26 +0000365
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000366 pr_info("L2TP ethernet pseudowire support (L2TPv3)\n");
James Chapmand9e31d12010-04-02 06:19:26 +0000367
368 return 0;
369
Guillaume Nault9f775ea2017-09-28 15:44:38 +0200370err:
James Chapmand9e31d12010-04-02 06:19:26 +0000371 return err;
372}
373
374static void __exit l2tp_eth_exit(void)
375{
James Chapmand9e31d12010-04-02 06:19:26 +0000376 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH);
377}
378
379module_init(l2tp_eth_init);
380module_exit(l2tp_eth_exit);
381
382MODULE_LICENSE("GPL");
383MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
384MODULE_DESCRIPTION("L2TP ethernet pseudowire driver");
385MODULE_VERSION("1.0");
stephen hemmingerf1f39f92015-09-23 21:33:34 -0700386MODULE_ALIAS_L2TP_PWTYPE(5);