blob: 8b21af7321b928b4dcc5d7af3a6667380e9a949a [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 {
44 struct net_device *dev;
45 struct sock *tunnel_sock;
46 struct l2tp_session *session;
47 struct list_head list;
Eric Dumazeta2842a12012-06-25 05:35:45 +000048 atomic_long_t tx_bytes;
49 atomic_long_t tx_packets;
Eric Dumazetb8c84302012-06-28 20:15:13 +000050 atomic_long_t tx_dropped;
Eric Dumazeta2842a12012-06-25 05:35:45 +000051 atomic_long_t rx_bytes;
52 atomic_long_t rx_packets;
53 atomic_long_t rx_errors;
James Chapmand9e31d12010-04-02 06:19:26 +000054};
55
56/* via l2tp_session_priv() */
57struct l2tp_eth_sess {
58 struct net_device *dev;
59};
60
61/* per-net private data for this module */
62static unsigned int l2tp_eth_net_id;
63struct l2tp_eth_net {
64 struct list_head l2tp_eth_dev_list;
65 spinlock_t l2tp_eth_lock;
66};
67
68static inline struct l2tp_eth_net *l2tp_eth_pernet(struct net *net)
69{
70 return net_generic(net, l2tp_eth_net_id);
71}
72
73static int l2tp_eth_dev_init(struct net_device *dev)
74{
75 struct l2tp_eth *priv = netdev_priv(dev);
76
77 priv->dev = dev;
Danny Kukawkaf2cedb62012-02-15 06:45:39 +000078 eth_hw_addr_random(dev);
Joe Perches1cea7e22015-03-02 19:54:59 -080079 eth_broadcast_addr(dev->broadcast);
Eric Dumazetd3fff6c2016-06-09 07:45:12 -070080 netdev_lockdep_set_classes(dev);
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -070081
James Chapmand9e31d12010-04-02 06:19:26 +000082 return 0;
83}
84
85static void l2tp_eth_dev_uninit(struct net_device *dev)
86{
87 struct l2tp_eth *priv = netdev_priv(dev);
88 struct l2tp_eth_net *pn = l2tp_eth_pernet(dev_net(dev));
89
90 spin_lock(&pn->l2tp_eth_lock);
91 list_del_init(&priv->list);
92 spin_unlock(&pn->l2tp_eth_lock);
93 dev_put(dev);
94}
95
96static int l2tp_eth_dev_xmit(struct sk_buff *skb, struct net_device *dev)
97{
98 struct l2tp_eth *priv = netdev_priv(dev);
99 struct l2tp_session *session = priv->session;
Eric Dumazetb8c84302012-06-28 20:15:13 +0000100 unsigned int len = skb->len;
101 int ret = l2tp_xmit_skb(session, skb, session->hdr_len);
James Chapmand9e31d12010-04-02 06:19:26 +0000102
WANG Conga4cd0272016-11-21 23:24:43 -0800103 if (likely(ret == NET_XMIT_SUCCESS)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +0000104 atomic_long_add(len, &priv->tx_bytes);
105 atomic_long_inc(&priv->tx_packets);
106 } else {
107 atomic_long_inc(&priv->tx_dropped);
108 }
Eric Dumazetaa214de02012-06-25 00:45:14 +0000109 return NETDEV_TX_OK;
James Chapmand9e31d12010-04-02 06:19:26 +0000110}
111
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800112static void l2tp_eth_get_stats64(struct net_device *dev,
113 struct rtnl_link_stats64 *stats)
Eric Dumazeta2842a12012-06-25 05:35:45 +0000114{
115 struct l2tp_eth *priv = netdev_priv(dev);
116
117 stats->tx_bytes = atomic_long_read(&priv->tx_bytes);
118 stats->tx_packets = atomic_long_read(&priv->tx_packets);
Eric Dumazetb8c84302012-06-28 20:15:13 +0000119 stats->tx_dropped = atomic_long_read(&priv->tx_dropped);
Eric Dumazeta2842a12012-06-25 05:35:45 +0000120 stats->rx_bytes = atomic_long_read(&priv->rx_bytes);
121 stats->rx_packets = atomic_long_read(&priv->rx_packets);
122 stats->rx_errors = atomic_long_read(&priv->rx_errors);
Eric Dumazeta2842a12012-06-25 05:35:45 +0000123}
124
Julia Lawalleb947372016-09-15 22:23:26 +0200125static const struct net_device_ops l2tp_eth_netdev_ops = {
James Chapmand9e31d12010-04-02 06:19:26 +0000126 .ndo_init = l2tp_eth_dev_init,
127 .ndo_uninit = l2tp_eth_dev_uninit,
128 .ndo_start_xmit = l2tp_eth_dev_xmit,
Eric Dumazeta2842a12012-06-25 05:35:45 +0000129 .ndo_get_stats64 = l2tp_eth_get_stats64,
Alexander Couzensfe159122014-11-19 13:24:39 +0100130 .ndo_set_mac_address = eth_mac_addr,
James Chapmand9e31d12010-04-02 06:19:26 +0000131};
132
Guillaume Naulta485c2b2017-04-24 14:16:07 +0200133static struct device_type l2tpeth_type = {
134 .name = "l2tpeth",
135};
136
James Chapmand9e31d12010-04-02 06:19:26 +0000137static void l2tp_eth_dev_setup(struct net_device *dev)
138{
Guillaume Naulta485c2b2017-04-24 14:16:07 +0200139 SET_NETDEV_DEVTYPE(dev, &l2tpeth_type);
James Chapmand9e31d12010-04-02 06:19:26 +0000140 ether_setup(dev);
Eric Dumazeta2842a12012-06-25 05:35:45 +0000141 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
142 dev->features |= NETIF_F_LLTX;
James Chapmand9e31d12010-04-02 06:19:26 +0000143 dev->netdev_ops = &l2tp_eth_netdev_ops;
144 dev->destructor = free_netdev;
145}
146
147static void l2tp_eth_dev_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
148{
149 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
150 struct net_device *dev = spriv->dev;
Eric Dumazeta2842a12012-06-25 05:35:45 +0000151 struct l2tp_eth *priv = netdev_priv(dev);
James Chapmand9e31d12010-04-02 06:19:26 +0000152
153 if (session->debug & L2TP_MSG_DATA) {
154 unsigned int length;
James Chapmand9e31d12010-04-02 06:19:26 +0000155
156 length = min(32u, skb->len);
157 if (!pskb_may_pull(skb, length))
158 goto error;
159
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000160 pr_debug("%s: eth recv\n", session->name);
Eric Dumazeta2842a12012-06-25 05:35:45 +0000161 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmand9e31d12010-04-02 06:19:26 +0000162 }
163
Eric Dumazetc0cc88a2012-09-04 15:54:55 -0400164 if (!pskb_may_pull(skb, ETH_HLEN))
James Chapmand9e31d12010-04-02 06:19:26 +0000165 goto error;
166
167 secpath_reset(skb);
168
169 /* checksums verified by L2TP */
170 skb->ip_summed = CHECKSUM_NONE;
171
172 skb_dst_drop(skb);
173 nf_reset(skb);
174
175 if (dev_forward_skb(dev, skb) == NET_RX_SUCCESS) {
Eric Dumazeta2842a12012-06-25 05:35:45 +0000176 atomic_long_inc(&priv->rx_packets);
177 atomic_long_add(data_len, &priv->rx_bytes);
178 } else {
179 atomic_long_inc(&priv->rx_errors);
180 }
James Chapmand9e31d12010-04-02 06:19:26 +0000181 return;
182
183error:
Eric Dumazeta2842a12012-06-25 05:35:45 +0000184 atomic_long_inc(&priv->rx_errors);
James Chapmand9e31d12010-04-02 06:19:26 +0000185 kfree_skb(skb);
186}
187
188static void l2tp_eth_delete(struct l2tp_session *session)
189{
190 struct l2tp_eth_sess *spriv;
191 struct net_device *dev;
192
193 if (session) {
194 spriv = l2tp_session_priv(session);
195 dev = spriv->dev;
196 if (dev) {
197 unregister_netdev(dev);
198 spriv->dev = NULL;
Eric Dumazeta06998b2012-06-07 00:07:20 +0000199 module_put(THIS_MODULE);
James Chapmand9e31d12010-04-02 06:19:26 +0000200 }
201 }
202}
203
Javier Martinez Canillas9dd79942016-09-09 08:43:17 -0400204#if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
James Chapman0ad66142010-04-02 06:19:33 +0000205static void l2tp_eth_show(struct seq_file *m, void *arg)
206{
207 struct l2tp_session *session = arg;
208 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
209 struct net_device *dev = spriv->dev;
210
211 seq_printf(m, " interface %s\n", dev->name);
212}
213#endif
214
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700215static void l2tp_eth_adjust_mtu(struct l2tp_tunnel *tunnel,
216 struct l2tp_session *session,
217 struct net_device *dev)
218{
219 unsigned int overhead = 0;
220 struct dst_entry *dst;
221 u32 l3_overhead = 0;
222
223 /* if the encap is UDP, account for UDP header size */
224 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
225 overhead += sizeof(struct udphdr);
226 dev->needed_headroom += sizeof(struct udphdr);
227 }
228 if (session->mtu != 0) {
229 dev->mtu = session->mtu;
230 dev->needed_headroom += session->hdr_len;
231 return;
232 }
R. Parameswaran57240d02017-04-12 18:31:04 -0700233 lock_sock(tunnel->sock);
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700234 l3_overhead = kernel_sock_ip_overhead(tunnel->sock);
R. Parameswaran57240d02017-04-12 18:31:04 -0700235 release_sock(tunnel->sock);
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
249 /* If PMTU discovery was enabled, use discovered MTU on L2TP device */
250 dst = sk_dst_get(tunnel->sock);
251 if (dst) {
252 /* dst_mtu will use PMTU if found, else fallback to intf MTU */
253 u32 pmtu = dst_mtu(dst);
254
255 if (pmtu != 0)
256 dev->mtu = pmtu;
257 dst_release(dst);
258 }
259 session->mtu = dev->mtu - overhead;
260 dev->mtu = session->mtu;
261 dev->needed_headroom += session->hdr_len;
262}
263
James Chapmand9e31d12010-04-02 06:19:26 +0000264static int l2tp_eth_create(struct net *net, u32 tunnel_id, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
265{
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];
269 struct l2tp_tunnel *tunnel;
270 struct l2tp_session *session;
271 struct l2tp_eth *priv;
272 struct l2tp_eth_sess *spriv;
273 int rc;
274 struct l2tp_eth_net *pn;
275
276 tunnel = l2tp_tunnel_find(net, tunnel_id);
277 if (!tunnel) {
278 rc = -ENODEV;
279 goto out;
280 }
281
James Chapmand9e31d12010-04-02 06:19:26 +0000282 if (cfg->ifname) {
James Chapmand9e31d12010-04-02 06:19:26 +0000283 strlcpy(name, cfg->ifname, IFNAMSIZ);
Guillaume Naultc39855f2017-04-24 14:16:06 +0200284 name_assign_type = NET_NAME_USER;
285 } else {
James Chapmand9e31d12010-04-02 06:19:26 +0000286 strcpy(name, L2TP_ETH_DEV_NAME);
Guillaume Naultc39855f2017-04-24 14:16:06 +0200287 name_assign_type = NET_NAME_ENUM;
288 }
James Chapmand9e31d12010-04-02 06:19:26 +0000289
290 session = l2tp_session_create(sizeof(*spriv), tunnel, session_id,
291 peer_session_id, cfg);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200292 if (IS_ERR(session)) {
293 rc = PTR_ERR(session);
James Chapmand9e31d12010-04-02 06:19:26 +0000294 goto out;
295 }
296
Guillaume Naultc39855f2017-04-24 14:16:06 +0200297 dev = alloc_netdev(sizeof(*priv), name, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +0200298 l2tp_eth_dev_setup);
James Chapmand9e31d12010-04-02 06:19:26 +0000299 if (!dev) {
300 rc = -ENOMEM;
301 goto out_del_session;
302 }
303
304 dev_net_set(dev, net);
Jarod Wilson8b1efc02016-10-20 23:25:27 -0400305 dev->min_mtu = 0;
306 dev->max_mtu = ETH_MAX_MTU;
R. Parameswaranb784e7e2017-04-05 17:00:07 -0700307 l2tp_eth_adjust_mtu(tunnel, session, dev);
James Chapmand9e31d12010-04-02 06:19:26 +0000308
309 priv = netdev_priv(dev);
310 priv->dev = dev;
311 priv->session = session;
312 INIT_LIST_HEAD(&priv->list);
313
314 priv->tunnel_sock = tunnel->sock;
315 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);
322 spriv->dev = dev;
323
324 rc = register_netdev(dev);
325 if (rc < 0)
326 goto out_del_dev;
327
Eric Dumazeta06998b2012-06-07 00:07:20 +0000328 __module_get(THIS_MODULE);
James Chapmand9e31d12010-04-02 06:19:26 +0000329 /* Must be done after register_netdev() */
330 strlcpy(session->ifname, dev->name, IFNAMSIZ);
331
332 dev_hold(dev);
333 pn = l2tp_eth_pernet(dev_net(dev));
334 spin_lock(&pn->l2tp_eth_lock);
335 list_add(&priv->list, &pn->l2tp_eth_dev_list);
336 spin_unlock(&pn->l2tp_eth_lock);
337
338 return 0;
339
340out_del_dev:
341 free_netdev(dev);
Tom Parkin78933632012-10-29 23:41:48 +0000342 spriv->dev = NULL;
James Chapmand9e31d12010-04-02 06:19:26 +0000343out_del_session:
344 l2tp_session_delete(session);
345out:
346 return rc;
347}
348
349static __net_init int l2tp_eth_init_net(struct net *net)
350{
Jiri Pirko3a737022010-04-23 01:01:52 +0000351 struct l2tp_eth_net *pn = net_generic(net, l2tp_eth_net_id);
James Chapmand9e31d12010-04-02 06:19:26 +0000352
353 INIT_LIST_HEAD(&pn->l2tp_eth_dev_list);
354 spin_lock_init(&pn->l2tp_eth_lock);
355
James Chapmand9e31d12010-04-02 06:19:26 +0000356 return 0;
James Chapmand9e31d12010-04-02 06:19:26 +0000357}
358
James Chapman8aa525a2011-03-21 18:10:25 -0700359static struct pernet_operations l2tp_eth_net_ops = {
James Chapmand9e31d12010-04-02 06:19:26 +0000360 .init = l2tp_eth_init_net,
James Chapmand9e31d12010-04-02 06:19:26 +0000361 .id = &l2tp_eth_net_id,
362 .size = sizeof(struct l2tp_eth_net),
363};
364
365
366static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops = {
367 .session_create = l2tp_eth_create,
368 .session_delete = l2tp_session_delete,
369};
370
371
372static int __init l2tp_eth_init(void)
373{
374 int err = 0;
375
376 err = l2tp_nl_register_ops(L2TP_PWTYPE_ETH, &l2tp_eth_nl_cmd_ops);
377 if (err)
378 goto out;
379
380 err = register_pernet_device(&l2tp_eth_net_ops);
381 if (err)
382 goto out_unreg;
383
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000384 pr_info("L2TP ethernet pseudowire support (L2TPv3)\n");
James Chapmand9e31d12010-04-02 06:19:26 +0000385
386 return 0;
387
388out_unreg:
389 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH);
390out:
391 return err;
392}
393
394static void __exit l2tp_eth_exit(void)
395{
396 unregister_pernet_device(&l2tp_eth_net_ops);
397 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH);
398}
399
400module_init(l2tp_eth_init);
401module_exit(l2tp_eth_exit);
402
403MODULE_LICENSE("GPL");
404MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
405MODULE_DESCRIPTION("L2TP ethernet pseudowire driver");
406MODULE_VERSION("1.0");
stephen hemmingerf1f39f92015-09-23 21:33:34 -0700407MODULE_ALIAS_L2TP_PWTYPE(5);