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