blob: d29bfee291cbfe89cb535fd60b3435075b4ee935 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
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#include <linux/ip.h>
34#include <linux/ipv6.h>
35#include <linux/udp.h>
36
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 atomic_long_t tx_bytes;
48 atomic_long_t tx_packets;
49 atomic_long_t tx_dropped;
50 atomic_long_t rx_bytes;
51 atomic_long_t rx_packets;
52 atomic_long_t rx_errors;
53};
54
55/* via l2tp_session_priv() */
56struct l2tp_eth_sess {
57 struct net_device __rcu *dev;
58};
59
60
61static int l2tp_eth_dev_init(struct net_device *dev)
62{
63 struct l2tp_eth *priv = netdev_priv(dev);
64
65 priv->dev = dev;
66 eth_hw_addr_random(dev);
67 eth_broadcast_addr(dev->broadcast);
68 netdev_lockdep_set_classes(dev);
69
70 return 0;
71}
72
73static void l2tp_eth_dev_uninit(struct net_device *dev)
74{
75 struct l2tp_eth *priv = netdev_priv(dev);
76 struct l2tp_eth_sess *spriv;
77
78 spriv = l2tp_session_priv(priv->session);
79 RCU_INIT_POINTER(spriv->dev, NULL);
80 /* No need for synchronize_net() here. We're called by
81 * unregister_netdev*(), which does the synchronisation for us.
82 */
83}
84
85static int l2tp_eth_dev_xmit(struct sk_buff *skb, struct net_device *dev)
86{
87 struct l2tp_eth *priv = netdev_priv(dev);
88 struct l2tp_session *session = priv->session;
89 unsigned int len = skb->len;
90 int ret = l2tp_xmit_skb(session, skb, session->hdr_len);
91
92 if (likely(ret == NET_XMIT_SUCCESS)) {
93 atomic_long_add(len, &priv->tx_bytes);
94 atomic_long_inc(&priv->tx_packets);
95 } else {
96 atomic_long_inc(&priv->tx_dropped);
97 }
98 return NETDEV_TX_OK;
99}
100
101static void l2tp_eth_get_stats64(struct net_device *dev,
102 struct rtnl_link_stats64 *stats)
103{
104 struct l2tp_eth *priv = netdev_priv(dev);
105
106 stats->tx_bytes = (unsigned long) atomic_long_read(&priv->tx_bytes);
107 stats->tx_packets = (unsigned long) atomic_long_read(&priv->tx_packets);
108 stats->tx_dropped = (unsigned long) atomic_long_read(&priv->tx_dropped);
109 stats->rx_bytes = (unsigned long) atomic_long_read(&priv->rx_bytes);
110 stats->rx_packets = (unsigned long) atomic_long_read(&priv->rx_packets);
111 stats->rx_errors = (unsigned long) atomic_long_read(&priv->rx_errors);
112
113}
114
115static const struct net_device_ops l2tp_eth_netdev_ops = {
116 .ndo_init = l2tp_eth_dev_init,
117 .ndo_uninit = l2tp_eth_dev_uninit,
118 .ndo_start_xmit = l2tp_eth_dev_xmit,
119 .ndo_get_stats64 = l2tp_eth_get_stats64,
120 .ndo_set_mac_address = eth_mac_addr,
121};
122
123static struct device_type l2tpeth_type = {
124 .name = "l2tpeth",
125};
126
127static void l2tp_eth_dev_setup(struct net_device *dev)
128{
129 SET_NETDEV_DEVTYPE(dev, &l2tpeth_type);
130 ether_setup(dev);
131 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
132 dev->features |= NETIF_F_LLTX;
133 dev->netdev_ops = &l2tp_eth_netdev_ops;
134 dev->needs_free_netdev = true;
135}
136
137static void l2tp_eth_dev_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
138{
139 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
140 struct net_device *dev;
141 struct l2tp_eth *priv;
142
143 if (session->debug & L2TP_MSG_DATA) {
144 unsigned int length;
145
146 length = min(32u, skb->len);
147 if (!pskb_may_pull(skb, length))
148 goto error;
149
150 pr_debug("%s: eth recv\n", session->name);
151 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
152 }
153
154 if (!pskb_may_pull(skb, ETH_HLEN))
155 goto error;
156
157 secpath_reset(skb);
158
159 /* checksums verified by L2TP */
160 skb->ip_summed = CHECKSUM_NONE;
161
162 skb_dst_drop(skb);
163 nf_reset(skb);
164
165 rcu_read_lock();
166 dev = rcu_dereference(spriv->dev);
167 if (!dev)
168 goto error_rcu;
169
170 priv = netdev_priv(dev);
171 if (dev_forward_skb(dev, skb) == NET_RX_SUCCESS) {
172 atomic_long_inc(&priv->rx_packets);
173 atomic_long_add(data_len, &priv->rx_bytes);
174 } else {
175 atomic_long_inc(&priv->rx_errors);
176 }
177 rcu_read_unlock();
178
179 return;
180
181error_rcu:
182 rcu_read_unlock();
183error:
184 kfree_skb(skb);
185}
186
187static void l2tp_eth_delete(struct l2tp_session *session)
188{
189 struct l2tp_eth_sess *spriv;
190 struct net_device *dev;
191
192 if (session) {
193 spriv = l2tp_session_priv(session);
194
195 rtnl_lock();
196 dev = rtnl_dereference(spriv->dev);
197 if (dev) {
198 unregister_netdevice(dev);
199 rtnl_unlock();
200 module_put(THIS_MODULE);
201 } else {
202 rtnl_unlock();
203 }
204 }
205}
206
207#if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
208static void l2tp_eth_show(struct seq_file *m, void *arg)
209{
210 struct l2tp_session *session = arg;
211 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
212 struct net_device *dev;
213
214 rcu_read_lock();
215 dev = rcu_dereference(spriv->dev);
216 if (!dev) {
217 rcu_read_unlock();
218 return;
219 }
220 dev_hold(dev);
221 rcu_read_unlock();
222
223 seq_printf(m, " interface %s\n", dev->name);
224
225 dev_put(dev);
226}
227#endif
228
229static void l2tp_eth_adjust_mtu(struct l2tp_tunnel *tunnel,
230 struct l2tp_session *session,
231 struct net_device *dev)
232{
233 unsigned int overhead = 0;
234 struct dst_entry *dst;
235 u32 l3_overhead = 0;
236
237 /* if the encap is UDP, account for UDP header size */
238 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
239 overhead += sizeof(struct udphdr);
240 dev->needed_headroom += sizeof(struct udphdr);
241 }
242 if (session->mtu != 0) {
243 dev->mtu = session->mtu;
244 dev->needed_headroom += session->hdr_len;
245 return;
246 }
247 lock_sock(tunnel->sock);
248 l3_overhead = kernel_sock_ip_overhead(tunnel->sock);
249 release_sock(tunnel->sock);
250 if (l3_overhead == 0) {
251 /* L3 Overhead couldn't be identified, this could be
252 * because tunnel->sock was NULL or the socket's
253 * address family was not IPv4 or IPv6,
254 * dev mtu stays at 1500.
255 */
256 return;
257 }
258 /* Adjust MTU, factor overhead - underlay L3, overlay L2 hdr
259 * UDP overhead, if any, was already factored in above.
260 */
261 overhead += session->hdr_len + ETH_HLEN + l3_overhead;
262
263 /* If PMTU discovery was enabled, use discovered MTU on L2TP device */
264 dst = sk_dst_get(tunnel->sock);
265 if (dst) {
266 /* dst_mtu will use PMTU if found, else fallback to intf MTU */
267 u32 pmtu = dst_mtu(dst);
268
269 if (pmtu != 0)
270 dev->mtu = pmtu;
271 dst_release(dst);
272 }
273 session->mtu = dev->mtu - overhead;
274 dev->mtu = session->mtu;
275 dev->needed_headroom += session->hdr_len;
276}
277
278static int l2tp_eth_create(struct net *net, struct l2tp_tunnel *tunnel,
279 u32 session_id, u32 peer_session_id,
280 struct l2tp_session_cfg *cfg)
281{
282 unsigned char name_assign_type;
283 struct net_device *dev;
284 char name[IFNAMSIZ];
285 struct l2tp_session *session;
286 struct l2tp_eth *priv;
287 struct l2tp_eth_sess *spriv;
288 int rc;
289
290 if (cfg->ifname) {
291 strlcpy(name, cfg->ifname, IFNAMSIZ);
292 name_assign_type = NET_NAME_USER;
293 } else {
294 strcpy(name, L2TP_ETH_DEV_NAME);
295 name_assign_type = NET_NAME_ENUM;
296 }
297
298 session = l2tp_session_create(sizeof(*spriv), tunnel, session_id,
299 peer_session_id, cfg);
300 if (IS_ERR(session)) {
301 rc = PTR_ERR(session);
302 goto err;
303 }
304
305 dev = alloc_netdev(sizeof(*priv), name, name_assign_type,
306 l2tp_eth_dev_setup);
307 if (!dev) {
308 rc = -ENOMEM;
309 goto err_sess;
310 }
311
312 dev_net_set(dev, net);
313 dev->min_mtu = 0;
314 dev->max_mtu = ETH_MAX_MTU;
315 l2tp_eth_adjust_mtu(tunnel, session, dev);
316
317 priv = netdev_priv(dev);
318 priv->dev = dev;
319 priv->session = session;
320
321 priv->tunnel_sock = tunnel->sock;
322 session->recv_skb = l2tp_eth_dev_recv;
323 session->session_close = l2tp_eth_delete;
324#if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
325 session->show = l2tp_eth_show;
326#endif
327
328 spriv = l2tp_session_priv(session);
329
330 l2tp_session_inc_refcount(session);
331
332 rtnl_lock();
333
334 /* Register both device and session while holding the rtnl lock. This
335 * ensures that l2tp_eth_delete() will see that there's a device to
336 * unregister, even if it happened to run before we assign spriv->dev.
337 */
338 rc = l2tp_session_register(session, tunnel);
339 if (rc < 0) {
340 rtnl_unlock();
341 goto err_sess_dev;
342 }
343
344 rc = register_netdevice(dev);
345 if (rc < 0) {
346 rtnl_unlock();
347 l2tp_session_delete(session);
348 l2tp_session_dec_refcount(session);
349 free_netdev(dev);
350
351 return rc;
352 }
353
354 strlcpy(session->ifname, dev->name, IFNAMSIZ);
355 rcu_assign_pointer(spriv->dev, dev);
356
357 rtnl_unlock();
358
359 l2tp_session_dec_refcount(session);
360
361 __module_get(THIS_MODULE);
362
363 return 0;
364
365err_sess_dev:
366 l2tp_session_dec_refcount(session);
367 free_netdev(dev);
368err_sess:
369 kfree(session);
370err:
371 return rc;
372}
373
374
375static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops = {
376 .session_create = l2tp_eth_create,
377 .session_delete = l2tp_session_delete,
378};
379
380
381static int __init l2tp_eth_init(void)
382{
383 int err = 0;
384
385 err = l2tp_nl_register_ops(L2TP_PWTYPE_ETH, &l2tp_eth_nl_cmd_ops);
386 if (err)
387 goto err;
388
389 pr_info("L2TP ethernet pseudowire support (L2TPv3)\n");
390
391 return 0;
392
393err:
394 return err;
395}
396
397static void __exit l2tp_eth_exit(void)
398{
399 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH);
400}
401
402module_init(l2tp_eth_init);
403module_exit(l2tp_eth_exit);
404
405MODULE_LICENSE("GPL");
406MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
407MODULE_DESCRIPTION("L2TP ethernet pseudowire driver");
408MODULE_VERSION("1.0");
409MODULE_ALIAS_L2TP_PWTYPE(5);