@@ -335,26 +335,6 @@ int l2tp_session_register(struct l2tp_session *session,
335
335
}
336
336
EXPORT_SYMBOL_GPL (l2tp_session_register );
337
337
338
- /* Lookup a tunnel by id
339
- */
340
- struct l2tp_tunnel * l2tp_tunnel_find (const struct net * net , u32 tunnel_id )
341
- {
342
- struct l2tp_tunnel * tunnel ;
343
- struct l2tp_net * pn = l2tp_pernet (net );
344
-
345
- rcu_read_lock_bh ();
346
- list_for_each_entry_rcu (tunnel , & pn -> l2tp_tunnel_list , list ) {
347
- if (tunnel -> tunnel_id == tunnel_id ) {
348
- rcu_read_unlock_bh ();
349
- return tunnel ;
350
- }
351
- }
352
- rcu_read_unlock_bh ();
353
-
354
- return NULL ;
355
- }
356
- EXPORT_SYMBOL_GPL (l2tp_tunnel_find );
357
-
358
338
struct l2tp_tunnel * l2tp_tunnel_find_nth (const struct net * net , int nth )
359
339
{
360
340
struct l2tp_net * pn = l2tp_pernet (net );
@@ -1436,74 +1416,11 @@ int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32
1436
1416
{
1437
1417
struct l2tp_tunnel * tunnel = NULL ;
1438
1418
int err ;
1439
- struct socket * sock = NULL ;
1440
- struct sock * sk = NULL ;
1441
- struct l2tp_net * pn ;
1442
1419
enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP ;
1443
1420
1444
- /* Get the tunnel socket from the fd, which was opened by
1445
- * the userspace L2TP daemon. If not specified, create a
1446
- * kernel socket.
1447
- */
1448
- if (fd < 0 ) {
1449
- err = l2tp_tunnel_sock_create (net , tunnel_id , peer_tunnel_id ,
1450
- cfg , & sock );
1451
- if (err < 0 )
1452
- goto err ;
1453
- } else {
1454
- sock = sockfd_lookup (fd , & err );
1455
- if (!sock ) {
1456
- pr_err ("tunl %u: sockfd_lookup(fd=%d) returned %d\n" ,
1457
- tunnel_id , fd , err );
1458
- err = - EBADF ;
1459
- goto err ;
1460
- }
1461
-
1462
- /* Reject namespace mismatches */
1463
- if (!net_eq (sock_net (sock -> sk ), net )) {
1464
- pr_err ("tunl %u: netns mismatch\n" , tunnel_id );
1465
- err = - EINVAL ;
1466
- goto err ;
1467
- }
1468
- }
1469
-
1470
- sk = sock -> sk ;
1471
-
1472
1421
if (cfg != NULL )
1473
1422
encap = cfg -> encap ;
1474
1423
1475
- /* Quick sanity checks */
1476
- err = - EPROTONOSUPPORT ;
1477
- if (sk -> sk_type != SOCK_DGRAM ) {
1478
- pr_debug ("tunl %hu: fd %d wrong socket type\n" ,
1479
- tunnel_id , fd );
1480
- goto err ;
1481
- }
1482
- switch (encap ) {
1483
- case L2TP_ENCAPTYPE_UDP :
1484
- if (sk -> sk_protocol != IPPROTO_UDP ) {
1485
- pr_err ("tunl %hu: fd %d wrong protocol, got %d, expected %d\n" ,
1486
- tunnel_id , fd , sk -> sk_protocol , IPPROTO_UDP );
1487
- goto err ;
1488
- }
1489
- break ;
1490
- case L2TP_ENCAPTYPE_IP :
1491
- if (sk -> sk_protocol != IPPROTO_L2TP ) {
1492
- pr_err ("tunl %hu: fd %d wrong protocol, got %d, expected %d\n" ,
1493
- tunnel_id , fd , sk -> sk_protocol , IPPROTO_L2TP );
1494
- goto err ;
1495
- }
1496
- break ;
1497
- }
1498
-
1499
- /* Check if this socket has already been prepped */
1500
- tunnel = l2tp_tunnel (sk );
1501
- if (tunnel != NULL ) {
1502
- /* This socket has already been prepped */
1503
- err = - EBUSY ;
1504
- goto err ;
1505
- }
1506
-
1507
1424
tunnel = kzalloc (sizeof (struct l2tp_tunnel ), GFP_KERNEL );
1508
1425
if (tunnel == NULL ) {
1509
1426
err = - ENOMEM ;
@@ -1520,72 +1437,126 @@ int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32
1520
1437
rwlock_init (& tunnel -> hlist_lock );
1521
1438
tunnel -> acpt_newsess = true;
1522
1439
1523
- /* The net we belong to */
1524
- tunnel -> l2tp_net = net ;
1525
- pn = l2tp_pernet (net );
1526
-
1527
1440
if (cfg != NULL )
1528
1441
tunnel -> debug = cfg -> debug ;
1529
1442
1530
- /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1531
1443
tunnel -> encap = encap ;
1532
- if (encap == L2TP_ENCAPTYPE_UDP ) {
1533
- struct udp_tunnel_sock_cfg udp_cfg = { };
1534
-
1535
- udp_cfg .sk_user_data = tunnel ;
1536
- udp_cfg .encap_type = UDP_ENCAP_L2TPINUDP ;
1537
- udp_cfg .encap_rcv = l2tp_udp_encap_recv ;
1538
- udp_cfg .encap_destroy = l2tp_udp_encap_destroy ;
1539
-
1540
- setup_udp_tunnel_sock (net , sock , & udp_cfg );
1541
- } else {
1542
- sk -> sk_user_data = tunnel ;
1543
- }
1544
1444
1545
- /* Bump the reference count. The tunnel context is deleted
1546
- * only when this drops to zero. A reference is also held on
1547
- * the tunnel socket to ensure that it is not released while
1548
- * the tunnel is extant. Must be done before sk_destruct is
1549
- * set.
1550
- */
1551
1445
refcount_set (& tunnel -> ref_count , 1 );
1552
- sock_hold (sk );
1553
- tunnel -> sock = sk ;
1554
1446
tunnel -> fd = fd ;
1555
1447
1556
- /* Hook on the tunnel socket destructor so that we can cleanup
1557
- * if the tunnel socket goes away.
1558
- */
1559
- tunnel -> old_sk_destruct = sk -> sk_destruct ;
1560
- sk -> sk_destruct = & l2tp_tunnel_destruct ;
1561
- lockdep_set_class_and_name (& sk -> sk_lock .slock , & l2tp_socket_class , "l2tp_sock" );
1562
-
1563
- sk -> sk_allocation = GFP_ATOMIC ;
1564
-
1565
1448
/* Init delete workqueue struct */
1566
1449
INIT_WORK (& tunnel -> del_work , l2tp_tunnel_del_work );
1567
1450
1568
- /* Add tunnel to our list */
1569
1451
INIT_LIST_HEAD (& tunnel -> list );
1570
- spin_lock_bh (& pn -> l2tp_tunnel_list_lock );
1571
- list_add_rcu (& tunnel -> list , & pn -> l2tp_tunnel_list );
1572
- spin_unlock_bh (& pn -> l2tp_tunnel_list_lock );
1573
1452
1574
1453
err = 0 ;
1575
1454
err :
1576
1455
if (tunnelp )
1577
1456
* tunnelp = tunnel ;
1578
1457
1579
- /* If tunnel's socket was created by the kernel, it doesn't
1580
- * have a file.
1581
- */
1582
- if (sock && sock -> file )
1583
- sockfd_put (sock );
1584
-
1585
1458
return err ;
1586
1459
}
1587
1460
EXPORT_SYMBOL_GPL (l2tp_tunnel_create );
1588
1461
1462
+ static int l2tp_validate_socket (const struct sock * sk , const struct net * net ,
1463
+ enum l2tp_encap_type encap )
1464
+ {
1465
+ if (!net_eq (sock_net (sk ), net ))
1466
+ return - EINVAL ;
1467
+
1468
+ if (sk -> sk_type != SOCK_DGRAM )
1469
+ return - EPROTONOSUPPORT ;
1470
+
1471
+ if ((encap == L2TP_ENCAPTYPE_UDP && sk -> sk_protocol != IPPROTO_UDP ) ||
1472
+ (encap == L2TP_ENCAPTYPE_IP && sk -> sk_protocol != IPPROTO_L2TP ))
1473
+ return - EPROTONOSUPPORT ;
1474
+
1475
+ if (sk -> sk_user_data )
1476
+ return - EBUSY ;
1477
+
1478
+ return 0 ;
1479
+ }
1480
+
1481
+ int l2tp_tunnel_register (struct l2tp_tunnel * tunnel , struct net * net ,
1482
+ struct l2tp_tunnel_cfg * cfg )
1483
+ {
1484
+ struct l2tp_tunnel * tunnel_walk ;
1485
+ struct l2tp_net * pn ;
1486
+ struct socket * sock ;
1487
+ struct sock * sk ;
1488
+ int ret ;
1489
+
1490
+ if (tunnel -> fd < 0 ) {
1491
+ ret = l2tp_tunnel_sock_create (net , tunnel -> tunnel_id ,
1492
+ tunnel -> peer_tunnel_id , cfg ,
1493
+ & sock );
1494
+ if (ret < 0 )
1495
+ goto err ;
1496
+ } else {
1497
+ sock = sockfd_lookup (tunnel -> fd , & ret );
1498
+ if (!sock )
1499
+ goto err ;
1500
+
1501
+ ret = l2tp_validate_socket (sock -> sk , net , tunnel -> encap );
1502
+ if (ret < 0 )
1503
+ goto err_sock ;
1504
+ }
1505
+
1506
+ sk = sock -> sk ;
1507
+
1508
+ sock_hold (sk );
1509
+ tunnel -> sock = sk ;
1510
+ tunnel -> l2tp_net = net ;
1511
+
1512
+ pn = l2tp_pernet (net );
1513
+
1514
+ spin_lock_bh (& pn -> l2tp_tunnel_list_lock );
1515
+ list_for_each_entry (tunnel_walk , & pn -> l2tp_tunnel_list , list ) {
1516
+ if (tunnel_walk -> tunnel_id == tunnel -> tunnel_id ) {
1517
+ spin_unlock_bh (& pn -> l2tp_tunnel_list_lock );
1518
+
1519
+ ret = - EEXIST ;
1520
+ goto err_sock ;
1521
+ }
1522
+ }
1523
+ list_add_rcu (& tunnel -> list , & pn -> l2tp_tunnel_list );
1524
+ spin_unlock_bh (& pn -> l2tp_tunnel_list_lock );
1525
+
1526
+ if (tunnel -> encap == L2TP_ENCAPTYPE_UDP ) {
1527
+ struct udp_tunnel_sock_cfg udp_cfg = {
1528
+ .sk_user_data = tunnel ,
1529
+ .encap_type = UDP_ENCAP_L2TPINUDP ,
1530
+ .encap_rcv = l2tp_udp_encap_recv ,
1531
+ .encap_destroy = l2tp_udp_encap_destroy ,
1532
+ };
1533
+
1534
+ setup_udp_tunnel_sock (net , sock , & udp_cfg );
1535
+ } else {
1536
+ sk -> sk_user_data = tunnel ;
1537
+ }
1538
+
1539
+ tunnel -> old_sk_destruct = sk -> sk_destruct ;
1540
+ sk -> sk_destruct = & l2tp_tunnel_destruct ;
1541
+ lockdep_set_class_and_name (& sk -> sk_lock .slock , & l2tp_socket_class ,
1542
+ "l2tp_sock" );
1543
+ sk -> sk_allocation = GFP_ATOMIC ;
1544
+
1545
+ if (tunnel -> fd >= 0 )
1546
+ sockfd_put (sock );
1547
+
1548
+ return 0 ;
1549
+
1550
+ err_sock :
1551
+ if (tunnel -> fd < 0 )
1552
+ sock_release (sock );
1553
+ else
1554
+ sockfd_put (sock );
1555
+ err :
1556
+ return ret ;
1557
+ }
1558
+ EXPORT_SYMBOL_GPL (l2tp_tunnel_register );
1559
+
1589
1560
/* This function is used by the netlink TUNNEL_DELETE command.
1590
1561
*/
1591
1562
void l2tp_tunnel_delete (struct l2tp_tunnel * tunnel )
0 commit comments