Merge branch 'for-davem' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[cascardo/linux.git] / net / tipc / socket.c
index 51bddc2..341fbd1 100644 (file)
@@ -121,6 +121,14 @@ static const struct proto_ops msg_ops;
 static struct proto tipc_proto;
 static struct proto tipc_proto_kern;
 
+static const struct nla_policy tipc_nl_sock_policy[TIPC_NLA_SOCK_MAX + 1] = {
+       [TIPC_NLA_SOCK_UNSPEC]          = { .type = NLA_UNSPEC },
+       [TIPC_NLA_SOCK_ADDR]            = { .type = NLA_U32 },
+       [TIPC_NLA_SOCK_REF]             = { .type = NLA_U32 },
+       [TIPC_NLA_SOCK_CON]             = { .type = NLA_NESTED },
+       [TIPC_NLA_SOCK_HAS_PUBL]        = { .type = NLA_FLAG }
+};
+
 /*
  * Revised TIPC socket locking policy:
  *
@@ -692,7 +700,7 @@ static unsigned int tipc_poll(struct file *file, struct socket *sock,
  * tipc_sendmcast - send multicast message
  * @sock: socket structure
  * @seq: destination address
- * @iov: message data to send
+ * @msg: message to send
  * @dsz: total length of message data
  * @timeo: timeout to wait for wakeup
  *
@@ -700,7 +708,7 @@ static unsigned int tipc_poll(struct file *file, struct socket *sock,
  * Returns the number of bytes sent on success, or errno
  */
 static int tipc_sendmcast(struct  socket *sock, struct tipc_name_seq *seq,
-                         struct iovec *iov, size_t dsz, long timeo)
+                         struct msghdr *msg, size_t dsz, long timeo)
 {
        struct sock *sk = sock->sk;
        struct tipc_msg *mhdr = &tipc_sk(sk)->phdr;
@@ -719,7 +727,7 @@ static int tipc_sendmcast(struct  socket *sock, struct tipc_name_seq *seq,
 
 new_mtu:
        mtu = tipc_bclink_get_mtu();
-       rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
+       rc = tipc_msg_build(mhdr, msg, 0, dsz, mtu, &buf);
        if (unlikely(rc < 0))
                return rc;
 
@@ -897,7 +905,6 @@ static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
        struct sock *sk = sock->sk;
        struct tipc_sock *tsk = tipc_sk(sk);
        struct tipc_msg *mhdr = &tsk->phdr;
-       struct iovec *iov = m->msg_iov;
        u32 dnode, dport;
        struct sk_buff *buf;
        struct tipc_name_seq *seq = &dest->addr.nameseq;
@@ -943,7 +950,7 @@ static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
        timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
 
        if (dest->addrtype == TIPC_ADDR_MCAST) {
-               rc = tipc_sendmcast(sock, seq, iov, dsz, timeo);
+               rc = tipc_sendmcast(sock, seq, m, dsz, timeo);
                goto exit;
        } else if (dest->addrtype == TIPC_ADDR_NAME) {
                u32 type = dest->addr.name.name.type;
@@ -974,7 +981,7 @@ static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
 
 new_mtu:
        mtu = tipc_node_get_mtu(dnode, tsk->ref);
-       rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
+       rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, &buf);
        if (rc < 0)
                goto exit;
 
@@ -1086,7 +1093,7 @@ static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
 next:
        mtu = tsk->max_pkt;
        send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
-       rc = tipc_msg_build(mhdr, m->msg_iov, sent, send, mtu, &buf);
+       rc = tipc_msg_build(mhdr, m, sent, send, mtu, &buf);
        if (unlikely(rc < 0))
                goto exit;
        do {
@@ -1372,8 +1379,7 @@ restart:
                        sz = buf_len;
                        m->msg_flags |= MSG_TRUNC;
                }
-               res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
-                                             m->msg_iov, sz);
+               res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg), m, sz);
                if (res)
                        goto exit;
                res = sz;
@@ -1473,8 +1479,8 @@ restart:
                needed = (buf_len - sz_copied);
                sz_to_copy = (sz <= needed) ? sz : needed;
 
-               res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
-                                             m->msg_iov, sz_to_copy);
+               res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg) + offset,
+                                           m, sz_to_copy);
                if (res)
                        goto exit;
 
@@ -1556,7 +1562,7 @@ static void tipc_data_ready(struct sock *sk)
  * @tsk: TIPC socket
  * @msg: message
  *
- * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise
+ * Returns 0 (TIPC_OK) if everything ok, -TIPC_ERR_NO_PORT otherwise
  */
 static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
 {
@@ -2802,3 +2808,233 @@ void tipc_socket_stop(void)
        sock_unregister(tipc_family_ops.family);
        proto_unregister(&tipc_proto);
 }
+
+/* Caller should hold socket lock for the passed tipc socket. */
+static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
+{
+       u32 peer_node;
+       u32 peer_port;
+       struct nlattr *nest;
+
+       peer_node = tsk_peer_node(tsk);
+       peer_port = tsk_peer_port(tsk);
+
+       nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
+
+       if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
+               goto msg_full;
+       if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
+               goto msg_full;
+
+       if (tsk->conn_type != 0) {
+               if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
+                       goto msg_full;
+               if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
+                       goto msg_full;
+               if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
+                       goto msg_full;
+       }
+       nla_nest_end(skb, nest);
+
+       return 0;
+
+msg_full:
+       nla_nest_cancel(skb, nest);
+
+       return -EMSGSIZE;
+}
+
+/* Caller should hold socket lock for the passed tipc socket. */
+static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
+                           struct tipc_sock *tsk)
+{
+       int err;
+       void *hdr;
+       struct nlattr *attrs;
+
+       hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
+                         &tipc_genl_v2_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
+       if (!hdr)
+               goto msg_cancel;
+
+       attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
+       if (!attrs)
+               goto genlmsg_cancel;
+       if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->ref))
+               goto attr_msg_cancel;
+       if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tipc_own_addr))
+               goto attr_msg_cancel;
+
+       if (tsk->connected) {
+               err = __tipc_nl_add_sk_con(skb, tsk);
+               if (err)
+                       goto attr_msg_cancel;
+       } else if (!list_empty(&tsk->publications)) {
+               if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
+                       goto attr_msg_cancel;
+       }
+       nla_nest_end(skb, attrs);
+       genlmsg_end(skb, hdr);
+
+       return 0;
+
+attr_msg_cancel:
+       nla_nest_cancel(skb, attrs);
+genlmsg_cancel:
+       genlmsg_cancel(skb, hdr);
+msg_cancel:
+       return -EMSGSIZE;
+}
+
+int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
+{
+       int err;
+       struct tipc_sock *tsk;
+       u32 prev_ref = cb->args[0];
+       u32 ref = prev_ref;
+
+       tsk = tipc_sk_get_next(&ref);
+       for (; tsk; tsk = tipc_sk_get_next(&ref)) {
+               lock_sock(&tsk->sk);
+               err = __tipc_nl_add_sk(skb, cb, tsk);
+               release_sock(&tsk->sk);
+               tipc_sk_put(tsk);
+               if (err)
+                       break;
+
+               prev_ref = ref;
+       }
+
+       cb->args[0] = prev_ref;
+
+       return skb->len;
+}
+
+/* Caller should hold socket lock for the passed tipc socket. */
+static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
+                                struct netlink_callback *cb,
+                                struct publication *publ)
+{
+       void *hdr;
+       struct nlattr *attrs;
+
+       hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
+                         &tipc_genl_v2_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
+       if (!hdr)
+               goto msg_cancel;
+
+       attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
+       if (!attrs)
+               goto genlmsg_cancel;
+
+       if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
+               goto attr_msg_cancel;
+       if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
+               goto attr_msg_cancel;
+       if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
+               goto attr_msg_cancel;
+       if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
+               goto attr_msg_cancel;
+
+       nla_nest_end(skb, attrs);
+       genlmsg_end(skb, hdr);
+
+       return 0;
+
+attr_msg_cancel:
+       nla_nest_cancel(skb, attrs);
+genlmsg_cancel:
+       genlmsg_cancel(skb, hdr);
+msg_cancel:
+       return -EMSGSIZE;
+}
+
+/* Caller should hold socket lock for the passed tipc socket. */
+static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
+                                 struct netlink_callback *cb,
+                                 struct tipc_sock *tsk, u32 *last_publ)
+{
+       int err;
+       struct publication *p;
+
+       if (*last_publ) {
+               list_for_each_entry(p, &tsk->publications, pport_list) {
+                       if (p->key == *last_publ)
+                               break;
+               }
+               if (p->key != *last_publ) {
+                       /* We never set seq or call nl_dump_check_consistent()
+                        * this means that setting prev_seq here will cause the
+                        * consistence check to fail in the netlink callback
+                        * handler. Resulting in the last NLMSG_DONE message
+                        * having the NLM_F_DUMP_INTR flag set.
+                        */
+                       cb->prev_seq = 1;
+                       *last_publ = 0;
+                       return -EPIPE;
+               }
+       } else {
+               p = list_first_entry(&tsk->publications, struct publication,
+                                    pport_list);
+       }
+
+       list_for_each_entry_from(p, &tsk->publications, pport_list) {
+               err = __tipc_nl_add_sk_publ(skb, cb, p);
+               if (err) {
+                       *last_publ = p->key;
+                       return err;
+               }
+       }
+       *last_publ = 0;
+
+       return 0;
+}
+
+int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
+{
+       int err;
+       u32 tsk_ref = cb->args[0];
+       u32 last_publ = cb->args[1];
+       u32 done = cb->args[2];
+       struct tipc_sock *tsk;
+
+       if (!tsk_ref) {
+               struct nlattr **attrs;
+               struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
+
+               err = tipc_nlmsg_parse(cb->nlh, &attrs);
+               if (err)
+                       return err;
+
+               err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
+                                      attrs[TIPC_NLA_SOCK],
+                                      tipc_nl_sock_policy);
+               if (err)
+                       return err;
+
+               if (!sock[TIPC_NLA_SOCK_REF])
+                       return -EINVAL;
+
+               tsk_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
+       }
+
+       if (done)
+               return 0;
+
+       tsk = tipc_sk_get(tsk_ref);
+       if (!tsk)
+               return -EINVAL;
+
+       lock_sock(&tsk->sk);
+       err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
+       if (!err)
+               done = 1;
+       release_sock(&tsk->sk);
+       tipc_sk_put(tsk);
+
+       cb->args[0] = tsk_ref;
+       cb->args[1] = last_publ;
+       cb->args[2] = done;
+
+       return skb->len;
+}