Loading...
1/*
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
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14#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;
45 atomic_long_t tx_bytes;
46 atomic_long_t tx_packets;
47 atomic_long_t rx_bytes;
48 atomic_long_t rx_packets;
49 atomic_long_t rx_errors;
50};
51
52/* via l2tp_session_priv() */
53struct l2tp_eth_sess {
54 struct net_device *dev;
55};
56
57/* per-net private data for this module */
58static unsigned int l2tp_eth_net_id;
59struct l2tp_eth_net {
60 struct list_head l2tp_eth_dev_list;
61 spinlock_t l2tp_eth_lock;
62};
63
64static inline struct l2tp_eth_net *l2tp_eth_pernet(struct net *net)
65{
66 return net_generic(net, l2tp_eth_net_id);
67}
68
69static int l2tp_eth_dev_init(struct net_device *dev)
70{
71 struct l2tp_eth *priv = netdev_priv(dev);
72
73 priv->dev = dev;
74 eth_hw_addr_random(dev);
75 memset(&dev->broadcast[0], 0xff, 6);
76
77 return 0;
78}
79
80static void l2tp_eth_dev_uninit(struct net_device *dev)
81{
82 struct l2tp_eth *priv = netdev_priv(dev);
83 struct l2tp_eth_net *pn = l2tp_eth_pernet(dev_net(dev));
84
85 spin_lock(&pn->l2tp_eth_lock);
86 list_del_init(&priv->list);
87 spin_unlock(&pn->l2tp_eth_lock);
88 dev_put(dev);
89}
90
91static int l2tp_eth_dev_xmit(struct sk_buff *skb, struct net_device *dev)
92{
93 struct l2tp_eth *priv = netdev_priv(dev);
94 struct l2tp_session *session = priv->session;
95
96 atomic_long_add(skb->len, &priv->tx_bytes);
97 atomic_long_inc(&priv->tx_packets);
98
99 l2tp_xmit_skb(session, skb, session->hdr_len);
100
101 return NETDEV_TX_OK;
102}
103
104static struct rtnl_link_stats64 *l2tp_eth_get_stats64(struct net_device *dev,
105 struct rtnl_link_stats64 *stats)
106{
107 struct l2tp_eth *priv = netdev_priv(dev);
108
109 stats->tx_bytes = atomic_long_read(&priv->tx_bytes);
110 stats->tx_packets = atomic_long_read(&priv->tx_packets);
111 stats->rx_bytes = atomic_long_read(&priv->rx_bytes);
112 stats->rx_packets = atomic_long_read(&priv->rx_packets);
113 stats->rx_errors = atomic_long_read(&priv->rx_errors);
114 return stats;
115}
116
117
118static struct net_device_ops l2tp_eth_netdev_ops = {
119 .ndo_init = l2tp_eth_dev_init,
120 .ndo_uninit = l2tp_eth_dev_uninit,
121 .ndo_start_xmit = l2tp_eth_dev_xmit,
122 .ndo_get_stats64 = l2tp_eth_get_stats64,
123};
124
125static void l2tp_eth_dev_setup(struct net_device *dev)
126{
127 ether_setup(dev);
128 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
129 dev->features |= NETIF_F_LLTX;
130 dev->netdev_ops = &l2tp_eth_netdev_ops;
131 dev->destructor = free_netdev;
132}
133
134static void l2tp_eth_dev_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
135{
136 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
137 struct net_device *dev = spriv->dev;
138 struct l2tp_eth *priv = netdev_priv(dev);
139
140 if (session->debug & L2TP_MSG_DATA) {
141 unsigned int length;
142
143 length = min(32u, skb->len);
144 if (!pskb_may_pull(skb, length))
145 goto error;
146
147 pr_debug("%s: eth recv\n", session->name);
148 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
149 }
150
151 if (!pskb_may_pull(skb, sizeof(ETH_HLEN)))
152 goto error;
153
154 secpath_reset(skb);
155
156 /* checksums verified by L2TP */
157 skb->ip_summed = CHECKSUM_NONE;
158
159 skb_dst_drop(skb);
160 nf_reset(skb);
161
162 if (dev_forward_skb(dev, skb) == NET_RX_SUCCESS) {
163 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 }
168 return;
169
170error:
171 atomic_long_inc(&priv->rx_errors);
172 kfree_skb(skb);
173}
174
175static void l2tp_eth_delete(struct l2tp_session *session)
176{
177 struct l2tp_eth_sess *spriv;
178 struct net_device *dev;
179
180 if (session) {
181 spriv = l2tp_session_priv(session);
182 dev = spriv->dev;
183 if (dev) {
184 unregister_netdev(dev);
185 spriv->dev = NULL;
186 module_put(THIS_MODULE);
187 }
188 }
189}
190
191#if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
192static void l2tp_eth_show(struct seq_file *m, void *arg)
193{
194 struct l2tp_session *session = arg;
195 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
196 struct net_device *dev = spriv->dev;
197
198 seq_printf(m, " interface %s\n", dev->name);
199}
200#endif
201
202static int l2tp_eth_create(struct net *net, u32 tunnel_id, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
203{
204 struct net_device *dev;
205 char name[IFNAMSIZ];
206 struct l2tp_tunnel *tunnel;
207 struct l2tp_session *session;
208 struct l2tp_eth *priv;
209 struct l2tp_eth_sess *spriv;
210 int rc;
211 struct l2tp_eth_net *pn;
212
213 tunnel = l2tp_tunnel_find(net, tunnel_id);
214 if (!tunnel) {
215 rc = -ENODEV;
216 goto out;
217 }
218
219 session = l2tp_session_find(net, tunnel, session_id);
220 if (session) {
221 rc = -EEXIST;
222 goto out;
223 }
224
225 if (cfg->ifname) {
226 dev = dev_get_by_name(net, cfg->ifname);
227 if (dev) {
228 dev_put(dev);
229 rc = -EEXIST;
230 goto out;
231 }
232 strlcpy(name, cfg->ifname, IFNAMSIZ);
233 } else
234 strcpy(name, L2TP_ETH_DEV_NAME);
235
236 session = l2tp_session_create(sizeof(*spriv), tunnel, session_id,
237 peer_session_id, cfg);
238 if (!session) {
239 rc = -ENOMEM;
240 goto out;
241 }
242
243 dev = alloc_netdev(sizeof(*priv), name, l2tp_eth_dev_setup);
244 if (!dev) {
245 rc = -ENOMEM;
246 goto out_del_session;
247 }
248
249 dev_net_set(dev, net);
250 if (session->mtu == 0)
251 session->mtu = dev->mtu - session->hdr_len;
252 dev->mtu = session->mtu;
253 dev->needed_headroom += session->hdr_len;
254
255 priv = netdev_priv(dev);
256 priv->dev = dev;
257 priv->session = session;
258 INIT_LIST_HEAD(&priv->list);
259
260 priv->tunnel_sock = tunnel->sock;
261 session->recv_skb = l2tp_eth_dev_recv;
262 session->session_close = l2tp_eth_delete;
263#if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
264 session->show = l2tp_eth_show;
265#endif
266
267 spriv = l2tp_session_priv(session);
268 spriv->dev = dev;
269
270 rc = register_netdev(dev);
271 if (rc < 0)
272 goto out_del_dev;
273
274 __module_get(THIS_MODULE);
275 /* Must be done after register_netdev() */
276 strlcpy(session->ifname, dev->name, IFNAMSIZ);
277
278 dev_hold(dev);
279 pn = l2tp_eth_pernet(dev_net(dev));
280 spin_lock(&pn->l2tp_eth_lock);
281 list_add(&priv->list, &pn->l2tp_eth_dev_list);
282 spin_unlock(&pn->l2tp_eth_lock);
283
284 return 0;
285
286out_del_dev:
287 free_netdev(dev);
288out_del_session:
289 l2tp_session_delete(session);
290out:
291 return rc;
292}
293
294static __net_init int l2tp_eth_init_net(struct net *net)
295{
296 struct l2tp_eth_net *pn = net_generic(net, l2tp_eth_net_id);
297
298 INIT_LIST_HEAD(&pn->l2tp_eth_dev_list);
299 spin_lock_init(&pn->l2tp_eth_lock);
300
301 return 0;
302}
303
304static struct pernet_operations l2tp_eth_net_ops = {
305 .init = l2tp_eth_init_net,
306 .id = &l2tp_eth_net_id,
307 .size = sizeof(struct l2tp_eth_net),
308};
309
310
311static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops = {
312 .session_create = l2tp_eth_create,
313 .session_delete = l2tp_session_delete,
314};
315
316
317static int __init l2tp_eth_init(void)
318{
319 int err = 0;
320
321 err = l2tp_nl_register_ops(L2TP_PWTYPE_ETH, &l2tp_eth_nl_cmd_ops);
322 if (err)
323 goto out;
324
325 err = register_pernet_device(&l2tp_eth_net_ops);
326 if (err)
327 goto out_unreg;
328
329 pr_info("L2TP ethernet pseudowire support (L2TPv3)\n");
330
331 return 0;
332
333out_unreg:
334 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH);
335out:
336 return err;
337}
338
339static void __exit l2tp_eth_exit(void)
340{
341 unregister_pernet_device(&l2tp_eth_net_ops);
342 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH);
343}
344
345module_init(l2tp_eth_init);
346module_exit(l2tp_eth_exit);
347
348MODULE_LICENSE("GPL");
349MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
350MODULE_DESCRIPTION("L2TP ethernet pseudowire driver");
351MODULE_VERSION("1.0");
1// SPDX-License-Identifier: GPL-2.0-or-later
2/* L2TPv3 ethernet pseudowire driver
3 *
4 * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
5 */
6
7#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8
9#include <linux/module.h>
10#include <linux/skbuff.h>
11#include <linux/socket.h>
12#include <linux/hash.h>
13#include <linux/l2tp.h>
14#include <linux/in.h>
15#include <linux/etherdevice.h>
16#include <linux/spinlock.h>
17#include <net/sock.h>
18#include <net/ip.h>
19#include <net/icmp.h>
20#include <net/udp.h>
21#include <net/inet_common.h>
22#include <net/inet_hashtables.h>
23#include <net/tcp_states.h>
24#include <net/protocol.h>
25#include <net/xfrm.h>
26#include <net/net_namespace.h>
27#include <net/netns/generic.h>
28#include <linux/ip.h>
29#include <linux/ipv6.h>
30#include <linux/udp.h>
31
32#include "l2tp_core.h"
33
34/* Default device name. May be overridden by name specified by user */
35#define L2TP_ETH_DEV_NAME "l2tpeth%d"
36
37/* via netdev_priv() */
38struct l2tp_eth {
39 struct l2tp_session *session;
40 atomic_long_t tx_bytes;
41 atomic_long_t tx_packets;
42 atomic_long_t tx_dropped;
43 atomic_long_t rx_bytes;
44 atomic_long_t rx_packets;
45 atomic_long_t rx_errors;
46};
47
48/* via l2tp_session_priv() */
49struct l2tp_eth_sess {
50 struct net_device __rcu *dev;
51};
52
53static int l2tp_eth_dev_init(struct net_device *dev)
54{
55 eth_hw_addr_random(dev);
56 eth_broadcast_addr(dev->broadcast);
57 netdev_lockdep_set_classes(dev);
58
59 return 0;
60}
61
62static void l2tp_eth_dev_uninit(struct net_device *dev)
63{
64 struct l2tp_eth *priv = netdev_priv(dev);
65 struct l2tp_eth_sess *spriv;
66
67 spriv = l2tp_session_priv(priv->session);
68 RCU_INIT_POINTER(spriv->dev, NULL);
69 /* No need for synchronize_net() here. We're called by
70 * unregister_netdev*(), which does the synchronisation for us.
71 */
72}
73
74static netdev_tx_t l2tp_eth_dev_xmit(struct sk_buff *skb, struct net_device *dev)
75{
76 struct l2tp_eth *priv = netdev_priv(dev);
77 struct l2tp_session *session = priv->session;
78 unsigned int len = skb->len;
79 int ret = l2tp_xmit_skb(session, skb);
80
81 if (likely(ret == NET_XMIT_SUCCESS)) {
82 atomic_long_add(len, &priv->tx_bytes);
83 atomic_long_inc(&priv->tx_packets);
84 } else {
85 atomic_long_inc(&priv->tx_dropped);
86 }
87 return NETDEV_TX_OK;
88}
89
90static void l2tp_eth_get_stats64(struct net_device *dev,
91 struct rtnl_link_stats64 *stats)
92{
93 struct l2tp_eth *priv = netdev_priv(dev);
94
95 stats->tx_bytes = (unsigned long)atomic_long_read(&priv->tx_bytes);
96 stats->tx_packets = (unsigned long)atomic_long_read(&priv->tx_packets);
97 stats->tx_dropped = (unsigned long)atomic_long_read(&priv->tx_dropped);
98 stats->rx_bytes = (unsigned long)atomic_long_read(&priv->rx_bytes);
99 stats->rx_packets = (unsigned long)atomic_long_read(&priv->rx_packets);
100 stats->rx_errors = (unsigned long)atomic_long_read(&priv->rx_errors);
101}
102
103static const struct net_device_ops l2tp_eth_netdev_ops = {
104 .ndo_init = l2tp_eth_dev_init,
105 .ndo_uninit = l2tp_eth_dev_uninit,
106 .ndo_start_xmit = l2tp_eth_dev_xmit,
107 .ndo_get_stats64 = l2tp_eth_get_stats64,
108 .ndo_set_mac_address = eth_mac_addr,
109};
110
111static struct device_type l2tpeth_type = {
112 .name = "l2tpeth",
113};
114
115static void l2tp_eth_dev_setup(struct net_device *dev)
116{
117 SET_NETDEV_DEVTYPE(dev, &l2tpeth_type);
118 ether_setup(dev);
119 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
120 dev->features |= NETIF_F_LLTX;
121 dev->netdev_ops = &l2tp_eth_netdev_ops;
122 dev->needs_free_netdev = true;
123}
124
125static void l2tp_eth_dev_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
126{
127 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
128 struct net_device *dev;
129 struct l2tp_eth *priv;
130
131 if (!pskb_may_pull(skb, ETH_HLEN))
132 goto error;
133
134 secpath_reset(skb);
135
136 /* checksums verified by L2TP */
137 skb->ip_summed = CHECKSUM_NONE;
138
139 skb_dst_drop(skb);
140 nf_reset_ct(skb);
141
142 rcu_read_lock();
143 dev = rcu_dereference(spriv->dev);
144 if (!dev)
145 goto error_rcu;
146
147 priv = netdev_priv(dev);
148 if (dev_forward_skb(dev, skb) == NET_RX_SUCCESS) {
149 atomic_long_inc(&priv->rx_packets);
150 atomic_long_add(data_len, &priv->rx_bytes);
151 } else {
152 atomic_long_inc(&priv->rx_errors);
153 }
154 rcu_read_unlock();
155
156 return;
157
158error_rcu:
159 rcu_read_unlock();
160error:
161 kfree_skb(skb);
162}
163
164static void l2tp_eth_delete(struct l2tp_session *session)
165{
166 struct l2tp_eth_sess *spriv;
167 struct net_device *dev;
168
169 if (session) {
170 spriv = l2tp_session_priv(session);
171
172 rtnl_lock();
173 dev = rtnl_dereference(spriv->dev);
174 if (dev) {
175 unregister_netdevice(dev);
176 rtnl_unlock();
177 module_put(THIS_MODULE);
178 } else {
179 rtnl_unlock();
180 }
181 }
182}
183
184static void l2tp_eth_show(struct seq_file *m, void *arg)
185{
186 struct l2tp_session *session = arg;
187 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
188 struct net_device *dev;
189
190 rcu_read_lock();
191 dev = rcu_dereference(spriv->dev);
192 if (!dev) {
193 rcu_read_unlock();
194 return;
195 }
196 dev_hold(dev);
197 rcu_read_unlock();
198
199 seq_printf(m, " interface %s\n", dev->name);
200
201 dev_put(dev);
202}
203
204static void l2tp_eth_adjust_mtu(struct l2tp_tunnel *tunnel,
205 struct l2tp_session *session,
206 struct net_device *dev)
207{
208 unsigned int overhead = 0;
209 u32 l3_overhead = 0;
210 u32 mtu;
211
212 /* if the encap is UDP, account for UDP header size */
213 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
214 overhead += sizeof(struct udphdr);
215 dev->needed_headroom += sizeof(struct udphdr);
216 }
217
218 lock_sock(tunnel->sock);
219 l3_overhead = kernel_sock_ip_overhead(tunnel->sock);
220 release_sock(tunnel->sock);
221
222 if (l3_overhead == 0) {
223 /* L3 Overhead couldn't be identified, this could be
224 * because tunnel->sock was NULL or the socket's
225 * address family was not IPv4 or IPv6,
226 * dev mtu stays at 1500.
227 */
228 return;
229 }
230 /* Adjust MTU, factor overhead - underlay L3, overlay L2 hdr
231 * UDP overhead, if any, was already factored in above.
232 */
233 overhead += session->hdr_len + ETH_HLEN + l3_overhead;
234
235 mtu = l2tp_tunnel_dst_mtu(tunnel) - overhead;
236 if (mtu < dev->min_mtu || mtu > dev->max_mtu)
237 dev->mtu = ETH_DATA_LEN - overhead;
238 else
239 dev->mtu = mtu;
240
241 dev->needed_headroom += session->hdr_len;
242}
243
244static int l2tp_eth_create(struct net *net, struct l2tp_tunnel *tunnel,
245 u32 session_id, u32 peer_session_id,
246 struct l2tp_session_cfg *cfg)
247{
248 unsigned char name_assign_type;
249 struct net_device *dev;
250 char name[IFNAMSIZ];
251 struct l2tp_session *session;
252 struct l2tp_eth *priv;
253 struct l2tp_eth_sess *spriv;
254 int rc;
255
256 if (cfg->ifname) {
257 strscpy(name, cfg->ifname, IFNAMSIZ);
258 name_assign_type = NET_NAME_USER;
259 } else {
260 strcpy(name, L2TP_ETH_DEV_NAME);
261 name_assign_type = NET_NAME_ENUM;
262 }
263
264 session = l2tp_session_create(sizeof(*spriv), tunnel, session_id,
265 peer_session_id, cfg);
266 if (IS_ERR(session)) {
267 rc = PTR_ERR(session);
268 goto err;
269 }
270
271 dev = alloc_netdev(sizeof(*priv), name, name_assign_type,
272 l2tp_eth_dev_setup);
273 if (!dev) {
274 rc = -ENOMEM;
275 goto err_sess;
276 }
277
278 dev_net_set(dev, net);
279 dev->min_mtu = 0;
280 dev->max_mtu = ETH_MAX_MTU;
281 l2tp_eth_adjust_mtu(tunnel, session, dev);
282
283 priv = netdev_priv(dev);
284 priv->session = session;
285
286 session->recv_skb = l2tp_eth_dev_recv;
287 session->session_close = l2tp_eth_delete;
288 if (IS_ENABLED(CONFIG_L2TP_DEBUGFS))
289 session->show = l2tp_eth_show;
290
291 spriv = l2tp_session_priv(session);
292
293 l2tp_session_inc_refcount(session);
294
295 rtnl_lock();
296
297 /* Register both device and session while holding the rtnl lock. This
298 * ensures that l2tp_eth_delete() will see that there's a device to
299 * unregister, even if it happened to run before we assign spriv->dev.
300 */
301 rc = l2tp_session_register(session, tunnel);
302 if (rc < 0) {
303 rtnl_unlock();
304 goto err_sess_dev;
305 }
306
307 rc = register_netdevice(dev);
308 if (rc < 0) {
309 rtnl_unlock();
310 l2tp_session_delete(session);
311 l2tp_session_dec_refcount(session);
312 free_netdev(dev);
313
314 return rc;
315 }
316
317 strscpy(session->ifname, dev->name, IFNAMSIZ);
318 rcu_assign_pointer(spriv->dev, dev);
319
320 rtnl_unlock();
321
322 l2tp_session_dec_refcount(session);
323
324 __module_get(THIS_MODULE);
325
326 return 0;
327
328err_sess_dev:
329 l2tp_session_dec_refcount(session);
330 free_netdev(dev);
331err_sess:
332 kfree(session);
333err:
334 return rc;
335}
336
337static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops = {
338 .session_create = l2tp_eth_create,
339 .session_delete = l2tp_session_delete,
340};
341
342static int __init l2tp_eth_init(void)
343{
344 int err = 0;
345
346 err = l2tp_nl_register_ops(L2TP_PWTYPE_ETH, &l2tp_eth_nl_cmd_ops);
347 if (err)
348 goto err;
349
350 pr_info("L2TP ethernet pseudowire support (L2TPv3)\n");
351
352 return 0;
353
354err:
355 return err;
356}
357
358static void __exit l2tp_eth_exit(void)
359{
360 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH);
361}
362
363module_init(l2tp_eth_init);
364module_exit(l2tp_eth_exit);
365
366MODULE_LICENSE("GPL");
367MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
368MODULE_DESCRIPTION("L2TP ethernet pseudowire driver");
369MODULE_VERSION("1.0");
370MODULE_ALIAS_L2TP_PWTYPE(5);