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
;
42 struct list_head list
;
45 /* via l2tp_session_priv() */
46 struct l2tp_eth_sess
{
47 struct net_device
*dev
;
50 /* per-net private data for this module */
51 static unsigned int l2tp_eth_net_id
;
53 struct list_head l2tp_eth_dev_list
;
54 spinlock_t l2tp_eth_lock
;
57 static inline struct l2tp_eth_net
*l2tp_eth_pernet(struct net
*net
)
59 return net_generic(net
, l2tp_eth_net_id
);
62 static int l2tp_eth_dev_init(struct net_device
*dev
)
64 struct l2tp_eth
*priv
= netdev_priv(dev
);
67 random_ether_addr(dev
->dev_addr
);
68 memset(&dev
->broadcast
[0], 0xff, 6);
73 static void l2tp_eth_dev_uninit(struct net_device
*dev
)
75 struct l2tp_eth
*priv
= netdev_priv(dev
);
76 struct l2tp_eth_net
*pn
= l2tp_eth_pernet(dev_net(dev
));
78 spin_lock(&pn
->l2tp_eth_lock
);
79 list_del_init(&priv
->list
);
80 spin_unlock(&pn
->l2tp_eth_lock
);
84 static int l2tp_eth_dev_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
86 struct l2tp_eth
*priv
= netdev_priv(dev
);
87 struct l2tp_session
*session
= priv
->session
;
89 l2tp_xmit_skb(session
, skb
, session
->hdr_len
);
91 dev
->stats
.tx_bytes
+= skb
->len
;
92 dev
->stats
.tx_packets
++;
97 static struct net_device_ops l2tp_eth_netdev_ops
= {
98 .ndo_init
= l2tp_eth_dev_init
,
99 .ndo_uninit
= l2tp_eth_dev_uninit
,
100 .ndo_start_xmit
= l2tp_eth_dev_xmit
,
103 static void l2tp_eth_dev_setup(struct net_device
*dev
)
107 dev
->netdev_ops
= &l2tp_eth_netdev_ops
;
108 dev
->destructor
= free_netdev
;
111 static void l2tp_eth_dev_recv(struct l2tp_session
*session
, struct sk_buff
*skb
, int data_len
)
113 struct l2tp_eth_sess
*spriv
= l2tp_session_priv(session
);
114 struct net_device
*dev
= spriv
->dev
;
116 if (session
->debug
& L2TP_MSG_DATA
) {
121 length
= min(32u, skb
->len
);
122 if (!pskb_may_pull(skb
, length
))
125 printk(KERN_DEBUG
"%s: eth recv: ", session
->name
);
129 printk(" %02X", ptr
[offset
]);
130 } while (++offset
< length
);
135 if (data_len
< ETH_HLEN
)
140 /* checksums verified by L2TP */
141 skb
->ip_summed
= CHECKSUM_NONE
;
146 if (dev_forward_skb(dev
, skb
) == NET_RX_SUCCESS
) {
147 dev
->last_rx
= jiffies
;
148 dev
->stats
.rx_packets
++;
149 dev
->stats
.rx_bytes
+= data_len
;
151 dev
->stats
.rx_errors
++;
156 dev
->stats
.rx_errors
++;
160 static void l2tp_eth_delete(struct l2tp_session
*session
)
162 struct l2tp_eth_sess
*spriv
;
163 struct net_device
*dev
;
166 spriv
= l2tp_session_priv(session
);
169 unregister_netdev(dev
);
175 #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
176 static void l2tp_eth_show(struct seq_file
*m
, void *arg
)
178 struct l2tp_session
*session
= arg
;
179 struct l2tp_eth_sess
*spriv
= l2tp_session_priv(session
);
180 struct net_device
*dev
= spriv
->dev
;
182 seq_printf(m
, " interface %s\n", dev
->name
);
186 static int l2tp_eth_create(struct net
*net
, u32 tunnel_id
, u32 session_id
, u32 peer_session_id
, struct l2tp_session_cfg
*cfg
)
188 struct net_device
*dev
;
190 struct l2tp_tunnel
*tunnel
;
191 struct l2tp_session
*session
;
192 struct l2tp_eth
*priv
;
193 struct l2tp_eth_sess
*spriv
;
195 struct l2tp_eth_net
*pn
;
197 tunnel
= l2tp_tunnel_find(net
, tunnel_id
);
203 session
= l2tp_session_find(net
, tunnel
, session_id
);
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
);
227 dev
= alloc_netdev(sizeof(*priv
), name
, l2tp_eth_dev_setup
);
230 goto out_del_session
;
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
;
242 INIT_LIST_HEAD(&priv
->list
);
244 priv
->tunnel_sock
= tunnel
->sock
;
245 session
->recv_skb
= l2tp_eth_dev_recv
;
246 session
->session_close
= l2tp_eth_delete
;
247 #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
248 session
->show
= l2tp_eth_show
;
251 spriv
= l2tp_session_priv(session
);
254 rc
= register_netdev(dev
);
258 /* Must be done after register_netdev() */
259 strlcpy(session
->ifname
, dev
->name
, IFNAMSIZ
);
262 pn
= l2tp_eth_pernet(dev_net(dev
));
263 spin_lock(&pn
->l2tp_eth_lock
);
264 list_add(&priv
->list
, &pn
->l2tp_eth_dev_list
);
265 spin_unlock(&pn
->l2tp_eth_lock
);
272 l2tp_session_delete(session
);
277 static __net_init
int l2tp_eth_init_net(struct net
*net
)
279 struct l2tp_eth_net
*pn
= net_generic(net
, l2tp_eth_net_id
);
281 INIT_LIST_HEAD(&pn
->l2tp_eth_dev_list
);
282 spin_lock_init(&pn
->l2tp_eth_lock
);
287 static __net_initdata
struct pernet_operations l2tp_eth_net_ops
= {
288 .init
= l2tp_eth_init_net
,
289 .id
= &l2tp_eth_net_id
,
290 .size
= sizeof(struct l2tp_eth_net
),
294 static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops
= {
295 .session_create
= l2tp_eth_create
,
296 .session_delete
= l2tp_session_delete
,
300 static int __init
l2tp_eth_init(void)
304 err
= l2tp_nl_register_ops(L2TP_PWTYPE_ETH
, &l2tp_eth_nl_cmd_ops
);
308 err
= register_pernet_device(&l2tp_eth_net_ops
);
312 printk(KERN_INFO
"L2TP ethernet pseudowire support (L2TPv3)\n");
317 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH
);
322 static void __exit
l2tp_eth_exit(void)
324 unregister_pernet_device(&l2tp_eth_net_ops
);
325 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH
);
328 module_init(l2tp_eth_init
);
329 module_exit(l2tp_eth_exit
);
331 MODULE_LICENSE("GPL");
332 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
333 MODULE_DESCRIPTION("L2TP ethernet pseudowire driver");
334 MODULE_VERSION("1.0");