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