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