Merge branch 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[linux-2.6-microblaze.git] / net / l2tp / l2tp_eth.c
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 static void l2tp_eth_show(struct seq_file *m, void *arg)
203 {
204         struct l2tp_session *session = arg;
205         struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
206         struct net_device *dev;
207
208         rcu_read_lock();
209         dev = rcu_dereference(spriv->dev);
210         if (!dev) {
211                 rcu_read_unlock();
212                 return;
213         }
214         dev_hold(dev);
215         rcu_read_unlock();
216
217         seq_printf(m, "   interface %s\n", dev->name);
218
219         dev_put(dev);
220 }
221
222 static void l2tp_eth_adjust_mtu(struct l2tp_tunnel *tunnel,
223                                 struct l2tp_session *session,
224                                 struct net_device *dev)
225 {
226         unsigned int overhead = 0;
227         u32 l3_overhead = 0;
228         u32 mtu;
229
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);
234         }
235
236         lock_sock(tunnel->sock);
237         l3_overhead = kernel_sock_ip_overhead(tunnel->sock);
238         release_sock(tunnel->sock);
239
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.
245                  */
246                 return;
247         }
248         /* Adjust MTU, factor overhead - underlay L3, overlay L2 hdr
249          * UDP overhead, if any, was already factored in above.
250          */
251         overhead += session->hdr_len + ETH_HLEN + l3_overhead;
252
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;
258
259         dev->needed_headroom += session->hdr_len;
260 }
261
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)
265 {
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;
273
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;
280         }
281
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;
287         }
288
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;
294         }
295
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);
300
301         priv = netdev_priv(dev);
302         priv->session = session;
303
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;
308
309         spriv = l2tp_session_priv(session);
310
311         l2tp_session_inc_refcount(session);
312
313         rtnl_lock();
314
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.
318          */
319         rc = l2tp_session_register(session, tunnel);
320         if (rc < 0) {
321                 rtnl_unlock();
322                 goto err_sess_dev;
323         }
324
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);
331
332                 return rc;
333         }
334
335         strlcpy(session->ifname, dev->name, IFNAMSIZ);
336         rcu_assign_pointer(spriv->dev, dev);
337
338         rtnl_unlock();
339
340         l2tp_session_dec_refcount(session);
341
342         __module_get(THIS_MODULE);
343
344         return 0;
345
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;
353 }
354
355
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,
359 };
360
361
362 static int __init l2tp_eth_init(void)
363 {
364         int err = 0;
365
366         err = l2tp_nl_register_ops(L2TP_PWTYPE_ETH, &l2tp_eth_nl_cmd_ops);
367         if (err)
368                 goto err;
369
370         pr_info("L2TP ethernet pseudowire support (L2TPv3)\n");
371
372         return 0;
373
374 err:
375         return err;
376 }
377
378 static void __exit l2tp_eth_exit(void)
379 {
380         l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH);
381 }
382
383 module_init(l2tp_eth_init);
384 module_exit(l2tp_eth_exit);
385
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);