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