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