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