xref: /openbmc/linux/net/l2tp/l2tp_eth.c (revision 11788d9b)
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 	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() */
49 struct l2tp_eth_sess {
50 	struct net_device __rcu *dev;
51 };
52 
53 static 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 
62 static 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 
74 static 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, session->hdr_len);
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 
90 static 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 
103 static 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 
111 static struct device_type l2tpeth_type = {
112 	.name = "l2tpeth",
113 };
114 
115 static 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 
125 static 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 (session->debug & L2TP_MSG_DATA) {
132 		unsigned int length;
133 
134 		length = min(32u, skb->len);
135 		if (!pskb_may_pull(skb, length))
136 			goto error;
137 
138 		pr_debug("%s: eth recv\n", session->name);
139 		print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
140 	}
141 
142 	if (!pskb_may_pull(skb, ETH_HLEN))
143 		goto error;
144 
145 	secpath_reset(skb);
146 
147 	/* checksums verified by L2TP */
148 	skb->ip_summed = CHECKSUM_NONE;
149 
150 	skb_dst_drop(skb);
151 	nf_reset_ct(skb);
152 
153 	rcu_read_lock();
154 	dev = rcu_dereference(spriv->dev);
155 	if (!dev)
156 		goto error_rcu;
157 
158 	priv = netdev_priv(dev);
159 	if (dev_forward_skb(dev, skb) == NET_RX_SUCCESS) {
160 		atomic_long_inc(&priv->rx_packets);
161 		atomic_long_add(data_len, &priv->rx_bytes);
162 	} else {
163 		atomic_long_inc(&priv->rx_errors);
164 	}
165 	rcu_read_unlock();
166 
167 	return;
168 
169 error_rcu:
170 	rcu_read_unlock();
171 error:
172 	kfree_skb(skb);
173 }
174 
175 static 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 
183 		rtnl_lock();
184 		dev = rtnl_dereference(spriv->dev);
185 		if (dev) {
186 			unregister_netdevice(dev);
187 			rtnl_unlock();
188 			module_put(THIS_MODULE);
189 		} else {
190 			rtnl_unlock();
191 		}
192 	}
193 }
194 
195 static void l2tp_eth_show(struct seq_file *m, void *arg)
196 {
197 	struct l2tp_session *session = arg;
198 	struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
199 	struct net_device *dev;
200 
201 	rcu_read_lock();
202 	dev = rcu_dereference(spriv->dev);
203 	if (!dev) {
204 		rcu_read_unlock();
205 		return;
206 	}
207 	dev_hold(dev);
208 	rcu_read_unlock();
209 
210 	seq_printf(m, "   interface %s\n", dev->name);
211 
212 	dev_put(dev);
213 }
214 
215 static 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 	u32 l3_overhead = 0;
221 	u32 mtu;
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 
229 	lock_sock(tunnel->sock);
230 	l3_overhead = kernel_sock_ip_overhead(tunnel->sock);
231 	release_sock(tunnel->sock);
232 
233 	if (l3_overhead == 0) {
234 		/* L3 Overhead couldn't be identified, this could be
235 		 * because tunnel->sock was NULL or the socket's
236 		 * address family was not IPv4 or IPv6,
237 		 * dev mtu stays at 1500.
238 		 */
239 		return;
240 	}
241 	/* Adjust MTU, factor overhead - underlay L3, overlay L2 hdr
242 	 * UDP overhead, if any, was already factored in above.
243 	 */
244 	overhead += session->hdr_len + ETH_HLEN + l3_overhead;
245 
246 	mtu = l2tp_tunnel_dst_mtu(tunnel) - overhead;
247 	if (mtu < dev->min_mtu || mtu > dev->max_mtu)
248 		dev->mtu = ETH_DATA_LEN - overhead;
249 	else
250 		dev->mtu = mtu;
251 
252 	dev->needed_headroom += session->hdr_len;
253 }
254 
255 static int l2tp_eth_create(struct net *net, struct l2tp_tunnel *tunnel,
256 			   u32 session_id, u32 peer_session_id,
257 			   struct l2tp_session_cfg *cfg)
258 {
259 	unsigned char name_assign_type;
260 	struct net_device *dev;
261 	char name[IFNAMSIZ];
262 	struct l2tp_session *session;
263 	struct l2tp_eth *priv;
264 	struct l2tp_eth_sess *spriv;
265 	int rc;
266 
267 	if (cfg->ifname) {
268 		strlcpy(name, cfg->ifname, IFNAMSIZ);
269 		name_assign_type = NET_NAME_USER;
270 	} else {
271 		strcpy(name, L2TP_ETH_DEV_NAME);
272 		name_assign_type = NET_NAME_ENUM;
273 	}
274 
275 	session = l2tp_session_create(sizeof(*spriv), tunnel, session_id,
276 				      peer_session_id, cfg);
277 	if (IS_ERR(session)) {
278 		rc = PTR_ERR(session);
279 		goto err;
280 	}
281 
282 	dev = alloc_netdev(sizeof(*priv), name, name_assign_type,
283 			   l2tp_eth_dev_setup);
284 	if (!dev) {
285 		rc = -ENOMEM;
286 		goto err_sess;
287 	}
288 
289 	dev_net_set(dev, net);
290 	dev->min_mtu = 0;
291 	dev->max_mtu = ETH_MAX_MTU;
292 	l2tp_eth_adjust_mtu(tunnel, session, dev);
293 
294 	priv = netdev_priv(dev);
295 	priv->session = session;
296 
297 	session->recv_skb = l2tp_eth_dev_recv;
298 	session->session_close = l2tp_eth_delete;
299 	if (IS_ENABLED(CONFIG_L2TP_DEBUGFS))
300 		session->show = l2tp_eth_show;
301 
302 	spriv = l2tp_session_priv(session);
303 
304 	l2tp_session_inc_refcount(session);
305 
306 	rtnl_lock();
307 
308 	/* Register both device and session while holding the rtnl lock. This
309 	 * ensures that l2tp_eth_delete() will see that there's a device to
310 	 * unregister, even if it happened to run before we assign spriv->dev.
311 	 */
312 	rc = l2tp_session_register(session, tunnel);
313 	if (rc < 0) {
314 		rtnl_unlock();
315 		goto err_sess_dev;
316 	}
317 
318 	rc = register_netdevice(dev);
319 	if (rc < 0) {
320 		rtnl_unlock();
321 		l2tp_session_delete(session);
322 		l2tp_session_dec_refcount(session);
323 		free_netdev(dev);
324 
325 		return rc;
326 	}
327 
328 	strlcpy(session->ifname, dev->name, IFNAMSIZ);
329 	rcu_assign_pointer(spriv->dev, dev);
330 
331 	rtnl_unlock();
332 
333 	l2tp_session_dec_refcount(session);
334 
335 	__module_get(THIS_MODULE);
336 
337 	return 0;
338 
339 err_sess_dev:
340 	l2tp_session_dec_refcount(session);
341 	free_netdev(dev);
342 err_sess:
343 	kfree(session);
344 err:
345 	return rc;
346 }
347 
348 static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops = {
349 	.session_create	= l2tp_eth_create,
350 	.session_delete	= l2tp_session_delete,
351 };
352 
353 static int __init l2tp_eth_init(void)
354 {
355 	int err = 0;
356 
357 	err = l2tp_nl_register_ops(L2TP_PWTYPE_ETH, &l2tp_eth_nl_cmd_ops);
358 	if (err)
359 		goto err;
360 
361 	pr_info("L2TP ethernet pseudowire support (L2TPv3)\n");
362 
363 	return 0;
364 
365 err:
366 	return err;
367 }
368 
369 static void __exit l2tp_eth_exit(void)
370 {
371 	l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH);
372 }
373 
374 module_init(l2tp_eth_init);
375 module_exit(l2tp_eth_exit);
376 
377 MODULE_LICENSE("GPL");
378 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
379 MODULE_DESCRIPTION("L2TP ethernet pseudowire driver");
380 MODULE_VERSION("1.0");
381 MODULE_ALIAS_L2TP_PWTYPE(5);
382