Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * UDP over IPv6 |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3 | * Linux INET6 implementation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * Authors: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 6 | * Pedro Roque <roque@di.fc.ul.pt> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 8 | * Based on linux/ipv4/udp.c |
| 9 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * Fixes: |
| 11 | * Hideaki YOSHIFUJI : sin6_scope_id support |
| 12 | * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which |
| 13 | * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind |
| 14 | * a single port at the same time. |
| 15 | * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data |
| 16 | * YOSHIFUJI Hideaki @USAGI: convert /proc/net/udp6 to seq_file. |
| 17 | * |
| 18 | * This program is free software; you can redistribute it and/or |
| 19 | * modify it under the terms of the GNU General Public License |
| 20 | * as published by the Free Software Foundation; either version |
| 21 | * 2 of the License, or (at your option) any later version. |
| 22 | */ |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/errno.h> |
| 25 | #include <linux/types.h> |
| 26 | #include <linux/socket.h> |
| 27 | #include <linux/sockios.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/net.h> |
| 29 | #include <linux/in6.h> |
| 30 | #include <linux/netdevice.h> |
| 31 | #include <linux/if_arp.h> |
| 32 | #include <linux/ipv6.h> |
| 33 | #include <linux/icmpv6.h> |
| 34 | #include <linux/init.h> |
Herbert Xu | 1781f7f | 2007-12-11 11:30:32 -0800 | [diff] [blame] | 35 | #include <linux/module.h> |
Herbert Xu | 3305b80 | 2005-12-13 23:16:37 -0800 | [diff] [blame] | 36 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <asm/uaccess.h> |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <net/ndisc.h> |
| 40 | #include <net/protocol.h> |
| 41 | #include <net/transp_v6.h> |
| 42 | #include <net/ip6_route.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <net/raw.h> |
Arnaldo Carvalho de Melo | c752f07 | 2005-08-09 20:08:28 -0700 | [diff] [blame] | 44 | #include <net/tcp_states.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <net/ip6_checksum.h> |
| 46 | #include <net/xfrm.h> |
| 47 | |
| 48 | #include <linux/proc_fs.h> |
| 49 | #include <linux/seq_file.h> |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 50 | #include "udp_impl.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Pavel Emelyanov | 6ba5a3c | 2008-03-22 16:51:21 -0700 | [diff] [blame] | 52 | int udp_v6_get_port(struct sock *sk, unsigned short snum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | { |
Pavel Emelyanov | 6ba5a3c | 2008-03-22 16:51:21 -0700 | [diff] [blame] | 54 | return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Pavel Emelyanov | fa4d3c6 | 2008-01-31 05:07:57 -0800 | [diff] [blame] | 57 | static struct sock *__udp6_lib_lookup(struct net *net, |
| 58 | struct in6_addr *saddr, __be16 sport, |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 59 | struct in6_addr *daddr, __be16 dport, |
| 60 | int dif, struct hlist_head udptable[]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
| 62 | struct sock *sk, *result = NULL; |
| 63 | struct hlist_node *node; |
| 64 | unsigned short hnum = ntohs(dport); |
| 65 | int badness = -1; |
| 66 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 67 | read_lock(&udp_hash_lock); |
Pavel Emelyanov | 19c7578 | 2008-06-16 17:12:29 -0700 | [diff] [blame] | 68 | sk_for_each(sk, node, &udptable[udp_hashfn(net, hnum)]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | struct inet_sock *inet = inet_sk(sk); |
| 70 | |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 71 | if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum && |
Pavel Emelyanov | fa4d3c6 | 2008-01-31 05:07:57 -0800 | [diff] [blame] | 72 | sk->sk_family == PF_INET6) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 74 | int score = 0; |
| 75 | if (inet->dport) { |
| 76 | if (inet->dport != sport) |
| 77 | continue; |
| 78 | score++; |
| 79 | } |
| 80 | if (!ipv6_addr_any(&np->rcv_saddr)) { |
| 81 | if (!ipv6_addr_equal(&np->rcv_saddr, daddr)) |
| 82 | continue; |
| 83 | score++; |
| 84 | } |
| 85 | if (!ipv6_addr_any(&np->daddr)) { |
| 86 | if (!ipv6_addr_equal(&np->daddr, saddr)) |
| 87 | continue; |
| 88 | score++; |
| 89 | } |
| 90 | if (sk->sk_bound_dev_if) { |
| 91 | if (sk->sk_bound_dev_if != dif) |
| 92 | continue; |
| 93 | score++; |
| 94 | } |
Stephen Hemminger | add459a | 2007-03-08 20:42:35 -0800 | [diff] [blame] | 95 | if (score == 4) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | result = sk; |
| 97 | break; |
Stephen Hemminger | add459a | 2007-03-08 20:42:35 -0800 | [diff] [blame] | 98 | } else if (score > badness) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | result = sk; |
| 100 | badness = score; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | if (result) |
| 105 | sock_hold(result); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 106 | read_unlock(&udp_hash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | return result; |
| 108 | } |
| 109 | |
KOVACS Krisztian | 607c4aa | 2008-10-07 12:38:32 -0700 | [diff] [blame] | 110 | static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb, |
| 111 | __be16 sport, __be16 dport, |
| 112 | struct hlist_head udptable[]) |
| 113 | { |
KOVACS Krisztian | 2354261 | 2008-10-07 12:41:01 -0700 | [diff] [blame] | 114 | struct sock *sk; |
KOVACS Krisztian | 607c4aa | 2008-10-07 12:38:32 -0700 | [diff] [blame] | 115 | struct ipv6hdr *iph = ipv6_hdr(skb); |
| 116 | |
KOVACS Krisztian | 2354261 | 2008-10-07 12:41:01 -0700 | [diff] [blame] | 117 | if (unlikely(sk = skb_steal_sock(skb))) |
| 118 | return sk; |
| 119 | else |
| 120 | return __udp6_lib_lookup(dev_net(skb->dst->dev), &iph->saddr, sport, |
| 121 | &iph->daddr, dport, inet6_iif(skb), |
| 122 | udptable); |
KOVACS Krisztian | 607c4aa | 2008-10-07 12:38:32 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | * This should be easy, if there is something there we |
| 127 | * return it, otherwise we block. |
| 128 | */ |
| 129 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 130 | int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | struct msghdr *msg, size_t len, |
| 132 | int noblock, int flags, int *addr_len) |
| 133 | { |
| 134 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 135 | struct inet_sock *inet = inet_sk(sk); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 136 | struct sk_buff *skb; |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 137 | unsigned int ulen, copied; |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 138 | int peeked; |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 139 | int err; |
| 140 | int is_udplite = IS_UDPLITE(sk); |
Wei Yongjun | f26ba17 | 2008-11-02 16:11:01 +0000 | [diff] [blame^] | 141 | int is_udp4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 143 | if (addr_len) |
| 144 | *addr_len=sizeof(struct sockaddr_in6); |
| 145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | if (flags & MSG_ERRQUEUE) |
| 147 | return ipv6_recv_error(sk, msg, len); |
| 148 | |
| 149 | try_again: |
Herbert Xu | a59322b | 2007-12-05 01:53:40 -0800 | [diff] [blame] | 150 | skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), |
| 151 | &peeked, &err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | if (!skb) |
| 153 | goto out; |
| 154 | |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 155 | ulen = skb->len - sizeof(struct udphdr); |
| 156 | copied = len; |
| 157 | if (copied > ulen) |
| 158 | copied = ulen; |
| 159 | else if (copied < ulen) |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 160 | msg->msg_flags |= MSG_TRUNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
Wei Yongjun | f26ba17 | 2008-11-02 16:11:01 +0000 | [diff] [blame^] | 162 | is_udp4 = (skb->protocol == htons(ETH_P_IP)); |
| 163 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 164 | /* |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 165 | * If checksum is needed at all, try to do it while copying the |
| 166 | * data. If the data is truncated, or if we only want a partial |
| 167 | * coverage checksum (UDP-Lite), do it before the copy. |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 168 | */ |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 169 | |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 170 | if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) { |
| 171 | if (udp_lib_checksum_complete(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | goto csum_copy_err; |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Herbert Xu | 6047637 | 2007-04-09 11:59:39 -0700 | [diff] [blame] | 175 | if (skb_csum_unnecessary(skb)) |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 176 | err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), |
| 177 | msg->msg_iov, copied ); |
| 178 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov); |
| 180 | if (err == -EINVAL) |
| 181 | goto csum_copy_err; |
| 182 | } |
| 183 | if (err) |
| 184 | goto out_free; |
| 185 | |
Wei Yongjun | f26ba17 | 2008-11-02 16:11:01 +0000 | [diff] [blame^] | 186 | if (!peeked) { |
| 187 | if (is_udp4) |
| 188 | UDP_INC_STATS_USER(sock_net(sk), |
| 189 | UDP_MIB_INDATAGRAMS, is_udplite); |
| 190 | else |
| 191 | UDP6_INC_STATS_USER(sock_net(sk), |
| 192 | UDP_MIB_INDATAGRAMS, is_udplite); |
| 193 | } |
Wang Chen | cb75994 | 2007-12-03 22:33:28 +1100 | [diff] [blame] | 194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | sock_recv_timestamp(msg, sk, skb); |
| 196 | |
| 197 | /* Copy the address. */ |
| 198 | if (msg->msg_name) { |
| 199 | struct sockaddr_in6 *sin6; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | sin6 = (struct sockaddr_in6 *) msg->msg_name; |
| 202 | sin6->sin6_family = AF_INET6; |
Arnaldo Carvalho de Melo | 4bedb45 | 2007-03-13 14:28:48 -0300 | [diff] [blame] | 203 | sin6->sin6_port = udp_hdr(skb)->source; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | sin6->sin6_flowinfo = 0; |
| 205 | sin6->sin6_scope_id = 0; |
| 206 | |
Wei Yongjun | f26ba17 | 2008-11-02 16:11:01 +0000 | [diff] [blame^] | 207 | if (is_udp4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | ipv6_addr_set(&sin6->sin6_addr, 0, 0, |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 209 | htonl(0xffff), ip_hdr(skb)->saddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | else { |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 211 | ipv6_addr_copy(&sin6->sin6_addr, |
| 212 | &ipv6_hdr(skb)->saddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) |
| 214 | sin6->sin6_scope_id = IP6CB(skb)->iif; |
| 215 | } |
| 216 | |
| 217 | } |
Wei Yongjun | f26ba17 | 2008-11-02 16:11:01 +0000 | [diff] [blame^] | 218 | if (is_udp4) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | if (inet->cmsg_flags) |
| 220 | ip_cmsg_recv(msg, skb); |
| 221 | } else { |
| 222 | if (np->rxopt.all) |
| 223 | datagram_recv_ctl(sk, msg, skb); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 224 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
| 226 | err = copied; |
| 227 | if (flags & MSG_TRUNC) |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 228 | err = ulen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | out_free: |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 231 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | skb_free_datagram(sk, skb); |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 233 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | out: |
| 235 | return err; |
| 236 | |
| 237 | csum_copy_err: |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 238 | lock_sock(sk); |
Herbert Xu | 27ab256 | 2007-12-05 01:51:58 -0800 | [diff] [blame] | 239 | if (!skb_kill_datagram(sk, skb, flags)) |
Pavel Emelyanov | 235b9f7 | 2008-07-05 21:19:20 -0700 | [diff] [blame] | 240 | UDP6_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite); |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 241 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
Mitsuru Chinen | 7a0ff71 | 2007-11-05 21:29:17 -0800 | [diff] [blame] | 243 | if (flags & MSG_DONTWAIT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | goto try_again; |
| 246 | } |
| 247 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 248 | void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt, |
| 249 | int type, int code, int offset, __be32 info, |
| 250 | struct hlist_head udptable[] ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | { |
| 252 | struct ipv6_pinfo *np; |
| 253 | struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | struct in6_addr *saddr = &hdr->saddr; |
| 255 | struct in6_addr *daddr = &hdr->daddr; |
| 256 | struct udphdr *uh = (struct udphdr*)(skb->data+offset); |
| 257 | struct sock *sk; |
| 258 | int err; |
| 259 | |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 260 | sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest, |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 261 | saddr, uh->source, inet6_iif(skb), udptable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | if (sk == NULL) |
| 263 | return; |
| 264 | |
| 265 | np = inet6_sk(sk); |
| 266 | |
| 267 | if (!icmpv6_err_convert(type, code, &err) && !np->recverr) |
| 268 | goto out; |
| 269 | |
| 270 | if (sk->sk_state != TCP_ESTABLISHED && !np->recverr) |
| 271 | goto out; |
| 272 | |
| 273 | if (np->recverr) |
| 274 | ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1)); |
| 275 | |
| 276 | sk->sk_err = err; |
| 277 | sk->sk_error_report(sk); |
| 278 | out: |
| 279 | sock_put(sk); |
| 280 | } |
| 281 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 282 | static __inline__ void udpv6_err(struct sk_buff *skb, |
| 283 | struct inet6_skb_parm *opt, int type, |
Al Viro | 8e5200f | 2006-11-20 18:06:37 -0800 | [diff] [blame] | 284 | int code, int offset, __be32 info ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | { |
YOSHIFUJI Hideaki | 77d0d35 | 2008-01-22 17:09:55 +0900 | [diff] [blame] | 286 | __udp6_lib_err(skb, opt, type, code, offset, info, udp_hash); |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb) |
| 290 | { |
| 291 | struct udp_sock *up = udp_sk(sk); |
David S. Miller | a18135e | 2006-08-15 00:00:09 -0700 | [diff] [blame] | 292 | int rc; |
Wang Chen | b2bf1e2 | 2007-12-03 22:34:16 +1100 | [diff] [blame] | 293 | int is_udplite = IS_UDPLITE(sk); |
David S. Miller | a18135e | 2006-08-15 00:00:09 -0700 | [diff] [blame] | 294 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 295 | if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) |
| 296 | goto drop; |
| 297 | |
| 298 | /* |
| 299 | * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c). |
| 300 | */ |
Wang Chen | b2bf1e2 | 2007-12-03 22:34:16 +1100 | [diff] [blame] | 301 | if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 302 | |
| 303 | if (up->pcrlen == 0) { /* full coverage was set */ |
| 304 | LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage" |
| 305 | " %d while full coverage %d requested\n", |
| 306 | UDP_SKB_CB(skb)->cscov, skb->len); |
| 307 | goto drop; |
| 308 | } |
| 309 | if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { |
| 310 | LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d " |
| 311 | "too small, need min %d\n", |
| 312 | UDP_SKB_CB(skb)->cscov, up->pcrlen); |
| 313 | goto drop; |
| 314 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Herbert Xu | 1ab6eb6 | 2007-03-06 20:29:58 -0800 | [diff] [blame] | 317 | if (sk->sk_filter) { |
| 318 | if (udp_lib_checksum_complete(skb)) |
| 319 | goto drop; |
| 320 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
David S. Miller | a18135e | 2006-08-15 00:00:09 -0700 | [diff] [blame] | 322 | if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) { |
| 323 | /* Note that an ENOMEM error is charged twice */ |
Eric Dumazet | cb61cb9 | 2008-06-17 21:04:56 -0700 | [diff] [blame] | 324 | if (rc == -ENOMEM) { |
Pavel Emelyanov | ef28d1a | 2008-07-05 21:19:40 -0700 | [diff] [blame] | 325 | UDP6_INC_STATS_BH(sock_net(sk), |
| 326 | UDP_MIB_RCVBUFERRORS, is_udplite); |
Eric Dumazet | cb61cb9 | 2008-06-17 21:04:56 -0700 | [diff] [blame] | 327 | atomic_inc(&sk->sk_drops); |
| 328 | } |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 329 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
Wang Chen | cb75994 | 2007-12-03 22:33:28 +1100 | [diff] [blame] | 331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | return 0; |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 333 | drop: |
Pavel Emelyanov | ef28d1a | 2008-07-05 21:19:40 -0700 | [diff] [blame] | 334 | UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite); |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 335 | kfree_skb(skb); |
| 336 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Eric Dumazet | 920a461 | 2008-11-01 21:22:23 -0700 | [diff] [blame] | 339 | static struct sock *udp_v6_mcast_next(struct net *net, struct sock *sk, |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 340 | __be16 loc_port, struct in6_addr *loc_addr, |
| 341 | __be16 rmt_port, struct in6_addr *rmt_addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | int dif) |
| 343 | { |
| 344 | struct hlist_node *node; |
| 345 | struct sock *s = sk; |
| 346 | unsigned short num = ntohs(loc_port); |
| 347 | |
| 348 | sk_for_each_from(s, node) { |
| 349 | struct inet_sock *inet = inet_sk(s); |
| 350 | |
Eric Dumazet | 920a461 | 2008-11-01 21:22:23 -0700 | [diff] [blame] | 351 | if (!net_eq(sock_net(s), net)) |
Daniel Lezcano | b8ad0cb | 2008-03-07 11:16:55 -0800 | [diff] [blame] | 352 | continue; |
| 353 | |
Eric Dumazet | 95f30b3 | 2007-02-09 15:44:52 -0800 | [diff] [blame] | 354 | if (s->sk_hash == num && s->sk_family == PF_INET6) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | struct ipv6_pinfo *np = inet6_sk(s); |
| 356 | if (inet->dport) { |
| 357 | if (inet->dport != rmt_port) |
| 358 | continue; |
| 359 | } |
| 360 | if (!ipv6_addr_any(&np->daddr) && |
| 361 | !ipv6_addr_equal(&np->daddr, rmt_addr)) |
| 362 | continue; |
| 363 | |
| 364 | if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif) |
| 365 | continue; |
| 366 | |
| 367 | if (!ipv6_addr_any(&np->rcv_saddr)) { |
David L Stevens | 40796c5 | 2005-09-14 21:10:20 -0700 | [diff] [blame] | 368 | if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr)) |
| 369 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | } |
Stephen Hemminger | add459a | 2007-03-08 20:42:35 -0800 | [diff] [blame] | 371 | if (!inet6_mc_check(s, loc_addr, rmt_addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | continue; |
| 373 | return s; |
| 374 | } |
| 375 | } |
| 376 | return NULL; |
| 377 | } |
| 378 | |
| 379 | /* |
| 380 | * Note: called only from the BH handler context, |
| 381 | * so we don't need to lock the hashes. |
| 382 | */ |
Pavel Emelyanov | e316349 | 2008-06-16 17:12:11 -0700 | [diff] [blame] | 383 | static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb, |
| 384 | struct in6_addr *saddr, struct in6_addr *daddr, |
| 385 | struct hlist_head udptable[]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | { |
| 387 | struct sock *sk, *sk2; |
Arnaldo Carvalho de Melo | 4bedb45 | 2007-03-13 14:28:48 -0300 | [diff] [blame] | 388 | const struct udphdr *uh = udp_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | int dif; |
| 390 | |
| 391 | read_lock(&udp_hash_lock); |
Pavel Emelyanov | 19c7578 | 2008-06-16 17:12:29 -0700 | [diff] [blame] | 392 | sk = sk_head(&udptable[udp_hashfn(net, ntohs(uh->dest))]); |
YOSHIFUJI Hideaki | f2776ff | 2006-11-21 17:41:56 -0800 | [diff] [blame] | 393 | dif = inet6_iif(skb); |
Eric Dumazet | 920a461 | 2008-11-01 21:22:23 -0700 | [diff] [blame] | 394 | sk = udp_v6_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | if (!sk) { |
| 396 | kfree_skb(skb); |
| 397 | goto out; |
| 398 | } |
| 399 | |
| 400 | sk2 = sk; |
Eric Dumazet | 920a461 | 2008-11-01 21:22:23 -0700 | [diff] [blame] | 401 | while ((sk2 = udp_v6_mcast_next(net, sk_next(sk2), uh->dest, daddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | uh->source, saddr, dif))) { |
| 403 | struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC); |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 404 | if (buff) { |
Herbert Xu | d97106e | 2008-08-09 00:35:05 -0700 | [diff] [blame] | 405 | bh_lock_sock(sk2); |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 406 | if (!sock_owned_by_user(sk2)) |
| 407 | udpv6_queue_rcv_skb(sk2, buff); |
| 408 | else |
| 409 | sk_add_backlog(sk2, buff); |
| 410 | bh_unlock_sock(sk2); |
| 411 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | } |
Herbert Xu | d97106e | 2008-08-09 00:35:05 -0700 | [diff] [blame] | 413 | bh_lock_sock(sk); |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 414 | if (!sock_owned_by_user(sk)) |
| 415 | udpv6_queue_rcv_skb(sk, skb); |
| 416 | else |
| 417 | sk_add_backlog(sk, skb); |
| 418 | bh_unlock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | out: |
| 420 | read_unlock(&udp_hash_lock); |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 421 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 424 | static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh, |
| 425 | int proto) |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 426 | { |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 427 | int err; |
| 428 | |
| 429 | UDP_SKB_CB(skb)->partial_cov = 0; |
| 430 | UDP_SKB_CB(skb)->cscov = skb->len; |
| 431 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 432 | if (proto == IPPROTO_UDPLITE) { |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 433 | err = udplite_checksum_init(skb, uh); |
| 434 | if (err) |
| 435 | return err; |
| 436 | } |
| 437 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 438 | if (uh->check == 0) { |
| 439 | /* RFC 2460 section 8.1 says that we SHOULD log |
| 440 | this error. Well, it is reasonable. |
| 441 | */ |
| 442 | LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n"); |
| 443 | return 1; |
| 444 | } |
| 445 | if (skb->ip_summed == CHECKSUM_COMPLETE && |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 446 | !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr, |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 447 | skb->len, proto, skb->csum)) |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 448 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 449 | |
Herbert Xu | 6047637 | 2007-04-09 11:59:39 -0700 | [diff] [blame] | 450 | if (!skb_csum_unnecessary(skb)) |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 451 | skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr, |
| 452 | &ipv6_hdr(skb)->daddr, |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 453 | skb->len, proto, 0)); |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 454 | |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 455 | return 0; |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 456 | } |
| 457 | |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 458 | int __udp6_lib_rcv(struct sk_buff *skb, struct hlist_head udptable[], |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 459 | int proto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | struct sock *sk; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 462 | struct udphdr *uh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | struct net_device *dev = skb->dev; |
| 464 | struct in6_addr *saddr, *daddr; |
| 465 | u32 ulen = 0; |
Pavel Emelyanov | ef28d1a | 2008-07-05 21:19:40 -0700 | [diff] [blame] | 466 | struct net *net = dev_net(skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | |
| 468 | if (!pskb_may_pull(skb, sizeof(struct udphdr))) |
| 469 | goto short_packet; |
| 470 | |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 471 | saddr = &ipv6_hdr(skb)->saddr; |
| 472 | daddr = &ipv6_hdr(skb)->daddr; |
Arnaldo Carvalho de Melo | 4bedb45 | 2007-03-13 14:28:48 -0300 | [diff] [blame] | 473 | uh = udp_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
| 475 | ulen = ntohs(uh->len); |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 476 | if (ulen > skb->len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | goto short_packet; |
| 478 | |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 479 | if (proto == IPPROTO_UDP) { |
| 480 | /* UDP validates ulen. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 482 | /* Check for jumbo payload */ |
| 483 | if (ulen == 0) |
| 484 | ulen = skb->len; |
| 485 | |
| 486 | if (ulen < sizeof(*uh)) |
| 487 | goto short_packet; |
| 488 | |
| 489 | if (ulen < skb->len) { |
| 490 | if (pskb_trim_rcsum(skb, ulen)) |
| 491 | goto short_packet; |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 492 | saddr = &ipv6_hdr(skb)->saddr; |
| 493 | daddr = &ipv6_hdr(skb)->daddr; |
Arnaldo Carvalho de Melo | 4bedb45 | 2007-03-13 14:28:48 -0300 | [diff] [blame] | 494 | uh = udp_hdr(skb); |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 495 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 498 | if (udp6_csum_init(skb, uh, proto)) |
| 499 | goto discard; |
| 500 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 501 | /* |
| 502 | * Multicast receive code |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | */ |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 504 | if (ipv6_addr_is_multicast(daddr)) |
Pavel Emelyanov | e316349 | 2008-06-16 17:12:11 -0700 | [diff] [blame] | 505 | return __udp6_lib_mcast_deliver(net, skb, |
| 506 | saddr, daddr, udptable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
| 508 | /* Unicast */ |
| 509 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 510 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | * check socket cache ... must talk to Alan about his plans |
| 512 | * for sock caches... i'll skip this for now. |
| 513 | */ |
KOVACS Krisztian | 607c4aa | 2008-10-07 12:38:32 -0700 | [diff] [blame] | 514 | sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | |
| 516 | if (sk == NULL) { |
| 517 | if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) |
| 518 | goto discard; |
| 519 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 520 | if (udp_lib_checksum_complete(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | goto discard; |
Pavel Emelyanov | ef28d1a | 2008-07-05 21:19:40 -0700 | [diff] [blame] | 522 | UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS, |
| 523 | proto == IPPROTO_UDPLITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | |
| 525 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev); |
| 526 | |
| 527 | kfree_skb(skb); |
Stephen Hemminger | add459a | 2007-03-08 20:42:35 -0800 | [diff] [blame] | 528 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | } |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | /* deliver */ |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 532 | |
Herbert Xu | d97106e | 2008-08-09 00:35:05 -0700 | [diff] [blame] | 533 | bh_lock_sock(sk); |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 534 | if (!sock_owned_by_user(sk)) |
| 535 | udpv6_queue_rcv_skb(sk, skb); |
| 536 | else |
| 537 | sk_add_backlog(sk, skb); |
| 538 | bh_unlock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | sock_put(sk); |
Stephen Hemminger | add459a | 2007-03-08 20:42:35 -0800 | [diff] [blame] | 540 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 542 | short_packet: |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 543 | LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: %d/%u\n", |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 544 | proto == IPPROTO_UDPLITE ? "-Lite" : "", |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 545 | ulen, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
| 547 | discard: |
Pavel Emelyanov | ef28d1a | 2008-07-05 21:19:40 -0700 | [diff] [blame] | 548 | UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | kfree_skb(skb); |
Stephen Hemminger | add459a | 2007-03-08 20:42:35 -0800 | [diff] [blame] | 550 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | } |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 552 | |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 553 | static __inline__ int udpv6_rcv(struct sk_buff *skb) |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 554 | { |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 555 | return __udp6_lib_rcv(skb, udp_hash, IPPROTO_UDP); |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 556 | } |
| 557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | /* |
| 559 | * Throw away all pending data and cancel the corking. Socket is locked. |
| 560 | */ |
| 561 | static void udp_v6_flush_pending_frames(struct sock *sk) |
| 562 | { |
| 563 | struct udp_sock *up = udp_sk(sk); |
| 564 | |
Denis V. Lunev | 36d926b | 2008-06-04 15:49:07 +0400 | [diff] [blame] | 565 | if (up->pending == AF_INET) |
| 566 | udp_flush_pending_frames(sk); |
| 567 | else if (up->pending) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | up->len = 0; |
| 569 | up->pending = 0; |
| 570 | ip6_flush_pending_frames(sk); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 571 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /* |
| 575 | * Sending |
| 576 | */ |
| 577 | |
Gerrit Renker | 4c0a6cb | 2006-11-27 09:29:59 -0800 | [diff] [blame] | 578 | static int udp_v6_push_pending_frames(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | { |
| 580 | struct sk_buff *skb; |
| 581 | struct udphdr *uh; |
Gerrit Renker | 4c0a6cb | 2006-11-27 09:29:59 -0800 | [diff] [blame] | 582 | struct udp_sock *up = udp_sk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | struct inet_sock *inet = inet_sk(sk); |
| 584 | struct flowi *fl = &inet->cork.fl; |
| 585 | int err = 0; |
Wang Chen | b2bf1e2 | 2007-12-03 22:34:16 +1100 | [diff] [blame] | 586 | int is_udplite = IS_UDPLITE(sk); |
Al Viro | 868c86b | 2006-11-14 21:35:48 -0800 | [diff] [blame] | 587 | __wsum csum = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | |
| 589 | /* Grab the skbuff where UDP header space exists. */ |
| 590 | if ((skb = skb_peek(&sk->sk_write_queue)) == NULL) |
| 591 | goto out; |
| 592 | |
| 593 | /* |
| 594 | * Create a UDP header |
| 595 | */ |
Arnaldo Carvalho de Melo | 4bedb45 | 2007-03-13 14:28:48 -0300 | [diff] [blame] | 596 | uh = udp_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | uh->source = fl->fl_ip_sport; |
| 598 | uh->dest = fl->fl_ip_dport; |
| 599 | uh->len = htons(up->len); |
| 600 | uh->check = 0; |
| 601 | |
Wang Chen | b2bf1e2 | 2007-12-03 22:34:16 +1100 | [diff] [blame] | 602 | if (is_udplite) |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 603 | csum = udplite_csum_outgoing(sk, skb); |
| 604 | else |
| 605 | csum = udp_csum_outgoing(sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 607 | /* add protocol-dependent pseudo-header */ |
| 608 | uh->check = csum_ipv6_magic(&fl->fl6_src, &fl->fl6_dst, |
| 609 | up->len, fl->proto, csum ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | if (uh->check == 0) |
Al Viro | f6ab028 | 2006-11-16 02:36:50 -0800 | [diff] [blame] | 611 | uh->check = CSUM_MANGLED_0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | err = ip6_push_pending_frames(sk); |
| 614 | out: |
| 615 | up->len = 0; |
| 616 | up->pending = 0; |
YOSHIFUJI Hideaki | cd562c9 | 2007-09-14 17:15:01 -0700 | [diff] [blame] | 617 | if (!err) |
Pavel Emelyanov | 235b9f7 | 2008-07-05 21:19:20 -0700 | [diff] [blame] | 618 | UDP6_INC_STATS_USER(sock_net(sk), |
| 619 | UDP_MIB_OUTDATAGRAMS, is_udplite); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | return err; |
| 621 | } |
| 622 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 623 | int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | struct msghdr *msg, size_t len) |
| 625 | { |
| 626 | struct ipv6_txoptions opt_space; |
| 627 | struct udp_sock *up = udp_sk(sk); |
| 628 | struct inet_sock *inet = inet_sk(sk); |
| 629 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 630 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name; |
| 631 | struct in6_addr *daddr, *final_p = NULL, final; |
| 632 | struct ipv6_txoptions *opt = NULL; |
| 633 | struct ip6_flowlabel *flowlabel = NULL; |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 634 | struct flowi fl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | struct dst_entry *dst; |
| 636 | int addr_len = msg->msg_namelen; |
| 637 | int ulen = len; |
| 638 | int hlimit = -1; |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 639 | int tclass = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; |
| 641 | int err; |
Mitsuru KANDA | 987905d | 2005-09-18 00:30:08 -0700 | [diff] [blame] | 642 | int connected = 0; |
Wang Chen | b2bf1e2 | 2007-12-03 22:34:16 +1100 | [diff] [blame] | 643 | int is_udplite = IS_UDPLITE(sk); |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 644 | int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
| 646 | /* destination address check */ |
| 647 | if (sin6) { |
| 648 | if (addr_len < offsetof(struct sockaddr, sa_data)) |
| 649 | return -EINVAL; |
| 650 | |
| 651 | switch (sin6->sin6_family) { |
| 652 | case AF_INET6: |
| 653 | if (addr_len < SIN6_LEN_RFC2133) |
| 654 | return -EINVAL; |
| 655 | daddr = &sin6->sin6_addr; |
| 656 | break; |
| 657 | case AF_INET: |
| 658 | goto do_udp_sendmsg; |
| 659 | case AF_UNSPEC: |
| 660 | msg->msg_name = sin6 = NULL; |
| 661 | msg->msg_namelen = addr_len = 0; |
| 662 | daddr = NULL; |
| 663 | break; |
| 664 | default: |
| 665 | return -EINVAL; |
| 666 | } |
| 667 | } else if (!up->pending) { |
| 668 | if (sk->sk_state != TCP_ESTABLISHED) |
| 669 | return -EDESTADDRREQ; |
| 670 | daddr = &np->daddr; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 671 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | daddr = NULL; |
| 673 | |
| 674 | if (daddr) { |
Brian Haley | e773e4f | 2007-08-24 23:16:08 -0700 | [diff] [blame] | 675 | if (ipv6_addr_v4mapped(daddr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | struct sockaddr_in sin; |
| 677 | sin.sin_family = AF_INET; |
| 678 | sin.sin_port = sin6 ? sin6->sin6_port : inet->dport; |
| 679 | sin.sin_addr.s_addr = daddr->s6_addr32[3]; |
| 680 | msg->msg_name = &sin; |
| 681 | msg->msg_namelen = sizeof(sin); |
| 682 | do_udp_sendmsg: |
| 683 | if (__ipv6_only_sock(sk)) |
| 684 | return -ENETUNREACH; |
| 685 | return udp_sendmsg(iocb, sk, msg, len); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | if (up->pending == AF_INET) |
| 690 | return udp_sendmsg(iocb, sk, msg, len); |
| 691 | |
| 692 | /* Rough check on arithmetic overflow, |
YOSHIFUJI Hideaki | b59e139 | 2007-03-30 14:45:35 -0700 | [diff] [blame] | 693 | better check is made in ip6_append_data(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | */ |
| 695 | if (len > INT_MAX - sizeof(struct udphdr)) |
| 696 | return -EMSGSIZE; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 697 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | if (up->pending) { |
| 699 | /* |
| 700 | * There are pending frames. |
| 701 | * The socket lock must be held while it's corked. |
| 702 | */ |
| 703 | lock_sock(sk); |
| 704 | if (likely(up->pending)) { |
| 705 | if (unlikely(up->pending != AF_INET6)) { |
| 706 | release_sock(sk); |
| 707 | return -EAFNOSUPPORT; |
| 708 | } |
| 709 | dst = NULL; |
| 710 | goto do_append_data; |
| 711 | } |
| 712 | release_sock(sk); |
| 713 | } |
| 714 | ulen += sizeof(struct udphdr); |
| 715 | |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 716 | memset(&fl, 0, sizeof(fl)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | |
| 718 | if (sin6) { |
| 719 | if (sin6->sin6_port == 0) |
| 720 | return -EINVAL; |
| 721 | |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 722 | fl.fl_ip_dport = sin6->sin6_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | daddr = &sin6->sin6_addr; |
| 724 | |
| 725 | if (np->sndflow) { |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 726 | fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK; |
| 727 | if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) { |
| 728 | flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | if (flowlabel == NULL) |
| 730 | return -EINVAL; |
| 731 | daddr = &flowlabel->dst; |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | /* |
| 736 | * Otherwise it will be difficult to maintain |
| 737 | * sk->sk_dst_cache. |
| 738 | */ |
| 739 | if (sk->sk_state == TCP_ESTABLISHED && |
| 740 | ipv6_addr_equal(daddr, &np->daddr)) |
| 741 | daddr = &np->daddr; |
| 742 | |
| 743 | if (addr_len >= sizeof(struct sockaddr_in6) && |
| 744 | sin6->sin6_scope_id && |
| 745 | ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL) |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 746 | fl.oif = sin6->sin6_scope_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | } else { |
| 748 | if (sk->sk_state != TCP_ESTABLISHED) |
| 749 | return -EDESTADDRREQ; |
| 750 | |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 751 | fl.fl_ip_dport = inet->dport; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | daddr = &np->daddr; |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 753 | fl.fl6_flowlabel = np->flow_label; |
Mitsuru KANDA | 987905d | 2005-09-18 00:30:08 -0700 | [diff] [blame] | 754 | connected = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | } |
| 756 | |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 757 | if (!fl.oif) |
| 758 | fl.oif = sk->sk_bound_dev_if; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | |
| 760 | if (msg->msg_controllen) { |
| 761 | opt = &opt_space; |
| 762 | memset(opt, 0, sizeof(struct ipv6_txoptions)); |
| 763 | opt->tot_len = sizeof(*opt); |
| 764 | |
YOSHIFUJI Hideaki | 91e1908 | 2008-06-04 13:02:49 +0900 | [diff] [blame] | 765 | err = datagram_send_ctl(sock_net(sk), msg, &fl, opt, &hlimit, &tclass); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | if (err < 0) { |
| 767 | fl6_sock_release(flowlabel); |
| 768 | return err; |
| 769 | } |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 770 | if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) { |
| 771 | flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | if (flowlabel == NULL) |
| 773 | return -EINVAL; |
| 774 | } |
| 775 | if (!(opt->opt_nflen|opt->opt_flen)) |
| 776 | opt = NULL; |
Mitsuru KANDA | 987905d | 2005-09-18 00:30:08 -0700 | [diff] [blame] | 777 | connected = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | } |
| 779 | if (opt == NULL) |
| 780 | opt = np->opt; |
YOSHIFUJI Hideaki | df9890c | 2005-11-20 12:23:18 +0900 | [diff] [blame] | 781 | if (flowlabel) |
| 782 | opt = fl6_merge_options(&opt_space, flowlabel, opt); |
| 783 | opt = ipv6_fixup_options(&opt_space, opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 785 | fl.proto = sk->sk_protocol; |
Brian Haley | 876c7f4 | 2008-04-11 00:38:24 -0400 | [diff] [blame] | 786 | if (!ipv6_addr_any(daddr)) |
| 787 | ipv6_addr_copy(&fl.fl6_dst, daddr); |
| 788 | else |
| 789 | fl.fl6_dst.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */ |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 790 | if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr)) |
| 791 | ipv6_addr_copy(&fl.fl6_src, &np->saddr); |
| 792 | fl.fl_ip_sport = inet->sport; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 793 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | /* merge ip6_build_xmit from ip6_output */ |
| 795 | if (opt && opt->srcrt) { |
| 796 | struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt; |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 797 | ipv6_addr_copy(&final, &fl.fl6_dst); |
| 798 | ipv6_addr_copy(&fl.fl6_dst, rt0->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | final_p = &final; |
Mitsuru KANDA | 987905d | 2005-09-18 00:30:08 -0700 | [diff] [blame] | 800 | connected = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 803 | if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst)) { |
| 804 | fl.oif = np->mcast_oif; |
Mitsuru KANDA | 987905d | 2005-09-18 00:30:08 -0700 | [diff] [blame] | 805 | connected = 0; |
| 806 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 808 | security_sk_classify_flow(sk, &fl); |
Venkat Yekkirala | beb8d13 | 2006-08-04 23:12:42 -0700 | [diff] [blame] | 809 | |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 810 | err = ip6_sk_dst_lookup(sk, &dst, &fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | if (err) |
| 812 | goto out; |
| 813 | if (final_p) |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 814 | ipv6_addr_copy(&fl.fl6_dst, final_p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | |
Herbert Xu | bb72845 | 2007-12-12 18:48:58 -0800 | [diff] [blame] | 816 | if ((err = __xfrm_lookup(&dst, &fl, sk, XFRM_LOOKUP_WAIT)) < 0) { |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 817 | if (err == -EREMOTE) |
| 818 | err = ip6_dst_blackhole(sk, &dst, &fl); |
| 819 | if (err < 0) |
| 820 | goto out; |
| 821 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | |
| 823 | if (hlimit < 0) { |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 824 | if (ipv6_addr_is_multicast(&fl.fl6_dst)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | hlimit = np->mcast_hops; |
| 826 | else |
| 827 | hlimit = np->hop_limit; |
| 828 | if (hlimit < 0) |
YOSHIFUJI Hideaki | 6b75d09 | 2008-03-10 06:00:30 -0400 | [diff] [blame] | 829 | hlimit = ip6_dst_hoplimit(dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | } |
| 831 | |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 832 | if (tclass < 0) { |
| 833 | tclass = np->tclass; |
| 834 | if (tclass < 0) |
| 835 | tclass = 0; |
| 836 | } |
| 837 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | if (msg->msg_flags&MSG_CONFIRM) |
| 839 | goto do_confirm; |
| 840 | back_from_confirm: |
| 841 | |
| 842 | lock_sock(sk); |
| 843 | if (unlikely(up->pending)) { |
| 844 | /* The socket is already corked while preparing it. */ |
| 845 | /* ... which is an evident application bug. --ANK */ |
| 846 | release_sock(sk); |
| 847 | |
Patrick McHardy | 64ce207 | 2005-08-09 20:50:53 -0700 | [diff] [blame] | 848 | LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | err = -EINVAL; |
| 850 | goto out; |
| 851 | } |
| 852 | |
| 853 | up->pending = AF_INET6; |
| 854 | |
| 855 | do_append_data: |
| 856 | up->len += ulen; |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 857 | getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; |
| 858 | err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen, |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 859 | sizeof(struct udphdr), hlimit, tclass, opt, &fl, |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 860 | (struct rt6_info*)dst, |
| 861 | corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | if (err) |
| 863 | udp_v6_flush_pending_frames(sk); |
| 864 | else if (!corkreq) |
Gerrit Renker | 4c0a6cb | 2006-11-27 09:29:59 -0800 | [diff] [blame] | 865 | err = udp_v6_push_pending_frames(sk); |
Herbert Xu | 1e0c14f | 2006-10-03 14:35:49 -0700 | [diff] [blame] | 866 | else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) |
| 867 | up->pending = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | |
David S. Miller | a5e7c21 | 2005-10-03 14:21:58 -0700 | [diff] [blame] | 869 | if (dst) { |
| 870 | if (connected) { |
| 871 | ip6_dst_store(sk, dst, |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 872 | ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ? |
YOSHIFUJI Hideaki | 8e1ef0a | 2006-08-29 17:15:09 -0700 | [diff] [blame] | 873 | &np->daddr : NULL, |
| 874 | #ifdef CONFIG_IPV6_SUBTREES |
Herbert Xu | 132a55f | 2006-10-03 14:34:00 -0700 | [diff] [blame] | 875 | ipv6_addr_equal(&fl.fl6_src, &np->saddr) ? |
YOSHIFUJI Hideaki | 8e1ef0a | 2006-08-29 17:15:09 -0700 | [diff] [blame] | 876 | &np->saddr : |
| 877 | #endif |
| 878 | NULL); |
David S. Miller | a5e7c21 | 2005-10-03 14:21:58 -0700 | [diff] [blame] | 879 | } else { |
| 880 | dst_release(dst); |
| 881 | } |
YOSHIFUJI Hideaki | a3c9608 | 2008-06-04 01:30:25 +0900 | [diff] [blame] | 882 | dst = NULL; |
David S. Miller | a5e7c21 | 2005-10-03 14:21:58 -0700 | [diff] [blame] | 883 | } |
| 884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | if (err > 0) |
| 886 | err = np->recverr ? net_xmit_errno(err) : 0; |
| 887 | release_sock(sk); |
| 888 | out: |
YOSHIFUJI Hideaki | a3c9608 | 2008-06-04 01:30:25 +0900 | [diff] [blame] | 889 | dst_release(dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | fl6_sock_release(flowlabel); |
YOSHIFUJI Hideaki | cd562c9 | 2007-09-14 17:15:01 -0700 | [diff] [blame] | 891 | if (!err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | return len; |
David S. Miller | a18135e | 2006-08-15 00:00:09 -0700 | [diff] [blame] | 893 | /* |
| 894 | * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting |
| 895 | * ENOBUFS might not be good (it's not tunable per se), but otherwise |
| 896 | * we don't have a good statistic (IpOutDiscards but it can be too many |
| 897 | * things). We could add another new stat but at least for now that |
| 898 | * seems like overkill. |
| 899 | */ |
| 900 | if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { |
Pavel Emelyanov | 235b9f7 | 2008-07-05 21:19:20 -0700 | [diff] [blame] | 901 | UDP6_INC_STATS_USER(sock_net(sk), |
| 902 | UDP_MIB_SNDBUFERRORS, is_udplite); |
David S. Miller | a18135e | 2006-08-15 00:00:09 -0700 | [diff] [blame] | 903 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | return err; |
| 905 | |
| 906 | do_confirm: |
| 907 | dst_confirm(dst); |
| 908 | if (!(msg->msg_flags&MSG_PROBE) || len) |
| 909 | goto back_from_confirm; |
| 910 | err = 0; |
| 911 | goto out; |
| 912 | } |
| 913 | |
Brian Haley | 7d06b2e | 2008-06-14 17:04:49 -0700 | [diff] [blame] | 914 | void udpv6_destroy_sock(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | { |
| 916 | lock_sock(sk); |
| 917 | udp_v6_flush_pending_frames(sk); |
| 918 | release_sock(sk); |
| 919 | |
| 920 | inet6_destroy_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | /* |
| 924 | * Socket option code for UDP |
| 925 | */ |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 926 | int udpv6_setsockopt(struct sock *sk, int level, int optname, |
| 927 | char __user *optval, int optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 928 | { |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 929 | if (level == SOL_UDP || level == SOL_UDPLITE) |
Gerrit Renker | 4c0a6cb | 2006-11-27 09:29:59 -0800 | [diff] [blame] | 930 | return udp_lib_setsockopt(sk, level, optname, optval, optlen, |
| 931 | udp_v6_push_pending_frames); |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 932 | return ipv6_setsockopt(sk, level, optname, optval, optlen); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | #ifdef CONFIG_COMPAT |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 936 | int compat_udpv6_setsockopt(struct sock *sk, int level, int optname, |
| 937 | char __user *optval, int optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 938 | { |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 939 | if (level == SOL_UDP || level == SOL_UDPLITE) |
Gerrit Renker | 4c0a6cb | 2006-11-27 09:29:59 -0800 | [diff] [blame] | 940 | return udp_lib_setsockopt(sk, level, optname, optval, optlen, |
| 941 | udp_v6_push_pending_frames); |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 942 | return compat_ipv6_setsockopt(sk, level, optname, optval, optlen); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 943 | } |
| 944 | #endif |
| 945 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 946 | int udpv6_getsockopt(struct sock *sk, int level, int optname, |
| 947 | char __user *optval, int __user *optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 948 | { |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 949 | if (level == SOL_UDP || level == SOL_UDPLITE) |
Gerrit Renker | 4c0a6cb | 2006-11-27 09:29:59 -0800 | [diff] [blame] | 950 | return udp_lib_getsockopt(sk, level, optname, optval, optlen); |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 951 | return ipv6_getsockopt(sk, level, optname, optval, optlen); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | #ifdef CONFIG_COMPAT |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 955 | int compat_udpv6_getsockopt(struct sock *sk, int level, int optname, |
| 956 | char __user *optval, int __user *optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 957 | { |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 958 | if (level == SOL_UDP || level == SOL_UDPLITE) |
Gerrit Renker | 4c0a6cb | 2006-11-27 09:29:59 -0800 | [diff] [blame] | 959 | return udp_lib_getsockopt(sk, level, optname, optval, optlen); |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 960 | return compat_ipv6_getsockopt(sk, level, optname, optval, optlen); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 961 | } |
| 962 | #endif |
| 963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | static struct inet6_protocol udpv6_protocol = { |
| 965 | .handler = udpv6_rcv, |
| 966 | .err_handler = udpv6_err, |
| 967 | .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, |
| 968 | }; |
| 969 | |
| 970 | /* ------------------------------------------------------------------------ */ |
| 971 | #ifdef CONFIG_PROC_FS |
| 972 | |
| 973 | static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket) |
| 974 | { |
| 975 | struct inet_sock *inet = inet_sk(sp); |
| 976 | struct ipv6_pinfo *np = inet6_sk(sp); |
| 977 | struct in6_addr *dest, *src; |
| 978 | __u16 destp, srcp; |
| 979 | |
| 980 | dest = &np->daddr; |
| 981 | src = &np->rcv_saddr; |
| 982 | destp = ntohs(inet->dport); |
| 983 | srcp = ntohs(inet->sport); |
| 984 | seq_printf(seq, |
| 985 | "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X " |
Eric Dumazet | cb61cb9 | 2008-06-17 21:04:56 -0700 | [diff] [blame] | 986 | "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | bucket, |
| 988 | src->s6_addr32[0], src->s6_addr32[1], |
| 989 | src->s6_addr32[2], src->s6_addr32[3], srcp, |
| 990 | dest->s6_addr32[0], dest->s6_addr32[1], |
| 991 | dest->s6_addr32[2], dest->s6_addr32[3], destp, |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 992 | sp->sk_state, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | atomic_read(&sp->sk_wmem_alloc), |
| 994 | atomic_read(&sp->sk_rmem_alloc), |
| 995 | 0, 0L, 0, |
| 996 | sock_i_uid(sp), 0, |
| 997 | sock_i_ino(sp), |
Eric Dumazet | cb61cb9 | 2008-06-17 21:04:56 -0700 | [diff] [blame] | 998 | atomic_read(&sp->sk_refcnt), sp, |
| 999 | atomic_read(&sp->sk_drops)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1002 | int udp6_seq_show(struct seq_file *seq, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | { |
| 1004 | if (v == SEQ_START_TOKEN) |
| 1005 | seq_printf(seq, |
| 1006 | " sl " |
| 1007 | "local_address " |
| 1008 | "remote_address " |
| 1009 | "st tx_queue rx_queue tr tm->when retrnsmt" |
Eric Dumazet | cb61cb9 | 2008-06-17 21:04:56 -0700 | [diff] [blame] | 1010 | " uid timeout inode ref pointer drops\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | else |
| 1012 | udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket); |
| 1013 | return 0; |
| 1014 | } |
| 1015 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | static struct udp_seq_afinfo udp6_seq_afinfo = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | .name = "udp6", |
| 1018 | .family = AF_INET6, |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1019 | .hashtable = udp_hash, |
Denis V. Lunev | 4ad96d3 | 2008-03-28 18:25:53 -0700 | [diff] [blame] | 1020 | .seq_fops = { |
| 1021 | .owner = THIS_MODULE, |
| 1022 | }, |
Denis V. Lunev | dda6192 | 2008-03-28 18:24:26 -0700 | [diff] [blame] | 1023 | .seq_ops = { |
| 1024 | .show = udp6_seq_show, |
| 1025 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | }; |
| 1027 | |
Daniel Lezcano | 0c96d8c | 2008-03-21 04:14:17 -0700 | [diff] [blame] | 1028 | int udp6_proc_init(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | { |
Daniel Lezcano | 0c96d8c | 2008-03-21 04:14:17 -0700 | [diff] [blame] | 1030 | return udp_proc_register(net, &udp6_seq_afinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
Daniel Lezcano | 0c96d8c | 2008-03-21 04:14:17 -0700 | [diff] [blame] | 1033 | void udp6_proc_exit(struct net *net) { |
| 1034 | udp_proc_unregister(net, &udp6_seq_afinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | } |
| 1036 | #endif /* CONFIG_PROC_FS */ |
| 1037 | |
| 1038 | /* ------------------------------------------------------------------------ */ |
| 1039 | |
| 1040 | struct proto udpv6_prot = { |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1041 | .name = "UDPv6", |
| 1042 | .owner = THIS_MODULE, |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1043 | .close = udp_lib_close, |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1044 | .connect = ip6_datagram_connect, |
| 1045 | .disconnect = udp_disconnect, |
| 1046 | .ioctl = udp_ioctl, |
| 1047 | .destroy = udpv6_destroy_sock, |
| 1048 | .setsockopt = udpv6_setsockopt, |
| 1049 | .getsockopt = udpv6_getsockopt, |
| 1050 | .sendmsg = udpv6_sendmsg, |
| 1051 | .recvmsg = udpv6_recvmsg, |
| 1052 | .backlog_rcv = udpv6_queue_rcv_skb, |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1053 | .hash = udp_lib_hash, |
| 1054 | .unhash = udp_lib_unhash, |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1055 | .get_port = udp_v6_get_port, |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 1056 | .memory_allocated = &udp_memory_allocated, |
| 1057 | .sysctl_mem = sysctl_udp_mem, |
| 1058 | .sysctl_wmem = &sysctl_udp_wmem_min, |
| 1059 | .sysctl_rmem = &sysctl_udp_rmem_min, |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1060 | .obj_size = sizeof(struct udp6_sock), |
Pavel Emelyanov | 6ba5a3c | 2008-03-22 16:51:21 -0700 | [diff] [blame] | 1061 | .h.udp_hash = udp_hash, |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1062 | #ifdef CONFIG_COMPAT |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1063 | .compat_setsockopt = compat_udpv6_setsockopt, |
| 1064 | .compat_getsockopt = compat_udpv6_getsockopt, |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1065 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | }; |
| 1067 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | static struct inet_protosw udpv6_protosw = { |
| 1069 | .type = SOCK_DGRAM, |
| 1070 | .protocol = IPPROTO_UDP, |
| 1071 | .prot = &udpv6_prot, |
| 1072 | .ops = &inet6_dgram_ops, |
| 1073 | .capability =-1, |
| 1074 | .no_check = UDP_CSUM_DEFAULT, |
| 1075 | .flags = INET_PROTOSW_PERMANENT, |
| 1076 | }; |
| 1077 | |
| 1078 | |
Daniel Lezcano | 7f4e486 | 2007-12-11 02:25:35 -0800 | [diff] [blame] | 1079 | int __init udpv6_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | { |
Daniel Lezcano | 7f4e486 | 2007-12-11 02:25:35 -0800 | [diff] [blame] | 1081 | int ret; |
| 1082 | |
| 1083 | ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP); |
| 1084 | if (ret) |
| 1085 | goto out; |
| 1086 | |
| 1087 | ret = inet6_register_protosw(&udpv6_protosw); |
| 1088 | if (ret) |
| 1089 | goto out_udpv6_protocol; |
| 1090 | out: |
| 1091 | return ret; |
| 1092 | |
| 1093 | out_udpv6_protocol: |
| 1094 | inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP); |
| 1095 | goto out; |
| 1096 | } |
| 1097 | |
Daniel Lezcano | 09f7709 | 2007-12-13 05:34:58 -0800 | [diff] [blame] | 1098 | void udpv6_exit(void) |
Daniel Lezcano | 7f4e486 | 2007-12-11 02:25:35 -0800 | [diff] [blame] | 1099 | { |
| 1100 | inet6_unregister_protosw(&udpv6_protosw); |
| 1101 | inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | } |