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
;
47 /* via l2tp_session_priv() */
48 struct l2tp_eth_sess
{
49 struct net_device
*dev
;
52 /* per-net private data for this module */
53 static unsigned int l2tp_eth_net_id
;
55 struct list_head l2tp_eth_dev_list
;
56 spinlock_t l2tp_eth_lock
;
59 static inline struct l2tp_eth_net
*l2tp_eth_pernet(struct net
*net
)
61 return net_generic(net
, l2tp_eth_net_id
);
64 static int l2tp_eth_dev_init(struct net_device
*dev
)
66 struct l2tp_eth
*priv
= netdev_priv(dev
);
69 eth_hw_addr_random(dev
);
70 memset(&dev
->broadcast
[0], 0xff, 6);
75 static void l2tp_eth_dev_uninit(struct net_device
*dev
)
77 struct l2tp_eth
*priv
= netdev_priv(dev
);
78 struct l2tp_eth_net
*pn
= l2tp_eth_pernet(dev_net(dev
));
80 spin_lock(&pn
->l2tp_eth_lock
);
81 list_del_init(&priv
->list
);
82 spin_unlock(&pn
->l2tp_eth_lock
);
86 static int l2tp_eth_dev_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
88 struct l2tp_eth
*priv
= netdev_priv(dev
);
89 struct l2tp_session
*session
= priv
->session
;
91 l2tp_xmit_skb(session
, skb
, session
->hdr_len
);
93 dev
->stats
.tx_bytes
+= skb
->len
;
94 dev
->stats
.tx_packets
++;
99 static struct net_device_ops l2tp_eth_netdev_ops
= {
100 .ndo_init
= l2tp_eth_dev_init
,
101 .ndo_uninit
= l2tp_eth_dev_uninit
,
102 .ndo_start_xmit
= l2tp_eth_dev_xmit
,
105 static void l2tp_eth_dev_setup(struct net_device
*dev
)
108 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
109 dev
->netdev_ops
= &l2tp_eth_netdev_ops
;
110 dev
->destructor
= free_netdev
;
113 static void l2tp_eth_dev_recv(struct l2tp_session
*session
, struct sk_buff
*skb
, int data_len
)
115 struct l2tp_eth_sess
*spriv
= l2tp_session_priv(session
);
116 struct net_device
*dev
= spriv
->dev
;
118 if (session
->debug
& L2TP_MSG_DATA
) {
122 length
= min(32u, skb
->len
);
123 if (!pskb_may_pull(skb
, length
))
126 pr_debug("%s: eth recv\n", session
->name
);
127 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
, ptr
, length
);
130 if (!pskb_may_pull(skb
, sizeof(ETH_HLEN
)))
135 /* checksums verified by L2TP */
136 skb
->ip_summed
= CHECKSUM_NONE
;
141 if (dev_forward_skb(dev
, skb
) == NET_RX_SUCCESS
) {
142 dev
->stats
.rx_packets
++;
143 dev
->stats
.rx_bytes
+= data_len
;
145 dev
->stats
.rx_errors
++;
150 dev
->stats
.rx_errors
++;
154 static void l2tp_eth_delete(struct l2tp_session
*session
)
156 struct l2tp_eth_sess
*spriv
;
157 struct net_device
*dev
;
160 spriv
= l2tp_session_priv(session
);
163 unregister_netdev(dev
);
165 module_put(THIS_MODULE
);
170 #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
171 static void l2tp_eth_show(struct seq_file
*m
, void *arg
)
173 struct l2tp_session
*session
= arg
;
174 struct l2tp_eth_sess
*spriv
= l2tp_session_priv(session
);
175 struct net_device
*dev
= spriv
->dev
;
177 seq_printf(m
, " interface %s\n", dev
->name
);
181 static int l2tp_eth_create(struct net
*net
, u32 tunnel_id
, u32 session_id
, u32 peer_session_id
, struct l2tp_session_cfg
*cfg
)
183 struct net_device
*dev
;
185 struct l2tp_tunnel
*tunnel
;
186 struct l2tp_session
*session
;
187 struct l2tp_eth
*priv
;
188 struct l2tp_eth_sess
*spriv
;
190 struct l2tp_eth_net
*pn
;
192 tunnel
= l2tp_tunnel_find(net
, tunnel_id
);
198 session
= l2tp_session_find(net
, tunnel
, session_id
);
205 dev
= dev_get_by_name(net
, cfg
->ifname
);
211 strlcpy(name
, cfg
->ifname
, IFNAMSIZ
);
213 strcpy(name
, L2TP_ETH_DEV_NAME
);
215 session
= l2tp_session_create(sizeof(*spriv
), tunnel
, session_id
,
216 peer_session_id
, cfg
);
222 dev
= alloc_netdev(sizeof(*priv
), name
, l2tp_eth_dev_setup
);
225 goto out_del_session
;
228 dev_net_set(dev
, net
);
229 if (session
->mtu
== 0)
230 session
->mtu
= dev
->mtu
- session
->hdr_len
;
231 dev
->mtu
= session
->mtu
;
232 dev
->needed_headroom
+= session
->hdr_len
;
234 priv
= netdev_priv(dev
);
236 priv
->session
= session
;
237 INIT_LIST_HEAD(&priv
->list
);
239 priv
->tunnel_sock
= tunnel
->sock
;
240 session
->recv_skb
= l2tp_eth_dev_recv
;
241 session
->session_close
= l2tp_eth_delete
;
242 #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
243 session
->show
= l2tp_eth_show
;
246 spriv
= l2tp_session_priv(session
);
249 rc
= register_netdev(dev
);
253 __module_get(THIS_MODULE
);
254 /* Must be done after register_netdev() */
255 strlcpy(session
->ifname
, dev
->name
, IFNAMSIZ
);
258 pn
= l2tp_eth_pernet(dev_net(dev
));
259 spin_lock(&pn
->l2tp_eth_lock
);
260 list_add(&priv
->list
, &pn
->l2tp_eth_dev_list
);
261 spin_unlock(&pn
->l2tp_eth_lock
);
268 l2tp_session_delete(session
);
273 static __net_init
int l2tp_eth_init_net(struct net
*net
)
275 struct l2tp_eth_net
*pn
= net_generic(net
, l2tp_eth_net_id
);
277 INIT_LIST_HEAD(&pn
->l2tp_eth_dev_list
);
278 spin_lock_init(&pn
->l2tp_eth_lock
);
283 static struct pernet_operations l2tp_eth_net_ops
= {
284 .init
= l2tp_eth_init_net
,
285 .id
= &l2tp_eth_net_id
,
286 .size
= sizeof(struct l2tp_eth_net
),
290 static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops
= {
291 .session_create
= l2tp_eth_create
,
292 .session_delete
= l2tp_session_delete
,
296 static int __init
l2tp_eth_init(void)
300 err
= l2tp_nl_register_ops(L2TP_PWTYPE_ETH
, &l2tp_eth_nl_cmd_ops
);
304 err
= register_pernet_device(&l2tp_eth_net_ops
);
308 pr_info("L2TP ethernet pseudowire support (L2TPv3)\n");
313 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH
);
318 static void __exit
l2tp_eth_exit(void)
320 unregister_pernet_device(&l2tp_eth_net_ops
);
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");