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