Linux 4.19.133
[linux/fpc-iii.git] / net / l2tp / l2tp_eth.c
blob8aadc4f3bb9e91a6f8c323b91c7ca8d188c58050
1 /*
2 * L2TPv3 ethernet pseudowire driver
4 * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
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.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
37 #include "l2tp_core.h"
39 /* Default device name. May be overridden by name specified by user */
40 #define L2TP_ETH_DEV_NAME "l2tpeth%d"
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;
53 /* via l2tp_session_priv() */
54 struct l2tp_eth_sess {
55 struct net_device __rcu *dev;
59 static int l2tp_eth_dev_init(struct net_device *dev)
61 eth_hw_addr_random(dev);
62 eth_broadcast_addr(dev->broadcast);
63 netdev_lockdep_set_classes(dev);
65 return 0;
68 static void l2tp_eth_dev_uninit(struct net_device *dev)
70 struct l2tp_eth *priv = netdev_priv(dev);
71 struct l2tp_eth_sess *spriv;
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.
80 static int l2tp_eth_dev_xmit(struct sk_buff *skb, struct net_device *dev)
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);
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);
93 return NETDEV_TX_OK;
96 static void l2tp_eth_get_stats64(struct net_device *dev,
97 struct rtnl_link_stats64 *stats)
99 struct l2tp_eth *priv = netdev_priv(dev);
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);
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,
118 static struct device_type l2tpeth_type = {
119 .name = "l2tpeth",
122 static void l2tp_eth_dev_setup(struct net_device *dev)
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;
132 static void l2tp_eth_dev_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
134 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
135 struct net_device *dev;
136 struct l2tp_eth *priv;
138 if (session->debug & L2TP_MSG_DATA) {
139 unsigned int length;
141 length = min(32u, skb->len);
142 if (!pskb_may_pull(skb, length))
143 goto error;
145 pr_debug("%s: eth recv\n", session->name);
146 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
149 if (!pskb_may_pull(skb, ETH_HLEN))
150 goto error;
152 secpath_reset(skb);
154 /* checksums verified by L2TP */
155 skb->ip_summed = CHECKSUM_NONE;
157 skb_dst_drop(skb);
158 nf_reset(skb);
160 rcu_read_lock();
161 dev = rcu_dereference(spriv->dev);
162 if (!dev)
163 goto error_rcu;
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);
172 rcu_read_unlock();
174 return;
176 error_rcu:
177 rcu_read_unlock();
178 error:
179 kfree_skb(skb);
182 static void l2tp_eth_delete(struct l2tp_session *session)
184 struct l2tp_eth_sess *spriv;
185 struct net_device *dev;
187 if (session) {
188 spriv = l2tp_session_priv(session);
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();
202 static void l2tp_eth_show(struct seq_file *m, void *arg)
204 struct l2tp_session *session = arg;
205 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
206 struct net_device *dev;
208 rcu_read_lock();
209 dev = rcu_dereference(spriv->dev);
210 if (!dev) {
211 rcu_read_unlock();
212 return;
214 dev_hold(dev);
215 rcu_read_unlock();
217 seq_printf(m, " interface %s\n", dev->name);
219 dev_put(dev);
222 static void l2tp_eth_adjust_mtu(struct l2tp_tunnel *tunnel,
223 struct l2tp_session *session,
224 struct net_device *dev)
226 unsigned int overhead = 0;
227 u32 l3_overhead = 0;
228 u32 mtu;
230 /* if the encap is UDP, account for UDP header size */
231 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
232 overhead += sizeof(struct udphdr);
233 dev->needed_headroom += sizeof(struct udphdr);
236 lock_sock(tunnel->sock);
237 l3_overhead = kernel_sock_ip_overhead(tunnel->sock);
238 release_sock(tunnel->sock);
240 if (l3_overhead == 0) {
241 /* L3 Overhead couldn't be identified, this could be
242 * because tunnel->sock was NULL or the socket's
243 * address family was not IPv4 or IPv6,
244 * dev mtu stays at 1500.
246 return;
248 /* Adjust MTU, factor overhead - underlay L3, overlay L2 hdr
249 * UDP overhead, if any, was already factored in above.
251 overhead += session->hdr_len + ETH_HLEN + l3_overhead;
253 mtu = l2tp_tunnel_dst_mtu(tunnel) - overhead;
254 if (mtu < dev->min_mtu || mtu > dev->max_mtu)
255 dev->mtu = ETH_DATA_LEN - overhead;
256 else
257 dev->mtu = mtu;
259 dev->needed_headroom += session->hdr_len;
262 static int l2tp_eth_create(struct net *net, struct l2tp_tunnel *tunnel,
263 u32 session_id, u32 peer_session_id,
264 struct l2tp_session_cfg *cfg)
266 unsigned char name_assign_type;
267 struct net_device *dev;
268 char name[IFNAMSIZ];
269 struct l2tp_session *session;
270 struct l2tp_eth *priv;
271 struct l2tp_eth_sess *spriv;
272 int rc;
274 if (cfg->ifname) {
275 strlcpy(name, cfg->ifname, IFNAMSIZ);
276 name_assign_type = NET_NAME_USER;
277 } else {
278 strcpy(name, L2TP_ETH_DEV_NAME);
279 name_assign_type = NET_NAME_ENUM;
282 session = l2tp_session_create(sizeof(*spriv), tunnel, session_id,
283 peer_session_id, cfg);
284 if (IS_ERR(session)) {
285 rc = PTR_ERR(session);
286 goto err;
289 dev = alloc_netdev(sizeof(*priv), name, name_assign_type,
290 l2tp_eth_dev_setup);
291 if (!dev) {
292 rc = -ENOMEM;
293 goto err_sess;
296 dev_net_set(dev, net);
297 dev->min_mtu = 0;
298 dev->max_mtu = ETH_MAX_MTU;
299 l2tp_eth_adjust_mtu(tunnel, session, dev);
301 priv = netdev_priv(dev);
302 priv->session = session;
304 session->recv_skb = l2tp_eth_dev_recv;
305 session->session_close = l2tp_eth_delete;
306 if (IS_ENABLED(CONFIG_L2TP_DEBUGFS))
307 session->show = l2tp_eth_show;
309 spriv = l2tp_session_priv(session);
311 l2tp_session_inc_refcount(session);
313 rtnl_lock();
315 /* Register both device and session while holding the rtnl lock. This
316 * ensures that l2tp_eth_delete() will see that there's a device to
317 * unregister, even if it happened to run before we assign spriv->dev.
319 rc = l2tp_session_register(session, tunnel);
320 if (rc < 0) {
321 rtnl_unlock();
322 goto err_sess_dev;
325 rc = register_netdevice(dev);
326 if (rc < 0) {
327 rtnl_unlock();
328 l2tp_session_delete(session);
329 l2tp_session_dec_refcount(session);
330 free_netdev(dev);
332 return rc;
335 strlcpy(session->ifname, dev->name, IFNAMSIZ);
336 rcu_assign_pointer(spriv->dev, dev);
338 rtnl_unlock();
340 l2tp_session_dec_refcount(session);
342 __module_get(THIS_MODULE);
344 return 0;
346 err_sess_dev:
347 l2tp_session_dec_refcount(session);
348 free_netdev(dev);
349 err_sess:
350 kfree(session);
351 err:
352 return rc;
356 static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops = {
357 .session_create = l2tp_eth_create,
358 .session_delete = l2tp_session_delete,
362 static int __init l2tp_eth_init(void)
364 int err = 0;
366 err = l2tp_nl_register_ops(L2TP_PWTYPE_ETH, &l2tp_eth_nl_cmd_ops);
367 if (err)
368 goto err;
370 pr_info("L2TP ethernet pseudowire support (L2TPv3)\n");
372 return 0;
374 err:
375 return err;
378 static void __exit l2tp_eth_exit(void)
380 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH);
383 module_init(l2tp_eth_init);
384 module_exit(l2tp_eth_exit);
386 MODULE_LICENSE("GPL");
387 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
388 MODULE_DESCRIPTION("L2TP ethernet pseudowire driver");
389 MODULE_VERSION("1.0");
390 MODULE_ALIAS_L2TP_PWTYPE(5);