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 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/socket.h>
15 #include <linux/hash.h>
16 #include <linux/l2tp.h>
18 #include <linux/etherdevice.h>
19 #include <linux/spinlock.h>
24 #include <net/inet_common.h>
25 #include <net/inet_hashtables.h>
26 #include <net/tcp_states.h>
27 #include <net/protocol.h>
29 #include <net/net_namespace.h>
30 #include <net/netns/generic.h>
32 #include "l2tp_core.h"
34 /* Default device name. May be overridden by name specified by user */
35 #define L2TP_ETH_DEV_NAME "l2tpeth%d"
37 /* via netdev_priv() */
39 struct net_device *dev;
40 struct sock *tunnel_sock;
41 struct l2tp_session *session;
44 /* via l2tp_session_priv() */
45 struct l2tp_eth_sess {
46 struct net_device __rcu *dev;
50 static int l2tp_eth_dev_init(struct net_device *dev)
52 struct l2tp_eth *priv = netdev_priv(dev);
55 random_ether_addr(dev->dev_addr);
56 memset(&dev->broadcast[0], 0xff, 6);
61 static void l2tp_eth_dev_uninit(struct net_device *dev)
63 struct l2tp_eth *priv = netdev_priv(dev);
64 struct l2tp_eth_sess *spriv;
66 spriv = l2tp_session_priv(priv->session);
67 RCU_INIT_POINTER(spriv->dev, NULL);
68 /* No need for synchronize_net() here. We're called by
69 * unregister_netdev*(), which does the synchronisation for us.
73 static int l2tp_eth_dev_xmit(struct sk_buff *skb, struct net_device *dev)
75 struct l2tp_eth *priv = netdev_priv(dev);
76 struct l2tp_session *session = priv->session;
78 l2tp_xmit_skb(session, skb, session->hdr_len);
80 dev->stats.tx_bytes += skb->len;
81 dev->stats.tx_packets++;
86 static struct net_device_ops l2tp_eth_netdev_ops = {
87 .ndo_init = l2tp_eth_dev_init,
88 .ndo_uninit = l2tp_eth_dev_uninit,
89 .ndo_start_xmit = l2tp_eth_dev_xmit,
92 static void l2tp_eth_dev_setup(struct net_device *dev)
95 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
96 dev->netdev_ops = &l2tp_eth_netdev_ops;
97 dev->destructor = free_netdev;
100 static void l2tp_eth_dev_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
102 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
103 struct net_device *dev;
105 if (session->debug & L2TP_MSG_DATA) {
110 length = min(32u, skb->len);
111 if (!pskb_may_pull(skb, length))
114 printk(KERN_DEBUG "%s: eth recv: ", session->name);
118 printk(" %02X", ptr[offset]);
119 } while (++offset < length);
124 if (!pskb_may_pull(skb, ETH_HLEN))
129 /* checksums verified by L2TP */
130 skb->ip_summed = CHECKSUM_NONE;
136 dev = rcu_dereference(spriv->dev);
140 if (dev_forward_skb(dev, skb) == NET_RX_SUCCESS) {
141 dev->stats.rx_packets++;
142 dev->stats.rx_bytes += data_len;
144 dev->stats.rx_errors++;
152 dev->stats.rx_errors++;
156 static void l2tp_eth_delete(struct l2tp_session *session)
158 struct l2tp_eth_sess *spriv;
159 struct net_device *dev;
162 spriv = l2tp_session_priv(session);
165 dev = rtnl_dereference(spriv->dev);
167 unregister_netdevice(dev);
169 module_put(THIS_MODULE);
176 #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
177 static void l2tp_eth_show(struct seq_file *m, void *arg)
179 struct l2tp_session *session = arg;
180 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
181 struct net_device *dev;
184 dev = rcu_dereference(spriv->dev);
192 seq_printf(m, " interface %s\n", dev->name);
198 static int l2tp_eth_create(struct net *net, struct l2tp_tunnel *tunnel,
199 u32 session_id, u32 peer_session_id,
200 struct l2tp_session_cfg *cfg)
202 struct net_device *dev;
204 struct l2tp_session *session;
205 struct l2tp_eth *priv;
206 struct l2tp_eth_sess *spriv;
210 dev = dev_get_by_name(net, cfg->ifname);
216 strlcpy(name, cfg->ifname, IFNAMSIZ);
218 strcpy(name, L2TP_ETH_DEV_NAME);
220 session = l2tp_session_create(sizeof(*spriv), tunnel, session_id,
221 peer_session_id, cfg);
222 if (IS_ERR(session)) {
223 rc = PTR_ERR(session);
227 dev = alloc_netdev(sizeof(*priv), name, l2tp_eth_dev_setup);
233 dev_net_set(dev, net);
234 if (session->mtu == 0)
235 session->mtu = dev->mtu - session->hdr_len;
236 dev->mtu = session->mtu;
237 dev->needed_headroom += session->hdr_len;
239 priv = netdev_priv(dev);
241 priv->session = session;
243 priv->tunnel_sock = tunnel->sock;
244 session->recv_skb = l2tp_eth_dev_recv;
245 session->session_close = l2tp_eth_delete;
246 #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
247 session->show = l2tp_eth_show;
250 spriv = l2tp_session_priv(session);
252 l2tp_session_inc_refcount(session);
256 /* Register both device and session while holding the rtnl lock. This
257 * ensures that l2tp_eth_delete() will see that there's a device to
258 * unregister, even if it happened to run before we assign spriv->dev.
260 rc = l2tp_session_register(session, tunnel);
266 rc = register_netdevice(dev);
269 l2tp_session_delete(session);
270 l2tp_session_dec_refcount(session);
276 strlcpy(session->ifname, dev->name, IFNAMSIZ);
277 rcu_assign_pointer(spriv->dev, dev);
281 l2tp_session_dec_refcount(session);
283 __module_get(THIS_MODULE);
288 l2tp_session_dec_refcount(session);
297 static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops = {
298 .session_create = l2tp_eth_create,
299 .session_delete = l2tp_session_delete,
303 static int __init l2tp_eth_init(void)
307 err = l2tp_nl_register_ops(L2TP_PWTYPE_ETH, &l2tp_eth_nl_cmd_ops);
311 printk(KERN_INFO "L2TP ethernet pseudowire support (L2TPv3)\n");
319 static void __exit l2tp_eth_exit(void)
321 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH);
324 module_init(l2tp_eth_init);
325 module_exit(l2tp_eth_exit);
327 MODULE_LICENSE("GPL");
328 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
329 MODULE_DESCRIPTION("L2TP ethernet pseudowire driver");
330 MODULE_VERSION("1.0");