[IPV6]: Use in6addr_any where appropriate.
[safe/jmp/linux-2.6] / net / ipv6 / udp.c
index e991e60..30ef7dc 100644 (file)
@@ -34,6 +34,7 @@
 #include <linux/ipv6.h>
 #include <linux/icmpv6.h>
 #include <linux/init.h>
+#include <linux/module.h>
 #include <linux/skbuff.h>
 #include <asm/uaccess.h>
 
 #include <linux/seq_file.h>
 #include "udp_impl.h"
 
-DEFINE_SNMP_STAT(struct udp_mib, udp_stats_in6) __read_mostly;
-
-static inline int udp_v6_get_port(struct sock *sk, unsigned short snum)
+int udp_v6_get_port(struct sock *sk, unsigned short snum)
 {
-       return udp_get_port(sk, snum, ipv6_rcv_saddr_equal);
+       return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal);
 }
 
-static struct sock *__udp6_lib_lookup(struct in6_addr *saddr, __be16 sport,
+static struct sock *__udp6_lib_lookup(struct net *net,
+                                     struct in6_addr *saddr, __be16 sport,
                                      struct in6_addr *daddr, __be16 dport,
                                      int dif, struct hlist_head udptable[])
 {
@@ -70,7 +70,8 @@ static struct sock *__udp6_lib_lookup(struct in6_addr *saddr, __be16 sport,
        sk_for_each(sk, node, &udptable[hnum & (UDP_HTABLE_SIZE - 1)]) {
                struct inet_sock *inet = inet_sk(sk);
 
-               if (sk->sk_hash == hnum && sk->sk_family == PF_INET6) {
+               if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum &&
+                               sk->sk_family == PF_INET6) {
                        struct ipv6_pinfo *np = inet6_sk(sk);
                        int score = 0;
                        if (inet->dport) {
@@ -121,6 +122,7 @@ int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
        struct inet_sock *inet = inet_sk(sk);
        struct sk_buff *skb;
        unsigned int ulen, copied;
+       int peeked;
        int err;
        int is_udplite = IS_UDPLITE(sk);
 
@@ -131,7 +133,8 @@ int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
                return ipv6_recv_error(sk, msg, len);
 
 try_again:
-       skb = skb_recv_datagram(sk, flags, noblock, &err);
+       skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
+                                 &peeked, &err);
        if (!skb)
                goto out;
 
@@ -153,7 +156,7 @@ try_again:
                        goto csum_copy_err;
        }
 
-       if (skb->ip_summed == CHECKSUM_UNNECESSARY)
+       if (skb_csum_unnecessary(skb))
                err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
                                              msg->msg_iov, copied       );
        else {
@@ -164,6 +167,9 @@ try_again:
        if (err)
                goto out_free;
 
+       if (!peeked)
+               UDP6_INC_STATS_USER(UDP_MIB_INDATAGRAMS, is_udplite);
+
        sock_recv_timestamp(msg, sk, skb);
 
        /* Copy the address. */
@@ -172,7 +178,7 @@ try_again:
 
                sin6 = (struct sockaddr_in6 *) msg->msg_name;
                sin6->sin6_family = AF_INET6;
-               sin6->sin6_port = skb->h.uh->source;
+               sin6->sin6_port = udp_hdr(skb)->source;
                sin6->sin6_flowinfo = 0;
                sin6->sin6_scope_id = 0;
 
@@ -180,7 +186,8 @@ try_again:
                        ipv6_addr_set(&sin6->sin6_addr, 0, 0,
                                      htonl(0xffff), ip_hdr(skb)->saddr);
                else {
-                       ipv6_addr_copy(&sin6->sin6_addr, &skb->nh.ipv6h->saddr);
+                       ipv6_addr_copy(&sin6->sin6_addr,
+                                      &ipv6_hdr(skb)->saddr);
                        if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
                                sin6->sin6_scope_id = IP6CB(skb)->iif;
                }
@@ -199,17 +206,20 @@ try_again:
                err = ulen;
 
 out_free:
+       lock_sock(sk);
        skb_free_datagram(sk, skb);
+       release_sock(sk);
 out:
        return err;
 
 csum_copy_err:
-       skb_kill_datagram(sk, skb, flags);
-
-       if (flags & MSG_DONTWAIT) {
+       lock_sock(sk);
+       if (!skb_kill_datagram(sk, skb, flags))
                UDP6_INC_STATS_USER(UDP_MIB_INERRORS, is_udplite);
+       release_sock(sk);
+
+       if (flags & MSG_DONTWAIT)
                return -EAGAIN;
-       }
        goto try_again;
 }
 
@@ -225,7 +235,7 @@ void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
        struct sock *sk;
        int err;
 
-       sk = __udp6_lib_lookup(daddr, uh->dest,
+       sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest,
                               saddr, uh->source, inet6_iif(skb), udptable);
        if (sk == NULL)
                return;
@@ -251,13 +261,14 @@ static __inline__ void udpv6_err(struct sk_buff *skb,
                                 struct inet6_skb_parm *opt, int type,
                                 int code, int offset, __be32 info     )
 {
-       return __udp6_lib_err(skb, opt, type, code, offset, info, udp_hash);
+       __udp6_lib_err(skb, opt, type, code, offset, info, udp_hash);
 }
 
 int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
 {
        struct udp_sock *up = udp_sk(sk);
        int rc;
+       int is_udplite = IS_UDPLITE(sk);
 
        if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
                goto drop;
@@ -265,7 +276,7 @@ int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
        /*
         * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
         */
-       if ((up->pcflag & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
+       if ((is_udplite & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
 
                if (up->pcrlen == 0) {          /* full coverage was set  */
                        LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage"
@@ -289,13 +300,13 @@ int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
        if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) {
                /* Note that an ENOMEM error is charged twice */
                if (rc == -ENOMEM)
-                       UDP6_INC_STATS_BH(UDP_MIB_RCVBUFERRORS, up->pcflag);
+                       UDP6_INC_STATS_BH(UDP_MIB_RCVBUFERRORS, is_udplite);
                goto drop;
        }
-       UDP6_INC_STATS_BH(UDP_MIB_INDATAGRAMS, up->pcflag);
+
        return 0;
 drop:
-       UDP6_INC_STATS_BH(UDP_MIB_INERRORS, up->pcflag);
+       UDP6_INC_STATS_BH(UDP_MIB_INERRORS, is_udplite);
        kfree_skb(skb);
        return -1;
 }
@@ -312,6 +323,9 @@ static struct sock *udp_v6_mcast_next(struct sock *sk,
        sk_for_each_from(s, node) {
                struct inet_sock *inet = inet_sk(s);
 
+               if (sock_net(s) != sock_net(sk))
+                       continue;
+
                if (s->sk_hash == num && s->sk_family == PF_INET6) {
                        struct ipv6_pinfo *np = inet6_sk(s);
                        if (inet->dport) {
@@ -345,7 +359,7 @@ static int __udp6_lib_mcast_deliver(struct sk_buff *skb, struct in6_addr *saddr,
                           struct in6_addr *daddr, struct hlist_head udptable[])
 {
        struct sock *sk, *sk2;
-       const struct udphdr *uh = skb->h.uh;
+       const struct udphdr *uh = udp_hdr(skb);
        int dif;
 
        read_lock(&udp_hash_lock);
@@ -361,10 +375,21 @@ static int __udp6_lib_mcast_deliver(struct sk_buff *skb, struct in6_addr *saddr,
        while ((sk2 = udp_v6_mcast_next(sk_next(sk2), uh->dest, daddr,
                                        uh->source, saddr, dif))) {
                struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC);
-               if (buff)
-                       udpv6_queue_rcv_skb(sk2, buff);
+               if (buff) {
+                       bh_lock_sock_nested(sk2);
+                       if (!sock_owned_by_user(sk2))
+                               udpv6_queue_rcv_skb(sk2, buff);
+                       else
+                               sk_add_backlog(sk2, buff);
+                       bh_unlock_sock(sk2);
+               }
        }
-       udpv6_queue_rcv_skb(sk, skb);
+       bh_lock_sock_nested(sk);
+       if (!sock_owned_by_user(sk))
+               udpv6_queue_rcv_skb(sk, skb);
+       else
+               sk_add_backlog(sk, skb);
+       bh_unlock_sock(sk);
 out:
        read_unlock(&udp_hash_lock);
        return 0;
@@ -392,22 +417,21 @@ static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh,
                return 1;
        }
        if (skb->ip_summed == CHECKSUM_COMPLETE &&
-           !csum_ipv6_magic(&skb->nh.ipv6h->saddr, &skb->nh.ipv6h->daddr,
+           !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
                             skb->len, proto, skb->csum))
                skb->ip_summed = CHECKSUM_UNNECESSARY;
 
-       if (skb->ip_summed != CHECKSUM_UNNECESSARY)
-               skb->csum = ~csum_unfold(csum_ipv6_magic(&skb->nh.ipv6h->saddr,
-                                                        &skb->nh.ipv6h->daddr,
+       if (!skb_csum_unnecessary(skb))
+               skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
+                                                        &ipv6_hdr(skb)->daddr,
                                                         skb->len, proto, 0));
 
        return 0;
 }
 
-int __udp6_lib_rcv(struct sk_buff **pskb, struct hlist_head udptable[],
+int __udp6_lib_rcv(struct sk_buff *skb, struct hlist_head udptable[],
                   int proto)
 {
-       struct sk_buff *skb = *pskb;
        struct sock *sk;
        struct udphdr *uh;
        struct net_device *dev = skb->dev;
@@ -417,9 +441,9 @@ int __udp6_lib_rcv(struct sk_buff **pskb, struct hlist_head udptable[],
        if (!pskb_may_pull(skb, sizeof(struct udphdr)))
                goto short_packet;
 
-       saddr = &skb->nh.ipv6h->saddr;
-       daddr = &skb->nh.ipv6h->daddr;
-       uh = skb->h.uh;
+       saddr = &ipv6_hdr(skb)->saddr;
+       daddr = &ipv6_hdr(skb)->daddr;
+       uh = udp_hdr(skb);
 
        ulen = ntohs(uh->len);
        if (ulen > skb->len)
@@ -438,9 +462,9 @@ int __udp6_lib_rcv(struct sk_buff **pskb, struct hlist_head udptable[],
                if (ulen < skb->len) {
                        if (pskb_trim_rcsum(skb, ulen))
                                goto short_packet;
-                       saddr = &skb->nh.ipv6h->saddr;
-                       daddr = &skb->nh.ipv6h->daddr;
-                       uh = skb->h.uh;
+                       saddr = &ipv6_hdr(skb)->saddr;
+                       daddr = &ipv6_hdr(skb)->daddr;
+                       uh = udp_hdr(skb);
                }
        }
 
@@ -459,7 +483,7 @@ int __udp6_lib_rcv(struct sk_buff **pskb, struct hlist_head udptable[],
         * check socket cache ... must talk to Alan about his plans
         * for sock caches... i'll skip this for now.
         */
-       sk = __udp6_lib_lookup(saddr, uh->source,
+       sk = __udp6_lib_lookup(dev_net(skb->dev), saddr, uh->source,
                               daddr, uh->dest, inet6_iif(skb), udptable);
 
        if (sk == NULL) {
@@ -478,7 +502,12 @@ int __udp6_lib_rcv(struct sk_buff **pskb, struct hlist_head udptable[],
 
        /* deliver */
 
-       udpv6_queue_rcv_skb(sk, skb);
+       bh_lock_sock_nested(sk);
+       if (!sock_owned_by_user(sk))
+               udpv6_queue_rcv_skb(sk, skb);
+       else
+               sk_add_backlog(sk, skb);
+       bh_unlock_sock(sk);
        sock_put(sk);
        return 0;
 
@@ -493,9 +522,9 @@ discard:
        return 0;
 }
 
-static __inline__ int udpv6_rcv(struct sk_buff **pskb)
+static __inline__ int udpv6_rcv(struct sk_buff *skb)
 {
-       return __udp6_lib_rcv(pskb, udp_hash, IPPROTO_UDP);
+       return __udp6_lib_rcv(skb, udp_hash, IPPROTO_UDP);
 }
 
 /*
@@ -524,6 +553,7 @@ static int udp_v6_push_pending_frames(struct sock *sk)
        struct inet_sock *inet = inet_sk(sk);
        struct flowi *fl = &inet->cork.fl;
        int err = 0;
+       int is_udplite = IS_UDPLITE(sk);
        __wsum csum = 0;
 
        /* Grab the skbuff where UDP header space exists. */
@@ -533,13 +563,13 @@ static int udp_v6_push_pending_frames(struct sock *sk)
        /*
         * Create a UDP header
         */
-       uh = skb->h.uh;
+       uh = udp_hdr(skb);
        uh->source = fl->fl_ip_sport;
        uh->dest = fl->fl_ip_dport;
        uh->len = htons(up->len);
        uh->check = 0;
 
-       if (up->pcflag)
+       if (is_udplite)
                csum = udplite_csum_outgoing(sk, skb);
         else
                csum = udp_csum_outgoing(sk, skb);
@@ -554,6 +584,8 @@ static int udp_v6_push_pending_frames(struct sock *sk)
 out:
        up->len = 0;
        up->pending = 0;
+       if (!err)
+               UDP6_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS, is_udplite);
        return err;
 }
 
@@ -577,7 +609,7 @@ int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
        int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
        int err;
        int connected = 0;
-       int is_udplite = up->pcflag;
+       int is_udplite = IS_UDPLITE(sk);
        int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
 
        /* destination address check */
@@ -609,7 +641,7 @@ int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
                daddr = NULL;
 
        if (daddr) {
-               if (ipv6_addr_type(daddr) == IPV6_ADDR_MAPPED) {
+               if (ipv6_addr_v4mapped(daddr)) {
                        struct sockaddr_in sin;
                        sin.sin_family = AF_INET;
                        sin.sin_port = sin6 ? sin6->sin6_port : inet->dport;
@@ -747,8 +779,12 @@ do_udp_sendmsg:
        if (final_p)
                ipv6_addr_copy(&fl.fl6_dst, final_p);
 
-       if ((err = xfrm_lookup(&dst, &fl, sk, 1)) < 0)
-               goto out;
+       if ((err = __xfrm_lookup(&dst, &fl, sk, XFRM_LOOKUP_WAIT)) < 0) {
+               if (err == -EREMOTE)
+                       err = ip6_dst_blackhole(sk, &dst, &fl);
+               if (err < 0)
+                       goto out;
+       }
 
        if (hlimit < 0) {
                if (ipv6_addr_is_multicast(&fl.fl6_dst))
@@ -756,9 +792,7 @@ do_udp_sendmsg:
                else
                        hlimit = np->hop_limit;
                if (hlimit < 0)
-                       hlimit = dst_metric(dst, RTAX_HOPLIMIT);
-               if (hlimit < 0)
-                       hlimit = ipv6_get_hoplimit(dst->dev);
+                       hlimit = ip6_dst_hoplimit(dst);
        }
 
        if (tclass < 0) {
@@ -818,10 +852,8 @@ do_append_data:
        release_sock(sk);
 out:
        fl6_sock_release(flowlabel);
-       if (!err) {
-               UDP6_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS, is_udplite);
+       if (!err)
                return len;
-       }
        /*
         * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
         * ENOBUFS might not be good (it's not tunable per se), but otherwise
@@ -945,23 +977,25 @@ int udp6_seq_show(struct seq_file *seq, void *v)
        return 0;
 }
 
-static struct file_operations udp6_seq_fops;
 static struct udp_seq_afinfo udp6_seq_afinfo = {
-       .owner          = THIS_MODULE,
        .name           = "udp6",
        .family         = AF_INET6,
        .hashtable      = udp_hash,
-       .seq_show       = udp6_seq_show,
-       .seq_fops       = &udp6_seq_fops,
+       .seq_fops       = {
+               .owner  =       THIS_MODULE,
+       },
+       .seq_ops        = {
+               .show           = udp6_seq_show,
+       },
 };
 
-int __init udp6_proc_init(void)
+int udp6_proc_init(struct net *net)
 {
-       return udp_proc_register(&udp6_seq_afinfo);
+       return udp_proc_register(net, &udp6_seq_afinfo);
 }
 
-void udp6_proc_exit(void) {
-       udp_proc_unregister(&udp6_seq_afinfo);
+void udp6_proc_exit(struct net *net) {
+       udp_proc_unregister(net, &udp6_seq_afinfo);
 }
 #endif /* CONFIG_PROC_FS */
 
@@ -983,7 +1017,12 @@ struct proto udpv6_prot = {
        .hash              = udp_lib_hash,
        .unhash            = udp_lib_unhash,
        .get_port          = udp_v6_get_port,
+       .memory_allocated  = &udp_memory_allocated,
+       .sysctl_mem        = sysctl_udp_mem,
+       .sysctl_wmem       = &sysctl_udp_wmem_min,
+       .sysctl_rmem       = &sysctl_udp_rmem_min,
        .obj_size          = sizeof(struct udp6_sock),
+       .h.udp_hash        = udp_hash,
 #ifdef CONFIG_COMPAT
        .compat_setsockopt = compat_udpv6_setsockopt,
        .compat_getsockopt = compat_udpv6_getsockopt,
@@ -1001,9 +1040,27 @@ static struct inet_protosw udpv6_protosw = {
 };
 
 
-void __init udpv6_init(void)
+int __init udpv6_init(void)
+{
+       int ret;
+
+       ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
+       if (ret)
+               goto out;
+
+       ret = inet6_register_protosw(&udpv6_protosw);
+       if (ret)
+               goto out_udpv6_protocol;
+out:
+       return ret;
+
+out_udpv6_protocol:
+       inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
+       goto out;
+}
+
+void udpv6_exit(void)
 {
-       if (inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP) < 0)
-               printk(KERN_ERR "udpv6_init: Could not register protocol\n");
-       inet6_register_protosw(&udpv6_protosw);
+       inet6_unregister_protosw(&udpv6_protosw);
+       inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
 }