Commit | Line | Data |
---|---|---|
d9e31d17 JC |
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 | ||
a4ca44fa JP |
12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
13 | ||
d9e31d17 JC |
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; | |
f2cedb63 | 69 | eth_hw_addr_random(dev); |
d9e31d17 JC |
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); | |
550fd08c | 108 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
d9e31d17 JC |
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; | |
d9e31d17 JC |
120 | u8 *ptr = skb->data; |
121 | ||
122 | length = min(32u, skb->len); | |
123 | if (!pskb_may_pull(skb, length)) | |
124 | goto error; | |
125 | ||
a4ca44fa JP |
126 | pr_debug("%s: eth recv\n", session->name); |
127 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, ptr, length); | |
d9e31d17 JC |
128 | } |
129 | ||
bfc960a8 | 130 | if (!pskb_may_pull(skb, sizeof(ETH_HLEN))) |
d9e31d17 JC |
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) { | |
d9e31d17 JC |
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; | |
a06998b8 | 165 | module_put(THIS_MODULE); |
d9e31d17 JC |
166 | } |
167 | } | |
168 | } | |
169 | ||
f66ef2d0 | 170 | #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE) |
0ad66140 JC |
171 | static void l2tp_eth_show(struct seq_file *m, void *arg) |
172 | { | |
173 | struct l2tp_session *session = arg; | |
174 | struct l2tp_eth_sess *spriv = l2tp_session_priv(session); | |
175 | struct net_device *dev = spriv->dev; | |
176 | ||
177 | seq_printf(m, " interface %s\n", dev->name); | |
178 | } | |
179 | #endif | |
180 | ||
d9e31d17 JC |
181 | static int l2tp_eth_create(struct net *net, u32 tunnel_id, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg) |
182 | { | |
183 | struct net_device *dev; | |
184 | char name[IFNAMSIZ]; | |
185 | struct l2tp_tunnel *tunnel; | |
186 | struct l2tp_session *session; | |
187 | struct l2tp_eth *priv; | |
188 | struct l2tp_eth_sess *spriv; | |
189 | int rc; | |
190 | struct l2tp_eth_net *pn; | |
191 | ||
192 | tunnel = l2tp_tunnel_find(net, tunnel_id); | |
193 | if (!tunnel) { | |
194 | rc = -ENODEV; | |
195 | goto out; | |
196 | } | |
197 | ||
198 | session = l2tp_session_find(net, tunnel, session_id); | |
199 | if (session) { | |
200 | rc = -EEXIST; | |
201 | goto out; | |
202 | } | |
203 | ||
204 | if (cfg->ifname) { | |
205 | dev = dev_get_by_name(net, cfg->ifname); | |
206 | if (dev) { | |
207 | dev_put(dev); | |
208 | rc = -EEXIST; | |
209 | goto out; | |
210 | } | |
211 | strlcpy(name, cfg->ifname, IFNAMSIZ); | |
212 | } else | |
213 | strcpy(name, L2TP_ETH_DEV_NAME); | |
214 | ||
215 | session = l2tp_session_create(sizeof(*spriv), tunnel, session_id, | |
216 | peer_session_id, cfg); | |
217 | if (!session) { | |
218 | rc = -ENOMEM; | |
219 | goto out; | |
220 | } | |
221 | ||
222 | dev = alloc_netdev(sizeof(*priv), name, l2tp_eth_dev_setup); | |
223 | if (!dev) { | |
224 | rc = -ENOMEM; | |
225 | goto out_del_session; | |
226 | } | |
227 | ||
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; | |
233 | ||
234 | priv = netdev_priv(dev); | |
235 | priv->dev = dev; | |
236 | priv->session = session; | |
237 | INIT_LIST_HEAD(&priv->list); | |
238 | ||
239 | priv->tunnel_sock = tunnel->sock; | |
240 | session->recv_skb = l2tp_eth_dev_recv; | |
241 | session->session_close = l2tp_eth_delete; | |
f66ef2d0 | 242 | #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE) |
0ad66140 JC |
243 | session->show = l2tp_eth_show; |
244 | #endif | |
d9e31d17 JC |
245 | |
246 | spriv = l2tp_session_priv(session); | |
247 | spriv->dev = dev; | |
248 | ||
249 | rc = register_netdev(dev); | |
250 | if (rc < 0) | |
251 | goto out_del_dev; | |
252 | ||
a06998b8 | 253 | __module_get(THIS_MODULE); |
d9e31d17 JC |
254 | /* Must be done after register_netdev() */ |
255 | strlcpy(session->ifname, dev->name, IFNAMSIZ); | |
256 | ||
257 | dev_hold(dev); | |
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); | |
262 | ||
263 | return 0; | |
264 | ||
265 | out_del_dev: | |
266 | free_netdev(dev); | |
267 | out_del_session: | |
268 | l2tp_session_delete(session); | |
269 | out: | |
270 | return rc; | |
271 | } | |
272 | ||
273 | static __net_init int l2tp_eth_init_net(struct net *net) | |
274 | { | |
3a737028 | 275 | struct l2tp_eth_net *pn = net_generic(net, l2tp_eth_net_id); |
d9e31d17 JC |
276 | |
277 | INIT_LIST_HEAD(&pn->l2tp_eth_dev_list); | |
278 | spin_lock_init(&pn->l2tp_eth_lock); | |
279 | ||
d9e31d17 | 280 | return 0; |
d9e31d17 JC |
281 | } |
282 | ||
8aa525a9 | 283 | static struct pernet_operations l2tp_eth_net_ops = { |
d9e31d17 | 284 | .init = l2tp_eth_init_net, |
d9e31d17 JC |
285 | .id = &l2tp_eth_net_id, |
286 | .size = sizeof(struct l2tp_eth_net), | |
287 | }; | |
288 | ||
289 | ||
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, | |
293 | }; | |
294 | ||
295 | ||
296 | static int __init l2tp_eth_init(void) | |
297 | { | |
298 | int err = 0; | |
299 | ||
300 | err = l2tp_nl_register_ops(L2TP_PWTYPE_ETH, &l2tp_eth_nl_cmd_ops); | |
301 | if (err) | |
302 | goto out; | |
303 | ||
304 | err = register_pernet_device(&l2tp_eth_net_ops); | |
305 | if (err) | |
306 | goto out_unreg; | |
307 | ||
a4ca44fa | 308 | pr_info("L2TP ethernet pseudowire support (L2TPv3)\n"); |
d9e31d17 JC |
309 | |
310 | return 0; | |
311 | ||
312 | out_unreg: | |
313 | l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH); | |
314 | out: | |
315 | return err; | |
316 | } | |
317 | ||
318 | static void __exit l2tp_eth_exit(void) | |
319 | { | |
320 | unregister_pernet_device(&l2tp_eth_net_ops); | |
321 | l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH); | |
322 | } | |
323 | ||
324 | module_init(l2tp_eth_init); | |
325 | module_exit(l2tp_eth_exit); | |
326 | ||
327 | MODULE_LICENSE("GPL"); | |
328 | MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); | |
329 | MODULE_DESCRIPTION("L2TP ethernet pseudowire driver"); | |
330 | MODULE_VERSION("1.0"); |