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