percpu: add __percpu sparse annotations to net
[safe/jmp/linux-2.6] / net / sctp / protocol.c
index b435a19..9687177 100644 (file)
@@ -52,6 +52,8 @@
 #include <linux/inetdevice.h>
 #include <linux/seq_file.h>
 #include <linux/bootmem.h>
+#include <linux/highmem.h>
+#include <linux/swap.h>
 #include <net/net_namespace.h>
 #include <net/protocol.h>
 #include <net/ip.h>
 
 /* Global data structures. */
 struct sctp_globals sctp_globals __read_mostly;
-struct proc_dir_entry  *proc_net_sctp;
 DEFINE_SNMP_STAT(struct sctp_mib, sctp_statistics) __read_mostly;
 
+#ifdef CONFIG_PROC_FS
+struct proc_dir_entry  *proc_net_sctp;
+#endif
+
 struct idr sctp_assocs_id;
 DEFINE_SPINLOCK(sctp_assocs_id_lock);
 
@@ -97,25 +102,43 @@ struct sock *sctp_get_ctl_sock(void)
 /* Set up the proc fs entry for the SCTP protocol. */
 static __init int sctp_proc_init(void)
 {
+       if (percpu_counter_init(&sctp_sockets_allocated, 0))
+               goto out_nomem;
+#ifdef CONFIG_PROC_FS
        if (!proc_net_sctp) {
-               struct proc_dir_entry *ent;
-               ent = proc_mkdir("sctp", init_net.proc_net);
-               if (ent) {
-                       ent->owner = THIS_MODULE;
-                       proc_net_sctp = ent;
-               } else
-                       goto out_nomem;
+               proc_net_sctp = proc_mkdir("sctp", init_net.proc_net);
+               if (!proc_net_sctp)
+                       goto out_free_percpu;
        }
 
        if (sctp_snmp_proc_init())
-               goto out_nomem;
+               goto out_snmp_proc_init;
        if (sctp_eps_proc_init())
-               goto out_nomem;
+               goto out_eps_proc_init;
        if (sctp_assocs_proc_init())
-               goto out_nomem;
+               goto out_assocs_proc_init;
+       if (sctp_remaddr_proc_init())
+               goto out_remaddr_proc_init;
 
        return 0;
 
+out_remaddr_proc_init:
+       sctp_assocs_proc_exit();
+out_assocs_proc_init:
+       sctp_eps_proc_exit();
+out_eps_proc_init:
+       sctp_snmp_proc_exit();
+out_snmp_proc_init:
+       if (proc_net_sctp) {
+               proc_net_sctp = NULL;
+               remove_proc_entry("sctp", init_net.proc_net);
+       }
+out_free_percpu:
+       percpu_counter_destroy(&sctp_sockets_allocated);
+#else
+       return 0;
+#endif /* CONFIG_PROC_FS */
+
 out_nomem:
        return -ENOMEM;
 }
@@ -126,14 +149,18 @@ out_nomem:
  */
 static void sctp_proc_exit(void)
 {
+#ifdef CONFIG_PROC_FS
        sctp_snmp_proc_exit();
        sctp_eps_proc_exit();
        sctp_assocs_proc_exit();
+       sctp_remaddr_proc_exit();
 
        if (proc_net_sctp) {
                proc_net_sctp = NULL;
                remove_proc_entry("sctp", init_net.proc_net);
        }
+#endif
+       percpu_counter_destroy(&sctp_sockets_allocated);
 }
 
 /* Private helper to extract ipv4 address and stash them in
@@ -178,14 +205,14 @@ static void sctp_get_local_addr_list(void)
        struct list_head *pos;
        struct sctp_af *af;
 
-       read_lock(&dev_base_lock);
-       for_each_netdev(&init_net, dev) {
+       rcu_read_lock();
+       for_each_netdev_rcu(&init_net, dev) {
                __list_for_each(pos, &sctp_address_families) {
                        af = list_entry(pos, struct sctp_af, list);
                        af->copy_addrlist(&sctp_local_addr_list, dev);
                }
        }
-       read_unlock(&dev_base_lock);
+       rcu_read_unlock();
 }
 
 /* Free the existing local addresses.  */
@@ -269,19 +296,19 @@ static void sctp_v4_from_sk(union sctp_addr *addr, struct sock *sk)
 {
        addr->v4.sin_family = AF_INET;
        addr->v4.sin_port = 0;
-       addr->v4.sin_addr.s_addr = inet_sk(sk)->rcv_saddr;
+       addr->v4.sin_addr.s_addr = inet_sk(sk)->inet_rcv_saddr;
 }
 
 /* Initialize sk->sk_rcv_saddr from sctp_addr. */
 static void sctp_v4_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
 {
-       inet_sk(sk)->rcv_saddr = addr->v4.sin_addr.s_addr;
+       inet_sk(sk)->inet_rcv_saddr = addr->v4.sin_addr.s_addr;
 }
 
 /* Initialize sk->sk_daddr from sctp_addr. */
 static void sctp_v4_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
 {
-       inet_sk(sk)->daddr = addr->v4.sin_addr.s_addr;
+       inet_sk(sk)->inet_daddr = addr->v4.sin_addr.s_addr;
 }
 
 /* Initialize a sctp_addr from an address parameter. */
@@ -358,12 +385,16 @@ static int sctp_v4_addr_valid(union sctp_addr *addr,
                              struct sctp_sock *sp,
                              const struct sk_buff *skb)
 {
+       /* IPv4 addresses not allowed */
+       if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
+               return 0;
+
        /* Is this a non-unicast address or a unusable SCTP address? */
        if (IS_IPV4_UNUSABLE_ADDRESS(addr->v4.sin_addr.s_addr))
                return 0;
 
        /* Is this a broadcast address? */
-       if (skb && skb->rtable->rt_flags & RTCF_BROADCAST)
+       if (skb && skb_rtable(skb)->rt_flags & RTCF_BROADCAST)
                return 0;
 
        return 1;
@@ -381,6 +412,9 @@ static int sctp_v4_available(union sctp_addr *addr, struct sctp_sock *sp)
           !sysctl_ip_nonlocal_bind)
                return 0;
 
+       if (ipv6_only_sock(sctp_opt2sk(sp)))
+               return 0;
+
        return 1;
 }
 
@@ -397,16 +431,14 @@ static int sctp_v4_available(union sctp_addr *addr, struct sctp_sock *sp)
  * of requested destination address, sender and receiver
  * SHOULD include all of its addresses with level greater
  * than or equal to L.
+ *
+ * IPv4 scoping can be controlled through sysctl option
+ * net.sctp.addr_scope_policy
  */
 static sctp_scope_t sctp_v4_scope(union sctp_addr *addr)
 {
        sctp_scope_t retval;
 
-       /* Should IPv4 scoping be a sysctl configurable option
-        * so users can turn it off (default on) for certain
-        * unconventional networking environments?
-        */
-
        /* Check for unusable SCTP addresses. */
        if (IS_IPV4_UNUSABLE_ADDRESS(addr->v4.sin_addr.s_addr)) {
                retval =  SCTP_SCOPE_UNUSABLE;
@@ -450,9 +482,8 @@ static struct dst_entry *sctp_v4_get_dst(struct sctp_association *asoc,
        if (saddr)
                fl.fl4_src = saddr->v4.sin_addr.s_addr;
 
-       SCTP_DEBUG_PRINTK("%s: DST:%u.%u.%u.%u, SRC:%u.%u.%u.%u - ",
-                         __func__, NIPQUAD(fl.fl4_dst),
-                         NIPQUAD(fl.fl4_src));
+       SCTP_DEBUG_PRINTK("%s: DST:%pI4, SRC:%pI4 - ",
+                         __func__, &fl.fl4_dst, &fl.fl4_src);
 
        if (!ip_route_output_key(&init_net, &rt, &fl)) {
                dst = &rt->u.dst;
@@ -508,8 +539,8 @@ out_unlock:
        rcu_read_unlock();
 out:
        if (dst)
-               SCTP_DEBUG_PRINTK("rt_dst:%u.%u.%u.%u, rt_src:%u.%u.%u.%u\n",
-                                 NIPQUAD(rt->rt_dst), NIPQUAD(rt->rt_src));
+               SCTP_DEBUG_PRINTK("rt_dst:%pI4, rt_src:%pI4\n",
+                                 &rt->rt_dst, &rt->rt_src);
        else
                SCTP_DEBUG_PRINTK("NO ROUTE\n");
 
@@ -540,7 +571,7 @@ static void sctp_v4_get_saddr(struct sctp_sock *sk,
 /* What interface did this skb arrive on? */
 static int sctp_v4_skb_iif(const struct sk_buff *skb)
 {
-       return skb->rtable->rt_iif;
+       return skb_rtable(skb)->rt_iif;
 }
 
 /* Was this packet marked by Explicit Congestion Notification? */
@@ -553,46 +584,21 @@ static int sctp_v4_is_ce(const struct sk_buff *skb)
 static struct sock *sctp_v4_create_accept_sk(struct sock *sk,
                                             struct sctp_association *asoc)
 {
-       struct inet_sock *inet = inet_sk(sk);
-       struct inet_sock *newinet;
        struct sock *newsk = sk_alloc(sock_net(sk), PF_INET, GFP_KERNEL,
                        sk->sk_prot);
+       struct inet_sock *newinet;
 
        if (!newsk)
                goto out;
 
        sock_init_data(NULL, newsk);
 
-       newsk->sk_type = SOCK_STREAM;
-
-       newsk->sk_no_check = sk->sk_no_check;
-       newsk->sk_reuse = sk->sk_reuse;
-       newsk->sk_shutdown = sk->sk_shutdown;
-
-       newsk->sk_destruct = inet_sock_destruct;
-       newsk->sk_family = PF_INET;
-       newsk->sk_protocol = IPPROTO_SCTP;
-       newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
+       sctp_copy_sock(newsk, sk, asoc);
        sock_reset_flag(newsk, SOCK_ZAPPED);
 
        newinet = inet_sk(newsk);
 
-       /* Initialize sk's sport, dport, rcv_saddr and daddr for
-        * getsockname() and getpeername()
-        */
-       newinet->sport = inet->sport;
-       newinet->saddr = inet->saddr;
-       newinet->rcv_saddr = inet->rcv_saddr;
-       newinet->dport = htons(asoc->peer.port);
-       newinet->daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr;
-       newinet->pmtudisc = inet->pmtudisc;
-       newinet->id = asoc->next_tsn ^ jiffies;
-
-       newinet->uc_ttl = -1;
-       newinet->mc_loop = 1;
-       newinet->mc_ttl = 1;
-       newinet->mc_index = 0;
-       newinet->mc_list = NULL;
+       newinet->inet_daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr;
 
        sk_refcnt_debug_inc(newsk);
 
@@ -614,7 +620,7 @@ static void sctp_v4_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr)
 /* Dump the v4 addr to the seq file. */
 static void sctp_v4_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
 {
-       seq_printf(seq, "%d.%d.%d.%d ", NIPQUAD(addr->v4.sin_addr));
+       seq_printf(seq, "%pI4 ", &addr->v4.sin_addr);
 }
 
 static void sctp_v4_ecn_capable(struct sock *sk)
@@ -636,7 +642,7 @@ static int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev,
        struct sctp_sockaddr_entry *temp;
        int found = 0;
 
-       if (dev_net(ifa->ifa_dev->dev) != &init_net)
+       if (!net_eq(dev_net(ifa->ifa_dev->dev), &init_net))
                return NOTIFY_DONE;
 
        switch (ev) {
@@ -681,15 +687,20 @@ static int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev,
 static int sctp_ctl_sock_init(void)
 {
        int err;
-       sa_family_t family;
+       sa_family_t family = PF_INET;
 
        if (sctp_get_pf_specific(PF_INET6))
                family = PF_INET6;
-       else
-               family = PF_INET;
 
        err = inet_ctl_sock_create(&sctp_ctl_sock, family,
                                   SOCK_SEQPACKET, IPPROTO_SCTP, &init_net);
+
+       /* If IPv6 socket could not be created, try the IPv4 socket */
+       if (err < 0 && family == PF_INET6)
+               err = inet_ctl_sock_create(&sctp_ctl_sock, AF_INET,
+                                          SOCK_SEQPACKET, IPPROTO_SCTP,
+                                          &init_net);
+
        if (err < 0) {
                printk(KERN_ERR
                       "SCTP: Failed to create the SCTP control socket.\n");
@@ -830,16 +841,20 @@ static int sctp_inet_supported_addrs(const struct sctp_sock *opt,
 
 /* Wrapper routine that calls the ip transmit routine. */
 static inline int sctp_v4_xmit(struct sk_buff *skb,
-                              struct sctp_transport *transport, int ipfragok)
+                              struct sctp_transport *transport)
 {
-       SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, "
-                         "src:%u.%u.%u.%u, dst:%u.%u.%u.%u\n",
+       struct inet_sock *inet = inet_sk(skb->sk);
+
+       SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, src:%pI4, dst:%pI4\n",
                          __func__, skb, skb->len,
-                         NIPQUAD(skb->rtable->rt_src),
-                         NIPQUAD(skb->rtable->rt_dst));
+                         &skb_rtable(skb)->rt_src,
+                         &skb_rtable(skb)->rt_dst);
+
+       inet->pmtudisc = transport->param_flags & SPP_PMTUD_ENABLE ?
+                        IP_PMTUDISC_DO : IP_PMTUDISC_DONT;
 
        SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS);
-       return ip_queue_xmit(skb, ipfragok);
+       return ip_queue_xmit(skb, 0);
 }
 
 static struct sctp_af sctp_af_inet;
@@ -894,7 +909,6 @@ static struct inet_protosw sctp_seqpacket_protosw = {
        .protocol   = IPPROTO_SCTP,
        .prot       = &sctp_prot,
        .ops        = &inet_seqpacket_ops,
-       .capability = -1,
        .no_check   = 0,
        .flags      = SCTP_PROTOSW_FLAG
 };
@@ -903,13 +917,12 @@ static struct inet_protosw sctp_stream_protosw = {
        .protocol   = IPPROTO_SCTP,
        .prot       = &sctp_prot,
        .ops        = &inet_seqpacket_ops,
-       .capability = -1,
        .no_check   = 0,
        .flags      = SCTP_PROTOSW_FLAG
 };
 
 /* Register with IP layer.  */
-static struct net_protocol sctp_protocol = {
+static const struct net_protocol sctp_protocol = {
        .handler     = sctp_rcv,
        .err_handler = sctp_v4_err,
        .no_policy   = 1,
@@ -983,12 +996,13 @@ int sctp_register_pf(struct sctp_pf *pf, sa_family_t family)
 
 static inline int init_sctp_mibs(void)
 {
-       return snmp_mib_init((void**)sctp_statistics, sizeof(struct sctp_mib));
+       return snmp_mib_init((void __percpu **)sctp_statistics,
+                            sizeof(struct sctp_mib));
 }
 
 static inline void cleanup_sctp_mibs(void)
 {
-       snmp_mib_free((void**)sctp_statistics);
+       snmp_mib_free((void __percpu **)sctp_statistics);
 }
 
 static void sctp_v4_pf_init(void)
@@ -1050,6 +1064,7 @@ SCTP_STATIC __init int sctp_init(void)
        int status = -EINVAL;
        unsigned long goal;
        unsigned long limit;
+       unsigned long nr_pages;
        int max_share;
        int order;
 
@@ -1145,8 +1160,9 @@ SCTP_STATIC __init int sctp_init(void)
         * Note this initalizes the data in sctpv6_prot too
         * Unabashedly stolen from tcp_init
         */
-       limit = min(num_physpages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT);
-       limit = (limit * (num_physpages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11);
+       nr_pages = totalram_pages - totalhigh_pages;
+       limit = min(nr_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT);
+       limit = (limit * (nr_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11);
        limit = max(limit, 128UL);
        sysctl_sctp_mem[0] = limit / 4 * 3;
        sysctl_sctp_mem[1] = limit;
@@ -1156,7 +1172,7 @@ SCTP_STATIC __init int sctp_init(void)
        limit = (sysctl_sctp_mem[1]) << (PAGE_SHIFT - 7);
        max_share = min(4UL*1024*1024, limit);
 
-       sysctl_sctp_rmem[0] = PAGE_SIZE; /* give each asoc 1 page min */
+       sysctl_sctp_rmem[0] = SK_MEM_QUANTUM; /* give each asoc 1 page min */
        sysctl_sctp_rmem[1] = (1500 *(sizeof(struct sk_buff) + 1));
        sysctl_sctp_rmem[2] = max(sysctl_sctp_rmem[1], max_share);
 
@@ -1167,10 +1183,10 @@ SCTP_STATIC __init int sctp_init(void)
        /* Size and allocate the association hash table.
         * The methodology is similar to that of the tcp hash tables.
         */
-       if (num_physpages >= (128 * 1024))
-               goal = num_physpages >> (22 - PAGE_SHIFT);
+       if (totalram_pages >= (128 * 1024))
+               goal = totalram_pages >> (22 - PAGE_SHIFT);
        else
-               goal = num_physpages >> (24 - PAGE_SHIFT);
+               goal = totalram_pages >> (24 - PAGE_SHIFT);
 
        for (order = 0; (1UL << order) < goal; order++)
                ;
@@ -1240,6 +1256,12 @@ SCTP_STATIC __init int sctp_init(void)
        /* Disable AUTH by default. */
        sctp_auth_enable = 0;
 
+       /* Set SCOPE policy to enabled */
+       sctp_scope_policy = SCTP_SCOPE_POLICY_ENABLE;
+
+       /* Set the default rwnd update threshold */
+       sctp_rwnd_upd_shift             = SCTP_DEFAULT_RWND_SHIFT;
+
        sctp_sysctl_register();
 
        INIT_LIST_HEAD(&sctp_address_families);
@@ -1280,9 +1302,8 @@ SCTP_STATIC __init int sctp_init(void)
 out:
        return status;
 err_v6_add_protocol:
-       sctp_v6_del_protocol();
-err_add_protocol:
        sctp_v4_del_protocol();
+err_add_protocol:
        inet_ctl_sock_destroy(sctp_ctl_sock);
 err_ctl_sock_init:
        sctp_v6_protosw_exit();
@@ -1293,7 +1314,6 @@ err_protosw_init:
        sctp_v4_pf_exit();
        sctp_v6_pf_exit();
        sctp_sysctl_unregister();
-       list_del(&sctp_af_inet.list);
        free_pages((unsigned long)sctp_port_hashtable,
                   get_order(sctp_port_hashsize *
                             sizeof(struct sctp_bind_hashbucket)));
@@ -1341,7 +1361,6 @@ SCTP_STATIC __exit void sctp_exit(void)
        sctp_v4_pf_exit();
 
        sctp_sysctl_unregister();
-       list_del(&sctp_af_inet.list);
 
        free_pages((unsigned long)sctp_assoc_hashtable,
                   get_order(sctp_assoc_hashsize *
@@ -1355,6 +1374,8 @@ SCTP_STATIC __exit void sctp_exit(void)
        sctp_proc_exit();
        cleanup_sctp_mibs();
 
+       rcu_barrier(); /* Wait for completion of call_rcu()'s */
+
        kmem_cache_destroy(sctp_chunk_cachep);
        kmem_cache_destroy(sctp_bucket_cachep);
 }
@@ -1369,4 +1390,6 @@ MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-132");
 MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-132");
 MODULE_AUTHOR("Linux Kernel SCTP developers <lksctp-developers@lists.sourceforge.net>");
 MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)");
+module_param_named(no_checksums, sctp_checksum_disable, bool, 0644);
+MODULE_PARM_DESC(no_checksums, "Disable checksums computing and verification");
 MODULE_LICENSE("GPL");