1
2
3
4
5
6
7
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10#include <linux/module.h>
11#include <linux/skbuff.h>
12#include <linux/socket.h>
13#include <linux/hash.h>
14#include <linux/l2tp.h>
15#include <linux/in.h>
16#include <linux/etherdevice.h>
17#include <linux/spinlock.h>
18#include <net/sock.h>
19#include <net/ip.h>
20#include <net/icmp.h>
21#include <net/udp.h>
22#include <net/inet_common.h>
23#include <net/inet_hashtables.h>
24#include <net/tcp_states.h>
25#include <net/protocol.h>
26#include <net/xfrm.h>
27#include <net/net_namespace.h>
28#include <net/netns/generic.h>
29#include <linux/ip.h>
30#include <linux/ipv6.h>
31#include <linux/udp.h>
32
33#include "l2tp_core.h"
34
35
36#define L2TP_ETH_DEV_NAME "l2tpeth%d"
37
38
39struct l2tp_eth {
40 struct l2tp_session *session;
41 atomic_long_t tx_bytes;
42 atomic_long_t tx_packets;
43 atomic_long_t tx_dropped;
44 atomic_long_t rx_bytes;
45 atomic_long_t rx_packets;
46 atomic_long_t rx_errors;
47};
48
49
50struct l2tp_eth_sess {
51 struct net_device __rcu *dev;
52};
53
54
55static int l2tp_eth_dev_init(struct net_device *dev)
56{
57 eth_hw_addr_random(dev);
58 eth_broadcast_addr(dev->broadcast);
59
60 return 0;
61}
62
63static void l2tp_eth_dev_uninit(struct net_device *dev)
64{
65 struct l2tp_eth *priv = netdev_priv(dev);
66 struct l2tp_eth_sess *spriv;
67
68 spriv = l2tp_session_priv(priv->session);
69 RCU_INIT_POINTER(spriv->dev, NULL);
70
71
72
73}
74
75static int l2tp_eth_dev_xmit(struct sk_buff *skb, struct net_device *dev)
76{
77 struct l2tp_eth *priv = netdev_priv(dev);
78 struct l2tp_session *session = priv->session;
79 unsigned int len = skb->len;
80 int ret = l2tp_xmit_skb(session, skb, session->hdr_len);
81
82 if (likely(ret == NET_XMIT_SUCCESS)) {
83 atomic_long_add(len, &priv->tx_bytes);
84 atomic_long_inc(&priv->tx_packets);
85 } else {
86 atomic_long_inc(&priv->tx_dropped);
87 }
88 return NETDEV_TX_OK;
89}
90
91static void l2tp_eth_get_stats64(struct net_device *dev,
92 struct rtnl_link_stats64 *stats)
93{
94 struct l2tp_eth *priv = netdev_priv(dev);
95
96 stats->tx_bytes = (unsigned long) atomic_long_read(&priv->tx_bytes);
97 stats->tx_packets = (unsigned long) atomic_long_read(&priv->tx_packets);
98 stats->tx_dropped = (unsigned long) atomic_long_read(&priv->tx_dropped);
99 stats->rx_bytes = (unsigned long) atomic_long_read(&priv->rx_bytes);
100 stats->rx_packets = (unsigned long) atomic_long_read(&priv->rx_packets);
101 stats->rx_errors = (unsigned long) atomic_long_read(&priv->rx_errors);
102
103}
104
105static const struct net_device_ops l2tp_eth_netdev_ops = {
106 .ndo_init = l2tp_eth_dev_init,
107 .ndo_uninit = l2tp_eth_dev_uninit,
108 .ndo_start_xmit = l2tp_eth_dev_xmit,
109 .ndo_get_stats64 = l2tp_eth_get_stats64,
110 .ndo_set_mac_address = eth_mac_addr,
111};
112
113static struct device_type l2tpeth_type = {
114 .name = "l2tpeth",
115};
116
117static void l2tp_eth_dev_setup(struct net_device *dev)
118{
119 SET_NETDEV_DEVTYPE(dev, &l2tpeth_type);
120 ether_setup(dev);
121 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
122 dev->features |= NETIF_F_LLTX;
123 dev->netdev_ops = &l2tp_eth_netdev_ops;
124 dev->needs_free_netdev = true;
125}
126
127static void l2tp_eth_dev_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
128{
129 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
130 struct net_device *dev;
131 struct l2tp_eth *priv;
132
133 if (session->debug & L2TP_MSG_DATA) {
134 unsigned int length;
135
136 length = min(32u, skb->len);
137 if (!pskb_may_pull(skb, length))
138 goto error;
139
140 pr_debug("%s: eth recv\n", session->name);
141 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
142 }
143
144 if (!pskb_may_pull(skb, ETH_HLEN))
145 goto error;
146
147 secpath_reset(skb);
148
149
150 skb->ip_summed = CHECKSUM_NONE;
151
152 skb_dst_drop(skb);
153 nf_reset_ct(skb);
154
155 rcu_read_lock();
156 dev = rcu_dereference(spriv->dev);
157 if (!dev)
158 goto error_rcu;
159
160 priv = netdev_priv(dev);
161 if (dev_forward_skb(dev, skb) == NET_RX_SUCCESS) {
162 atomic_long_inc(&priv->rx_packets);
163 atomic_long_add(data_len, &priv->rx_bytes);
164 } else {
165 atomic_long_inc(&priv->rx_errors);
166 }
167 rcu_read_unlock();
168
169 return;
170
171error_rcu:
172 rcu_read_unlock();
173error:
174 kfree_skb(skb);
175}
176
177static void l2tp_eth_delete(struct l2tp_session *session)
178{
179 struct l2tp_eth_sess *spriv;
180 struct net_device *dev;
181
182 if (session) {
183 spriv = l2tp_session_priv(session);
184
185 rtnl_lock();
186 dev = rtnl_dereference(spriv->dev);
187 if (dev) {
188 unregister_netdevice(dev);
189 rtnl_unlock();
190 module_put(THIS_MODULE);
191 } else {
192 rtnl_unlock();
193 }
194 }
195}
196
197static void l2tp_eth_show(struct seq_file *m, void *arg)
198{
199 struct l2tp_session *session = arg;
200 struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
201 struct net_device *dev;
202
203 rcu_read_lock();
204 dev = rcu_dereference(spriv->dev);
205 if (!dev) {
206 rcu_read_unlock();
207 return;
208 }
209 dev_hold(dev);
210 rcu_read_unlock();
211
212 seq_printf(m, " interface %s\n", dev->name);
213
214 dev_put(dev);
215}
216
217static void l2tp_eth_adjust_mtu(struct l2tp_tunnel *tunnel,
218 struct l2tp_session *session,
219 struct net_device *dev)
220{
221 unsigned int overhead = 0;
222 u32 l3_overhead = 0;
223 u32 mtu;
224
225
226 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
227 overhead += sizeof(struct udphdr);
228 dev->needed_headroom += sizeof(struct udphdr);
229 }
230
231 lock_sock(tunnel->sock);
232 l3_overhead = kernel_sock_ip_overhead(tunnel->sock);
233 release_sock(tunnel->sock);
234
235 if (l3_overhead == 0) {
236
237
238
239
240
241 return;
242 }
243
244
245
246 overhead += session->hdr_len + ETH_HLEN + l3_overhead;
247
248 mtu = l2tp_tunnel_dst_mtu(tunnel) - overhead;
249 if (mtu < dev->min_mtu || mtu > dev->max_mtu)
250 dev->mtu = ETH_DATA_LEN - overhead;
251 else
252 dev->mtu = mtu;
253
254 dev->needed_headroom += session->hdr_len;
255}
256
257static int l2tp_eth_create(struct net *net, struct l2tp_tunnel *tunnel,
258 u32 session_id, u32 peer_session_id,
259 struct l2tp_session_cfg *cfg)
260{
261 unsigned char name_assign_type;
262 struct net_device *dev;
263 char name[IFNAMSIZ];
264 struct l2tp_session *session;
265 struct l2tp_eth *priv;
266 struct l2tp_eth_sess *spriv;
267 int rc;
268
269 if (cfg->ifname) {
270 strlcpy(name, cfg->ifname, IFNAMSIZ);
271 name_assign_type = NET_NAME_USER;
272 } else {
273 strcpy(name, L2TP_ETH_DEV_NAME);
274 name_assign_type = NET_NAME_ENUM;
275 }
276
277 session = l2tp_session_create(sizeof(*spriv), tunnel, session_id,
278 peer_session_id, cfg);
279 if (IS_ERR(session)) {
280 rc = PTR_ERR(session);
281 goto err;
282 }
283
284 dev = alloc_netdev(sizeof(*priv), name, name_assign_type,
285 l2tp_eth_dev_setup);
286 if (!dev) {
287 rc = -ENOMEM;
288 goto err_sess;
289 }
290
291 dev_net_set(dev, net);
292 dev->min_mtu = 0;
293 dev->max_mtu = ETH_MAX_MTU;
294 l2tp_eth_adjust_mtu(tunnel, session, dev);
295
296 priv = netdev_priv(dev);
297 priv->session = session;
298
299 session->recv_skb = l2tp_eth_dev_recv;
300 session->session_close = l2tp_eth_delete;
301 if (IS_ENABLED(CONFIG_L2TP_DEBUGFS))
302 session->show = l2tp_eth_show;
303
304 spriv = l2tp_session_priv(session);
305
306 l2tp_session_inc_refcount(session);
307
308 rtnl_lock();
309
310
311
312
313
314 rc = l2tp_session_register(session, tunnel);
315 if (rc < 0) {
316 rtnl_unlock();
317 goto err_sess_dev;
318 }
319
320 rc = register_netdevice(dev);
321 if (rc < 0) {
322 rtnl_unlock();
323 l2tp_session_delete(session);
324 l2tp_session_dec_refcount(session);
325 free_netdev(dev);
326
327 return rc;
328 }
329
330 strlcpy(session->ifname, dev->name, IFNAMSIZ);
331 rcu_assign_pointer(spriv->dev, dev);
332
333 rtnl_unlock();
334
335 l2tp_session_dec_refcount(session);
336
337 __module_get(THIS_MODULE);
338
339 return 0;
340
341err_sess_dev:
342 l2tp_session_dec_refcount(session);
343 free_netdev(dev);
344err_sess:
345 kfree(session);
346err:
347 return rc;
348}
349
350
351static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops = {
352 .session_create = l2tp_eth_create,
353 .session_delete = l2tp_session_delete,
354};
355
356
357static int __init l2tp_eth_init(void)
358{
359 int err = 0;
360
361 err = l2tp_nl_register_ops(L2TP_PWTYPE_ETH, &l2tp_eth_nl_cmd_ops);
362 if (err)
363 goto err;
364
365 pr_info("L2TP ethernet pseudowire support (L2TPv3)\n");
366
367 return 0;
368
369err:
370 return err;
371}
372
373static void __exit l2tp_eth_exit(void)
374{
375 l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH);
376}
377
378module_init(l2tp_eth_init);
379module_exit(l2tp_eth_exit);
380
381MODULE_LICENSE("GPL");
382MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
383MODULE_DESCRIPTION("L2TP ethernet pseudowire driver");
384MODULE_VERSION("1.0");
385MODULE_ALIAS_L2TP_PWTYPE(5);
386