]> nv-tegra.nvidia Code Review - linux-3.10.git/blobdiff - net/l2tp/l2tp_core.c
net: proc: change proc_net_remove to remove_proc_entry
[linux-3.10.git] / net / l2tp / l2tp_core.c
index 32b2155e7ab496e93fe5c2b7396a94c8f7bcd09b..dcfd64e83ab7abc26177dddc75d48b84c8d28558 100644 (file)
@@ -101,6 +101,7 @@ struct l2tp_skb_cb {
 
 static atomic_t l2tp_tunnel_count;
 static atomic_t l2tp_session_count;
+static struct workqueue_struct *l2tp_wq;
 
 /* per-net private data for this module */
 static unsigned int l2tp_net_id;
@@ -122,7 +123,6 @@ static inline struct l2tp_net *l2tp_pernet(struct net *net)
        return net_generic(net, l2tp_net_id);
 }
 
-
 /* Tunnel reference counts. Incremented per session that is added to
  * the tunnel.
  */
@@ -168,6 +168,51 @@ l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
 
 }
 
+/* Lookup the tunnel socket, possibly involving the fs code if the socket is
+ * owned by userspace.  A struct sock returned from this function must be
+ * released using l2tp_tunnel_sock_put once you're done with it.
+ */
+struct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel)
+{
+       int err = 0;
+       struct socket *sock = NULL;
+       struct sock *sk = NULL;
+
+       if (!tunnel)
+               goto out;
+
+       if (tunnel->fd >= 0) {
+               /* Socket is owned by userspace, who might be in the process
+                * of closing it.  Look the socket up using the fd to ensure
+                * consistency.
+                */
+               sock = sockfd_lookup(tunnel->fd, &err);
+               if (sock)
+                       sk = sock->sk;
+       } else {
+               /* Socket is owned by kernelspace */
+               sk = tunnel->sock;
+       }
+
+out:
+       return sk;
+}
+EXPORT_SYMBOL_GPL(l2tp_tunnel_sock_lookup);
+
+/* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */
+void l2tp_tunnel_sock_put(struct sock *sk)
+{
+       struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
+       if (tunnel) {
+               if (tunnel->fd >= 0) {
+                       /* Socket is owned by userspace */
+                       sockfd_put(sk->sk_socket);
+               }
+               sock_put(sk);
+       }
+}
+EXPORT_SYMBOL_GPL(l2tp_tunnel_sock_put);
+
 /* Lookup a session by id in the global session list
  */
 static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id)
@@ -1123,11 +1168,10 @@ int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len
        struct udphdr *uh;
        struct inet_sock *inet;
        __wsum csum;
-       int old_headroom;
-       int new_headroom;
        int headroom;
        int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
        int udp_len;
+       int ret = NET_XMIT_SUCCESS;
 
        /* Check that there's enough headroom in the skb to insert IP,
         * UDP and L2TP headers. If not enough, expand it to
@@ -1135,16 +1179,12 @@ int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len
         */
        headroom = NET_SKB_PAD + sizeof(struct iphdr) +
                uhlen + hdr_len;
-       old_headroom = skb_headroom(skb);
        if (skb_cow_head(skb, headroom)) {
-               dev_kfree_skb(skb);
-               goto abort;
+               kfree_skb(skb);
+               return NET_XMIT_DROP;
        }
 
-       new_headroom = skb_headroom(skb);
        skb_orphan(skb);
-       skb->truesize += new_headroom - old_headroom;
-
        /* Setup L2TP header */
        session->build_header(session, __skb_push(skb, hdr_len));
 
@@ -1156,7 +1196,8 @@ int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len
 
        bh_lock_sock(sk);
        if (sock_owned_by_user(sk)) {
-               dev_kfree_skb(skb);
+               kfree_skb(skb);
+               ret = NET_XMIT_DROP;
                goto out_unlock;
        }
 
@@ -1215,8 +1256,7 @@ int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len
 out_unlock:
        bh_unlock_sock(sk);
 
-abort:
-       return 0;
+       return ret;
 }
 EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
 
@@ -1231,6 +1271,7 @@ EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
 static void l2tp_tunnel_destruct(struct sock *sk)
 {
        struct l2tp_tunnel *tunnel;
+       struct l2tp_net *pn;
 
        tunnel = sk->sk_user_data;
        if (tunnel == NULL)
@@ -1238,9 +1279,8 @@ static void l2tp_tunnel_destruct(struct sock *sk)
 
        l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
 
-       /* Close all sessions */
-       l2tp_tunnel_closeall(tunnel);
 
+       /* Disable udp encapsulation */
        switch (tunnel->encap) {
        case L2TP_ENCAPTYPE_UDP:
                /* No longer an encapsulation socket. See net/ipv4/udp.c */
@@ -1252,17 +1292,23 @@ static void l2tp_tunnel_destruct(struct sock *sk)
        }
 
        /* Remove hooks into tunnel socket */
-       tunnel->sock = NULL;
        sk->sk_destruct = tunnel->old_sk_destruct;
        sk->sk_user_data = NULL;
+       tunnel->sock = NULL;
 
-       /* Call the original destructor */
-       if (sk->sk_destruct)
-               (*sk->sk_destruct)(sk);
+       /* Remove the tunnel struct from the tunnel list */
+       pn = l2tp_pernet(tunnel->l2tp_net);
+       spin_lock_bh(&pn->l2tp_tunnel_list_lock);
+       list_del_rcu(&tunnel->list);
+       spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
+       atomic_dec(&l2tp_tunnel_count);
 
-       /* We're finished with the socket */
+       l2tp_tunnel_closeall(tunnel);
        l2tp_tunnel_dec_refcount(tunnel);
 
+       /* Call the original destructor */
+       if (sk->sk_destruct)
+               (*sk->sk_destruct)(sk);
 end:
        return;
 }
@@ -1336,49 +1382,77 @@ again:
  */
 static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
 {
-       struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
-
        BUG_ON(atomic_read(&tunnel->ref_count) != 0);
        BUG_ON(tunnel->sock != NULL);
-
        l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
+       kfree_rcu(tunnel, rcu);
+}
 
-       /* Remove from tunnel list */
-       spin_lock_bh(&pn->l2tp_tunnel_list_lock);
-       list_del_rcu(&tunnel->list);
-       spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
-       synchronize_rcu();
+/* Workqueue tunnel deletion function */
+static void l2tp_tunnel_del_work(struct work_struct *work)
+{
+       struct l2tp_tunnel *tunnel = NULL;
+       struct socket *sock = NULL;
+       struct sock *sk = NULL;
 
-       atomic_dec(&l2tp_tunnel_count);
-       kfree(tunnel);
+       tunnel = container_of(work, struct l2tp_tunnel, del_work);
+       sk = l2tp_tunnel_sock_lookup(tunnel);
+       if (!sk)
+               return;
+
+       sock = sk->sk_socket;
+       BUG_ON(!sock);
+
+       /* If the tunnel socket was created directly by the kernel, use the
+        * sk_* API to release the socket now.  Otherwise go through the
+        * inet_* layer to shut the socket down, and let userspace close it.
+        * In either case the tunnel resources are freed in the socket
+        * destructor when the tunnel socket goes away.
+        */
+       if (sock->file == NULL) {
+               kernel_sock_shutdown(sock, SHUT_RDWR);
+               sk_release_kernel(sk);
+       } else {
+               inet_shutdown(sock, 2);
+       }
+
+       l2tp_tunnel_sock_put(sk);
 }
 
 /* Create a socket for the tunnel, if one isn't set up by
  * userspace. This is used for static tunnels where there is no
  * managing L2TP daemon.
+ *
+ * Since we don't want these sockets to keep a namespace alive by
+ * themselves, we drop the socket's namespace refcount after creation.
+ * These sockets are freed when the namespace exits using the pernet
+ * exit hook.
  */
-static int l2tp_tunnel_sock_create(u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct socket **sockp)
+static int l2tp_tunnel_sock_create(struct net *net,
+                               u32 tunnel_id,
+                               u32 peer_tunnel_id,
+                               struct l2tp_tunnel_cfg *cfg,
+                               struct socket **sockp)
 {
        int err = -EINVAL;
-       struct sockaddr_in udp_addr;
+       struct socket *sock = NULL;
+       struct sockaddr_in udp_addr = {0};
+       struct sockaddr_l2tpip ip_addr = {0};
 #if IS_ENABLED(CONFIG_IPV6)
-       struct sockaddr_in6 udp6_addr;
-       struct sockaddr_l2tpip6 ip6_addr;
+       struct sockaddr_in6 udp6_addr = {0};
+       struct sockaddr_l2tpip6 ip6_addr = {0};
 #endif
-       struct sockaddr_l2tpip ip_addr;
-       struct socket *sock = NULL;
 
        switch (cfg->encap) {
        case L2TP_ENCAPTYPE_UDP:
 #if IS_ENABLED(CONFIG_IPV6)
                if (cfg->local_ip6 && cfg->peer_ip6) {
-                       err = sock_create(AF_INET6, SOCK_DGRAM, 0, sockp);
+                       err = sock_create_kern(AF_INET6, SOCK_DGRAM, 0, &sock);
                        if (err < 0)
                                goto out;
 
-                       sock = *sockp;
+                       sk_change_net(sock->sk, net);
 
-                       memset(&udp6_addr, 0, sizeof(udp6_addr));
                        udp6_addr.sin6_family = AF_INET6;
                        memcpy(&udp6_addr.sin6_addr, cfg->local_ip6,
                               sizeof(udp6_addr.sin6_addr));
@@ -1400,13 +1474,12 @@ static int l2tp_tunnel_sock_create(u32 tunnel_id, u32 peer_tunnel_id, struct l2t
                } else
 #endif
                {
-                       err = sock_create(AF_INET, SOCK_DGRAM, 0, sockp);
+                       err = sock_create_kern(AF_INET, SOCK_DGRAM, 0, &sock);
                        if (err < 0)
                                goto out;
 
-                       sock = *sockp;
+                       sk_change_net(sock->sk, net);
 
-                       memset(&udp_addr, 0, sizeof(udp_addr));
                        udp_addr.sin_family = AF_INET;
                        udp_addr.sin_addr = cfg->local_ip;
                        udp_addr.sin_port = htons(cfg->local_udp_port);
@@ -1433,14 +1506,13 @@ static int l2tp_tunnel_sock_create(u32 tunnel_id, u32 peer_tunnel_id, struct l2t
        case L2TP_ENCAPTYPE_IP:
 #if IS_ENABLED(CONFIG_IPV6)
                if (cfg->local_ip6 && cfg->peer_ip6) {
-                       err = sock_create(AF_INET6, SOCK_DGRAM, IPPROTO_L2TP,
-                                         sockp);
+                       err = sock_create_kern(AF_INET6, SOCK_DGRAM,
+                                         IPPROTO_L2TP, &sock);
                        if (err < 0)
                                goto out;
 
-                       sock = *sockp;
+                       sk_change_net(sock->sk, net);
 
-                       memset(&ip6_addr, 0, sizeof(ip6_addr));
                        ip6_addr.l2tp_family = AF_INET6;
                        memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
                               sizeof(ip6_addr.l2tp_addr));
@@ -1462,14 +1534,13 @@ static int l2tp_tunnel_sock_create(u32 tunnel_id, u32 peer_tunnel_id, struct l2t
                } else
 #endif
                {
-                       err = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_L2TP,
-                                         sockp);
+                       err = sock_create_kern(AF_INET, SOCK_DGRAM,
+                                         IPPROTO_L2TP, &sock);
                        if (err < 0)
                                goto out;
 
-                       sock = *sockp;
+                       sk_change_net(sock->sk, net);
 
-                       memset(&ip_addr, 0, sizeof(ip_addr));
                        ip_addr.l2tp_family = AF_INET;
                        ip_addr.l2tp_addr = cfg->local_ip;
                        ip_addr.l2tp_conn_id = tunnel_id;
@@ -1493,14 +1564,18 @@ static int l2tp_tunnel_sock_create(u32 tunnel_id, u32 peer_tunnel_id, struct l2t
        }
 
 out:
+       *sockp = sock;
        if ((err < 0) && sock) {
-               sock_release(sock);
+               kernel_sock_shutdown(sock, SHUT_RDWR);
+               sk_release_kernel(sock->sk);
                *sockp = NULL;
        }
 
        return err;
 }
 
+static struct lock_class_key l2tp_socket_class;
+
 int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp)
 {
        struct l2tp_tunnel *tunnel = NULL;
@@ -1515,15 +1590,23 @@ int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32
         * kernel socket.
         */
        if (fd < 0) {
-               err = l2tp_tunnel_sock_create(tunnel_id, peer_tunnel_id, cfg, &sock);
+               err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
+                               cfg, &sock);
                if (err < 0)
                        goto err;
        } else {
-               err = -EBADF;
                sock = sockfd_lookup(fd, &err);
                if (!sock) {
-                       pr_err("tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
+                       pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
                               tunnel_id, fd, err);
+                       err = -EBADF;
+                       goto err;
+               }
+
+               /* Reject namespace mismatches */
+               if (!net_eq(sock_net(sock->sk), net)) {
+                       pr_err("tunl %u: netns mismatch\n", tunnel_id);
+                       err = -EINVAL;
                        goto err;
                }
        }
@@ -1605,8 +1688,14 @@ int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32
        tunnel->old_sk_destruct = sk->sk_destruct;
        sk->sk_destruct = &l2tp_tunnel_destruct;
        tunnel->sock = sk;
+       tunnel->fd = fd;
+       lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
+
        sk->sk_allocation = GFP_ATOMIC;
 
+       /* Init delete workqueue struct */
+       INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
+
        /* Add tunnel to our list */
        INIT_LIST_HEAD(&tunnel->list);
        atomic_inc(&l2tp_tunnel_count);
@@ -1638,25 +1727,7 @@ EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
  */
 int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
 {
-       int err = 0;
-       struct socket *sock = tunnel->sock ? tunnel->sock->sk_socket : NULL;
-
-       /* Force the tunnel socket to close. This will eventually
-        * cause the tunnel to be deleted via the normal socket close
-        * mechanisms when userspace closes the tunnel socket.
-        */
-       if (sock != NULL) {
-               err = inet_shutdown(sock, 2);
-
-               /* If the tunnel's socket was created by the kernel,
-                * close the socket here since the socket was not
-                * created by userspace.
-                */
-               if (sock->file == NULL)
-                       err = inet_release(sock);
-       }
-
-       return err;
+       return (false == queue_work(l2tp_wq, &tunnel->del_work));
 }
 EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
 
@@ -1840,8 +1911,21 @@ static __net_init int l2tp_init_net(struct net *net)
        return 0;
 }
 
+static __net_exit void l2tp_exit_net(struct net *net)
+{
+       struct l2tp_net *pn = l2tp_pernet(net);
+       struct l2tp_tunnel *tunnel = NULL;
+
+       rcu_read_lock_bh();
+       list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
+               (void)l2tp_tunnel_delete(tunnel);
+       }
+       rcu_read_unlock_bh();
+}
+
 static struct pernet_operations l2tp_net_ops = {
        .init = l2tp_init_net,
+       .exit = l2tp_exit_net,
        .id   = &l2tp_net_id,
        .size = sizeof(struct l2tp_net),
 };
@@ -1854,6 +1938,13 @@ static int __init l2tp_init(void)
        if (rc)
                goto out;
 
+       l2tp_wq = alloc_workqueue("l2tp", WQ_NON_REENTRANT | WQ_UNBOUND, 0);
+       if (!l2tp_wq) {
+               pr_err("alloc_workqueue failed\n");
+               rc = -ENOMEM;
+               goto out;
+       }
+
        pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
 
 out:
@@ -1863,6 +1954,10 @@ out:
 static void __exit l2tp_exit(void)
 {
        unregister_pernet_device(&l2tp_net_ops);
+       if (l2tp_wq) {
+               destroy_workqueue(l2tp_wq);
+               l2tp_wq = NULL;
+       }
 }
 
 module_init(l2tp_init);