net: Fix use after free by removing length arg from sk_data_ready callbacks.
[linux.git] / net / tipc / socket.c
index 0ed0eaa62f29e7148052907892ff90ebd20d2fed..3c0256962f7dafa4ee3b11d69aed963822c888c2 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * net/tipc/socket.c: TIPC socket API
  *
- * Copyright (c) 2001-2007, 2012 Ericsson AB
+ * Copyright (c) 2001-2007, 2012-2014, Ericsson AB
  * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
  * All rights reserved.
  *
 #include "port.h"
 
 #include <linux/export.h>
-#include <net/sock.h>
 
 #define SS_LISTENING   -1      /* socket is listening */
 #define SS_READY       -2      /* socket is connectionless */
 
 #define CONN_TIMEOUT_DEFAULT   8000    /* default connect timeout = 8s */
 
-struct tipc_sock {
-       struct sock sk;
-       struct tipc_port *p;
-       struct tipc_portid peer_name;
-       unsigned int conn_timeout;
-};
-
-#define tipc_sk(sk) ((struct tipc_sock *)(sk))
-#define tipc_sk_port(sk) (tipc_sk(sk)->p)
-
 static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
-static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
-static void wakeupdispatch(struct tipc_port *tport);
-static void tipc_data_ready(struct sock *sk, int len);
+static void tipc_data_ready(struct sock *sk);
 static void tipc_write_space(struct sock *sk);
-static int release(struct socket *sock);
-static int accept(struct socket *sock, struct socket *new_sock, int flags);
+static int tipc_release(struct socket *sock);
+static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
 
 static const struct proto_ops packet_ops;
 static const struct proto_ops stream_ops;
@@ -115,6 +102,8 @@ static struct proto tipc_proto_kern;
  *   - port reference
  */
 
+#include "socket.h"
+
 /**
  * advance_rx_queue - discard first buffer in socket receive queue
  *
@@ -150,13 +139,15 @@ static void reject_rx_queue(struct sock *sk)
  *
  * Returns 0 on success, errno otherwise
  */
-static int tipc_sk_create(struct net *net, struct socket *sock, int protocol,
-                         int kern)
+static int tipc_sk_create(struct net *net, struct socket *sock,
+                         int protocol, int kern)
 {
        const struct proto_ops *ops;
        socket_state state;
        struct sock *sk;
-       struct tipc_port *tp_ptr;
+       struct tipc_sock *tsk;
+       struct tipc_port *port;
+       u32 ref;
 
        /* Validate arguments */
        if (unlikely(protocol != 0))
@@ -189,10 +180,12 @@ static int tipc_sk_create(struct net *net, struct socket *sock, int protocol,
        if (sk == NULL)
                return -ENOMEM;
 
-       /* Allocate TIPC port for socket to use */
-       tp_ptr = tipc_createport(sk, &dispatch, &wakeupdispatch,
-                                TIPC_LOW_IMPORTANCE);
-       if (unlikely(!tp_ptr)) {
+       tsk = tipc_sk(sk);
+       port = &tsk->port;
+
+       ref = tipc_port_init(port, TIPC_LOW_IMPORTANCE);
+       if (!ref) {
+               pr_warn("Socket registration failed, ref. table exhausted\n");
                sk_free(sk);
                return -ENOMEM;
        }
@@ -206,17 +199,14 @@ static int tipc_sk_create(struct net *net, struct socket *sock, int protocol,
        sk->sk_rcvbuf = sysctl_tipc_rmem[1];
        sk->sk_data_ready = tipc_data_ready;
        sk->sk_write_space = tipc_write_space;
-       tipc_sk(sk)->p = tp_ptr;
        tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
-
-       spin_unlock_bh(tp_ptr->lock);
+       tipc_port_unlock(port);
 
        if (sock->state == SS_READY) {
-               tipc_set_portunreturnable(tp_ptr->ref, 1);
+               tipc_port_set_unreturnable(port, true);
                if (sock->type == SOCK_DGRAM)
-                       tipc_set_portunreliable(tp_ptr->ref, 1);
+                       tipc_port_set_unreliable(port, true);
        }
-
        return 0;
 }
 
@@ -254,7 +244,7 @@ int tipc_sock_create_local(int type, struct socket **res)
  */
 void tipc_sock_release_local(struct socket *sock)
 {
-       release(sock);
+       tipc_release(sock);
        sock->ops = NULL;
        sock_release(sock);
 }
@@ -280,7 +270,7 @@ int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
        if (ret < 0)
                return ret;
 
-       ret = accept(sock, *newsock, flags);
+       ret = tipc_accept(sock, *newsock, flags);
        if (ret < 0) {
                sock_release(*newsock);
                return ret;
@@ -290,7 +280,7 @@ int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
 }
 
 /**
- * release - destroy a TIPC socket
+ * tipc_release - destroy a TIPC socket
  * @sock: socket to destroy
  *
  * This routine cleans up any messages that are still queued on the socket.
@@ -305,12 +295,12 @@ int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
  *
  * Returns 0 on success, errno otherwise
  */
-static int release(struct socket *sock)
+static int tipc_release(struct socket *sock)
 {
        struct sock *sk = sock->sk;
-       struct tipc_port *tport;
+       struct tipc_sock *tsk;
+       struct tipc_port *port;
        struct sk_buff *buf;
-       int res;
 
        /*
         * Exit if socket isn't fully initialized (occurs when a failed accept()
@@ -319,7 +309,8 @@ static int release(struct socket *sock)
        if (sk == NULL)
                return 0;
 
-       tport = tipc_sk_port(sk);
+       tsk = tipc_sk(sk);
+       port = &tsk->port;
        lock_sock(sk);
 
        /*
@@ -336,17 +327,16 @@ static int release(struct socket *sock)
                        if ((sock->state == SS_CONNECTING) ||
                            (sock->state == SS_CONNECTED)) {
                                sock->state = SS_DISCONNECTING;
-                               tipc_disconnect(tport->ref);
+                               tipc_port_disconnect(port->ref);
                        }
                        tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
                }
        }
 
-       /*
-        * Delete TIPC port; this ensures no more messages are queued
-        * (also disconnects an active connection & sends a 'FIN-' to peer)
+       /* Destroy TIPC port; also disconnects an active connection and
+        * sends a 'FIN-' to peer.
         */
-       res = tipc_deleteport(tport);
+       tipc_port_destroy(port);
 
        /* Discard any remaining (connection-based) messages in receive queue */
        __skb_queue_purge(&sk->sk_receive_queue);
@@ -358,11 +348,11 @@ static int release(struct socket *sock)
        sock_put(sk);
        sock->sk = NULL;
 
-       return res;
+       return 0;
 }
 
 /**
- * bind - associate or disassocate TIPC name(s) with a socket
+ * tipc_bind - associate or disassocate TIPC name(s) with a socket
  * @sock: socket structure
  * @uaddr: socket address describing name(s) and desired operation
  * @uaddr_len: size of socket address data structure
@@ -376,16 +366,17 @@ static int release(struct socket *sock)
  * NOTE: This routine doesn't need to take the socket lock since it doesn't
  *       access any non-constant socket information.
  */
-static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
+static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
+                    int uaddr_len)
 {
        struct sock *sk = sock->sk;
        struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
-       struct tipc_port *tport = tipc_sk_port(sock->sk);
+       struct tipc_sock *tsk = tipc_sk(sk);
        int res = -EINVAL;
 
        lock_sock(sk);
        if (unlikely(!uaddr_len)) {
-               res = tipc_withdraw(tport, 0, NULL);
+               res = tipc_withdraw(&tsk->port, 0, NULL);
                goto exit;
        }
 
@@ -413,15 +404,15 @@ static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
        }
 
        res = (addr->scope > 0) ?
-               tipc_publish(tport, addr->scope, &addr->addr.nameseq) :
-               tipc_withdraw(tport, -addr->scope, &addr->addr.nameseq);
+               tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) :
+               tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq);
 exit:
        release_sock(sk);
        return res;
 }
 
 /**
- * get_name - get port ID of socket or peer socket
+ * tipc_getname - get port ID of socket or peer socket
  * @sock: socket structure
  * @uaddr: area for returned socket address
  * @uaddr_len: area for returned length of socket address
@@ -433,21 +424,21 @@ exit:
  *       accesses socket information that is unchanging (or which changes in
  *       a completely predictable manner).
  */
-static int get_name(struct socket *sock, struct sockaddr *uaddr,
-                   int *uaddr_len, int peer)
+static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
+                       int *uaddr_len, int peer)
 {
        struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
-       struct tipc_sock *tsock = tipc_sk(sock->sk);
+       struct tipc_sock *tsk = tipc_sk(sock->sk);
 
        memset(addr, 0, sizeof(*addr));
        if (peer) {
                if ((sock->state != SS_CONNECTED) &&
                        ((peer != 2) || (sock->state != SS_DISCONNECTING)))
                        return -ENOTCONN;
-               addr->addr.id.ref = tsock->peer_name.ref;
-               addr->addr.id.node = tsock->peer_name.node;
+               addr->addr.id.ref = tipc_port_peerport(&tsk->port);
+               addr->addr.id.node = tipc_port_peernode(&tsk->port);
        } else {
-               addr->addr.id.ref = tsock->p->ref;
+               addr->addr.id.ref = tsk->port.ref;
                addr->addr.id.node = tipc_own_addr;
        }
 
@@ -461,7 +452,7 @@ static int get_name(struct socket *sock, struct sockaddr *uaddr,
 }
 
 /**
- * poll - read and possibly block on pollmask
+ * tipc_poll - read and possibly block on pollmask
  * @file: file structure associated with the socket
  * @sock: socket for which to calculate the poll bits
  * @wait: ???
@@ -500,22 +491,23 @@ static int get_name(struct socket *sock, struct sockaddr *uaddr,
  * imply that the operation will succeed, merely that it should be performed
  * and will not block.
  */
-static unsigned int poll(struct file *file, struct socket *sock,
-                        poll_table *wait)
+static unsigned int tipc_poll(struct file *file, struct socket *sock,
+                             poll_table *wait)
 {
        struct sock *sk = sock->sk;
+       struct tipc_sock *tsk = tipc_sk(sk);
        u32 mask = 0;
 
        sock_poll_wait(file, sk_sleep(sk), wait);
 
        switch ((int)sock->state) {
        case SS_UNCONNECTED:
-               if (!tipc_sk_port(sk)->congested)
+               if (!tsk->port.congested)
                        mask |= POLLOUT;
                break;
        case SS_READY:
        case SS_CONNECTED:
-               if (!tipc_sk_port(sk)->congested)
+               if (!tsk->port.congested)
                        mask |= POLLOUT;
                /* fall thru' */
        case SS_CONNECTING:
@@ -565,7 +557,7 @@ static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
 static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
 {
        struct sock *sk = sock->sk;
-       struct tipc_port *tport = tipc_sk_port(sk);
+       struct tipc_sock *tsk = tipc_sk(sk);
        DEFINE_WAIT(wait);
        int done;
 
@@ -581,14 +573,15 @@ static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
                        return sock_intr_errno(*timeo_p);
 
                prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
-               done = sk_wait_event(sk, timeo_p, !tport->congested);
+               done = sk_wait_event(sk, timeo_p, !tsk->port.congested);
                finish_wait(sk_sleep(sk), &wait);
        } while (!done);
        return 0;
 }
 
+
 /**
- * send_msg - send message in connectionless manner
+ * tipc_sendmsg - send message in connectionless manner
  * @iocb: if NULL, indicates that socket lock is already held
  * @sock: socket structure
  * @m: message to send
@@ -601,11 +594,12 @@ static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
  *
  * Returns the number of bytes sent on success, or errno otherwise
  */
-static int send_msg(struct kiocb *iocb, struct socket *sock,
-                   struct msghdr *m, size_t total_len)
+static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
+                       struct msghdr *m, size_t total_len)
 {
        struct sock *sk = sock->sk;
-       struct tipc_port *tport = tipc_sk_port(sk);
+       struct tipc_sock *tsk = tipc_sk(sk);
+       struct tipc_port *port = &tsk->port;
        DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
        int needs_conn;
        long timeo;
@@ -632,13 +626,13 @@ static int send_msg(struct kiocb *iocb, struct socket *sock,
                        res = -EISCONN;
                        goto exit;
                }
-               if (tport->published) {
+               if (tsk->port.published) {
                        res = -EOPNOTSUPP;
                        goto exit;
                }
                if (dest->addrtype == TIPC_ADDR_NAME) {
-                       tport->conn_type = dest->addr.name.name.type;
-                       tport->conn_instance = dest->addr.name.name.instance;
+                       tsk->port.conn_type = dest->addr.name.name.type;
+                       tsk->port.conn_instance = dest->addr.name.name.instance;
                }
 
                /* Abort any pending connection attempts (very unlikely) */
@@ -651,13 +645,13 @@ static int send_msg(struct kiocb *iocb, struct socket *sock,
                        res = dest_name_check(dest, m);
                        if (res)
                                break;
-                       res = tipc_send2name(tport->ref,
+                       res = tipc_send2name(port,
                                             &dest->addr.name.name,
                                             dest->addr.name.domain,
                                             m->msg_iov,
                                             total_len);
                } else if (dest->addrtype == TIPC_ADDR_ID) {
-                       res = tipc_send2port(tport->ref,
+                       res = tipc_send2port(port,
                                             &dest->addr.id,
                                             m->msg_iov,
                                             total_len);
@@ -669,10 +663,10 @@ static int send_msg(struct kiocb *iocb, struct socket *sock,
                        res = dest_name_check(dest, m);
                        if (res)
                                break;
-                       res = tipc_multicast(tport->ref,
-                                            &dest->addr.nameseq,
-                                            m->msg_iov,
-                                            total_len);
+                       res = tipc_port_mcast_xmit(port,
+                                                  &dest->addr.nameseq,
+                                                  m->msg_iov,
+                                                  total_len);
                }
                if (likely(res != -ELINKCONG)) {
                        if (needs_conn && (res >= 0))
@@ -693,7 +687,8 @@ exit:
 static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
 {
        struct sock *sk = sock->sk;
-       struct tipc_port *tport = tipc_sk_port(sk);
+       struct tipc_sock *tsk = tipc_sk(sk);
+       struct tipc_port *port = &tsk->port;
        DEFINE_WAIT(wait);
        int done;
 
@@ -712,14 +707,14 @@ static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
 
                prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
                done = sk_wait_event(sk, timeo_p,
-                                    (!tport->congested || !tport->connected));
+                                    (!port->congested || !port->connected));
                finish_wait(sk_sleep(sk), &wait);
        } while (!done);
        return 0;
 }
 
 /**
- * send_packet - send a connection-oriented message
+ * tipc_send_packet - send a connection-oriented message
  * @iocb: if NULL, indicates that socket lock is already held
  * @sock: socket structure
  * @m: message to send
@@ -729,18 +724,18 @@ static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
  *
  * Returns the number of bytes sent on success, or errno otherwise
  */
-static int send_packet(struct kiocb *iocb, struct socket *sock,
-                      struct msghdr *m, size_t total_len)
+static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
+                           struct msghdr *m, size_t total_len)
 {
        struct sock *sk = sock->sk;
-       struct tipc_port *tport = tipc_sk_port(sk);
+       struct tipc_sock *tsk = tipc_sk(sk);
        DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
        int res = -EINVAL;
        long timeo;
 
        /* Handle implied connection establishment */
        if (unlikely(dest))
-               return send_msg(iocb, sock, m, total_len);
+               return tipc_sendmsg(iocb, sock, m, total_len);
 
        if (total_len > TIPC_MAX_USER_MSG_SIZE)
                return -EMSGSIZE;
@@ -758,7 +753,7 @@ static int send_packet(struct kiocb *iocb, struct socket *sock,
 
        timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
        do {
-               res = tipc_send(tport->ref, m->msg_iov, total_len);
+               res = tipc_send(&tsk->port, m->msg_iov, total_len);
                if (likely(res != -ELINKCONG))
                        break;
                res = tipc_wait_for_sndpkt(sock, &timeo);
@@ -772,7 +767,7 @@ exit:
 }
 
 /**
- * send_stream - send stream-oriented data
+ * tipc_send_stream - send stream-oriented data
  * @iocb: (unused)
  * @sock: socket structure
  * @m: data to send
@@ -783,11 +778,11 @@ exit:
  * Returns the number of bytes sent on success (or partial success),
  * or errno if no data sent
  */
-static int send_stream(struct kiocb *iocb, struct socket *sock,
-                      struct msghdr *m, size_t total_len)
+static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
+                           struct msghdr *m, size_t total_len)
 {
        struct sock *sk = sock->sk;
-       struct tipc_port *tport = tipc_sk_port(sk);
+       struct tipc_sock *tsk = tipc_sk(sk);
        struct msghdr my_msg;
        struct iovec my_iov;
        struct iovec *curr_iov;
@@ -804,7 +799,7 @@ static int send_stream(struct kiocb *iocb, struct socket *sock,
        /* Handle special cases where there is no connection */
        if (unlikely(sock->state != SS_CONNECTED)) {
                if (sock->state == SS_UNCONNECTED)
-                       res = send_packet(NULL, sock, m, total_len);
+                       res = tipc_send_packet(NULL, sock, m, total_len);
                else
                        res = sock->state == SS_DISCONNECTING ? -EPIPE : -ENOTCONN;
                goto exit;
@@ -835,21 +830,22 @@ static int send_stream(struct kiocb *iocb, struct socket *sock,
        my_msg.msg_name = NULL;
        bytes_sent = 0;
 
-       hdr_size = msg_hdr_sz(&tport->phdr);
+       hdr_size = msg_hdr_sz(&tsk->port.phdr);
 
        while (curr_iovlen--) {
                curr_start = curr_iov->iov_base;
                curr_left = curr_iov->iov_len;
 
                while (curr_left) {
-                       bytes_to_send = tport->max_pkt - hdr_size;
+                       bytes_to_send = tsk->port.max_pkt - hdr_size;
                        if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
                                bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
                        if (curr_left < bytes_to_send)
                                bytes_to_send = curr_left;
                        my_iov.iov_base = curr_start;
                        my_iov.iov_len = bytes_to_send;
-                       res = send_packet(NULL, sock, &my_msg, bytes_to_send);
+                       res = tipc_send_packet(NULL, sock, &my_msg,
+                                              bytes_to_send);
                        if (res < 0) {
                                if (bytes_sent)
                                        res = bytes_sent;
@@ -870,27 +866,25 @@ exit:
 
 /**
  * auto_connect - complete connection setup to a remote port
- * @sock: socket structure
+ * @tsk: tipc socket structure
  * @msg: peer's response message
  *
  * Returns 0 on success, errno otherwise
  */
-static int auto_connect(struct socket *sock, struct tipc_msg *msg)
+static int auto_connect(struct tipc_sock *tsk, struct tipc_msg *msg)
 {
-       struct tipc_sock *tsock = tipc_sk(sock->sk);
-       struct tipc_port *p_ptr;
+       struct tipc_port *port = &tsk->port;
+       struct socket *sock = tsk->sk.sk_socket;
+       struct tipc_portid peer;
 
-       tsock->peer_name.ref = msg_origport(msg);
-       tsock->peer_name.node = msg_orignode(msg);
-       p_ptr = tipc_port_deref(tsock->p->ref);
-       if (!p_ptr)
-               return -EINVAL;
+       peer.ref = msg_origport(msg);
+       peer.node = msg_orignode(msg);
 
-       __tipc_connect(tsock->p->ref, p_ptr, &tsock->peer_name);
+       __tipc_port_connect(port->ref, port, &peer);
 
        if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
                return -EINVAL;
-       msg_set_importance(&p_ptr->phdr, (u32)msg_importance(msg));
+       msg_set_importance(&port->phdr, (u32)msg_importance(msg));
        sock->state = SS_CONNECTED;
        return 0;
 }
@@ -1021,7 +1015,7 @@ static int tipc_wait_for_rcvmsg(struct socket *sock, long timeo)
 }
 
 /**
- * recv_msg - receive packet-oriented message
+ * tipc_recvmsg - receive packet-oriented message
  * @iocb: (unused)
  * @m: descriptor for message info
  * @buf_len: total size of user buffer area
@@ -1032,11 +1026,12 @@ static int tipc_wait_for_rcvmsg(struct socket *sock, long timeo)
  *
  * Returns size of returned message data, errno otherwise
  */
-static int recv_msg(struct kiocb *iocb, struct socket *sock,
-                   struct msghdr *m, size_t buf_len, int flags)
+static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
+                       struct msghdr *m, size_t buf_len, int flags)
 {
        struct sock *sk = sock->sk;
-       struct tipc_port *tport = tipc_sk_port(sk);
+       struct tipc_sock *tsk = tipc_sk(sk);
+       struct tipc_port *port = &tsk->port;
        struct sk_buff *buf;
        struct tipc_msg *msg;
        long timeo;
@@ -1079,7 +1074,7 @@ restart:
        set_orig_addr(m, msg);
 
        /* Capture ancillary data (optional) */
-       res = anc_data_recv(m, msg, tport);
+       res = anc_data_recv(m, msg, port);
        if (res)
                goto exit;
 
@@ -1105,8 +1100,8 @@ restart:
        /* Consume received message (optional) */
        if (likely(!(flags & MSG_PEEK))) {
                if ((sock->state != SS_READY) &&
-                   (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
-                       tipc_acknowledge(tport->ref, tport->conn_unacked);
+                   (++port->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
+                       tipc_acknowledge(port->ref, port->conn_unacked);
                advance_rx_queue(sk);
        }
 exit:
@@ -1115,7 +1110,7 @@ exit:
 }
 
 /**
- * recv_stream - receive stream-oriented data
+ * tipc_recv_stream - receive stream-oriented data
  * @iocb: (unused)
  * @m: descriptor for message info
  * @buf_len: total size of user buffer area
@@ -1126,11 +1121,12 @@ exit:
  *
  * Returns size of returned message data, errno otherwise
  */
-static int recv_stream(struct kiocb *iocb, struct socket *sock,
-                      struct msghdr *m, size_t buf_len, int flags)
+static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
+                           struct msghdr *m, size_t buf_len, int flags)
 {
        struct sock *sk = sock->sk;
-       struct tipc_port *tport = tipc_sk_port(sk);
+       struct tipc_sock *tsk = tipc_sk(sk);
+       struct tipc_port *port = &tsk->port;
        struct sk_buff *buf;
        struct tipc_msg *msg;
        long timeo;
@@ -1175,7 +1171,7 @@ restart:
        /* Optionally capture sender's address & ancillary data of first msg */
        if (sz_copied == 0) {
                set_orig_addr(m, msg);
-               res = anc_data_recv(m, msg, tport);
+               res = anc_data_recv(m, msg, port);
                if (res)
                        goto exit;
        }
@@ -1213,8 +1209,8 @@ restart:
 
        /* Consume received message (optional) */
        if (likely(!(flags & MSG_PEEK))) {
-               if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
-                       tipc_acknowledge(tport->ref, tport->conn_unacked);
+               if (unlikely(++port->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
+                       tipc_acknowledge(port->ref, port->conn_unacked);
                advance_rx_queue(sk);
        }
 
@@ -1252,7 +1248,7 @@ static void tipc_write_space(struct sock *sk)
  * @sk: socket
  * @len: the length of messages
  */
-static void tipc_data_ready(struct sock *sk, int len)
+static void tipc_data_ready(struct sock *sk)
 {
        struct socket_wq *wq;
 
@@ -1266,17 +1262,19 @@ static void tipc_data_ready(struct sock *sk, int len)
 
 /**
  * filter_connect - Handle all incoming messages for a connection-based socket
- * @tsock: TIPC socket
+ * @tsk: TIPC socket
  * @msg: message
  *
  * Returns TIPC error status code and socket error status code
  * once it encounters some errors
  */
-static u32 filter_connect(struct tipc_sock *tsock, struct sk_buff **buf)
+static u32 filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
 {
-       struct socket *sock = tsock->sk.sk_socket;
+       struct sock *sk = &tsk->sk;
+       struct tipc_port *port = &tsk->port;
+       struct socket *sock = sk->sk_socket;
        struct tipc_msg *msg = buf_msg(*buf);
-       struct sock *sk = &tsock->sk;
+
        u32 retval = TIPC_ERR_NO_PORT;
        int res;
 
@@ -1286,10 +1284,10 @@ static u32 filter_connect(struct tipc_sock *tsock, struct sk_buff **buf)
        switch ((int)sock->state) {
        case SS_CONNECTED:
                /* Accept only connection-based messages sent by peer */
-               if (msg_connected(msg) && tipc_port_peer_msg(tsock->p, msg)) {
+               if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
                        if (unlikely(msg_errcode(msg))) {
                                sock->state = SS_DISCONNECTING;
-                               __tipc_disconnect(tsock->p);
+                               __tipc_port_disconnect(port);
                        }
                        retval = TIPC_OK;
                }
@@ -1306,7 +1304,7 @@ static u32 filter_connect(struct tipc_sock *tsock, struct sk_buff **buf)
                if (unlikely(!msg_connected(msg)))
                        break;
 
-               res = auto_connect(sock, msg);
+               res = auto_connect(tsk, msg);
                if (res) {
                        sock->state = SS_DISCONNECTING;
                        sk->sk_err = -res;
@@ -1385,6 +1383,7 @@ static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
 static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
 {
        struct socket *sock = sk->sk_socket;
+       struct tipc_sock *tsk = tipc_sk(sk);
        struct tipc_msg *msg = buf_msg(buf);
        unsigned int limit = rcvbuf_limit(sk, buf);
        u32 res = TIPC_OK;
@@ -1397,7 +1396,7 @@ static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
                if (msg_connected(msg))
                        return TIPC_ERR_NO_PORT;
        } else {
-               res = filter_connect(tipc_sk(sk), &buf);
+               res = filter_connect(tsk, &buf);
                if (res != TIPC_OK || buf == NULL)
                        return res;
        }
@@ -1411,7 +1410,7 @@ static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
        __skb_queue_tail(&sk->sk_receive_queue, buf);
        skb_set_owner_r(buf, sk);
 
-       sk->sk_data_ready(sk, 0);
+       sk->sk_data_ready(sk);
        return TIPC_OK;
 }
 
@@ -1435,17 +1434,16 @@ static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
 }
 
 /**
- * dispatch - handle incoming message
- * @tport: TIPC port that received message
+ * tipc_sk_rcv - handle incoming message
+ * @sk:  socket receiving message
  * @buf: message
  *
  * Called with port lock already taken.
  *
  * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
  */
-static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
+u32 tipc_sk_rcv(struct sock *sk, struct sk_buff *buf)
 {
-       struct sock *sk = tport->sk;
        u32 res;
 
        /*
@@ -1468,19 +1466,6 @@ static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
        return res;
 }
 
-/**
- * wakeupdispatch - wake up port after congestion
- * @tport: port to wakeup
- *
- * Called with port lock already taken.
- */
-static void wakeupdispatch(struct tipc_port *tport)
-{
-       struct sock *sk = tport->sk;
-
-       sk->sk_write_space(sk);
-}
-
 static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
 {
        struct sock *sk = sock->sk;
@@ -1504,7 +1489,7 @@ static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
 }
 
 /**
- * connect - establish a connection to another TIPC port
+ * tipc_connect - establish a connection to another TIPC port
  * @sock: socket structure
  * @dest: socket address for destination port
  * @destlen: size of socket address data structure
@@ -1512,8 +1497,8 @@ static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
  *
  * Returns 0 on success, errno otherwise
  */
-static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
-                  int flags)
+static int tipc_connect(struct socket *sock, struct sockaddr *dest,
+                       int destlen, int flags)
 {
        struct sock *sk = sock->sk;
        struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
@@ -1554,7 +1539,7 @@ static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
                if (!timeout)
                        m.msg_flags = MSG_DONTWAIT;
 
-               res = send_msg(NULL, sock, &m, 0);
+               res = tipc_sendmsg(NULL, sock, &m, 0);
                if ((res < 0) && (res != -EWOULDBLOCK))
                        goto exit;
 
@@ -1585,13 +1570,13 @@ exit:
 }
 
 /**
- * listen - allow socket to listen for incoming connections
+ * tipc_listen - allow socket to listen for incoming connections
  * @sock: socket structure
  * @len: (unused)
  *
  * Returns 0 on success, errno otherwise
  */
-static int listen(struct socket *sock, int len)
+static int tipc_listen(struct socket *sock, int len)
 {
        struct sock *sk = sock->sk;
        int res;
@@ -1646,20 +1631,20 @@ static int tipc_wait_for_accept(struct socket *sock, long timeo)
 }
 
 /**
- * accept - wait for connection request
+ * tipc_accept - wait for connection request
  * @sock: listening socket
  * @newsock: new socket that is to be connected
  * @flags: file-related flags associated with socket
  *
  * Returns 0 on success, errno otherwise
  */
-static int accept(struct socket *sock, struct socket *new_sock, int flags)
+static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
 {
        struct sock *new_sk, *sk = sock->sk;
        struct sk_buff *buf;
-       struct tipc_sock *new_tsock;
-       struct tipc_port *new_tport;
+       struct tipc_port *new_port;
        struct tipc_msg *msg;
+       struct tipc_portid peer;
        u32 new_ref;
        long timeo;
        int res;
@@ -1670,7 +1655,6 @@ static int accept(struct socket *sock, struct socket *new_sock, int flags)
                res = -EINVAL;
                goto exit;
        }
-
        timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
        res = tipc_wait_for_accept(sock, timeo);
        if (res)
@@ -1683,9 +1667,8 @@ static int accept(struct socket *sock, struct socket *new_sock, int flags)
                goto exit;
 
        new_sk = new_sock->sk;
-       new_tsock = tipc_sk(new_sk);
-       new_tport = new_tsock->p;
-       new_ref = new_tport->ref;
+       new_port = &tipc_sk(new_sk)->port;
+       new_ref = new_port->ref;
        msg = buf_msg(buf);
 
        /* we lock on new_sk; but lockdep sees the lock on sk */
@@ -1698,15 +1681,15 @@ static int accept(struct socket *sock, struct socket *new_sock, int flags)
        reject_rx_queue(new_sk);
 
        /* Connect new socket to it's peer */
-       new_tsock->peer_name.ref = msg_origport(msg);
-       new_tsock->peer_name.node = msg_orignode(msg);
-       tipc_connect(new_ref, &new_tsock->peer_name);
+       peer.ref = msg_origport(msg);
+       peer.node = msg_orignode(msg);
+       tipc_port_connect(new_ref, &peer);
        new_sock->state = SS_CONNECTED;
 
-       tipc_set_portimportance(new_ref, msg_importance(msg));
+       tipc_port_set_importance(new_port, msg_importance(msg));
        if (msg_named(msg)) {
-               new_tport->conn_type = msg_nametype(msg);
-               new_tport->conn_instance = msg_nameinst(msg);
+               new_port->conn_type = msg_nametype(msg);
+               new_port->conn_instance = msg_nameinst(msg);
        }
 
        /*
@@ -1717,21 +1700,20 @@ static int accept(struct socket *sock, struct socket *new_sock, int flags)
                struct msghdr m = {NULL,};
 
                advance_rx_queue(sk);
-               send_packet(NULL, new_sock, &m, 0);
+               tipc_send_packet(NULL, new_sock, &m, 0);
        } else {
                __skb_dequeue(&sk->sk_receive_queue);
                __skb_queue_head(&new_sk->sk_receive_queue, buf);
                skb_set_owner_r(buf, new_sk);
        }
        release_sock(new_sk);
-
 exit:
        release_sock(sk);
        return res;
 }
 
 /**
- * shutdown - shutdown socket connection
+ * tipc_shutdown - shutdown socket connection
  * @sock: socket structure
  * @how: direction to close (must be SHUT_RDWR)
  *
@@ -1739,10 +1721,11 @@ exit:
  *
  * Returns 0 on success, errno otherwise
  */
-static int shutdown(struct socket *sock, int how)
+static int tipc_shutdown(struct socket *sock, int how)
 {
        struct sock *sk = sock->sk;
-       struct tipc_port *tport = tipc_sk_port(sk);
+       struct tipc_sock *tsk = tipc_sk(sk);
+       struct tipc_port *port = &tsk->port;
        struct sk_buff *buf;
        int res;
 
@@ -1763,10 +1746,10 @@ restart:
                                kfree_skb(buf);
                                goto restart;
                        }
-                       tipc_disconnect(tport->ref);
+                       tipc_port_disconnect(port->ref);
                        tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
                } else {
-                       tipc_shutdown(tport->ref);
+                       tipc_port_shutdown(port->ref);
                }
 
                sock->state = SS_DISCONNECTING;
@@ -1792,7 +1775,7 @@ restart:
 }
 
 /**
- * setsockopt - set socket option
+ * tipc_setsockopt - set socket option
  * @sock: socket structure
  * @lvl: option level
  * @opt: option identifier
@@ -1804,11 +1787,12 @@ restart:
  *
  * Returns 0 on success, errno otherwise
  */
-static int setsockopt(struct socket *sock, int lvl, int opt, char __user *ov,
-                     unsigned int ol)
+static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
+                          char __user *ov, unsigned int ol)
 {
        struct sock *sk = sock->sk;
-       struct tipc_port *tport = tipc_sk_port(sk);
+       struct tipc_sock *tsk = tipc_sk(sk);
+       struct tipc_port *port = &tsk->port;
        u32 value;
        int res;
 
@@ -1826,16 +1810,16 @@ static int setsockopt(struct socket *sock, int lvl, int opt, char __user *ov,
 
        switch (opt) {
        case TIPC_IMPORTANCE:
-               res = tipc_set_portimportance(tport->ref, value);
+               tipc_port_set_importance(port, value);
                break;
        case TIPC_SRC_DROPPABLE:
                if (sock->type != SOCK_STREAM)
-                       res = tipc_set_portunreliable(tport->ref, value);
+                       tipc_port_set_unreliable(port, value);
                else
                        res = -ENOPROTOOPT;
                break;
        case TIPC_DEST_DROPPABLE:
-               res = tipc_set_portunreturnable(tport->ref, value);
+               tipc_port_set_unreturnable(port, value);
                break;
        case TIPC_CONN_TIMEOUT:
                tipc_sk(sk)->conn_timeout = value;
@@ -1851,7 +1835,7 @@ static int setsockopt(struct socket *sock, int lvl, int opt, char __user *ov,
 }
 
 /**
- * getsockopt - get socket option
+ * tipc_getsockopt - get socket option
  * @sock: socket structure
  * @lvl: option level
  * @opt: option identifier
@@ -1863,11 +1847,12 @@ static int setsockopt(struct socket *sock, int lvl, int opt, char __user *ov,
  *
  * Returns 0 on success, errno otherwise
  */
-static int getsockopt(struct socket *sock, int lvl, int opt, char __user *ov,
-                     int __user *ol)
+static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
+                          char __user *ov, int __user *ol)
 {
        struct sock *sk = sock->sk;
-       struct tipc_port *tport = tipc_sk_port(sk);
+       struct tipc_sock *tsk = tipc_sk(sk);
+       struct tipc_port *port = &tsk->port;
        int len;
        u32 value;
        int res;
@@ -1884,13 +1869,13 @@ static int getsockopt(struct socket *sock, int lvl, int opt, char __user *ov,
 
        switch (opt) {
        case TIPC_IMPORTANCE:
-               res = tipc_portimportance(tport->ref, &value);
+               value = tipc_port_importance(port);
                break;
        case TIPC_SRC_DROPPABLE:
-               res = tipc_portunreliable(tport->ref, &value);
+               value = tipc_port_unreliable(port);
                break;
        case TIPC_DEST_DROPPABLE:
-               res = tipc_portunreturnable(tport->ref, &value);
+               value = tipc_port_unreturnable(port);
                break;
        case TIPC_CONN_TIMEOUT:
                value = tipc_sk(sk)->conn_timeout;
@@ -1925,20 +1910,20 @@ static int getsockopt(struct socket *sock, int lvl, int opt, char __user *ov,
 static const struct proto_ops msg_ops = {
        .owner          = THIS_MODULE,
        .family         = AF_TIPC,
-       .release        = release,
-       .bind           = bind,
-       .connect        = connect,
+       .release        = tipc_release,
+       .bind           = tipc_bind,
+       .connect        = tipc_connect,
        .socketpair     = sock_no_socketpair,
        .accept         = sock_no_accept,
-       .getname        = get_name,
-       .poll           = poll,
+       .getname        = tipc_getname,
+       .poll           = tipc_poll,
        .ioctl          = sock_no_ioctl,
        .listen         = sock_no_listen,
-       .shutdown       = shutdown,
-       .setsockopt     = setsockopt,
-       .getsockopt     = getsockopt,
-       .sendmsg        = send_msg,
-       .recvmsg        = recv_msg,
+       .shutdown       = tipc_shutdown,
+       .setsockopt     = tipc_setsockopt,
+       .getsockopt     = tipc_getsockopt,
+       .sendmsg        = tipc_sendmsg,
+       .recvmsg        = tipc_recvmsg,
        .mmap           = sock_no_mmap,
        .sendpage       = sock_no_sendpage
 };
@@ -1946,20 +1931,20 @@ static const struct proto_ops msg_ops = {
 static const struct proto_ops packet_ops = {
        .owner          = THIS_MODULE,
        .family         = AF_TIPC,
-       .release        = release,
-       .bind           = bind,
-       .connect        = connect,
+       .release        = tipc_release,
+       .bind           = tipc_bind,
+       .connect        = tipc_connect,
        .socketpair     = sock_no_socketpair,
-       .accept         = accept,
-       .getname        = get_name,
-       .poll           = poll,
+       .accept         = tipc_accept,
+       .getname        = tipc_getname,
+       .poll           = tipc_poll,
        .ioctl          = sock_no_ioctl,
-       .listen         = listen,
-       .shutdown       = shutdown,
-       .setsockopt     = setsockopt,
-       .getsockopt     = getsockopt,
-       .sendmsg        = send_packet,
-       .recvmsg        = recv_msg,
+       .listen         = tipc_listen,
+       .shutdown       = tipc_shutdown,
+       .setsockopt     = tipc_setsockopt,
+       .getsockopt     = tipc_getsockopt,
+       .sendmsg        = tipc_send_packet,
+       .recvmsg        = tipc_recvmsg,
        .mmap           = sock_no_mmap,
        .sendpage       = sock_no_sendpage
 };
@@ -1967,20 +1952,20 @@ static const struct proto_ops packet_ops = {
 static const struct proto_ops stream_ops = {
        .owner          = THIS_MODULE,
        .family         = AF_TIPC,
-       .release        = release,
-       .bind           = bind,
-       .connect        = connect,
+       .release        = tipc_release,
+       .bind           = tipc_bind,
+       .connect        = tipc_connect,
        .socketpair     = sock_no_socketpair,
-       .accept         = accept,
-       .getname        = get_name,
-       .poll           = poll,
+       .accept         = tipc_accept,
+       .getname        = tipc_getname,
+       .poll           = tipc_poll,
        .ioctl          = sock_no_ioctl,
-       .listen         = listen,
-       .shutdown       = shutdown,
-       .setsockopt     = setsockopt,
-       .getsockopt     = getsockopt,
-       .sendmsg        = send_stream,
-       .recvmsg        = recv_stream,
+       .listen         = tipc_listen,
+       .shutdown       = tipc_shutdown,
+       .setsockopt     = tipc_setsockopt,
+       .getsockopt     = tipc_getsockopt,
+       .sendmsg        = tipc_send_stream,
+       .recvmsg        = tipc_recv_stream,
        .mmap           = sock_no_mmap,
        .sendpage       = sock_no_sendpage
 };