[ROSE]: Socket locking is a great invention.
[safe/jmp/linux-2.6] / net / rose / af_rose.c
index 7eb6a5b..f92d531 100644 (file)
@@ -9,7 +9,8 @@
  * Copyright (C) Terry Dawson VK2KTJ (terry@animats.net)
  * Copyright (C) Tomi Manninen OH2BNS (oh2bns@sral.fi)
  */
-#include <linux/config.h>
+
+#include <linux/capability.h>
 #include <linux/module.h>
 #include <linux/moduleparam.h>
 #include <linux/init.h>
@@ -41,7 +42,7 @@
 #include <net/rose.h>
 #include <linux/proc_fs.h>
 #include <linux/seq_file.h>
-#include <net/tcp.h>
+#include <net/tcp_states.h>
 #include <net/ip.h>
 #include <net/arp.h>
 
@@ -66,6 +67,14 @@ static struct proto_ops rose_proto_ops;
 ax25_address rose_callsign;
 
 /*
+ * ROSE network devices are virtual network devices encapsulating ROSE
+ * frames into AX.25 which will be sent through an AX.25 device, so form a
+ * special "super class" of normal net devices; split their locks off into a
+ * separate class since they always nest.
+ */
+static struct lock_class_key rose_netdev_xmit_lock_key;
+
+/*
  *     Convert a ROSE address into text.
  */
 const char *rose2asc(const rose_address *addr)
@@ -516,11 +525,11 @@ static int rose_create(struct socket *sock, int protocol)
        init_timer(&rose->timer);
        init_timer(&rose->idletimer);
 
-       rose->t1   = sysctl_rose_call_request_timeout;
-       rose->t2   = sysctl_rose_reset_request_timeout;
-       rose->t3   = sysctl_rose_clear_request_timeout;
-       rose->hb   = sysctl_rose_ack_hold_back_timeout;
-       rose->idle = sysctl_rose_no_activity_timeout;
+       rose->t1   = msecs_to_jiffies(sysctl_rose_call_request_timeout);
+       rose->t2   = msecs_to_jiffies(sysctl_rose_reset_request_timeout);
+       rose->t3   = msecs_to_jiffies(sysctl_rose_clear_request_timeout);
+       rose->hb   = msecs_to_jiffies(sysctl_rose_ack_hold_back_timeout);
+       rose->idle = msecs_to_jiffies(sysctl_rose_no_activity_timeout);
 
        rose->state = ROSE_STATE_0;
 
@@ -556,12 +565,7 @@ static struct sock *rose_make_new(struct sock *osk)
        sk->sk_sndbuf   = osk->sk_sndbuf;
        sk->sk_state    = TCP_ESTABLISHED;
        sk->sk_sleep    = osk->sk_sleep;
-
-       if (sock_flag(osk, SOCK_ZAPPED))
-               sock_set_flag(sk, SOCK_ZAPPED);
-
-       if (sock_flag(osk, SOCK_DBG))
-               sock_set_flag(sk, SOCK_DBG);
+       sock_copy_flags(sk, osk);
 
        init_timer(&rose->timer);
        init_timer(&rose->idletimer);
@@ -631,7 +635,8 @@ static int rose_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
        struct rose_sock *rose = rose_sk(sk);
        struct sockaddr_rose *addr = (struct sockaddr_rose *)uaddr;
        struct net_device *dev;
-       ax25_address *user, *source;
+       ax25_address *source;
+       ax25_uid_assoc *user;
        int n;
 
        if (!sock_flag(sk, SOCK_ZAPPED))
@@ -656,14 +661,17 @@ static int rose_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
 
        source = &addr->srose_call;
 
-       if ((user = ax25_findbyuid(current->euid)) == NULL) {
+       user = ax25_findbyuid(current->euid);
+       if (user) {
+               rose->source_call = user->call;
+               ax25_uid_put(user);
+       } else {
                if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE))
                        return -EACCES;
-               user = source;
+               rose->source_call   = *source;
        }
 
        rose->source_addr   = addr->srose_addr;
-       rose->source_call   = *user;
        rose->device        = dev;
        rose->source_ndigis = addr->srose_ndigis;
 
@@ -690,25 +698,9 @@ static int rose_connect(struct socket *sock, struct sockaddr *uaddr, int addr_le
        struct rose_sock *rose = rose_sk(sk);
        struct sockaddr_rose *addr = (struct sockaddr_rose *)uaddr;
        unsigned char cause, diagnostic;
-       ax25_address *user;
        struct net_device *dev;
-       int n;
-
-       if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
-               sock->state = SS_CONNECTED;
-               return 0;       /* Connect completed during a ERESTARTSYS event */
-       }
-
-       if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
-               sock->state = SS_UNCONNECTED;
-               return -ECONNREFUSED;
-       }
-
-       if (sk->sk_state == TCP_ESTABLISHED)
-               return -EISCONN;        /* No reconnect on a seqpacket socket */
-
-       sk->sk_state   = TCP_CLOSE;
-       sock->state = SS_UNCONNECTED;
+       ax25_uid_assoc *user;
+       int n, err = 0;
 
        if (addr_len != sizeof(struct sockaddr_rose) && addr_len != sizeof(struct full_sockaddr_rose))
                return -EINVAL;
@@ -726,31 +718,62 @@ static int rose_connect(struct socket *sock, struct sockaddr *uaddr, int addr_le
        if ((rose->source_ndigis + addr->srose_ndigis) > ROSE_MAX_DIGIS)
                return -EINVAL;
 
+       lock_sock(sk);
+
+       if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
+               /* Connect completed during a ERESTARTSYS event */
+               sock->state = SS_CONNECTED;
+               goto out_release;
+       }
+
+       if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
+               sock->state = SS_UNCONNECTED;
+               err = -ECONNREFUSED;
+               goto out_release;
+       }
+
+       if (sk->sk_state == TCP_ESTABLISHED) {
+               /* No reconnect on a seqpacket socket */
+               err = -EISCONN;
+               goto out_release;
+       }
+
+       sk->sk_state   = TCP_CLOSE;
+       sock->state = SS_UNCONNECTED;
+
        rose->neighbour = rose_get_neigh(&addr->srose_addr, &cause,
                                         &diagnostic);
        if (!rose->neighbour)
                return -ENETUNREACH;
 
        rose->lci = rose_new_lci(rose->neighbour);
-       if (!rose->lci)
-               return -ENETUNREACH;
+       if (!rose->lci) {
+               err = -ENETUNREACH;
+               goto out_release;
+       }
 
        if (sock_flag(sk, SOCK_ZAPPED)) {       /* Must bind first - autobinding in this may or may not work */
                sock_reset_flag(sk, SOCK_ZAPPED);
 
-               if ((dev = rose_dev_first()) == NULL)
-                       return -ENETUNREACH;
+               if ((dev = rose_dev_first()) == NULL) {
+                       err = -ENETUNREACH;
+                       goto out_release;
+               }
 
-               if ((user = ax25_findbyuid(current->euid)) == NULL)
-                       return -EINVAL;
+               user = ax25_findbyuid(current->euid);
+               if (!user) {
+                       err = -EINVAL;
+                       goto out_release;
+               }
 
                memcpy(&rose->source_addr, dev->dev_addr, ROSE_ADDR_LEN);
-               rose->source_call = *user;
+               rose->source_call = user->call;
                rose->device      = dev;
+               ax25_uid_put(user);
 
                rose_insert_socket(sk);         /* Finish the bind */
        }
-
+rose_try_next_neigh:
        rose->dest_addr   = addr->srose_addr;
        rose->dest_call   = addr->srose_call;
        rose->rand        = ((long)rose & 0xFFFF) + rose->lci;
@@ -779,8 +802,10 @@ static int rose_connect(struct socket *sock, struct sockaddr *uaddr, int addr_le
        rose_start_t1timer(sk);
 
        /* Now the loop */
-       if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
-               return -EINPROGRESS;
+       if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) {
+               err = -EINPROGRESS;
+               goto out_release;
+       }
 
        /*
         * A Connect Ack with Choke or timeout or failed routing will go to
@@ -795,8 +820,10 @@ static int rose_connect(struct socket *sock, struct sockaddr *uaddr, int addr_le
                        set_current_state(TASK_INTERRUPTIBLE);
                        if (sk->sk_state != TCP_SYN_SENT)
                                break;
+                       release_sock(sk);
                        if (!signal_pending(tsk)) {
                                schedule();
+                               lock_sock(sk);
                                continue;
                        }
                        current->state = TASK_RUNNING;
@@ -808,13 +835,23 @@ static int rose_connect(struct socket *sock, struct sockaddr *uaddr, int addr_le
        }
 
        if (sk->sk_state != TCP_ESTABLISHED) {
+       /* Try next neighbour */
+               rose->neighbour = rose_get_neigh(&addr->srose_addr, &cause, &diagnostic);
+               if (rose->neighbour)
+                       goto rose_try_next_neigh;
+
+               /* No more neighbours */
                sock->state = SS_UNCONNECTED;
-               return sock_error(sk);  /* Always set at this point */
+               err = sock_error(sk);   /* Always set at this point */
+               goto out_release;
        }
 
        sock->state = SS_CONNECTED;
 
-       return 0;
+out_release:
+       release_sock(sk);
+
+       return err;
 }
 
 static int rose_accept(struct socket *sock, struct socket *newsock, int flags)
@@ -862,6 +899,8 @@ static int rose_accept(struct socket *sock, struct socket *newsock, int flags)
                        lock_sock(sk);
                        continue;
                }
+               current->state = TASK_RUNNING;
+               remove_wait_queue(sk->sk_sleep, &wait);
                return -ERESTARTSYS;
        }
        current->state = TASK_RUNNING;
@@ -1242,7 +1281,7 @@ static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
                amount = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
                if (amount < 0)
                        amount = 0;
-               return put_user(amount, (unsigned int __user *)argp);
+               return put_user(amount, (unsigned int __user *) argp);
        }
 
        case TIOCINQ: {
@@ -1251,13 +1290,11 @@ static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
                /* These two are safe on a single CPU system as only user tasks fiddle here */
                if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
                        amount = skb->len;
-               return put_user(amount, (unsigned int __user *)argp);
+               return put_user(amount, (unsigned int __user *) argp);
        }
 
        case SIOCGSTAMP:
-               if (sk != NULL) 
-                       return sock_get_timestamp(sk, (struct timeval __user *)argp);
-               return -EINVAL;
+               return sock_get_timestamp(sk, (struct timeval __user *) argp);
 
        case SIOCGIFADDR:
        case SIOCSIFADDR:
@@ -1301,7 +1338,8 @@ static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
                if (copy_from_user(&rose_callsign, argp, sizeof(ax25_address)))
                        return -EFAULT;
                if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
-                       ax25_listen_register(&rose_callsign, NULL);
+                       return ax25_listen_register(&rose_callsign, NULL);
+
                return 0;
 
        case SIOCRSGL2CALL:
@@ -1321,7 +1359,7 @@ static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
                return 0;
 
        default:
-               return dev_ioctl(cmd, argp);
+               return -ENOIOCTLCMD;
        }
 
        return 0;
@@ -1337,7 +1375,7 @@ static void *rose_info_start(struct seq_file *seq, loff_t *pos)
        spin_lock_bh(&rose_list_lock);
        if (*pos == 0)
                return SEQ_START_TOKEN;
-       
+
        i = 1;
        sk_for_each(s, node, &rose_list) {
                if (i == *pos)
@@ -1351,10 +1389,10 @@ static void *rose_info_next(struct seq_file *seq, void *v, loff_t *pos)
 {
        ++*pos;
 
-       return (v == SEQ_START_TOKEN) ? sk_head(&rose_list) 
+       return (v == SEQ_START_TOKEN) ? sk_head(&rose_list)
                : sk_next((struct sock *)v);
 }
-       
+
 static void rose_info_stop(struct seq_file *seq, void *v)
 {
        spin_unlock_bh(&rose_list_lock);
@@ -1362,8 +1400,10 @@ static void rose_info_stop(struct seq_file *seq, void *v)
 
 static int rose_info_show(struct seq_file *seq, void *v)
 {
+       char buf[11];
+
        if (v == SEQ_START_TOKEN)
-               seq_puts(seq, 
+               seq_puts(seq,
                         "dest_addr  dest_call src_addr   src_call  dev   lci neigh st vs vr va   t  t1  t2  t3  hb    idle Snd-Q Rcv-Q inode\n");
 
        else {
@@ -1376,15 +1416,15 @@ static int rose_info_show(struct seq_file *seq, void *v)
                        devname = "???";
                else
                        devname = dev->name;
-               
+
                seq_printf(seq, "%-10s %-9s ",
                        rose2asc(&rose->dest_addr),
-                       ax2asc(&rose->dest_call));
+                       ax2asc(buf, &rose->dest_call));
 
                if (ax25cmp(&rose->source_call, &null_ax25_address) == 0)
                        callsign = "??????-?";
                else
-                       callsign = ax2asc(&rose->source_call);
+                       callsign = ax2asc(buf, &rose->source_call);
 
                seq_printf(seq,
                           "%-10s %-9s %-5s %3.3X %05d  %d  %d  %d  %d %3lu %3lu %3lu %3lu %3lu %3lu/%03lu %5d %5d %ld\n",
@@ -1424,7 +1464,7 @@ static int rose_info_open(struct inode *inode, struct file *file)
        return seq_open(file, &rose_info_seqops);
 }
 
-static struct file_operations rose_info_fops = {
+static const struct file_operations rose_info_fops = {
        .owner = THIS_MODULE,
        .open = rose_info_open,
        .read = seq_read,
@@ -1466,55 +1506,66 @@ static struct notifier_block rose_dev_notifier = {
 
 static struct net_device **dev_rose;
 
-static const char banner[] = KERN_INFO "F6FBB/G4KLX ROSE for Linux. Version 0.62 for AX25.037 Linux 2.4\n";
+static struct ax25_protocol rose_pid = {
+       .pid    = AX25_P_ROSE,
+       .func   = rose_route_frame
+};
+
+static struct ax25_linkfail rose_linkfail_notifier = {
+       .func   = rose_link_failed
+};
 
 static int __init rose_proto_init(void)
 {
        int i;
-       int rc = proto_register(&rose_proto, 0);
+       int rc;
+
+       if (rose_ndevs > 0x7FFFFFFF/sizeof(struct net_device *)) {
+               printk(KERN_ERR "ROSE: rose_proto_init - rose_ndevs parameter to large\n");
+               rc = -EINVAL;
+               goto out;
+       }
 
+       rc = proto_register(&rose_proto, 0);
        if (rc != 0)
                goto out;
 
        rose_callsign = null_ax25_address;
 
-       if (rose_ndevs > 0x7FFFFFFF/sizeof(struct net_device *)) {
-               printk(KERN_ERR "ROSE: rose_proto_init - rose_ndevs parameter to large\n");
-               return -1;
-       }
-
-       dev_rose = kmalloc(rose_ndevs * sizeof(struct net_device *), GFP_KERNEL);
+       dev_rose = kzalloc(rose_ndevs * sizeof(struct net_device *), GFP_KERNEL);
        if (dev_rose == NULL) {
                printk(KERN_ERR "ROSE: rose_proto_init - unable to allocate device structure\n");
-               return -1;
+               rc = -ENOMEM;
+               goto out_proto_unregister;
        }
 
-       memset(dev_rose, 0x00, rose_ndevs * sizeof(struct net_device*));
        for (i = 0; i < rose_ndevs; i++) {
                struct net_device *dev;
                char name[IFNAMSIZ];
 
                sprintf(name, "rose%d", i);
-               dev = alloc_netdev(sizeof(struct net_device_stats), 
+               dev = alloc_netdev(sizeof(struct net_device_stats),
                                   name, rose_setup);
                if (!dev) {
                        printk(KERN_ERR "ROSE: rose_proto_init - unable to allocate memory\n");
+                       rc = -ENOMEM;
                        goto fail;
                }
-               if (register_netdev(dev)) {
-                       printk(KERN_ERR "ROSE: netdevice regeistration failed\n");
+               rc = register_netdev(dev);
+               if (rc) {
+                       printk(KERN_ERR "ROSE: netdevice registration failed\n");
                        free_netdev(dev);
                        goto fail;
                }
+               lockdep_set_class(&dev->_xmit_lock, &rose_netdev_xmit_lock_key);
                dev_rose[i] = dev;
        }
 
        sock_register(&rose_family_ops);
        register_netdevice_notifier(&rose_dev_notifier);
-       printk(banner);
 
-       ax25_protocol_register(AX25_P_ROSE, rose_route_frame);
-       ax25_linkfail_register(rose_link_failed);
+       ax25_register_pid(&rose_pid);
+       ax25_linkfail_register(&rose_linkfail_notifier);
 
 #ifdef CONFIG_SYSCTL
        rose_register_sysctl();
@@ -1535,8 +1586,9 @@ fail:
                free_netdev(dev_rose[i]);
        }
        kfree(dev_rose);
+out_proto_unregister:
        proto_unregister(&rose_proto);
-       return -ENOMEM;
+       goto out;
 }
 module_init(rose_proto_init);
 
@@ -1561,7 +1613,7 @@ static void __exit rose_exit(void)
        rose_rt_free();
 
        ax25_protocol_release(AX25_P_ROSE);
-       ax25_linkfail_release(rose_link_failed);
+       ax25_linkfail_release(&rose_linkfail_notifier);
 
        if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
                ax25_listen_release(&rose_callsign, NULL);