blob: c00d72d182fa7be85759fce3ac88874b3cbb04af [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>
33
34#include "l2tp_core.h"
35
36/* Default device name. May be overridden by name specified by user */
37#define L2TP_ETH_DEV_NAME "l2tpeth%d"
38
39/* via netdev_priv() */
40struct l2tp_eth {
41 struct net_device *dev;
42 struct sock *tunnel_sock;
43 struct l2tp_session *session;
44 struct list_head list;
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 {
55 struct net_device *dev;
56};
57
58/* per-net private data for this module */
59static unsigned int l2tp_eth_net_id;
60struct l2tp_eth_net {
61 struct list_head l2tp_eth_dev_list;
62 spinlock_t l2tp_eth_lock;
63};
64
65static inline struct l2tp_eth_net *l2tp_eth_pernet(struct net *net)
66{
67 return net_generic(net, l2tp_eth_net_id);
68}
69
Eric Dumazet23d3b8b2012-09-05 01:02:56 +000070static struct lock_class_key l2tp_eth_tx_busylock;
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -070071static struct lock_class_key l2tp_qdisc_running_key;
72
James Chapmand9e31d12010-04-02 06:19:26 +000073static 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 Dumazet23d3b8b2012-09-05 01:02:56 +000080 dev->qdisc_tx_busylock = &l2tp_eth_tx_busylock;
Eric Dumazetf9eb8ae2016-06-06 09:37:15 -070081 dev->qdisc_running_key = &l2tp_qdisc_running_key;
82
James Chapmand9e31d12010-04-02 06:19:26 +000083 return 0;
84}
85
86static void l2tp_eth_dev_uninit(struct net_device *dev)
87{
88 struct l2tp_eth *priv = netdev_priv(dev);
89 struct l2tp_eth_net *pn = l2tp_eth_pernet(dev_net(dev));
90
91 spin_lock(&pn->l2tp_eth_lock);
92 list_del_init(&priv->list);
93 spin_unlock(&pn->l2tp_eth_lock);
94 dev_put(dev);
95}
96
97static int l2tp_eth_dev_xmit(struct sk_buff *skb, struct net_device *dev)
98{
99 struct l2tp_eth *priv = netdev_priv(dev);
100 struct l2tp_session *session = priv->session;
Eric Dumazetb8c84302012-06-28 20:15:13 +0000101 unsigned int len = skb->len;
102 int ret = l2tp_xmit_skb(session, skb, session->hdr_len);
James Chapmand9e31d12010-04-02 06:19:26 +0000103
Eric Dumazetb8c84302012-06-28 20:15:13 +0000104 if (likely(ret == NET_XMIT_SUCCESS)) {
105 atomic_long_add(len, &priv->tx_bytes);
106 atomic_long_inc(&priv->tx_packets);
107 } else {
108 atomic_long_inc(&priv->tx_dropped);
109 }
Eric Dumazetaa214de02012-06-25 00:45:14 +0000110 return NETDEV_TX_OK;
James Chapmand9e31d12010-04-02 06:19:26 +0000111}
112
Eric Dumazeta2842a12012-06-25 05:35:45 +0000113static struct rtnl_link_stats64 *l2tp_eth_get_stats64(struct net_device *dev,
114 struct rtnl_link_stats64 *stats)
115{
116 struct l2tp_eth *priv = netdev_priv(dev);
117
118 stats->tx_bytes = atomic_long_read(&priv->tx_bytes);
119 stats->tx_packets = atomic_long_read(&priv->tx_packets);
Eric Dumazetb8c84302012-06-28 20:15:13 +0000120 stats->tx_dropped = atomic_long_read(&priv->tx_dropped);
Eric Dumazeta2842a12012-06-25 05:35:45 +0000121 stats->rx_bytes = atomic_long_read(&priv->rx_bytes);
122 stats->rx_packets = atomic_long_read(&priv->rx_packets);
123 stats->rx_errors = atomic_long_read(&priv->rx_errors);
124 return stats;
125}
126
127
James Chapmand9e31d12010-04-02 06:19:26 +0000128static struct net_device_ops l2tp_eth_netdev_ops = {
129 .ndo_init = l2tp_eth_dev_init,
130 .ndo_uninit = l2tp_eth_dev_uninit,
131 .ndo_start_xmit = l2tp_eth_dev_xmit,
Eric Dumazeta2842a12012-06-25 05:35:45 +0000132 .ndo_get_stats64 = l2tp_eth_get_stats64,
Alexander Couzensfe159122014-11-19 13:24:39 +0100133 .ndo_set_mac_address = eth_mac_addr,
James Chapmand9e31d12010-04-02 06:19:26 +0000134};
135
136static void l2tp_eth_dev_setup(struct net_device *dev)
137{
138 ether_setup(dev);
Eric Dumazeta2842a12012-06-25 05:35:45 +0000139 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
140 dev->features |= NETIF_F_LLTX;
James Chapmand9e31d12010-04-02 06:19:26 +0000141 dev->netdev_ops = &l2tp_eth_netdev_ops;
142 dev->destructor = free_netdev;
143}
144
145static void l2tp_eth_dev_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
146{
147 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
148 struct net_device *dev = spriv->dev;
Eric Dumazeta2842a12012-06-25 05:35:45 +0000149 struct l2tp_eth *priv = netdev_priv(dev);
James Chapmand9e31d12010-04-02 06:19:26 +0000150
151 if (session->debug & L2TP_MSG_DATA) {
152 unsigned int length;
James Chapmand9e31d12010-04-02 06:19:26 +0000153
154 length = min(32u, skb->len);
155 if (!pskb_may_pull(skb, length))
156 goto error;
157
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000158 pr_debug("%s: eth recv\n", session->name);
Eric Dumazeta2842a12012-06-25 05:35:45 +0000159 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmand9e31d12010-04-02 06:19:26 +0000160 }
161
Eric Dumazetc0cc88a2012-09-04 15:54:55 -0400162 if (!pskb_may_pull(skb, ETH_HLEN))
James Chapmand9e31d12010-04-02 06:19:26 +0000163 goto error;
164
165 secpath_reset(skb);
166
167 /* checksums verified by L2TP */
168 skb->ip_summed = CHECKSUM_NONE;
169
170 skb_dst_drop(skb);
171 nf_reset(skb);
172
173 if (dev_forward_skb(dev, skb) == NET_RX_SUCCESS) {
Eric Dumazeta2842a12012-06-25 05:35:45 +0000174 atomic_long_inc(&priv->rx_packets);
175 atomic_long_add(data_len, &priv->rx_bytes);
176 } else {
177 atomic_long_inc(&priv->rx_errors);
178 }
James Chapmand9e31d12010-04-02 06:19:26 +0000179 return;
180
181error:
Eric Dumazeta2842a12012-06-25 05:35:45 +0000182 atomic_long_inc(&priv->rx_errors);
James Chapmand9e31d12010-04-02 06:19:26 +0000183 kfree_skb(skb);
184}
185
186static void l2tp_eth_delete(struct l2tp_session *session)
187{
188 struct l2tp_eth_sess *spriv;
189 struct net_device *dev;
190
191 if (session) {
192 spriv = l2tp_session_priv(session);
193 dev = spriv->dev;
194 if (dev) {
195 unregister_netdev(dev);
196 spriv->dev = NULL;
Eric Dumazeta06998b2012-06-07 00:07:20 +0000197 module_put(THIS_MODULE);
James Chapmand9e31d12010-04-02 06:19:26 +0000198 }
199 }
200}
201
David S. Millerf66ef2d2010-04-03 15:01:37 -0700202#if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
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);
207 struct net_device *dev = spriv->dev;
208
209 seq_printf(m, " interface %s\n", dev->name);
210}
211#endif
212
James Chapmand9e31d12010-04-02 06:19:26 +0000213static int l2tp_eth_create(struct net *net, u32 tunnel_id, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
214{
215 struct net_device *dev;
216 char name[IFNAMSIZ];
217 struct l2tp_tunnel *tunnel;
218 struct l2tp_session *session;
219 struct l2tp_eth *priv;
220 struct l2tp_eth_sess *spriv;
221 int rc;
222 struct l2tp_eth_net *pn;
223
224 tunnel = l2tp_tunnel_find(net, tunnel_id);
225 if (!tunnel) {
226 rc = -ENODEV;
227 goto out;
228 }
229
230 session = l2tp_session_find(net, tunnel, session_id);
231 if (session) {
232 rc = -EEXIST;
233 goto out;
234 }
235
236 if (cfg->ifname) {
237 dev = dev_get_by_name(net, cfg->ifname);
238 if (dev) {
239 dev_put(dev);
240 rc = -EEXIST;
241 goto out;
242 }
243 strlcpy(name, cfg->ifname, IFNAMSIZ);
244 } else
245 strcpy(name, L2TP_ETH_DEV_NAME);
246
247 session = l2tp_session_create(sizeof(*spriv), tunnel, session_id,
248 peer_session_id, cfg);
249 if (!session) {
250 rc = -ENOMEM;
251 goto out;
252 }
253
Tom Gundersenc835a672014-07-14 16:37:24 +0200254 dev = alloc_netdev(sizeof(*priv), name, NET_NAME_UNKNOWN,
255 l2tp_eth_dev_setup);
James Chapmand9e31d12010-04-02 06:19:26 +0000256 if (!dev) {
257 rc = -ENOMEM;
258 goto out_del_session;
259 }
260
261 dev_net_set(dev, net);
262 if (session->mtu == 0)
263 session->mtu = dev->mtu - session->hdr_len;
264 dev->mtu = session->mtu;
265 dev->needed_headroom += session->hdr_len;
266
267 priv = netdev_priv(dev);
268 priv->dev = dev;
269 priv->session = session;
270 INIT_LIST_HEAD(&priv->list);
271
272 priv->tunnel_sock = tunnel->sock;
273 session->recv_skb = l2tp_eth_dev_recv;
274 session->session_close = l2tp_eth_delete;
David S. Millerf66ef2d2010-04-03 15:01:37 -0700275#if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
James Chapman0ad66142010-04-02 06:19:33 +0000276 session->show = l2tp_eth_show;
277#endif
James Chapmand9e31d12010-04-02 06:19:26 +0000278
279 spriv = l2tp_session_priv(session);
280 spriv->dev = dev;
281
282 rc = register_netdev(dev);
283 if (rc < 0)
284 goto out_del_dev;
285
Eric Dumazeta06998b2012-06-07 00:07:20 +0000286 __module_get(THIS_MODULE);
James Chapmand9e31d12010-04-02 06:19:26 +0000287 /* Must be done after register_netdev() */
288 strlcpy(session->ifname, dev->name, IFNAMSIZ);
289
290 dev_hold(dev);
291 pn = l2tp_eth_pernet(dev_net(dev));
292 spin_lock(&pn->l2tp_eth_lock);
293 list_add(&priv->list, &pn->l2tp_eth_dev_list);
294 spin_unlock(&pn->l2tp_eth_lock);
295
296 return 0;
297
298out_del_dev:
299 free_netdev(dev);
Tom Parkin78933632012-10-29 23:41:48 +0000300 spriv->dev = NULL;
James Chapmand9e31d12010-04-02 06:19:26 +0000301out_del_session:
302 l2tp_session_delete(session);
303out:
304 return rc;
305}
306
307static __net_init int l2tp_eth_init_net(struct net *net)
308{
Jiri Pirko3a737022010-04-23 01:01:52 +0000309 struct l2tp_eth_net *pn = net_generic(net, l2tp_eth_net_id);
James Chapmand9e31d12010-04-02 06:19:26 +0000310
311 INIT_LIST_HEAD(&pn->l2tp_eth_dev_list);
312 spin_lock_init(&pn->l2tp_eth_lock);
313
James Chapmand9e31d12010-04-02 06:19:26 +0000314 return 0;
James Chapmand9e31d12010-04-02 06:19:26 +0000315}
316
James Chapman8aa525a2011-03-21 18:10:25 -0700317static struct pernet_operations l2tp_eth_net_ops = {
James Chapmand9e31d12010-04-02 06:19:26 +0000318 .init = l2tp_eth_init_net,
James Chapmand9e31d12010-04-02 06:19:26 +0000319 .id = &l2tp_eth_net_id,
320 .size = sizeof(struct l2tp_eth_net),
321};
322
323
324static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops = {
325 .session_create = l2tp_eth_create,
326 .session_delete = l2tp_session_delete,
327};
328
329
330static int __init l2tp_eth_init(void)
331{
332 int err = 0;
333
334 err = l2tp_nl_register_ops(L2TP_PWTYPE_ETH, &l2tp_eth_nl_cmd_ops);
335 if (err)
336 goto out;
337
338 err = register_pernet_device(&l2tp_eth_net_ops);
339 if (err)
340 goto out_unreg;
341
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000342 pr_info("L2TP ethernet pseudowire support (L2TPv3)\n");
James Chapmand9e31d12010-04-02 06:19:26 +0000343
344 return 0;
345
346out_unreg:
347 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH);
348out:
349 return err;
350}
351
352static void __exit l2tp_eth_exit(void)
353{
354 unregister_pernet_device(&l2tp_eth_net_ops);
355 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH);
356}
357
358module_init(l2tp_eth_init);
359module_exit(l2tp_eth_exit);
360
361MODULE_LICENSE("GPL");
362MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
363MODULE_DESCRIPTION("L2TP ethernet pseudowire driver");
364MODULE_VERSION("1.0");
stephen hemmingerf1f39f92015-09-23 21:33:34 -0700365MODULE_ALIAS_L2TP_PWTYPE(5);