443591d629caadff0da53b2c861b8748e61edf7e
[pandora-kernel.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
34 #include "l2tp_core.h"
35
36 /* Default device name. May be overridden by name specified by user */
37 #define L2TP_ETH_DEV_NAME       "l2tpeth%d"
38
39 /* via netdev_priv() */
40 struct l2tp_eth {
41         struct net_device       *dev;
42         struct sock             *tunnel_sock;
43         struct l2tp_session     *session;
44         struct list_head        list;
45 };
46
47 /* via l2tp_session_priv() */
48 struct l2tp_eth_sess {
49         struct net_device       *dev;
50 };
51
52 /* per-net private data for this module */
53 static unsigned int l2tp_eth_net_id;
54 struct l2tp_eth_net {
55         struct list_head l2tp_eth_dev_list;
56         spinlock_t l2tp_eth_lock;
57 };
58
59 static inline struct l2tp_eth_net *l2tp_eth_pernet(struct net *net)
60 {
61         return net_generic(net, l2tp_eth_net_id);
62 }
63
64 static int l2tp_eth_dev_init(struct net_device *dev)
65 {
66         struct l2tp_eth *priv = netdev_priv(dev);
67
68         priv->dev = dev;
69         eth_hw_addr_random(dev);
70         memset(&dev->broadcast[0], 0xff, 6);
71
72         return 0;
73 }
74
75 static void l2tp_eth_dev_uninit(struct net_device *dev)
76 {
77         struct l2tp_eth *priv = netdev_priv(dev);
78         struct l2tp_eth_net *pn = l2tp_eth_pernet(dev_net(dev));
79
80         spin_lock(&pn->l2tp_eth_lock);
81         list_del_init(&priv->list);
82         spin_unlock(&pn->l2tp_eth_lock);
83         dev_put(dev);
84 }
85
86 static int l2tp_eth_dev_xmit(struct sk_buff *skb, struct net_device *dev)
87 {
88         struct l2tp_eth *priv = netdev_priv(dev);
89         struct l2tp_session *session = priv->session;
90
91         l2tp_xmit_skb(session, skb, session->hdr_len);
92
93         dev->stats.tx_bytes += skb->len;
94         dev->stats.tx_packets++;
95
96         return 0;
97 }
98
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,
103 };
104
105 static void l2tp_eth_dev_setup(struct net_device *dev)
106 {
107         ether_setup(dev);
108         dev->priv_flags &= ~IFF_TX_SKB_SHARING;
109         dev->netdev_ops         = &l2tp_eth_netdev_ops;
110         dev->destructor         = free_netdev;
111 }
112
113 static void l2tp_eth_dev_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
114 {
115         struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
116         struct net_device *dev = spriv->dev;
117
118         if (session->debug & L2TP_MSG_DATA) {
119                 unsigned int length;
120                 u8 *ptr = skb->data;
121
122                 length = min(32u, skb->len);
123                 if (!pskb_may_pull(skb, length))
124                         goto error;
125
126                 pr_debug("%s: eth recv\n", session->name);
127                 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, ptr, length);
128         }
129
130         if (!pskb_may_pull(skb, sizeof(ETH_HLEN)))
131                 goto error;
132
133         secpath_reset(skb);
134
135         /* checksums verified by L2TP */
136         skb->ip_summed = CHECKSUM_NONE;
137
138         skb_dst_drop(skb);
139         nf_reset(skb);
140
141         if (dev_forward_skb(dev, skb) == NET_RX_SUCCESS) {
142                 dev->stats.rx_packets++;
143                 dev->stats.rx_bytes += data_len;
144         } else
145                 dev->stats.rx_errors++;
146
147         return;
148
149 error:
150         dev->stats.rx_errors++;
151         kfree_skb(skb);
152 }
153
154 static void l2tp_eth_delete(struct l2tp_session *session)
155 {
156         struct l2tp_eth_sess *spriv;
157         struct net_device *dev;
158
159         if (session) {
160                 spriv = l2tp_session_priv(session);
161                 dev = spriv->dev;
162                 if (dev) {
163                         unregister_netdev(dev);
164                         spriv->dev = NULL;
165                 }
166         }
167 }
168
169 #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
170 static void l2tp_eth_show(struct seq_file *m, void *arg)
171 {
172         struct l2tp_session *session = arg;
173         struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
174         struct net_device *dev = spriv->dev;
175
176         seq_printf(m, "   interface %s\n", dev->name);
177 }
178 #endif
179
180 static int l2tp_eth_create(struct net *net, u32 tunnel_id, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
181 {
182         struct net_device *dev;
183         char name[IFNAMSIZ];
184         struct l2tp_tunnel *tunnel;
185         struct l2tp_session *session;
186         struct l2tp_eth *priv;
187         struct l2tp_eth_sess *spriv;
188         int rc;
189         struct l2tp_eth_net *pn;
190
191         tunnel = l2tp_tunnel_find(net, tunnel_id);
192         if (!tunnel) {
193                 rc = -ENODEV;
194                 goto out;
195         }
196
197         session = l2tp_session_find(net, tunnel, session_id);
198         if (session) {
199                 rc = -EEXIST;
200                 goto out;
201         }
202
203         if (cfg->ifname) {
204                 dev = dev_get_by_name(net, cfg->ifname);
205                 if (dev) {
206                         dev_put(dev);
207                         rc = -EEXIST;
208                         goto out;
209                 }
210                 strlcpy(name, cfg->ifname, IFNAMSIZ);
211         } else
212                 strcpy(name, L2TP_ETH_DEV_NAME);
213
214         session = l2tp_session_create(sizeof(*spriv), tunnel, session_id,
215                                       peer_session_id, cfg);
216         if (!session) {
217                 rc = -ENOMEM;
218                 goto out;
219         }
220
221         dev = alloc_netdev(sizeof(*priv), name, l2tp_eth_dev_setup);
222         if (!dev) {
223                 rc = -ENOMEM;
224                 goto out_del_session;
225         }
226
227         dev_net_set(dev, net);
228         if (session->mtu == 0)
229                 session->mtu = dev->mtu - session->hdr_len;
230         dev->mtu = session->mtu;
231         dev->needed_headroom += session->hdr_len;
232
233         priv = netdev_priv(dev);
234         priv->dev = dev;
235         priv->session = session;
236         INIT_LIST_HEAD(&priv->list);
237
238         priv->tunnel_sock = tunnel->sock;
239         session->recv_skb = l2tp_eth_dev_recv;
240         session->session_close = l2tp_eth_delete;
241 #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
242         session->show = l2tp_eth_show;
243 #endif
244
245         spriv = l2tp_session_priv(session);
246         spriv->dev = dev;
247
248         rc = register_netdev(dev);
249         if (rc < 0)
250                 goto out_del_dev;
251
252         /* Must be done after register_netdev() */
253         strlcpy(session->ifname, dev->name, IFNAMSIZ);
254
255         dev_hold(dev);
256         pn = l2tp_eth_pernet(dev_net(dev));
257         spin_lock(&pn->l2tp_eth_lock);
258         list_add(&priv->list, &pn->l2tp_eth_dev_list);
259         spin_unlock(&pn->l2tp_eth_lock);
260
261         return 0;
262
263 out_del_dev:
264         free_netdev(dev);
265 out_del_session:
266         l2tp_session_delete(session);
267 out:
268         return rc;
269 }
270
271 static __net_init int l2tp_eth_init_net(struct net *net)
272 {
273         struct l2tp_eth_net *pn = net_generic(net, l2tp_eth_net_id);
274
275         INIT_LIST_HEAD(&pn->l2tp_eth_dev_list);
276         spin_lock_init(&pn->l2tp_eth_lock);
277
278         return 0;
279 }
280
281 static struct pernet_operations l2tp_eth_net_ops = {
282         .init = l2tp_eth_init_net,
283         .id   = &l2tp_eth_net_id,
284         .size = sizeof(struct l2tp_eth_net),
285 };
286
287
288 static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops = {
289         .session_create = l2tp_eth_create,
290         .session_delete = l2tp_session_delete,
291 };
292
293
294 static int __init l2tp_eth_init(void)
295 {
296         int err = 0;
297
298         err = l2tp_nl_register_ops(L2TP_PWTYPE_ETH, &l2tp_eth_nl_cmd_ops);
299         if (err)
300                 goto out;
301
302         err = register_pernet_device(&l2tp_eth_net_ops);
303         if (err)
304                 goto out_unreg;
305
306         pr_info("L2TP ethernet pseudowire support (L2TPv3)\n");
307
308         return 0;
309
310 out_unreg:
311         l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH);
312 out:
313         return err;
314 }
315
316 static void __exit l2tp_eth_exit(void)
317 {
318         unregister_pernet_device(&l2tp_eth_net_ops);
319         l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH);
320 }
321
322 module_init(l2tp_eth_init);
323 module_exit(l2tp_eth_exit);
324
325 MODULE_LICENSE("GPL");
326 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
327 MODULE_DESCRIPTION("L2TP ethernet pseudowire driver");
328 MODULE_VERSION("1.0");