Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / net / decnet / af_decnet.c
index 29bb3cd..2b289ef 100644 (file)
@@ -118,10 +118,11 @@ Version 0.0.6    2.1.110   07-aug-98   Eduardo Marcelo Serrat
 #include <linux/netfilter.h>
 #include <linux/seq_file.h>
 #include <net/sock.h>
-#include <net/tcp.h>
+#include <net/tcp_states.h>
 #include <net/flow.h>
 #include <asm/system.h>
 #include <asm/ioctls.h>
+#include <linux/capability.h>
 #include <linux/mm.h>
 #include <linux/interrupt.h>
 #include <linux/proc_fs.h>
@@ -149,10 +150,11 @@ static void dn_keepalive(struct sock *sk);
 #define DN_SK_HASH_MASK (DN_SK_HASH_SIZE - 1)
 
 
-static struct proto_ops dn_proto_ops;
+static const struct proto_ops dn_proto_ops;
 static DEFINE_RWLOCK(dn_hash_lock);
 static struct hlist_head dn_sk_hash[DN_SK_HASH_SIZE];
 static struct hlist_head dn_wild_sk;
+static atomic_t decnet_memory_allocated;
 
 static int __dn_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen, int flags);
 static int __dn_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen, int flags);
@@ -170,7 +172,7 @@ static struct hlist_head *dn_find_list(struct sock *sk)
 /* 
  * Valid ports are those greater than zero and not already in use.
  */
-static int check_port(unsigned short port)
+static int check_port(__le16 port)
 {
        struct sock *sk;
        struct hlist_node *node;
@@ -446,13 +448,29 @@ static void dn_destruct(struct sock *sk)
        dst_release(xchg(&sk->sk_dst_cache, NULL));
 }
 
+static int dn_memory_pressure;
+
+static void dn_enter_memory_pressure(void)
+{
+       if (!dn_memory_pressure) {
+               dn_memory_pressure = 1;
+       }
+}
+
 static struct proto dn_proto = {
-       .name     = "DECNET",
-       .owner    = THIS_MODULE,
-       .obj_size = sizeof(struct dn_sock),
+       .name                   = "NSP",
+       .owner                  = THIS_MODULE,
+       .enter_memory_pressure  = dn_enter_memory_pressure,
+       .memory_pressure        = &dn_memory_pressure,
+       .memory_allocated       = &decnet_memory_allocated,
+       .sysctl_mem             = sysctl_decnet_mem,
+       .sysctl_wmem            = sysctl_decnet_wmem,
+       .sysctl_rmem            = sysctl_decnet_rmem,
+       .max_header             = DN_MAX_NSP_DATA_HEADER + 64,
+       .obj_size               = sizeof(struct dn_sock),
 };
 
-static struct sock *dn_alloc_sock(struct socket *sock, int gfp)
+static struct sock *dn_alloc_sock(struct socket *sock, gfp_t gfp)
 {
        struct dn_scp *scp;
        struct sock *sk = sk_alloc(PF_DECnet, gfp, &dn_proto, 1);
@@ -470,6 +488,8 @@ static struct sock *dn_alloc_sock(struct socket *sock, int gfp)
        sk->sk_family      = PF_DECnet;
        sk->sk_protocol    = 0;
        sk->sk_allocation  = gfp;
+       sk->sk_sndbuf      = sysctl_decnet_wmem[1];
+       sk->sk_rcvbuf      = sysctl_decnet_rmem[1];
 
        /* Initialization of DECnet Session Control Port                */
        scp = DN_SK(sk);
@@ -536,7 +556,7 @@ static void dn_keepalive(struct sock *sk)
         * we are double checking that we are not sending too
         * many of these keepalive frames.
         */
-       if (skb_queue_len(&scp->other_xmit_queue) == 0)
+       if (skb_queue_empty(&scp->other_xmit_queue))
                dn_nsp_send_link(sk, DN_NOCHANGE, 0);
 }
 
@@ -641,7 +661,7 @@ disc_reject:
        }
 }
 
-char *dn_addr2asc(dn_address addr, char *buf)
+char *dn_addr2asc(__u16 addr, char *buf)
 {
        unsigned short node, area;
 
@@ -719,22 +739,9 @@ static int dn_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
        if (saddr->sdn_flags & ~SDF_WILD)
                return -EINVAL;
 
-#if 1
        if (!capable(CAP_NET_BIND_SERVICE) && (saddr->sdn_objnum ||
            (saddr->sdn_flags & SDF_WILD)))
                return -EACCES;
-#else
-       /*
-        * Maybe put the default actions in the default security ops for
-        * dn_prot_sock ? Would be nice if the capable call would go there
-        * too.
-        */
-       if (security_dn_prot_sock(saddr) &&
-           !capable(CAP_NET_BIND_SERVICE) || 
-           saddr->sdn_objnum || (saddr->sdn_flags & SDF_WILD))
-               return -EACCES;
-#endif
-
 
        if (!(saddr->sdn_flags & SDF_WILD)) {
                if (dn_ntohs(saddr->sdn_nodeaddrl)) {
@@ -794,7 +801,7 @@ static int dn_auto_bind(struct socket *sock)
        /* End of compatibility stuff */
 
        scp->addr.sdn_add.a_len = dn_htons(2);
-       rv = dn_dev_bind_default((dn_address *)scp->addr.sdn_add.a_addr);
+       rv = dn_dev_bind_default((__le16 *)scp->addr.sdn_add.a_addr);
        if (rv == 0) {
                rv = dn_hash_sock(sk);
                if (rv)
@@ -804,7 +811,7 @@ static int dn_auto_bind(struct socket *sock)
        return rv;
 }
 
-static int dn_confirm_accept(struct sock *sk, long *timeo, int allocation)
+static int dn_confirm_accept(struct sock *sk, long *timeo, gfp_t allocation)
 {
        struct dn_scp *scp = DN_SK(sk);
        DEFINE_WAIT(wait);
@@ -1014,7 +1021,7 @@ static void dn_user_copy(struct sk_buff *skb, struct optdata_dn *opt)
         opt->opt_optl   = *ptr++;
         opt->opt_status = 0;
         memcpy(opt->opt_data, ptr, opt->opt_optl);
-        skb_pull(skb, opt->opt_optl + 1);
+        skb_pull(skb, dn_ntohs(opt->opt_optl) + 1);
 
 }
 
@@ -1114,8 +1121,8 @@ static int dn_accept(struct socket *sock, struct socket *newsock, int flags)
 
        skb_pull(skb, dn_username2sockaddr(skb->data, skb->len, &(DN_SK(newsk)->addr), &type));
        skb_pull(skb, dn_username2sockaddr(skb->data, skb->len, &(DN_SK(newsk)->peer), &type));
-       *(dn_address *)(DN_SK(newsk)->peer.sdn_add.a_addr) = cb->src;
-       *(dn_address *)(DN_SK(newsk)->addr.sdn_add.a_addr) = cb->dst;
+       *(__le16 *)(DN_SK(newsk)->peer.sdn_add.a_addr) = cb->src;
+       *(__le16 *)(DN_SK(newsk)->addr.sdn_add.a_addr) = cb->dst;
 
        menuver = *skb->data;
        skb_pull(skb, 1);
@@ -1191,7 +1198,7 @@ static unsigned int dn_poll(struct file *file, struct socket *sock, poll_table
        struct dn_scp *scp = DN_SK(sk);
        int mask = datagram_poll(file, sock, wait);
 
-       if (skb_queue_len(&scp->other_receive_queue))
+       if (!skb_queue_empty(&scp->other_receive_queue))
                mask |= POLLRDBAND;
 
        return mask;
@@ -1214,7 +1221,7 @@ static int dn_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 
        case SIOCATMARK:
                lock_sock(sk);
-               val = (skb_queue_len(&scp->other_receive_queue) != 0);
+               val = !skb_queue_empty(&scp->other_receive_queue);
                if (scp->state != DN_RUN)
                        val = -ENOTCONN;
                release_sock(sk);
@@ -1246,7 +1253,7 @@ static int dn_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
                break;
 
        default:
-               err = dev_ioctl(cmd, (void __user *)arg);
+               err = -ENOIOCTLCMD;
                break;
        }
 
@@ -1358,7 +1365,7 @@ static int __dn_setsockopt(struct socket *sock, int level,int optname, char __us
                        if (optlen != sizeof(struct optdata_dn))
                                return -EINVAL;
 
-                       if (u.opt.opt_optl > 16)
+                       if (dn_ntohs(u.opt.opt_optl) > 16)
                                return -EINVAL;
 
                        memcpy(&scp->conndata_out, &u.opt, optlen);
@@ -1371,7 +1378,7 @@ static int __dn_setsockopt(struct socket *sock, int level,int optname, char __us
                        if (optlen != sizeof(struct optdata_dn))
                                return -EINVAL;
 
-                       if (u.opt.opt_optl > 16)
+                       if (dn_ntohs(u.opt.opt_optl) > 16)
                                return -EINVAL;
 
                        memcpy(&scp->discdata_out, &u.opt, optlen);
@@ -1630,7 +1637,7 @@ static int dn_data_ready(struct sock *sk, struct sk_buff_head *q, int flags, int
        int len = 0;
 
        if (flags & MSG_OOB)
-               return skb_queue_len(q) ? 1 : 0;
+               return !skb_queue_empty(q) ? 1 : 0;
 
        while(skb != (struct sk_buff *)q) {
                struct dn_skb_cb *cb = DN_SKB_CB(skb);
@@ -1677,18 +1684,16 @@ static int dn_recvmsg(struct kiocb *iocb, struct socket *sock,
                goto out;
        }
 
-       rv = dn_check_state(sk, NULL, 0, &timeo, flags);
-       if (rv)
-               goto out;
-
        if (sk->sk_shutdown & RCV_SHUTDOWN) {
-               if (!(flags & MSG_NOSIGNAL))
-                       send_sig(SIGPIPE, current, 0);
-               rv = -EPIPE;
+               rv = 0;
                goto out;
        }
 
-       if (flags & ~(MSG_PEEK|MSG_OOB|MSG_WAITALL|MSG_DONTWAIT|MSG_NOSIGNAL)) {
+       rv = dn_check_state(sk, NULL, 0, &timeo, flags);
+       if (rv)
+               goto out;
+
+       if (flags & ~(MSG_CMSG_COMPAT|MSG_PEEK|MSG_OOB|MSG_WAITALL|MSG_DONTWAIT|MSG_NOSIGNAL)) {
                rv = -EOPNOTSUPP;
                goto out;
        }
@@ -1707,7 +1712,7 @@ static int dn_recvmsg(struct kiocb *iocb, struct socket *sock,
                if (sk->sk_err)
                        goto out;
 
-               if (skb_queue_len(&scp->other_receive_queue)) {
+               if (!skb_queue_empty(&scp->other_receive_queue)) {
                        if (!(flags & MSG_OOB)) {
                                msg->msg_flags |= MSG_OOB;
                                if (!scp->other_report) {
@@ -1763,7 +1768,7 @@ static int dn_recvmsg(struct kiocb *iocb, struct socket *sock,
                nskb = skb->next;
 
                if (skb->len == 0) {
-                       skb_unlink(skb);
+                       skb_unlink(skb, queue);
                        kfree_skb(skb);
                        /* 
                         * N.B. Don't refer to skb or cb after this point
@@ -1876,17 +1881,27 @@ static inline unsigned int dn_current_mss(struct sock *sk, int flags)
        return mss_now;
 }
 
-static int dn_error(struct sock *sk, int flags, int err)
+/* 
+ * N.B. We get the timeout wrong here, but then we always did get it
+ * wrong before and this is another step along the road to correcting
+ * it. It ought to get updated each time we pass through the routine,
+ * but in practise it probably doesn't matter too much for now.
+ */
+static inline struct sk_buff *dn_alloc_send_pskb(struct sock *sk,
+                             unsigned long datalen, int noblock,
+                             int *errcode)
 {
-       if (err == -EPIPE)
-               err = sock_error(sk) ? : -EPIPE;
-       if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
-               send_sig(SIGPIPE, current, 0);
-       return err;
+       struct sk_buff *skb = sock_alloc_send_skb(sk, datalen,
+                                                  noblock, errcode);
+       if (skb) {
+               skb->protocol = __constant_htons(ETH_P_DNA_RT);
+               skb->pkt_type = PACKET_OUTGOING;
+       }
+       return skb;
 }
 
 static int dn_sendmsg(struct kiocb *iocb, struct socket *sock,
-          struct msghdr *msg, size_t size)
+                     struct msghdr *msg, size_t size)
 {
        struct sock *sk = sock->sk;
        struct dn_scp *scp = DN_SK(sk);
@@ -1901,7 +1916,7 @@ static int dn_sendmsg(struct kiocb *iocb, struct socket *sock,
        struct dn_skb_cb *cb;
        size_t len;
        unsigned char fctype;
-       long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
+       long timeo;
 
        if (flags & ~(MSG_TRYHARD|MSG_OOB|MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL|MSG_MORE|MSG_CMSG_COMPAT))
                return -EOPNOTSUPP;
@@ -1909,18 +1924,21 @@ static int dn_sendmsg(struct kiocb *iocb, struct socket *sock,
        if (addr_len && (addr_len != sizeof(struct sockaddr_dn)))
                return -EINVAL;
 
+       lock_sock(sk);
+       timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
        /*
         * The only difference between stream sockets and sequenced packet
         * sockets is that the stream sockets always behave as if MSG_EOR
         * has been set.
         */
        if (sock->type == SOCK_STREAM) {
-               if (flags & MSG_EOR)
-                       return -EINVAL;
+               if (flags & MSG_EOR) {
+                       err = -EINVAL;
+                       goto out;
+               }
                flags |= MSG_EOR;
        }
 
-       lock_sock(sk);
 
        err = dn_check_state(sk, addr, addr_len, &timeo, flags);
        if (err)
@@ -1928,6 +1946,8 @@ static int dn_sendmsg(struct kiocb *iocb, struct socket *sock,
 
        if (sk->sk_shutdown & SEND_SHUTDOWN) {
                err = -EPIPE;
+               if (!(flags & MSG_NOSIGNAL))
+                       send_sig(SIGPIPE, current, 0);
                goto out_err;
        }
 
@@ -1989,8 +2009,12 @@ static int dn_sendmsg(struct kiocb *iocb, struct socket *sock,
 
                /*
                 * Get a suitably sized skb.
+                * 64 is a bit of a hack really, but its larger than any
+                * link-layer headers and has served us well as a good
+                * guess as to their real length.
                 */
-               skb = dn_alloc_send_skb(sk, &len, flags & MSG_DONTWAIT, timeo, &err);
+               skb = dn_alloc_send_pskb(sk, len + 64 + DN_MAX_NSP_DATA_HEADER,
+                                        flags & MSG_DONTWAIT, &err);
 
                if (err)
                        break;
@@ -2000,7 +2024,7 @@ static int dn_sendmsg(struct kiocb *iocb, struct socket *sock,
 
                cb = DN_SKB_CB(skb);
 
-               skb_reserve(skb, DN_MAX_NSP_DATA_HEADER);
+               skb_reserve(skb, 64 + DN_MAX_NSP_DATA_HEADER);
 
                if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
                        err = -EFAULT;
@@ -2045,7 +2069,7 @@ out:
        return sent ? sent : err;
 
 out_err:
-       err = dn_error(sk, flags, err);
+       err = sk_stream_error(sk, flags, err);
        release_sock(sk);
        return err;
 }
@@ -2073,7 +2097,7 @@ static struct notifier_block dn_dev_notifier = {
        .notifier_call = dn_device_event,
 };
 
-extern int dn_route_rcv(struct sk_buff *, struct net_device *, struct packet_type *);
+extern int dn_route_rcv(struct sk_buff *, struct net_device *, struct packet_type *, struct net_device *);
 
 static struct packet_type dn_dix_packet_type = {
        .type =         __constant_htons(ETH_P_DNA_RT),
@@ -2319,7 +2343,7 @@ static struct net_proto_family    dn_family_ops = {
        .owner  =       THIS_MODULE,
 };
 
-static struct proto_ops dn_proto_ops = {
+static const struct proto_ops dn_proto_ops = {
        .family =       AF_DECnet,
        .owner =        THIS_MODULE,
        .release =      dn_release,