Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/net/l2tp/l2tp_ip6.c
50674 views
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/* L2TPv3 IP encapsulation support for IPv6
3
*
4
* Copyright (c) 2012 Katalix Systems Ltd
5
*/
6
7
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8
9
#include <linux/icmp.h>
10
#include <linux/module.h>
11
#include <linux/skbuff.h>
12
#include <linux/random.h>
13
#include <linux/socket.h>
14
#include <linux/l2tp.h>
15
#include <linux/in.h>
16
#include <linux/in6.h>
17
#include <net/sock.h>
18
#include <net/ip.h>
19
#include <net/icmp.h>
20
#include <net/udp.h>
21
#include <net/inet_common.h>
22
#include <net/tcp_states.h>
23
#include <net/protocol.h>
24
#include <net/xfrm.h>
25
#include <net/net_namespace.h>
26
#include <net/netns/generic.h>
27
28
#include <net/transp_v6.h>
29
#include <net/addrconf.h>
30
#include <net/ip6_route.h>
31
32
#include "l2tp_core.h"
33
34
/* per-net private data for this module */
35
static unsigned int l2tp_ip6_net_id;
36
struct l2tp_ip6_net {
37
rwlock_t l2tp_ip6_lock;
38
struct hlist_head l2tp_ip6_table;
39
struct hlist_head l2tp_ip6_bind_table;
40
};
41
42
struct l2tp_ip6_sock {
43
/* inet_sock has to be the first member of l2tp_ip6_sock */
44
struct inet_sock inet;
45
46
u32 conn_id;
47
u32 peer_conn_id;
48
49
struct ipv6_pinfo inet6;
50
};
51
52
static struct l2tp_ip6_sock *l2tp_ip6_sk(const struct sock *sk)
53
{
54
return (struct l2tp_ip6_sock *)sk;
55
}
56
57
static struct l2tp_ip6_net *l2tp_ip6_pernet(const struct net *net)
58
{
59
return net_generic(net, l2tp_ip6_net_id);
60
}
61
62
static struct sock *__l2tp_ip6_bind_lookup(const struct net *net,
63
const struct in6_addr *laddr,
64
const struct in6_addr *raddr,
65
int dif, u32 tunnel_id)
66
{
67
struct l2tp_ip6_net *pn = l2tp_ip6_pernet(net);
68
struct sock *sk;
69
70
sk_for_each_bound(sk, &pn->l2tp_ip6_bind_table) {
71
const struct in6_addr *sk_laddr = inet6_rcv_saddr(sk);
72
const struct in6_addr *sk_raddr = &sk->sk_v6_daddr;
73
const struct l2tp_ip6_sock *l2tp = l2tp_ip6_sk(sk);
74
int bound_dev_if;
75
76
if (!net_eq(sock_net(sk), net))
77
continue;
78
79
bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
80
if (bound_dev_if && dif && bound_dev_if != dif)
81
continue;
82
83
if (sk_laddr && !ipv6_addr_any(sk_laddr) &&
84
!ipv6_addr_any(laddr) && !ipv6_addr_equal(sk_laddr, laddr))
85
continue;
86
87
if (!ipv6_addr_any(sk_raddr) && raddr &&
88
!ipv6_addr_any(raddr) && !ipv6_addr_equal(sk_raddr, raddr))
89
continue;
90
91
if (l2tp->conn_id != tunnel_id)
92
continue;
93
94
goto found;
95
}
96
97
sk = NULL;
98
found:
99
return sk;
100
}
101
102
/* When processing receive frames, there are two cases to
103
* consider. Data frames consist of a non-zero session-id and an
104
* optional cookie. Control frames consist of a regular L2TP header
105
* preceded by 32-bits of zeros.
106
*
107
* L2TPv3 Session Header Over IP
108
*
109
* 0 1 2 3
110
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
111
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112
* | Session ID |
113
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
114
* | Cookie (optional, maximum 64 bits)...
115
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
116
* |
117
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
118
*
119
* L2TPv3 Control Message Header Over IP
120
*
121
* 0 1 2 3
122
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
123
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
124
* | (32 bits of zeros) |
125
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
126
* |T|L|x|x|S|x|x|x|x|x|x|x| Ver | Length |
127
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
128
* | Control Connection ID |
129
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
130
* | Ns | Nr |
131
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
132
*
133
* All control frames are passed to userspace.
134
*/
135
static int l2tp_ip6_recv(struct sk_buff *skb)
136
{
137
struct net *net = dev_net(skb->dev);
138
struct l2tp_ip6_net *pn;
139
struct sock *sk;
140
u32 session_id;
141
u32 tunnel_id;
142
unsigned char *ptr, *optr;
143
struct l2tp_session *session;
144
struct l2tp_tunnel *tunnel = NULL;
145
struct ipv6hdr *iph;
146
147
pn = l2tp_ip6_pernet(net);
148
149
if (!pskb_may_pull(skb, 4))
150
goto discard;
151
152
/* Point to L2TP header */
153
optr = skb->data;
154
ptr = skb->data;
155
session_id = ntohl(*((__be32 *)ptr));
156
ptr += 4;
157
158
/* RFC3931: L2TP/IP packets have the first 4 bytes containing
159
* the session_id. If it is 0, the packet is a L2TP control
160
* frame and the session_id value can be discarded.
161
*/
162
if (session_id == 0) {
163
__skb_pull(skb, 4);
164
goto pass_up;
165
}
166
167
/* Ok, this is a data packet. Lookup the session. */
168
session = l2tp_v3_session_get(net, NULL, session_id);
169
if (!session)
170
goto discard;
171
172
tunnel = session->tunnel;
173
if (!tunnel)
174
goto discard_sess;
175
176
if (l2tp_v3_ensure_opt_in_linear(session, skb, &ptr, &optr))
177
goto discard_sess;
178
179
l2tp_recv_common(session, skb, ptr, optr, 0, skb->len);
180
l2tp_session_put(session);
181
182
return 0;
183
184
pass_up:
185
/* Get the tunnel_id from the L2TP header */
186
if (!pskb_may_pull(skb, 12))
187
goto discard;
188
189
if ((skb->data[0] & 0xc0) != 0xc0)
190
goto discard;
191
192
tunnel_id = ntohl(*(__be32 *)&skb->data[4]);
193
iph = ipv6_hdr(skb);
194
195
read_lock_bh(&pn->l2tp_ip6_lock);
196
sk = __l2tp_ip6_bind_lookup(net, &iph->daddr, &iph->saddr,
197
inet6_iif(skb), tunnel_id);
198
if (!sk) {
199
read_unlock_bh(&pn->l2tp_ip6_lock);
200
goto discard;
201
}
202
sock_hold(sk);
203
read_unlock_bh(&pn->l2tp_ip6_lock);
204
205
if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
206
goto discard_put;
207
208
nf_reset_ct(skb);
209
210
return sk_receive_skb(sk, skb, 1);
211
212
discard_sess:
213
l2tp_session_put(session);
214
goto discard;
215
216
discard_put:
217
sock_put(sk);
218
219
discard:
220
kfree_skb(skb);
221
return 0;
222
}
223
224
static int l2tp_ip6_hash(struct sock *sk)
225
{
226
struct l2tp_ip6_net *pn = l2tp_ip6_pernet(sock_net(sk));
227
228
if (sk_unhashed(sk)) {
229
write_lock_bh(&pn->l2tp_ip6_lock);
230
sk_add_node(sk, &pn->l2tp_ip6_table);
231
write_unlock_bh(&pn->l2tp_ip6_lock);
232
}
233
return 0;
234
}
235
236
static void l2tp_ip6_unhash(struct sock *sk)
237
{
238
struct l2tp_ip6_net *pn = l2tp_ip6_pernet(sock_net(sk));
239
240
if (sk_unhashed(sk))
241
return;
242
write_lock_bh(&pn->l2tp_ip6_lock);
243
sk_del_node_init(sk);
244
write_unlock_bh(&pn->l2tp_ip6_lock);
245
}
246
247
static int l2tp_ip6_open(struct sock *sk)
248
{
249
/* Prevent autobind. We don't have ports. */
250
inet_sk(sk)->inet_num = IPPROTO_L2TP;
251
252
l2tp_ip6_hash(sk);
253
return 0;
254
}
255
256
static void l2tp_ip6_close(struct sock *sk, long timeout)
257
{
258
struct l2tp_ip6_net *pn = l2tp_ip6_pernet(sock_net(sk));
259
260
write_lock_bh(&pn->l2tp_ip6_lock);
261
hlist_del_init(&sk->sk_bind_node);
262
sk_del_node_init(sk);
263
write_unlock_bh(&pn->l2tp_ip6_lock);
264
265
sk_common_release(sk);
266
}
267
268
static void l2tp_ip6_destroy_sock(struct sock *sk)
269
{
270
struct l2tp_tunnel *tunnel;
271
272
lock_sock(sk);
273
ip6_flush_pending_frames(sk);
274
release_sock(sk);
275
276
tunnel = l2tp_sk_to_tunnel(sk);
277
if (tunnel) {
278
l2tp_tunnel_delete(tunnel);
279
l2tp_tunnel_put(tunnel);
280
}
281
}
282
283
static int l2tp_ip6_bind(struct sock *sk, struct sockaddr_unsized *uaddr,
284
int addr_len)
285
{
286
struct inet_sock *inet = inet_sk(sk);
287
struct ipv6_pinfo *np = inet6_sk(sk);
288
struct sockaddr_l2tpip6 *addr = (struct sockaddr_l2tpip6 *)uaddr;
289
struct net *net = sock_net(sk);
290
struct l2tp_ip6_net *pn;
291
__be32 v4addr = 0;
292
int bound_dev_if;
293
int addr_type;
294
int err;
295
296
pn = l2tp_ip6_pernet(net);
297
298
if (addr->l2tp_family != AF_INET6)
299
return -EINVAL;
300
if (addr_len < sizeof(*addr))
301
return -EINVAL;
302
303
addr_type = ipv6_addr_type(&addr->l2tp_addr);
304
305
/* l2tp_ip6 sockets are IPv6 only */
306
if (addr_type == IPV6_ADDR_MAPPED)
307
return -EADDRNOTAVAIL;
308
309
/* L2TP is point-point, not multicast */
310
if (addr_type & IPV6_ADDR_MULTICAST)
311
return -EADDRNOTAVAIL;
312
313
lock_sock(sk);
314
315
err = -EINVAL;
316
if (!sock_flag(sk, SOCK_ZAPPED))
317
goto out_unlock;
318
319
if (sk->sk_state != TCP_CLOSE)
320
goto out_unlock;
321
322
bound_dev_if = sk->sk_bound_dev_if;
323
324
/* Check if the address belongs to the host. */
325
rcu_read_lock();
326
if (addr_type != IPV6_ADDR_ANY) {
327
struct net_device *dev = NULL;
328
329
if (addr_type & IPV6_ADDR_LINKLOCAL) {
330
if (addr->l2tp_scope_id)
331
bound_dev_if = addr->l2tp_scope_id;
332
333
/* Binding to link-local address requires an
334
* interface.
335
*/
336
if (!bound_dev_if)
337
goto out_unlock_rcu;
338
339
err = -ENODEV;
340
dev = dev_get_by_index_rcu(sock_net(sk), bound_dev_if);
341
if (!dev)
342
goto out_unlock_rcu;
343
}
344
345
/* ipv4 addr of the socket is invalid. Only the
346
* unspecified and mapped address have a v4 equivalent.
347
*/
348
v4addr = LOOPBACK4_IPV6;
349
err = -EADDRNOTAVAIL;
350
if (!ipv6_chk_addr(sock_net(sk), &addr->l2tp_addr, dev, 0))
351
goto out_unlock_rcu;
352
}
353
rcu_read_unlock();
354
355
write_lock_bh(&pn->l2tp_ip6_lock);
356
if (__l2tp_ip6_bind_lookup(net, &addr->l2tp_addr, NULL, bound_dev_if,
357
addr->l2tp_conn_id)) {
358
write_unlock_bh(&pn->l2tp_ip6_lock);
359
err = -EADDRINUSE;
360
goto out_unlock;
361
}
362
363
inet->inet_saddr = v4addr;
364
inet->inet_rcv_saddr = v4addr;
365
sk->sk_bound_dev_if = bound_dev_if;
366
sk->sk_v6_rcv_saddr = addr->l2tp_addr;
367
np->saddr = addr->l2tp_addr;
368
369
l2tp_ip6_sk(sk)->conn_id = addr->l2tp_conn_id;
370
371
sk_add_bind_node(sk, &pn->l2tp_ip6_bind_table);
372
sk_del_node_init(sk);
373
write_unlock_bh(&pn->l2tp_ip6_lock);
374
375
sock_reset_flag(sk, SOCK_ZAPPED);
376
release_sock(sk);
377
return 0;
378
379
out_unlock_rcu:
380
rcu_read_unlock();
381
out_unlock:
382
release_sock(sk);
383
384
return err;
385
}
386
387
static int l2tp_ip6_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
388
int addr_len)
389
{
390
struct sockaddr_l2tpip6 *lsa = (struct sockaddr_l2tpip6 *)uaddr;
391
struct sockaddr_in6 *usin = (struct sockaddr_in6 *)uaddr;
392
struct in6_addr *daddr;
393
int addr_type;
394
int rc;
395
struct l2tp_ip6_net *pn;
396
397
if (addr_len < sizeof(*lsa))
398
return -EINVAL;
399
400
if (usin->sin6_family != AF_INET6)
401
return -EINVAL;
402
403
addr_type = ipv6_addr_type(&usin->sin6_addr);
404
if (addr_type & IPV6_ADDR_MULTICAST)
405
return -EINVAL;
406
407
if (addr_type & IPV6_ADDR_MAPPED) {
408
daddr = &usin->sin6_addr;
409
if (ipv4_is_multicast(daddr->s6_addr32[3]))
410
return -EINVAL;
411
}
412
413
lock_sock(sk);
414
415
/* Must bind first - autobinding does not work */
416
if (sock_flag(sk, SOCK_ZAPPED)) {
417
rc = -EINVAL;
418
goto out_sk;
419
}
420
421
rc = __ip6_datagram_connect(sk, uaddr, addr_len);
422
if (rc < 0)
423
goto out_sk;
424
425
l2tp_ip6_sk(sk)->peer_conn_id = lsa->l2tp_conn_id;
426
427
pn = l2tp_ip6_pernet(sock_net(sk));
428
write_lock_bh(&pn->l2tp_ip6_lock);
429
hlist_del_init(&sk->sk_bind_node);
430
sk_add_bind_node(sk, &pn->l2tp_ip6_bind_table);
431
write_unlock_bh(&pn->l2tp_ip6_lock);
432
433
out_sk:
434
release_sock(sk);
435
436
return rc;
437
}
438
439
static int l2tp_ip6_disconnect(struct sock *sk, int flags)
440
{
441
if (sock_flag(sk, SOCK_ZAPPED))
442
return 0;
443
444
return __udp_disconnect(sk, flags);
445
}
446
447
static int l2tp_ip6_getname(struct socket *sock, struct sockaddr *uaddr,
448
int peer)
449
{
450
struct sockaddr_l2tpip6 *lsa = (struct sockaddr_l2tpip6 *)uaddr;
451
struct sock *sk = sock->sk;
452
struct ipv6_pinfo *np = inet6_sk(sk);
453
struct l2tp_ip6_sock *lsk = l2tp_ip6_sk(sk);
454
455
lsa->l2tp_family = AF_INET6;
456
lsa->l2tp_flowinfo = 0;
457
lsa->l2tp_scope_id = 0;
458
lsa->l2tp_unused = 0;
459
if (peer) {
460
if (!lsk->peer_conn_id)
461
return -ENOTCONN;
462
lsa->l2tp_conn_id = lsk->peer_conn_id;
463
lsa->l2tp_addr = sk->sk_v6_daddr;
464
if (inet6_test_bit(SNDFLOW, sk))
465
lsa->l2tp_flowinfo = np->flow_label;
466
} else {
467
if (ipv6_addr_any(&sk->sk_v6_rcv_saddr))
468
lsa->l2tp_addr = np->saddr;
469
else
470
lsa->l2tp_addr = sk->sk_v6_rcv_saddr;
471
472
lsa->l2tp_conn_id = lsk->conn_id;
473
}
474
if (ipv6_addr_type(&lsa->l2tp_addr) & IPV6_ADDR_LINKLOCAL)
475
lsa->l2tp_scope_id = READ_ONCE(sk->sk_bound_dev_if);
476
return sizeof(*lsa);
477
}
478
479
static int l2tp_ip6_backlog_recv(struct sock *sk, struct sk_buff *skb)
480
{
481
int rc;
482
483
/* Charge it to the socket, dropping if the queue is full. */
484
rc = sock_queue_rcv_skb(sk, skb);
485
if (rc < 0)
486
goto drop;
487
488
return 0;
489
490
drop:
491
IP_INC_STATS(sock_net(sk), IPSTATS_MIB_INDISCARDS);
492
kfree_skb(skb);
493
return -1;
494
}
495
496
static int l2tp_ip6_push_pending_frames(struct sock *sk)
497
{
498
struct sk_buff *skb;
499
__be32 *transhdr = NULL;
500
int err = 0;
501
502
skb = skb_peek(&sk->sk_write_queue);
503
if (!skb)
504
goto out;
505
506
transhdr = (__be32 *)skb_transport_header(skb);
507
*transhdr = 0;
508
509
err = ip6_push_pending_frames(sk);
510
511
out:
512
return err;
513
}
514
515
/* Userspace will call sendmsg() on the tunnel socket to send L2TP
516
* control frames.
517
*/
518
static int l2tp_ip6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
519
{
520
struct ipv6_txoptions opt_space;
521
DECLARE_SOCKADDR(struct sockaddr_l2tpip6 *, lsa, msg->msg_name);
522
struct in6_addr *daddr, *final_p, final;
523
struct ipv6_pinfo *np = inet6_sk(sk);
524
struct ipv6_txoptions *opt_to_free = NULL;
525
struct ipv6_txoptions *opt = NULL;
526
struct ip6_flowlabel *flowlabel = NULL;
527
struct dst_entry *dst = NULL;
528
struct flowi6 fl6;
529
struct ipcm6_cookie ipc6;
530
int addr_len = msg->msg_namelen;
531
int transhdrlen = 4; /* zero session-id */
532
int ulen;
533
int err;
534
535
/* Rough check on arithmetic overflow,
536
* better check is made in ip6_append_data().
537
*/
538
if (len > INT_MAX - transhdrlen)
539
return -EMSGSIZE;
540
541
/* Mirror BSD error message compatibility */
542
if (msg->msg_flags & MSG_OOB)
543
return -EOPNOTSUPP;
544
545
/* Get and verify the address */
546
memset(&fl6, 0, sizeof(fl6));
547
548
fl6.flowi6_mark = READ_ONCE(sk->sk_mark);
549
fl6.flowi6_uid = sk_uid(sk);
550
551
ipcm6_init_sk(&ipc6, sk);
552
553
if (lsa) {
554
if (addr_len < SIN6_LEN_RFC2133)
555
return -EINVAL;
556
557
if (lsa->l2tp_family && lsa->l2tp_family != AF_INET6)
558
return -EAFNOSUPPORT;
559
560
daddr = &lsa->l2tp_addr;
561
if (inet6_test_bit(SNDFLOW, sk)) {
562
fl6.flowlabel = lsa->l2tp_flowinfo & IPV6_FLOWINFO_MASK;
563
if (fl6.flowlabel & IPV6_FLOWLABEL_MASK) {
564
flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
565
if (IS_ERR(flowlabel))
566
return -EINVAL;
567
}
568
}
569
570
/* Otherwise it will be difficult to maintain
571
* sk->sk_dst_cache.
572
*/
573
if (sk->sk_state == TCP_ESTABLISHED &&
574
ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
575
daddr = &sk->sk_v6_daddr;
576
577
if (addr_len >= sizeof(struct sockaddr_in6) &&
578
lsa->l2tp_scope_id &&
579
ipv6_addr_type(daddr) & IPV6_ADDR_LINKLOCAL)
580
fl6.flowi6_oif = lsa->l2tp_scope_id;
581
} else {
582
if (sk->sk_state != TCP_ESTABLISHED)
583
return -EDESTADDRREQ;
584
585
daddr = &sk->sk_v6_daddr;
586
fl6.flowlabel = np->flow_label;
587
}
588
589
if (fl6.flowi6_oif == 0)
590
fl6.flowi6_oif = READ_ONCE(sk->sk_bound_dev_if);
591
592
if (msg->msg_controllen) {
593
opt = &opt_space;
594
memset(opt, 0, sizeof(struct ipv6_txoptions));
595
opt->tot_len = sizeof(struct ipv6_txoptions);
596
ipc6.opt = opt;
597
598
err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, &ipc6);
599
if (err < 0) {
600
fl6_sock_release(flowlabel);
601
return err;
602
}
603
if ((fl6.flowlabel & IPV6_FLOWLABEL_MASK) && !flowlabel) {
604
flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
605
if (IS_ERR(flowlabel))
606
return -EINVAL;
607
}
608
if (!(opt->opt_nflen | opt->opt_flen))
609
opt = NULL;
610
}
611
612
if (!opt) {
613
opt = txopt_get(np);
614
opt_to_free = opt;
615
}
616
if (flowlabel)
617
opt = fl6_merge_options(&opt_space, flowlabel, opt);
618
opt = ipv6_fixup_options(&opt_space, opt);
619
ipc6.opt = opt;
620
621
fl6.flowi6_proto = sk->sk_protocol;
622
if (!ipv6_addr_any(daddr))
623
fl6.daddr = *daddr;
624
else
625
fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
626
if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
627
fl6.saddr = np->saddr;
628
629
final_p = fl6_update_dst(&fl6, opt, &final);
630
631
if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
632
fl6.flowi6_oif = READ_ONCE(np->mcast_oif);
633
else if (!fl6.flowi6_oif)
634
fl6.flowi6_oif = READ_ONCE(np->ucast_oif);
635
636
security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6));
637
638
fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
639
640
dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p);
641
if (IS_ERR(dst)) {
642
err = PTR_ERR(dst);
643
goto out;
644
}
645
646
if (ipc6.hlimit < 0)
647
ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
648
649
if (msg->msg_flags & MSG_CONFIRM)
650
goto do_confirm;
651
652
back_from_confirm:
653
lock_sock(sk);
654
ulen = len + (skb_queue_empty(&sk->sk_write_queue) ? transhdrlen : 0);
655
err = ip6_append_data(sk, ip_generic_getfrag, msg,
656
ulen, transhdrlen, &ipc6,
657
&fl6, dst_rt6_info(dst),
658
msg->msg_flags);
659
if (err)
660
ip6_flush_pending_frames(sk);
661
else if (!(msg->msg_flags & MSG_MORE))
662
err = l2tp_ip6_push_pending_frames(sk);
663
release_sock(sk);
664
done:
665
dst_release(dst);
666
out:
667
fl6_sock_release(flowlabel);
668
txopt_put(opt_to_free);
669
670
return err < 0 ? err : len;
671
672
do_confirm:
673
if (msg->msg_flags & MSG_PROBE)
674
dst_confirm_neigh(dst, &fl6.daddr);
675
if (!(msg->msg_flags & MSG_PROBE) || len)
676
goto back_from_confirm;
677
err = 0;
678
goto done;
679
}
680
681
static int l2tp_ip6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
682
int flags, int *addr_len)
683
{
684
struct ipv6_pinfo *np = inet6_sk(sk);
685
DECLARE_SOCKADDR(struct sockaddr_l2tpip6 *, lsa, msg->msg_name);
686
size_t copied = 0;
687
int err = -EOPNOTSUPP;
688
struct sk_buff *skb;
689
690
if (flags & MSG_OOB)
691
goto out;
692
693
if (flags & MSG_ERRQUEUE)
694
return ipv6_recv_error(sk, msg, len, addr_len);
695
696
skb = skb_recv_datagram(sk, flags, &err);
697
if (!skb)
698
goto out;
699
700
copied = skb->len;
701
if (len < copied) {
702
msg->msg_flags |= MSG_TRUNC;
703
copied = len;
704
}
705
706
err = skb_copy_datagram_msg(skb, 0, msg, copied);
707
if (err)
708
goto done;
709
710
sock_recv_timestamp(msg, sk, skb);
711
712
/* Copy the address. */
713
if (lsa) {
714
lsa->l2tp_family = AF_INET6;
715
lsa->l2tp_unused = 0;
716
lsa->l2tp_addr = ipv6_hdr(skb)->saddr;
717
lsa->l2tp_flowinfo = 0;
718
lsa->l2tp_scope_id = 0;
719
lsa->l2tp_conn_id = 0;
720
if (ipv6_addr_type(&lsa->l2tp_addr) & IPV6_ADDR_LINKLOCAL)
721
lsa->l2tp_scope_id = inet6_iif(skb);
722
*addr_len = sizeof(*lsa);
723
}
724
725
if (np->rxopt.all)
726
ip6_datagram_recv_ctl(sk, msg, skb);
727
728
if (flags & MSG_TRUNC)
729
copied = skb->len;
730
done:
731
skb_free_datagram(sk, skb);
732
out:
733
return err ? err : copied;
734
}
735
736
static struct proto l2tp_ip6_prot = {
737
.name = "L2TP/IPv6",
738
.owner = THIS_MODULE,
739
.init = l2tp_ip6_open,
740
.close = l2tp_ip6_close,
741
.bind = l2tp_ip6_bind,
742
.connect = l2tp_ip6_connect,
743
.disconnect = l2tp_ip6_disconnect,
744
.ioctl = l2tp_ioctl,
745
.destroy = l2tp_ip6_destroy_sock,
746
.setsockopt = ipv6_setsockopt,
747
.getsockopt = ipv6_getsockopt,
748
.sendmsg = l2tp_ip6_sendmsg,
749
.recvmsg = l2tp_ip6_recvmsg,
750
.backlog_rcv = l2tp_ip6_backlog_recv,
751
.hash = l2tp_ip6_hash,
752
.unhash = l2tp_ip6_unhash,
753
.obj_size = sizeof(struct l2tp_ip6_sock),
754
.ipv6_pinfo_offset = offsetof(struct l2tp_ip6_sock, inet6),
755
};
756
757
static const struct proto_ops l2tp_ip6_ops = {
758
.family = PF_INET6,
759
.owner = THIS_MODULE,
760
.release = inet6_release,
761
.bind = inet6_bind,
762
.connect = inet_dgram_connect,
763
.socketpair = sock_no_socketpair,
764
.accept = sock_no_accept,
765
.getname = l2tp_ip6_getname,
766
.poll = datagram_poll,
767
.ioctl = inet6_ioctl,
768
.gettstamp = sock_gettstamp,
769
.listen = sock_no_listen,
770
.shutdown = inet_shutdown,
771
.setsockopt = sock_common_setsockopt,
772
.getsockopt = sock_common_getsockopt,
773
.sendmsg = inet_sendmsg,
774
.recvmsg = sock_common_recvmsg,
775
.mmap = sock_no_mmap,
776
#ifdef CONFIG_COMPAT
777
.compat_ioctl = inet6_compat_ioctl,
778
#endif
779
};
780
781
static struct inet_protosw l2tp_ip6_protosw = {
782
.type = SOCK_DGRAM,
783
.protocol = IPPROTO_L2TP,
784
.prot = &l2tp_ip6_prot,
785
.ops = &l2tp_ip6_ops,
786
};
787
788
static struct inet6_protocol l2tp_ip6_protocol __read_mostly = {
789
.handler = l2tp_ip6_recv,
790
};
791
792
static __net_init int l2tp_ip6_init_net(struct net *net)
793
{
794
struct l2tp_ip6_net *pn = net_generic(net, l2tp_ip6_net_id);
795
796
rwlock_init(&pn->l2tp_ip6_lock);
797
INIT_HLIST_HEAD(&pn->l2tp_ip6_table);
798
INIT_HLIST_HEAD(&pn->l2tp_ip6_bind_table);
799
return 0;
800
}
801
802
static __net_exit void l2tp_ip6_exit_net(struct net *net)
803
{
804
struct l2tp_ip6_net *pn = l2tp_ip6_pernet(net);
805
806
write_lock_bh(&pn->l2tp_ip6_lock);
807
WARN_ON_ONCE(hlist_count_nodes(&pn->l2tp_ip6_table) != 0);
808
WARN_ON_ONCE(hlist_count_nodes(&pn->l2tp_ip6_bind_table) != 0);
809
write_unlock_bh(&pn->l2tp_ip6_lock);
810
}
811
812
static struct pernet_operations l2tp_ip6_net_ops = {
813
.init = l2tp_ip6_init_net,
814
.exit = l2tp_ip6_exit_net,
815
.id = &l2tp_ip6_net_id,
816
.size = sizeof(struct l2tp_ip6_net),
817
};
818
819
static int __init l2tp_ip6_init(void)
820
{
821
int err;
822
823
pr_info("L2TP IP encapsulation support for IPv6 (L2TPv3)\n");
824
825
err = register_pernet_device(&l2tp_ip6_net_ops);
826
if (err)
827
goto out;
828
829
err = proto_register(&l2tp_ip6_prot, 1);
830
if (err != 0)
831
goto out1;
832
833
err = inet6_add_protocol(&l2tp_ip6_protocol, IPPROTO_L2TP);
834
if (err)
835
goto out2;
836
837
inet6_register_protosw(&l2tp_ip6_protosw);
838
return 0;
839
840
out2:
841
proto_unregister(&l2tp_ip6_prot);
842
out1:
843
unregister_pernet_device(&l2tp_ip6_net_ops);
844
out:
845
return err;
846
}
847
848
static void __exit l2tp_ip6_exit(void)
849
{
850
inet6_unregister_protosw(&l2tp_ip6_protosw);
851
inet6_del_protocol(&l2tp_ip6_protocol, IPPROTO_L2TP);
852
proto_unregister(&l2tp_ip6_prot);
853
unregister_pernet_device(&l2tp_ip6_net_ops);
854
}
855
856
module_init(l2tp_ip6_init);
857
module_exit(l2tp_ip6_exit);
858
859
MODULE_LICENSE("GPL");
860
MODULE_AUTHOR("Chris Elston <[email protected]>");
861
MODULE_DESCRIPTION("L2TP IP encapsulation for IPv6");
862
MODULE_VERSION("1.0");
863
864
/* Use the values of SOCK_DGRAM (2) as type and IPPROTO_L2TP (115) as protocol,
865
* because __stringify doesn't like enums
866
*/
867
MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET6, 115, 2);
868
MODULE_ALIAS_NET_PF_PROTO(PF_INET6, 115);
869
870