blob: 18696af106d66c2876ac4c718ab38937a1172ef8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * UDP over IPv6
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Based on linux/ipv4/udp.c
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * 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 Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/errno.h>
25#include <linux/types.h>
26#include <linux/socket.h>
27#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#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 Xu1781f7f2007-12-11 11:30:32 -080035#include <linux/module.h>
Herbert Xu3305b802005-12-13 23:16:37 -080036#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/uaccess.h>
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <net/ndisc.h>
40#include <net/protocol.h>
41#include <net/transp_v6.h>
42#include <net/ip6_route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <net/raw.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070044#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <net/ip6_checksum.h>
46#include <net/xfrm.h>
47
48#include <linux/proc_fs.h>
49#include <linux/seq_file.h>
Gerrit Renkerba4e58e2006-11-27 11:10:57 -080050#include "udp_impl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Pavel Emelyanov6ba5a3c2008-03-22 16:51:21 -070052int udp_v6_get_port(struct sock *sk, unsigned short snum)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Pavel Emelyanov6ba5a3c2008-03-22 16:51:21 -070054 return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
Pavel Emelyanovfa4d3c62008-01-31 05:07:57 -080057static struct sock *__udp6_lib_lookup(struct net *net,
58 struct in6_addr *saddr, __be16 sport,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -080059 struct in6_addr *daddr, __be16 dport,
60 int dif, struct hlist_head udptable[])
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 struct sock *sk, *result = NULL;
63 struct hlist_node *node;
64 unsigned short hnum = ntohs(dport);
65 int badness = -1;
66
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090067 read_lock(&udp_hash_lock);
Pavel Emelyanov19c75782008-06-16 17:12:29 -070068 sk_for_each(sk, node, &udptable[udp_hashfn(net, hnum)]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 struct inet_sock *inet = inet_sk(sk);
70
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +090071 if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum &&
Pavel Emelyanovfa4d3c62008-01-31 05:07:57 -080072 sk->sk_family == PF_INET6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 struct ipv6_pinfo *np = inet6_sk(sk);
74 int score = 0;
75 if (inet->dport) {
76 if (inet->dport != sport)
77 continue;
78 score++;
79 }
80 if (!ipv6_addr_any(&np->rcv_saddr)) {
81 if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
82 continue;
83 score++;
84 }
85 if (!ipv6_addr_any(&np->daddr)) {
86 if (!ipv6_addr_equal(&np->daddr, saddr))
87 continue;
88 score++;
89 }
90 if (sk->sk_bound_dev_if) {
91 if (sk->sk_bound_dev_if != dif)
92 continue;
93 score++;
94 }
Stephen Hemmingeradd459a2007-03-08 20:42:35 -080095 if (score == 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 result = sk;
97 break;
Stephen Hemmingeradd459a2007-03-08 20:42:35 -080098 } else if (score > badness) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 result = sk;
100 badness = score;
101 }
102 }
103 }
104 if (result)
105 sock_hold(result);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900106 read_unlock(&udp_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return result;
108}
109
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700110static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
111 __be16 sport, __be16 dport,
112 struct hlist_head udptable[])
113{
KOVACS Krisztian23542612008-10-07 12:41:01 -0700114 struct sock *sk;
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700115 struct ipv6hdr *iph = ipv6_hdr(skb);
116
KOVACS Krisztian23542612008-10-07 12:41:01 -0700117 if (unlikely(sk = skb_steal_sock(skb)))
118 return sk;
119 else
120 return __udp6_lib_lookup(dev_net(skb->dst->dev), &iph->saddr, sport,
121 &iph->daddr, dport, inet6_iif(skb),
122 udptable);
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700123}
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 * This should be easy, if there is something there we
127 * return it, otherwise we block.
128 */
129
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800130int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 struct msghdr *msg, size_t len,
132 int noblock, int flags, int *addr_len)
133{
134 struct ipv6_pinfo *np = inet6_sk(sk);
135 struct inet_sock *inet = inet_sk(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900136 struct sk_buff *skb;
Herbert Xu759e5d02007-03-25 20:10:56 -0700137 unsigned int ulen, copied;
Herbert Xua59322b2007-12-05 01:53:40 -0800138 int peeked;
Herbert Xu759e5d02007-03-25 20:10:56 -0700139 int err;
140 int is_udplite = IS_UDPLITE(sk);
Wei Yongjunf26ba172008-11-02 16:11:01 +0000141 int is_udp4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900143 if (addr_len)
144 *addr_len=sizeof(struct sockaddr_in6);
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (flags & MSG_ERRQUEUE)
147 return ipv6_recv_error(sk, msg, len);
148
149try_again:
Herbert Xua59322b2007-12-05 01:53:40 -0800150 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
151 &peeked, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (!skb)
153 goto out;
154
Herbert Xu759e5d02007-03-25 20:10:56 -0700155 ulen = skb->len - sizeof(struct udphdr);
156 copied = len;
157 if (copied > ulen)
158 copied = ulen;
159 else if (copied < ulen)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900160 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Wei Yongjunf26ba172008-11-02 16:11:01 +0000162 is_udp4 = (skb->protocol == htons(ETH_P_IP));
163
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800164 /*
Herbert Xu759e5d02007-03-25 20:10:56 -0700165 * If checksum is needed at all, try to do it while copying the
166 * data. If the data is truncated, or if we only want a partial
167 * coverage checksum (UDP-Lite), do it before the copy.
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800168 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800169
Herbert Xu759e5d02007-03-25 20:10:56 -0700170 if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
171 if (udp_lib_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 goto csum_copy_err;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800173 }
174
Herbert Xu60476372007-04-09 11:59:39 -0700175 if (skb_csum_unnecessary(skb))
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800176 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
177 msg->msg_iov, copied );
178 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
180 if (err == -EINVAL)
181 goto csum_copy_err;
182 }
183 if (err)
184 goto out_free;
185
Wei Yongjunf26ba172008-11-02 16:11:01 +0000186 if (!peeked) {
187 if (is_udp4)
188 UDP_INC_STATS_USER(sock_net(sk),
189 UDP_MIB_INDATAGRAMS, is_udplite);
190 else
191 UDP6_INC_STATS_USER(sock_net(sk),
192 UDP_MIB_INDATAGRAMS, is_udplite);
193 }
Wang Chencb759942007-12-03 22:33:28 +1100194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 sock_recv_timestamp(msg, sk, skb);
196
197 /* Copy the address. */
198 if (msg->msg_name) {
199 struct sockaddr_in6 *sin6;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 sin6 = (struct sockaddr_in6 *) msg->msg_name;
202 sin6->sin6_family = AF_INET6;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300203 sin6->sin6_port = udp_hdr(skb)->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 sin6->sin6_flowinfo = 0;
205 sin6->sin6_scope_id = 0;
206
Wei Yongjunf26ba172008-11-02 16:11:01 +0000207 if (is_udp4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 ipv6_addr_set(&sin6->sin6_addr, 0, 0,
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700209 htonl(0xffff), ip_hdr(skb)->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 else {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700211 ipv6_addr_copy(&sin6->sin6_addr,
212 &ipv6_hdr(skb)->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
214 sin6->sin6_scope_id = IP6CB(skb)->iif;
215 }
216
217 }
Wei Yongjunf26ba172008-11-02 16:11:01 +0000218 if (is_udp4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (inet->cmsg_flags)
220 ip_cmsg_recv(msg, skb);
221 } else {
222 if (np->rxopt.all)
223 datagram_recv_ctl(sk, msg, skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 err = copied;
227 if (flags & MSG_TRUNC)
Herbert Xu759e5d02007-03-25 20:10:56 -0700228 err = ulen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230out_free:
Hideo Aoki95766ff2007-12-31 00:29:24 -0800231 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 skb_free_datagram(sk, skb);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800233 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234out:
235 return err;
236
237csum_copy_err:
Hideo Aoki95766ff2007-12-31 00:29:24 -0800238 lock_sock(sk);
Herbert Xu27ab2562007-12-05 01:51:58 -0800239 if (!skb_kill_datagram(sk, skb, flags))
Pavel Emelyanov235b9f72008-07-05 21:19:20 -0700240 UDP6_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800241 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Mitsuru Chinen7a0ff712007-11-05 21:29:17 -0800243 if (flags & MSG_DONTWAIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 goto try_again;
246}
247
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800248void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
249 int type, int code, int offset, __be32 info,
250 struct hlist_head udptable[] )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 struct ipv6_pinfo *np;
253 struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 struct in6_addr *saddr = &hdr->saddr;
255 struct in6_addr *daddr = &hdr->daddr;
256 struct udphdr *uh = (struct udphdr*)(skb->data+offset);
257 struct sock *sk;
258 int err;
259
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900260 sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800261 saddr, uh->source, inet6_iif(skb), udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (sk == NULL)
263 return;
264
265 np = inet6_sk(sk);
266
267 if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
268 goto out;
269
270 if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
271 goto out;
272
273 if (np->recverr)
274 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
275
276 sk->sk_err = err;
277 sk->sk_error_report(sk);
278out:
279 sock_put(sk);
280}
281
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800282static __inline__ void udpv6_err(struct sk_buff *skb,
283 struct inet6_skb_parm *opt, int type,
Al Viro8e5200f2006-11-20 18:06:37 -0800284 int code, int offset, __be32 info )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
YOSHIFUJI Hideaki77d0d352008-01-22 17:09:55 +0900286 __udp6_lib_err(skb, opt, type, code, offset, info, udp_hash);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800287}
288
289int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
290{
291 struct udp_sock *up = udp_sk(sk);
David S. Millera18135e2006-08-15 00:00:09 -0700292 int rc;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100293 int is_udplite = IS_UDPLITE(sk);
David S. Millera18135e2006-08-15 00:00:09 -0700294
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800295 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
296 goto drop;
297
298 /*
299 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
300 */
Wang Chenb2bf1e22007-12-03 22:34:16 +1100301 if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800302
303 if (up->pcrlen == 0) { /* full coverage was set */
304 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage"
305 " %d while full coverage %d requested\n",
306 UDP_SKB_CB(skb)->cscov, skb->len);
307 goto drop;
308 }
309 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
310 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d "
311 "too small, need min %d\n",
312 UDP_SKB_CB(skb)->cscov, up->pcrlen);
313 goto drop;
314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316
Herbert Xu1ab6eb62007-03-06 20:29:58 -0800317 if (sk->sk_filter) {
318 if (udp_lib_checksum_complete(skb))
319 goto drop;
320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
David S. Millera18135e2006-08-15 00:00:09 -0700322 if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) {
323 /* Note that an ENOMEM error is charged twice */
Eric Dumazetcb61cb92008-06-17 21:04:56 -0700324 if (rc == -ENOMEM) {
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700325 UDP6_INC_STATS_BH(sock_net(sk),
326 UDP_MIB_RCVBUFERRORS, is_udplite);
Eric Dumazetcb61cb92008-06-17 21:04:56 -0700327 atomic_inc(&sk->sk_drops);
328 }
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800329 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
Wang Chencb759942007-12-03 22:33:28 +1100331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return 0;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800333drop:
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700334 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800335 kfree_skb(skb);
336 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Eric Dumazet920a4612008-11-01 21:22:23 -0700339static struct sock *udp_v6_mcast_next(struct net *net, struct sock *sk,
Al Viroe69a4ad2006-11-14 20:56:00 -0800340 __be16 loc_port, struct in6_addr *loc_addr,
341 __be16 rmt_port, struct in6_addr *rmt_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 int dif)
343{
344 struct hlist_node *node;
345 struct sock *s = sk;
346 unsigned short num = ntohs(loc_port);
347
348 sk_for_each_from(s, node) {
349 struct inet_sock *inet = inet_sk(s);
350
Eric Dumazet920a4612008-11-01 21:22:23 -0700351 if (!net_eq(sock_net(s), net))
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800352 continue;
353
Eric Dumazet95f30b32007-02-09 15:44:52 -0800354 if (s->sk_hash == num && s->sk_family == PF_INET6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 struct ipv6_pinfo *np = inet6_sk(s);
356 if (inet->dport) {
357 if (inet->dport != rmt_port)
358 continue;
359 }
360 if (!ipv6_addr_any(&np->daddr) &&
361 !ipv6_addr_equal(&np->daddr, rmt_addr))
362 continue;
363
364 if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
365 continue;
366
367 if (!ipv6_addr_any(&np->rcv_saddr)) {
David L Stevens40796c52005-09-14 21:10:20 -0700368 if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
369 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800371 if (!inet6_mc_check(s, loc_addr, rmt_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 continue;
373 return s;
374 }
375 }
376 return NULL;
377}
378
379/*
380 * Note: called only from the BH handler context,
381 * so we don't need to lock the hashes.
382 */
Pavel Emelyanove3163492008-06-16 17:12:11 -0700383static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
384 struct in6_addr *saddr, struct in6_addr *daddr,
385 struct hlist_head udptable[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 struct sock *sk, *sk2;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300388 const struct udphdr *uh = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 int dif;
390
391 read_lock(&udp_hash_lock);
Pavel Emelyanov19c75782008-06-16 17:12:29 -0700392 sk = sk_head(&udptable[udp_hashfn(net, ntohs(uh->dest))]);
YOSHIFUJI Hideakif2776ff2006-11-21 17:41:56 -0800393 dif = inet6_iif(skb);
Eric Dumazet920a4612008-11-01 21:22:23 -0700394 sk = udp_v6_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (!sk) {
396 kfree_skb(skb);
397 goto out;
398 }
399
400 sk2 = sk;
Eric Dumazet920a4612008-11-01 21:22:23 -0700401 while ((sk2 = udp_v6_mcast_next(net, sk_next(sk2), uh->dest, daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 uh->source, saddr, dif))) {
403 struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800404 if (buff) {
Herbert Xud97106e2008-08-09 00:35:05 -0700405 bh_lock_sock(sk2);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800406 if (!sock_owned_by_user(sk2))
407 udpv6_queue_rcv_skb(sk2, buff);
408 else
409 sk_add_backlog(sk2, buff);
410 bh_unlock_sock(sk2);
411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
Herbert Xud97106e2008-08-09 00:35:05 -0700413 bh_lock_sock(sk);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800414 if (!sock_owned_by_user(sk))
415 udpv6_queue_rcv_skb(sk, skb);
416 else
417 sk_add_backlog(sk, skb);
418 bh_unlock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419out:
420 read_unlock(&udp_hash_lock);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800421 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Herbert Xu759e5d02007-03-25 20:10:56 -0700424static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh,
425 int proto)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800426{
Herbert Xu759e5d02007-03-25 20:10:56 -0700427 int err;
428
429 UDP_SKB_CB(skb)->partial_cov = 0;
430 UDP_SKB_CB(skb)->cscov = skb->len;
431
David S. Millerdb8dac22008-03-06 16:22:02 -0800432 if (proto == IPPROTO_UDPLITE) {
Herbert Xu759e5d02007-03-25 20:10:56 -0700433 err = udplite_checksum_init(skb, uh);
434 if (err)
435 return err;
436 }
437
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800438 if (uh->check == 0) {
439 /* RFC 2460 section 8.1 says that we SHOULD log
440 this error. Well, it is reasonable.
441 */
442 LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
443 return 1;
444 }
445 if (skb->ip_summed == CHECKSUM_COMPLETE &&
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700446 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
Herbert Xu759e5d02007-03-25 20:10:56 -0700447 skb->len, proto, skb->csum))
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800448 skb->ip_summed = CHECKSUM_UNNECESSARY;
449
Herbert Xu60476372007-04-09 11:59:39 -0700450 if (!skb_csum_unnecessary(skb))
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700451 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
452 &ipv6_hdr(skb)->daddr,
Herbert Xu759e5d02007-03-25 20:10:56 -0700453 skb->len, proto, 0));
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800454
Herbert Xu759e5d02007-03-25 20:10:56 -0700455 return 0;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800456}
457
Herbert Xue5bbef22007-10-15 12:50:28 -0700458int __udp6_lib_rcv(struct sk_buff *skb, struct hlist_head udptable[],
Herbert Xu759e5d02007-03-25 20:10:56 -0700459 int proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 struct sock *sk;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900462 struct udphdr *uh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 struct net_device *dev = skb->dev;
464 struct in6_addr *saddr, *daddr;
465 u32 ulen = 0;
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700466 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
469 goto short_packet;
470
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700471 saddr = &ipv6_hdr(skb)->saddr;
472 daddr = &ipv6_hdr(skb)->daddr;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300473 uh = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 ulen = ntohs(uh->len);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800476 if (ulen > skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 goto short_packet;
478
Herbert Xu759e5d02007-03-25 20:10:56 -0700479 if (proto == IPPROTO_UDP) {
480 /* UDP validates ulen. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800482 /* Check for jumbo payload */
483 if (ulen == 0)
484 ulen = skb->len;
485
486 if (ulen < sizeof(*uh))
487 goto short_packet;
488
489 if (ulen < skb->len) {
490 if (pskb_trim_rcsum(skb, ulen))
491 goto short_packet;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700492 saddr = &ipv6_hdr(skb)->saddr;
493 daddr = &ipv6_hdr(skb)->daddr;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300494 uh = udp_hdr(skb);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497
Herbert Xu759e5d02007-03-25 20:10:56 -0700498 if (udp6_csum_init(skb, uh, proto))
499 goto discard;
500
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900501 /*
502 * Multicast receive code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800504 if (ipv6_addr_is_multicast(daddr))
Pavel Emelyanove3163492008-06-16 17:12:11 -0700505 return __udp6_lib_mcast_deliver(net, skb,
506 saddr, daddr, udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 /* Unicast */
509
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900510 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 * check socket cache ... must talk to Alan about his plans
512 * for sock caches... i'll skip this for now.
513 */
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700514 sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 if (sk == NULL) {
517 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
518 goto discard;
519
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800520 if (udp_lib_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 goto discard;
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700522 UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS,
523 proto == IPPROTO_UDPLITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev);
526
527 kfree_skb(skb);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800528 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 /* deliver */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900532
Herbert Xud97106e2008-08-09 00:35:05 -0700533 bh_lock_sock(sk);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800534 if (!sock_owned_by_user(sk))
535 udpv6_queue_rcv_skb(sk, skb);
536 else
537 sk_add_backlog(sk, skb);
538 bh_unlock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 sock_put(sk);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800540 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900542short_packet:
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800543 LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: %d/%u\n",
David S. Millerdb8dac22008-03-06 16:22:02 -0800544 proto == IPPROTO_UDPLITE ? "-Lite" : "",
Herbert Xu759e5d02007-03-25 20:10:56 -0700545 ulen, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547discard:
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700548 UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 kfree_skb(skb);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800550 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800552
Herbert Xue5bbef22007-10-15 12:50:28 -0700553static __inline__ int udpv6_rcv(struct sk_buff *skb)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800554{
Herbert Xue5bbef22007-10-15 12:50:28 -0700555 return __udp6_lib_rcv(skb, udp_hash, IPPROTO_UDP);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800556}
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558/*
559 * Throw away all pending data and cancel the corking. Socket is locked.
560 */
561static void udp_v6_flush_pending_frames(struct sock *sk)
562{
563 struct udp_sock *up = udp_sk(sk);
564
Denis V. Lunev36d926b2008-06-04 15:49:07 +0400565 if (up->pending == AF_INET)
566 udp_flush_pending_frames(sk);
567 else if (up->pending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 up->len = 0;
569 up->pending = 0;
570 ip6_flush_pending_frames(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
574/*
575 * Sending
576 */
577
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800578static int udp_v6_push_pending_frames(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 struct sk_buff *skb;
581 struct udphdr *uh;
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800582 struct udp_sock *up = udp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 struct inet_sock *inet = inet_sk(sk);
584 struct flowi *fl = &inet->cork.fl;
585 int err = 0;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100586 int is_udplite = IS_UDPLITE(sk);
Al Viro868c86b2006-11-14 21:35:48 -0800587 __wsum csum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 /* Grab the skbuff where UDP header space exists. */
590 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
591 goto out;
592
593 /*
594 * Create a UDP header
595 */
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300596 uh = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 uh->source = fl->fl_ip_sport;
598 uh->dest = fl->fl_ip_dport;
599 uh->len = htons(up->len);
600 uh->check = 0;
601
Wang Chenb2bf1e22007-12-03 22:34:16 +1100602 if (is_udplite)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800603 csum = udplite_csum_outgoing(sk, skb);
604 else
605 csum = udp_csum_outgoing(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800607 /* add protocol-dependent pseudo-header */
608 uh->check = csum_ipv6_magic(&fl->fl6_src, &fl->fl6_dst,
609 up->len, fl->proto, csum );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (uh->check == 0)
Al Virof6ab0282006-11-16 02:36:50 -0800611 uh->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 err = ip6_push_pending_frames(sk);
614out:
615 up->len = 0;
616 up->pending = 0;
YOSHIFUJI Hideakicd562c92007-09-14 17:15:01 -0700617 if (!err)
Pavel Emelyanov235b9f72008-07-05 21:19:20 -0700618 UDP6_INC_STATS_USER(sock_net(sk),
619 UDP_MIB_OUTDATAGRAMS, is_udplite);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return err;
621}
622
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800623int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 struct msghdr *msg, size_t len)
625{
626 struct ipv6_txoptions opt_space;
627 struct udp_sock *up = udp_sk(sk);
628 struct inet_sock *inet = inet_sk(sk);
629 struct ipv6_pinfo *np = inet6_sk(sk);
630 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
631 struct in6_addr *daddr, *final_p = NULL, final;
632 struct ipv6_txoptions *opt = NULL;
633 struct ip6_flowlabel *flowlabel = NULL;
Herbert Xu132a55f2006-10-03 14:34:00 -0700634 struct flowi fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 struct dst_entry *dst;
636 int addr_len = msg->msg_namelen;
637 int ulen = len;
638 int hlimit = -1;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900639 int tclass = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
641 int err;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700642 int connected = 0;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100643 int is_udplite = IS_UDPLITE(sk);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800644 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 /* destination address check */
647 if (sin6) {
648 if (addr_len < offsetof(struct sockaddr, sa_data))
649 return -EINVAL;
650
651 switch (sin6->sin6_family) {
652 case AF_INET6:
653 if (addr_len < SIN6_LEN_RFC2133)
654 return -EINVAL;
655 daddr = &sin6->sin6_addr;
656 break;
657 case AF_INET:
658 goto do_udp_sendmsg;
659 case AF_UNSPEC:
660 msg->msg_name = sin6 = NULL;
661 msg->msg_namelen = addr_len = 0;
662 daddr = NULL;
663 break;
664 default:
665 return -EINVAL;
666 }
667 } else if (!up->pending) {
668 if (sk->sk_state != TCP_ESTABLISHED)
669 return -EDESTADDRREQ;
670 daddr = &np->daddr;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900671 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 daddr = NULL;
673
674 if (daddr) {
Brian Haleye773e4f2007-08-24 23:16:08 -0700675 if (ipv6_addr_v4mapped(daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 struct sockaddr_in sin;
677 sin.sin_family = AF_INET;
678 sin.sin_port = sin6 ? sin6->sin6_port : inet->dport;
679 sin.sin_addr.s_addr = daddr->s6_addr32[3];
680 msg->msg_name = &sin;
681 msg->msg_namelen = sizeof(sin);
682do_udp_sendmsg:
683 if (__ipv6_only_sock(sk))
684 return -ENETUNREACH;
685 return udp_sendmsg(iocb, sk, msg, len);
686 }
687 }
688
689 if (up->pending == AF_INET)
690 return udp_sendmsg(iocb, sk, msg, len);
691
692 /* Rough check on arithmetic overflow,
YOSHIFUJI Hideakib59e1392007-03-30 14:45:35 -0700693 better check is made in ip6_append_data().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 */
695 if (len > INT_MAX - sizeof(struct udphdr))
696 return -EMSGSIZE;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (up->pending) {
699 /*
700 * There are pending frames.
701 * The socket lock must be held while it's corked.
702 */
703 lock_sock(sk);
704 if (likely(up->pending)) {
705 if (unlikely(up->pending != AF_INET6)) {
706 release_sock(sk);
707 return -EAFNOSUPPORT;
708 }
709 dst = NULL;
710 goto do_append_data;
711 }
712 release_sock(sk);
713 }
714 ulen += sizeof(struct udphdr);
715
Herbert Xu132a55f2006-10-03 14:34:00 -0700716 memset(&fl, 0, sizeof(fl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 if (sin6) {
719 if (sin6->sin6_port == 0)
720 return -EINVAL;
721
Herbert Xu132a55f2006-10-03 14:34:00 -0700722 fl.fl_ip_dport = sin6->sin6_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 daddr = &sin6->sin6_addr;
724
725 if (np->sndflow) {
Herbert Xu132a55f2006-10-03 14:34:00 -0700726 fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
727 if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
728 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (flowlabel == NULL)
730 return -EINVAL;
731 daddr = &flowlabel->dst;
732 }
733 }
734
735 /*
736 * Otherwise it will be difficult to maintain
737 * sk->sk_dst_cache.
738 */
739 if (sk->sk_state == TCP_ESTABLISHED &&
740 ipv6_addr_equal(daddr, &np->daddr))
741 daddr = &np->daddr;
742
743 if (addr_len >= sizeof(struct sockaddr_in6) &&
744 sin6->sin6_scope_id &&
745 ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
Herbert Xu132a55f2006-10-03 14:34:00 -0700746 fl.oif = sin6->sin6_scope_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 } else {
748 if (sk->sk_state != TCP_ESTABLISHED)
749 return -EDESTADDRREQ;
750
Herbert Xu132a55f2006-10-03 14:34:00 -0700751 fl.fl_ip_dport = inet->dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 daddr = &np->daddr;
Herbert Xu132a55f2006-10-03 14:34:00 -0700753 fl.fl6_flowlabel = np->flow_label;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700754 connected = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
756
Herbert Xu132a55f2006-10-03 14:34:00 -0700757 if (!fl.oif)
758 fl.oif = sk->sk_bound_dev_if;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 if (msg->msg_controllen) {
761 opt = &opt_space;
762 memset(opt, 0, sizeof(struct ipv6_txoptions));
763 opt->tot_len = sizeof(*opt);
764
YOSHIFUJI Hideaki91e19082008-06-04 13:02:49 +0900765 err = datagram_send_ctl(sock_net(sk), msg, &fl, opt, &hlimit, &tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (err < 0) {
767 fl6_sock_release(flowlabel);
768 return err;
769 }
Herbert Xu132a55f2006-10-03 14:34:00 -0700770 if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
771 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (flowlabel == NULL)
773 return -EINVAL;
774 }
775 if (!(opt->opt_nflen|opt->opt_flen))
776 opt = NULL;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700777 connected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
779 if (opt == NULL)
780 opt = np->opt;
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900781 if (flowlabel)
782 opt = fl6_merge_options(&opt_space, flowlabel, opt);
783 opt = ipv6_fixup_options(&opt_space, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800785 fl.proto = sk->sk_protocol;
Brian Haley876c7f42008-04-11 00:38:24 -0400786 if (!ipv6_addr_any(daddr))
787 ipv6_addr_copy(&fl.fl6_dst, daddr);
788 else
789 fl.fl6_dst.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
Herbert Xu132a55f2006-10-03 14:34:00 -0700790 if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
791 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
792 fl.fl_ip_sport = inet->sport;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 /* merge ip6_build_xmit from ip6_output */
795 if (opt && opt->srcrt) {
796 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
Herbert Xu132a55f2006-10-03 14:34:00 -0700797 ipv6_addr_copy(&final, &fl.fl6_dst);
798 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 final_p = &final;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700800 connected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802
Herbert Xu132a55f2006-10-03 14:34:00 -0700803 if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst)) {
804 fl.oif = np->mcast_oif;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700805 connected = 0;
806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Herbert Xu132a55f2006-10-03 14:34:00 -0700808 security_sk_classify_flow(sk, &fl);
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -0700809
Herbert Xu132a55f2006-10-03 14:34:00 -0700810 err = ip6_sk_dst_lookup(sk, &dst, &fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (err)
812 goto out;
813 if (final_p)
Herbert Xu132a55f2006-10-03 14:34:00 -0700814 ipv6_addr_copy(&fl.fl6_dst, final_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Herbert Xubb728452007-12-12 18:48:58 -0800816 if ((err = __xfrm_lookup(&dst, &fl, sk, XFRM_LOOKUP_WAIT)) < 0) {
David S. Miller14e50e52007-05-24 18:17:54 -0700817 if (err == -EREMOTE)
818 err = ip6_dst_blackhole(sk, &dst, &fl);
819 if (err < 0)
820 goto out;
821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 if (hlimit < 0) {
Herbert Xu132a55f2006-10-03 14:34:00 -0700824 if (ipv6_addr_is_multicast(&fl.fl6_dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 hlimit = np->mcast_hops;
826 else
827 hlimit = np->hop_limit;
828 if (hlimit < 0)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -0400829 hlimit = ip6_dst_hoplimit(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900832 if (tclass < 0) {
833 tclass = np->tclass;
834 if (tclass < 0)
835 tclass = 0;
836 }
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (msg->msg_flags&MSG_CONFIRM)
839 goto do_confirm;
840back_from_confirm:
841
842 lock_sock(sk);
843 if (unlikely(up->pending)) {
844 /* The socket is already corked while preparing it. */
845 /* ... which is an evident application bug. --ANK */
846 release_sock(sk);
847
Patrick McHardy64ce2072005-08-09 20:50:53 -0700848 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 err = -EINVAL;
850 goto out;
851 }
852
853 up->pending = AF_INET6;
854
855do_append_data:
856 up->len += ulen;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800857 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
858 err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen,
Herbert Xu132a55f2006-10-03 14:34:00 -0700859 sizeof(struct udphdr), hlimit, tclass, opt, &fl,
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900860 (struct rt6_info*)dst,
861 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (err)
863 udp_v6_flush_pending_frames(sk);
864 else if (!corkreq)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800865 err = udp_v6_push_pending_frames(sk);
Herbert Xu1e0c14f2006-10-03 14:35:49 -0700866 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
867 up->pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
David S. Millera5e7c212005-10-03 14:21:58 -0700869 if (dst) {
870 if (connected) {
871 ip6_dst_store(sk, dst,
Herbert Xu132a55f2006-10-03 14:34:00 -0700872 ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700873 &np->daddr : NULL,
874#ifdef CONFIG_IPV6_SUBTREES
Herbert Xu132a55f2006-10-03 14:34:00 -0700875 ipv6_addr_equal(&fl.fl6_src, &np->saddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700876 &np->saddr :
877#endif
878 NULL);
David S. Millera5e7c212005-10-03 14:21:58 -0700879 } else {
880 dst_release(dst);
881 }
YOSHIFUJI Hideakia3c96082008-06-04 01:30:25 +0900882 dst = NULL;
David S. Millera5e7c212005-10-03 14:21:58 -0700883 }
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (err > 0)
886 err = np->recverr ? net_xmit_errno(err) : 0;
887 release_sock(sk);
888out:
YOSHIFUJI Hideakia3c96082008-06-04 01:30:25 +0900889 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 fl6_sock_release(flowlabel);
YOSHIFUJI Hideakicd562c92007-09-14 17:15:01 -0700891 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return len;
David S. Millera18135e2006-08-15 00:00:09 -0700893 /*
894 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
895 * ENOBUFS might not be good (it's not tunable per se), but otherwise
896 * we don't have a good statistic (IpOutDiscards but it can be too many
897 * things). We could add another new stat but at least for now that
898 * seems like overkill.
899 */
900 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
Pavel Emelyanov235b9f72008-07-05 21:19:20 -0700901 UDP6_INC_STATS_USER(sock_net(sk),
902 UDP_MIB_SNDBUFERRORS, is_udplite);
David S. Millera18135e2006-08-15 00:00:09 -0700903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return err;
905
906do_confirm:
907 dst_confirm(dst);
908 if (!(msg->msg_flags&MSG_PROBE) || len)
909 goto back_from_confirm;
910 err = 0;
911 goto out;
912}
913
Brian Haley7d06b2e2008-06-14 17:04:49 -0700914void udpv6_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916 lock_sock(sk);
917 udp_v6_flush_pending_frames(sk);
918 release_sock(sk);
919
920 inet6_destroy_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
923/*
924 * Socket option code for UDP
925 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800926int udpv6_setsockopt(struct sock *sk, int level, int optname,
927 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800928{
David S. Millerdb8dac22008-03-06 16:22:02 -0800929 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800930 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
931 udp_v6_push_pending_frames);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800932 return ipv6_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800933}
934
935#ifdef CONFIG_COMPAT
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800936int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
937 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800938{
David S. Millerdb8dac22008-03-06 16:22:02 -0800939 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800940 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
941 udp_v6_push_pending_frames);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800942 return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800943}
944#endif
945
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800946int udpv6_getsockopt(struct sock *sk, int level, int optname,
947 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800948{
David S. Millerdb8dac22008-03-06 16:22:02 -0800949 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800950 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800951 return ipv6_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800952}
953
954#ifdef CONFIG_COMPAT
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800955int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
956 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800957{
David S. Millerdb8dac22008-03-06 16:22:02 -0800958 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800959 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800960 return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800961}
962#endif
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964static struct inet6_protocol udpv6_protocol = {
965 .handler = udpv6_rcv,
966 .err_handler = udpv6_err,
967 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
968};
969
970/* ------------------------------------------------------------------------ */
971#ifdef CONFIG_PROC_FS
972
973static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
974{
975 struct inet_sock *inet = inet_sk(sp);
976 struct ipv6_pinfo *np = inet6_sk(sp);
977 struct in6_addr *dest, *src;
978 __u16 destp, srcp;
979
980 dest = &np->daddr;
981 src = &np->rcv_saddr;
982 destp = ntohs(inet->dport);
983 srcp = ntohs(inet->sport);
984 seq_printf(seq,
985 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
Eric Dumazetcb61cb92008-06-17 21:04:56 -0700986 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 bucket,
988 src->s6_addr32[0], src->s6_addr32[1],
989 src->s6_addr32[2], src->s6_addr32[3], srcp,
990 dest->s6_addr32[0], dest->s6_addr32[1],
991 dest->s6_addr32[2], dest->s6_addr32[3], destp,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900992 sp->sk_state,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 atomic_read(&sp->sk_wmem_alloc),
994 atomic_read(&sp->sk_rmem_alloc),
995 0, 0L, 0,
996 sock_i_uid(sp), 0,
997 sock_i_ino(sp),
Eric Dumazetcb61cb92008-06-17 21:04:56 -0700998 atomic_read(&sp->sk_refcnt), sp,
999 atomic_read(&sp->sk_drops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001002int udp6_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
1004 if (v == SEQ_START_TOKEN)
1005 seq_printf(seq,
1006 " sl "
1007 "local_address "
1008 "remote_address "
1009 "st tx_queue rx_queue tr tm->when retrnsmt"
Eric Dumazetcb61cb92008-06-17 21:04:56 -07001010 " uid timeout inode ref pointer drops\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 else
1012 udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
1013 return 0;
1014}
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016static struct udp_seq_afinfo udp6_seq_afinfo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 .name = "udp6",
1018 .family = AF_INET6,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001019 .hashtable = udp_hash,
Denis V. Lunev4ad96d32008-03-28 18:25:53 -07001020 .seq_fops = {
1021 .owner = THIS_MODULE,
1022 },
Denis V. Lunevdda61922008-03-28 18:24:26 -07001023 .seq_ops = {
1024 .show = udp6_seq_show,
1025 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026};
1027
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001028int udp6_proc_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001030 return udp_proc_register(net, &udp6_seq_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001033void udp6_proc_exit(struct net *net) {
1034 udp_proc_unregister(net, &udp6_seq_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036#endif /* CONFIG_PROC_FS */
1037
1038/* ------------------------------------------------------------------------ */
1039
1040struct proto udpv6_prot = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001041 .name = "UDPv6",
1042 .owner = THIS_MODULE,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001043 .close = udp_lib_close,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001044 .connect = ip6_datagram_connect,
1045 .disconnect = udp_disconnect,
1046 .ioctl = udp_ioctl,
1047 .destroy = udpv6_destroy_sock,
1048 .setsockopt = udpv6_setsockopt,
1049 .getsockopt = udpv6_getsockopt,
1050 .sendmsg = udpv6_sendmsg,
1051 .recvmsg = udpv6_recvmsg,
1052 .backlog_rcv = udpv6_queue_rcv_skb,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001053 .hash = udp_lib_hash,
1054 .unhash = udp_lib_unhash,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001055 .get_port = udp_v6_get_port,
Hideo Aoki95766ff2007-12-31 00:29:24 -08001056 .memory_allocated = &udp_memory_allocated,
1057 .sysctl_mem = sysctl_udp_mem,
1058 .sysctl_wmem = &sysctl_udp_wmem_min,
1059 .sysctl_rmem = &sysctl_udp_rmem_min,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001060 .obj_size = sizeof(struct udp6_sock),
Pavel Emelyanov6ba5a3c2008-03-22 16:51:21 -07001061 .h.udp_hash = udp_hash,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001062#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001063 .compat_setsockopt = compat_udpv6_setsockopt,
1064 .compat_getsockopt = compat_udpv6_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001065#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066};
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068static struct inet_protosw udpv6_protosw = {
1069 .type = SOCK_DGRAM,
1070 .protocol = IPPROTO_UDP,
1071 .prot = &udpv6_prot,
1072 .ops = &inet6_dgram_ops,
1073 .capability =-1,
1074 .no_check = UDP_CSUM_DEFAULT,
1075 .flags = INET_PROTOSW_PERMANENT,
1076};
1077
1078
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001079int __init udpv6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001081 int ret;
1082
1083 ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1084 if (ret)
1085 goto out;
1086
1087 ret = inet6_register_protosw(&udpv6_protosw);
1088 if (ret)
1089 goto out_udpv6_protocol;
1090out:
1091 return ret;
1092
1093out_udpv6_protocol:
1094 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1095 goto out;
1096}
1097
Daniel Lezcano09f77092007-12-13 05:34:58 -08001098void udpv6_exit(void)
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001099{
1100 inet6_unregister_protosw(&udpv6_protosw);
1101 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}