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