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