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>
20 #include <linux/etherdevice.h>
21 #include <linux/spinlock.h>
26 #include <net/inet_common.h>
27 #include <net/inet_hashtables.h>
28 #include <net/tcp_states.h>
29 #include <net/protocol.h>
31 #include <net/net_namespace.h>
32 #include <net/netns/generic.h>
34 #include "l2tp_core.h"
36 /* Default device name. May be overridden by name specified by user */
37 #define L2TP_ETH_DEV_NAME "l2tpeth%d"
39 /* via netdev_priv() */
41 struct net_device *dev;
42 struct sock *tunnel_sock;
43 struct l2tp_session *session;
44 struct list_head list;
45 atomic_long_t tx_bytes;
46 atomic_long_t tx_packets;
47 atomic_long_t rx_bytes;
48 atomic_long_t rx_packets;
49 atomic_long_t rx_errors;
52 /* via l2tp_session_priv() */
53 struct l2tp_eth_sess {
54 struct net_device *dev;
57 /* per-net private data for this module */
58 static unsigned int l2tp_eth_net_id;
60 struct list_head l2tp_eth_dev_list;
61 spinlock_t l2tp_eth_lock;
64 static inline struct l2tp_eth_net *l2tp_eth_pernet(struct net *net)
66 return net_generic(net, l2tp_eth_net_id);
69 static int l2tp_eth_dev_init(struct net_device *dev)
71 struct l2tp_eth *priv = netdev_priv(dev);
74 eth_hw_addr_random(dev);
75 memset(&dev->broadcast[0], 0xff, 6);
80 static void l2tp_eth_dev_uninit(struct net_device *dev)
82 struct l2tp_eth *priv = netdev_priv(dev);
83 struct l2tp_eth_net *pn = l2tp_eth_pernet(dev_net(dev));
85 spin_lock(&pn->l2tp_eth_lock);
86 list_del_init(&priv->list);
87 spin_unlock(&pn->l2tp_eth_lock);
91 static int l2tp_eth_dev_xmit(struct sk_buff *skb, struct net_device *dev)
93 struct l2tp_eth *priv = netdev_priv(dev);
94 struct l2tp_session *session = priv->session;
96 atomic_long_add(skb->len, &priv->tx_bytes);
97 atomic_long_inc(&priv->tx_packets);
99 l2tp_xmit_skb(session, skb, session->hdr_len);
104 static struct rtnl_link_stats64 *l2tp_eth_get_stats64(struct net_device *dev,
105 struct rtnl_link_stats64 *stats)
107 struct l2tp_eth *priv = netdev_priv(dev);
109 stats->tx_bytes = atomic_long_read(&priv->tx_bytes);
110 stats->tx_packets = atomic_long_read(&priv->tx_packets);
111 stats->rx_bytes = atomic_long_read(&priv->rx_bytes);
112 stats->rx_packets = atomic_long_read(&priv->rx_packets);
113 stats->rx_errors = atomic_long_read(&priv->rx_errors);
118 static struct net_device_ops l2tp_eth_netdev_ops = {
119 .ndo_init = l2tp_eth_dev_init,
120 .ndo_uninit = l2tp_eth_dev_uninit,
121 .ndo_start_xmit = l2tp_eth_dev_xmit,
122 .ndo_get_stats64 = l2tp_eth_get_stats64,
125 static void l2tp_eth_dev_setup(struct net_device *dev)
128 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
129 dev->features |= NETIF_F_LLTX;
130 dev->netdev_ops = &l2tp_eth_netdev_ops;
131 dev->destructor = free_netdev;
134 static void l2tp_eth_dev_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
136 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
137 struct net_device *dev = spriv->dev;
138 struct l2tp_eth *priv = netdev_priv(dev);
140 if (session->debug & L2TP_MSG_DATA) {
143 length = min(32u, skb->len);
144 if (!pskb_may_pull(skb, length))
147 pr_debug("%s: eth recv\n", session->name);
148 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
151 if (!pskb_may_pull(skb, sizeof(ETH_HLEN)))
156 /* checksums verified by L2TP */
157 skb->ip_summed = CHECKSUM_NONE;
162 if (dev_forward_skb(dev, skb) == NET_RX_SUCCESS) {
163 atomic_long_inc(&priv->rx_packets);
164 atomic_long_add(data_len, &priv->rx_bytes);
166 atomic_long_inc(&priv->rx_errors);
171 atomic_long_inc(&priv->rx_errors);
175 static void l2tp_eth_delete(struct l2tp_session *session)
177 struct l2tp_eth_sess *spriv;
178 struct net_device *dev;
181 spriv = l2tp_session_priv(session);
184 unregister_netdev(dev);
186 module_put(THIS_MODULE);
191 #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
192 static void l2tp_eth_show(struct seq_file *m, void *arg)
194 struct l2tp_session *session = arg;
195 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
196 struct net_device *dev = spriv->dev;
198 seq_printf(m, " interface %s\n", dev->name);
202 static int l2tp_eth_create(struct net *net, u32 tunnel_id, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
204 struct net_device *dev;
206 struct l2tp_tunnel *tunnel;
207 struct l2tp_session *session;
208 struct l2tp_eth *priv;
209 struct l2tp_eth_sess *spriv;
211 struct l2tp_eth_net *pn;
213 tunnel = l2tp_tunnel_find(net, tunnel_id);
219 session = l2tp_session_find(net, tunnel, session_id);
226 dev = dev_get_by_name(net, cfg->ifname);
232 strlcpy(name, cfg->ifname, IFNAMSIZ);
234 strcpy(name, L2TP_ETH_DEV_NAME);
236 session = l2tp_session_create(sizeof(*spriv), tunnel, session_id,
237 peer_session_id, cfg);
243 dev = alloc_netdev(sizeof(*priv), name, l2tp_eth_dev_setup);
246 goto out_del_session;
249 dev_net_set(dev, net);
250 if (session->mtu == 0)
251 session->mtu = dev->mtu - session->hdr_len;
252 dev->mtu = session->mtu;
253 dev->needed_headroom += session->hdr_len;
255 priv = netdev_priv(dev);
257 priv->session = session;
258 INIT_LIST_HEAD(&priv->list);
260 priv->tunnel_sock = tunnel->sock;
261 session->recv_skb = l2tp_eth_dev_recv;
262 session->session_close = l2tp_eth_delete;
263 #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
264 session->show = l2tp_eth_show;
267 spriv = l2tp_session_priv(session);
270 rc = register_netdev(dev);
274 __module_get(THIS_MODULE);
275 /* Must be done after register_netdev() */
276 strlcpy(session->ifname, dev->name, IFNAMSIZ);
279 pn = l2tp_eth_pernet(dev_net(dev));
280 spin_lock(&pn->l2tp_eth_lock);
281 list_add(&priv->list, &pn->l2tp_eth_dev_list);
282 spin_unlock(&pn->l2tp_eth_lock);
289 l2tp_session_delete(session);
294 static __net_init int l2tp_eth_init_net(struct net *net)
296 struct l2tp_eth_net *pn = net_generic(net, l2tp_eth_net_id);
298 INIT_LIST_HEAD(&pn->l2tp_eth_dev_list);
299 spin_lock_init(&pn->l2tp_eth_lock);
304 static struct pernet_operations l2tp_eth_net_ops = {
305 .init = l2tp_eth_init_net,
306 .id = &l2tp_eth_net_id,
307 .size = sizeof(struct l2tp_eth_net),
311 static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops = {
312 .session_create = l2tp_eth_create,
313 .session_delete = l2tp_session_delete,
317 static int __init l2tp_eth_init(void)
321 err = l2tp_nl_register_ops(L2TP_PWTYPE_ETH, &l2tp_eth_nl_cmd_ops);
325 err = register_pernet_device(&l2tp_eth_net_ops);
329 pr_info("L2TP ethernet pseudowire support (L2TPv3)\n");
334 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH);
339 static void __exit l2tp_eth_exit(void)
341 unregister_pernet_device(&l2tp_eth_net_ops);
342 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH);
345 module_init(l2tp_eth_init);
346 module_exit(l2tp_eth_exit);
348 MODULE_LICENSE("GPL");
349 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
350 MODULE_DESCRIPTION("L2TP ethernet pseudowire driver");
351 MODULE_VERSION("1.0");