blob: e51da8c092faf66aa7bcfa8476bdc2babbe3f96c [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900142 if (addr_len)
143 *addr_len=sizeof(struct sockaddr_in6);
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (flags & MSG_ERRQUEUE)
146 return ipv6_recv_error(sk, msg, len);
147
148try_again:
Herbert Xua59322b2007-12-05 01:53:40 -0800149 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
150 &peeked, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (!skb)
152 goto out;
153
Herbert Xu759e5d02007-03-25 20:10:56 -0700154 ulen = skb->len - sizeof(struct udphdr);
155 copied = len;
156 if (copied > ulen)
157 copied = ulen;
158 else if (copied < ulen)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900159 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800161 /*
Herbert Xu759e5d02007-03-25 20:10:56 -0700162 * If checksum is needed at all, try to do it while copying the
163 * data. If the data is truncated, or if we only want a partial
164 * coverage checksum (UDP-Lite), do it before the copy.
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800165 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800166
Herbert Xu759e5d02007-03-25 20:10:56 -0700167 if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
168 if (udp_lib_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 goto csum_copy_err;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800170 }
171
Herbert Xu60476372007-04-09 11:59:39 -0700172 if (skb_csum_unnecessary(skb))
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800173 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
174 msg->msg_iov, copied );
175 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
177 if (err == -EINVAL)
178 goto csum_copy_err;
179 }
180 if (err)
181 goto out_free;
182
Herbert Xua59322b2007-12-05 01:53:40 -0800183 if (!peeked)
Pavel Emelyanov235b9f72008-07-05 21:19:20 -0700184 UDP6_INC_STATS_USER(sock_net(sk),
185 UDP_MIB_INDATAGRAMS, is_udplite);
Wang Chencb759942007-12-03 22:33:28 +1100186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 sock_recv_timestamp(msg, sk, skb);
188
189 /* Copy the address. */
190 if (msg->msg_name) {
191 struct sockaddr_in6 *sin6;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 sin6 = (struct sockaddr_in6 *) msg->msg_name;
194 sin6->sin6_family = AF_INET6;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300195 sin6->sin6_port = udp_hdr(skb)->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 sin6->sin6_flowinfo = 0;
197 sin6->sin6_scope_id = 0;
198
199 if (skb->protocol == htons(ETH_P_IP))
200 ipv6_addr_set(&sin6->sin6_addr, 0, 0,
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700201 htonl(0xffff), ip_hdr(skb)->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 else {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700203 ipv6_addr_copy(&sin6->sin6_addr,
204 &ipv6_hdr(skb)->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
206 sin6->sin6_scope_id = IP6CB(skb)->iif;
207 }
208
209 }
210 if (skb->protocol == htons(ETH_P_IP)) {
211 if (inet->cmsg_flags)
212 ip_cmsg_recv(msg, skb);
213 } else {
214 if (np->rxopt.all)
215 datagram_recv_ctl(sk, msg, skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 err = copied;
219 if (flags & MSG_TRUNC)
Herbert Xu759e5d02007-03-25 20:10:56 -0700220 err = ulen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222out_free:
Hideo Aoki95766ff2007-12-31 00:29:24 -0800223 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 skb_free_datagram(sk, skb);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800225 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226out:
227 return err;
228
229csum_copy_err:
Hideo Aoki95766ff2007-12-31 00:29:24 -0800230 lock_sock(sk);
Herbert Xu27ab2562007-12-05 01:51:58 -0800231 if (!skb_kill_datagram(sk, skb, flags))
Pavel Emelyanov235b9f72008-07-05 21:19:20 -0700232 UDP6_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800233 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Mitsuru Chinen7a0ff7162007-11-05 21:29:17 -0800235 if (flags & MSG_DONTWAIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 goto try_again;
238}
239
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800240void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
241 int type, int code, int offset, __be32 info,
242 struct hlist_head udptable[] )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 struct ipv6_pinfo *np;
245 struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 struct in6_addr *saddr = &hdr->saddr;
247 struct in6_addr *daddr = &hdr->daddr;
248 struct udphdr *uh = (struct udphdr*)(skb->data+offset);
249 struct sock *sk;
250 int err;
251
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900252 sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800253 saddr, uh->source, inet6_iif(skb), udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (sk == NULL)
255 return;
256
257 np = inet6_sk(sk);
258
259 if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
260 goto out;
261
262 if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
263 goto out;
264
265 if (np->recverr)
266 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
267
268 sk->sk_err = err;
269 sk->sk_error_report(sk);
270out:
271 sock_put(sk);
272}
273
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800274static __inline__ void udpv6_err(struct sk_buff *skb,
275 struct inet6_skb_parm *opt, int type,
Al Viro8e5200f2006-11-20 18:06:37 -0800276 int code, int offset, __be32 info )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
YOSHIFUJI Hideaki77d0d352008-01-22 17:09:55 +0900278 __udp6_lib_err(skb, opt, type, code, offset, info, udp_hash);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800279}
280
281int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
282{
283 struct udp_sock *up = udp_sk(sk);
David S. Millera18135e2006-08-15 00:00:09 -0700284 int rc;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100285 int is_udplite = IS_UDPLITE(sk);
David S. Millera18135e2006-08-15 00:00:09 -0700286
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800287 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
288 goto drop;
289
290 /*
291 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
292 */
Wang Chenb2bf1e22007-12-03 22:34:16 +1100293 if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800294
295 if (up->pcrlen == 0) { /* full coverage was set */
296 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage"
297 " %d while full coverage %d requested\n",
298 UDP_SKB_CB(skb)->cscov, skb->len);
299 goto drop;
300 }
301 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
302 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d "
303 "too small, need min %d\n",
304 UDP_SKB_CB(skb)->cscov, up->pcrlen);
305 goto drop;
306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308
Herbert Xu1ab6eb62007-03-06 20:29:58 -0800309 if (sk->sk_filter) {
310 if (udp_lib_checksum_complete(skb))
311 goto drop;
312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
David S. Millera18135e2006-08-15 00:00:09 -0700314 if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) {
315 /* Note that an ENOMEM error is charged twice */
Eric Dumazetcb61cb92008-06-17 21:04:56 -0700316 if (rc == -ENOMEM) {
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700317 UDP6_INC_STATS_BH(sock_net(sk),
318 UDP_MIB_RCVBUFERRORS, is_udplite);
Eric Dumazetcb61cb92008-06-17 21:04:56 -0700319 atomic_inc(&sk->sk_drops);
320 }
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800321 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
Wang Chencb759942007-12-03 22:33:28 +1100323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return 0;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800325drop:
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700326 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800327 kfree_skb(skb);
328 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331static struct sock *udp_v6_mcast_next(struct sock *sk,
Al Viroe69a4adc2006-11-14 20:56:00 -0800332 __be16 loc_port, struct in6_addr *loc_addr,
333 __be16 rmt_port, struct in6_addr *rmt_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 int dif)
335{
336 struct hlist_node *node;
337 struct sock *s = sk;
338 unsigned short num = ntohs(loc_port);
339
340 sk_for_each_from(s, node) {
341 struct inet_sock *inet = inet_sk(s);
342
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900343 if (sock_net(s) != sock_net(sk))
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800344 continue;
345
Eric Dumazet95f30b32007-02-09 15:44:52 -0800346 if (s->sk_hash == num && s->sk_family == PF_INET6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 struct ipv6_pinfo *np = inet6_sk(s);
348 if (inet->dport) {
349 if (inet->dport != rmt_port)
350 continue;
351 }
352 if (!ipv6_addr_any(&np->daddr) &&
353 !ipv6_addr_equal(&np->daddr, rmt_addr))
354 continue;
355
356 if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
357 continue;
358
359 if (!ipv6_addr_any(&np->rcv_saddr)) {
David L Stevens40796c52005-09-14 21:10:20 -0700360 if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
361 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800363 if (!inet6_mc_check(s, loc_addr, rmt_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 continue;
365 return s;
366 }
367 }
368 return NULL;
369}
370
371/*
372 * Note: called only from the BH handler context,
373 * so we don't need to lock the hashes.
374 */
Pavel Emelyanove3163492008-06-16 17:12:11 -0700375static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
376 struct in6_addr *saddr, struct in6_addr *daddr,
377 struct hlist_head udptable[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 struct sock *sk, *sk2;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300380 const struct udphdr *uh = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 int dif;
382
383 read_lock(&udp_hash_lock);
Pavel Emelyanov19c75782008-06-16 17:12:29 -0700384 sk = sk_head(&udptable[udp_hashfn(net, ntohs(uh->dest))]);
YOSHIFUJI Hideakif2776ff2006-11-21 17:41:56 -0800385 dif = inet6_iif(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 sk = udp_v6_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
387 if (!sk) {
388 kfree_skb(skb);
389 goto out;
390 }
391
392 sk2 = sk;
393 while ((sk2 = udp_v6_mcast_next(sk_next(sk2), uh->dest, daddr,
394 uh->source, saddr, dif))) {
395 struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800396 if (buff) {
Herbert Xud97106e2008-08-09 00:35:05 -0700397 bh_lock_sock(sk2);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800398 if (!sock_owned_by_user(sk2))
399 udpv6_queue_rcv_skb(sk2, buff);
400 else
401 sk_add_backlog(sk2, buff);
402 bh_unlock_sock(sk2);
403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
Herbert Xud97106e2008-08-09 00:35:05 -0700405 bh_lock_sock(sk);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800406 if (!sock_owned_by_user(sk))
407 udpv6_queue_rcv_skb(sk, skb);
408 else
409 sk_add_backlog(sk, skb);
410 bh_unlock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411out:
412 read_unlock(&udp_hash_lock);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800413 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Herbert Xu759e5d02007-03-25 20:10:56 -0700416static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh,
417 int proto)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800418{
Herbert Xu759e5d02007-03-25 20:10:56 -0700419 int err;
420
421 UDP_SKB_CB(skb)->partial_cov = 0;
422 UDP_SKB_CB(skb)->cscov = skb->len;
423
David S. Millerdb8dac22008-03-06 16:22:02 -0800424 if (proto == IPPROTO_UDPLITE) {
Herbert Xu759e5d02007-03-25 20:10:56 -0700425 err = udplite_checksum_init(skb, uh);
426 if (err)
427 return err;
428 }
429
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800430 if (uh->check == 0) {
431 /* RFC 2460 section 8.1 says that we SHOULD log
432 this error. Well, it is reasonable.
433 */
434 LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
435 return 1;
436 }
437 if (skb->ip_summed == CHECKSUM_COMPLETE &&
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700438 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
Herbert Xu759e5d02007-03-25 20:10:56 -0700439 skb->len, proto, skb->csum))
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800440 skb->ip_summed = CHECKSUM_UNNECESSARY;
441
Herbert Xu60476372007-04-09 11:59:39 -0700442 if (!skb_csum_unnecessary(skb))
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700443 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
444 &ipv6_hdr(skb)->daddr,
Herbert Xu759e5d02007-03-25 20:10:56 -0700445 skb->len, proto, 0));
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800446
Herbert Xu759e5d02007-03-25 20:10:56 -0700447 return 0;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800448}
449
Herbert Xue5bbef22007-10-15 12:50:28 -0700450int __udp6_lib_rcv(struct sk_buff *skb, struct hlist_head udptable[],
Herbert Xu759e5d02007-03-25 20:10:56 -0700451 int proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 struct sock *sk;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900454 struct udphdr *uh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 struct net_device *dev = skb->dev;
456 struct in6_addr *saddr, *daddr;
457 u32 ulen = 0;
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700458 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
461 goto short_packet;
462
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700463 saddr = &ipv6_hdr(skb)->saddr;
464 daddr = &ipv6_hdr(skb)->daddr;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300465 uh = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 ulen = ntohs(uh->len);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800468 if (ulen > skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 goto short_packet;
470
Herbert Xu759e5d02007-03-25 20:10:56 -0700471 if (proto == IPPROTO_UDP) {
472 /* UDP validates ulen. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800474 /* Check for jumbo payload */
475 if (ulen == 0)
476 ulen = skb->len;
477
478 if (ulen < sizeof(*uh))
479 goto short_packet;
480
481 if (ulen < skb->len) {
482 if (pskb_trim_rcsum(skb, ulen))
483 goto short_packet;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700484 saddr = &ipv6_hdr(skb)->saddr;
485 daddr = &ipv6_hdr(skb)->daddr;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300486 uh = udp_hdr(skb);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489
Herbert Xu759e5d02007-03-25 20:10:56 -0700490 if (udp6_csum_init(skb, uh, proto))
491 goto discard;
492
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900493 /*
494 * Multicast receive code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800496 if (ipv6_addr_is_multicast(daddr))
Pavel Emelyanove3163492008-06-16 17:12:11 -0700497 return __udp6_lib_mcast_deliver(net, skb,
498 saddr, daddr, udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 /* Unicast */
501
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900502 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 * check socket cache ... must talk to Alan about his plans
504 * for sock caches... i'll skip this for now.
505 */
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700506 sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 if (sk == NULL) {
509 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
510 goto discard;
511
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800512 if (udp_lib_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 goto discard;
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700514 UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS,
515 proto == IPPROTO_UDPLITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev);
518
519 kfree_skb(skb);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800520 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 /* deliver */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900524
Herbert Xud97106e2008-08-09 00:35:05 -0700525 bh_lock_sock(sk);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800526 if (!sock_owned_by_user(sk))
527 udpv6_queue_rcv_skb(sk, skb);
528 else
529 sk_add_backlog(sk, skb);
530 bh_unlock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 sock_put(sk);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800532 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900534short_packet:
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800535 LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: %d/%u\n",
David S. Millerdb8dac22008-03-06 16:22:02 -0800536 proto == IPPROTO_UDPLITE ? "-Lite" : "",
Herbert Xu759e5d02007-03-25 20:10:56 -0700537 ulen, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539discard:
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700540 UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 kfree_skb(skb);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800544
Herbert Xue5bbef22007-10-15 12:50:28 -0700545static __inline__ int udpv6_rcv(struct sk_buff *skb)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800546{
Herbert Xue5bbef22007-10-15 12:50:28 -0700547 return __udp6_lib_rcv(skb, udp_hash, IPPROTO_UDP);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800548}
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550/*
551 * Throw away all pending data and cancel the corking. Socket is locked.
552 */
553static void udp_v6_flush_pending_frames(struct sock *sk)
554{
555 struct udp_sock *up = udp_sk(sk);
556
Denis V. Lunev36d926b2008-06-04 15:49:07 +0400557 if (up->pending == AF_INET)
558 udp_flush_pending_frames(sk);
559 else if (up->pending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 up->len = 0;
561 up->pending = 0;
562 ip6_flush_pending_frames(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
566/*
567 * Sending
568 */
569
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800570static int udp_v6_push_pending_frames(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 struct sk_buff *skb;
573 struct udphdr *uh;
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800574 struct udp_sock *up = udp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 struct inet_sock *inet = inet_sk(sk);
576 struct flowi *fl = &inet->cork.fl;
577 int err = 0;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100578 int is_udplite = IS_UDPLITE(sk);
Al Viro868c86b2006-11-14 21:35:48 -0800579 __wsum csum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 /* Grab the skbuff where UDP header space exists. */
582 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
583 goto out;
584
585 /*
586 * Create a UDP header
587 */
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300588 uh = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 uh->source = fl->fl_ip_sport;
590 uh->dest = fl->fl_ip_dport;
591 uh->len = htons(up->len);
592 uh->check = 0;
593
Wang Chenb2bf1e22007-12-03 22:34:16 +1100594 if (is_udplite)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800595 csum = udplite_csum_outgoing(sk, skb);
596 else
597 csum = udp_csum_outgoing(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800599 /* add protocol-dependent pseudo-header */
600 uh->check = csum_ipv6_magic(&fl->fl6_src, &fl->fl6_dst,
601 up->len, fl->proto, csum );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (uh->check == 0)
Al Virof6ab0282006-11-16 02:36:50 -0800603 uh->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 err = ip6_push_pending_frames(sk);
606out:
607 up->len = 0;
608 up->pending = 0;
YOSHIFUJI Hideakicd562c92007-09-14 17:15:01 -0700609 if (!err)
Pavel Emelyanov235b9f72008-07-05 21:19:20 -0700610 UDP6_INC_STATS_USER(sock_net(sk),
611 UDP_MIB_OUTDATAGRAMS, is_udplite);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return err;
613}
614
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800615int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 struct msghdr *msg, size_t len)
617{
618 struct ipv6_txoptions opt_space;
619 struct udp_sock *up = udp_sk(sk);
620 struct inet_sock *inet = inet_sk(sk);
621 struct ipv6_pinfo *np = inet6_sk(sk);
622 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
623 struct in6_addr *daddr, *final_p = NULL, final;
624 struct ipv6_txoptions *opt = NULL;
625 struct ip6_flowlabel *flowlabel = NULL;
Herbert Xu132a55f2006-10-03 14:34:00 -0700626 struct flowi fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 struct dst_entry *dst;
628 int addr_len = msg->msg_namelen;
629 int ulen = len;
630 int hlimit = -1;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900631 int tclass = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
633 int err;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700634 int connected = 0;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100635 int is_udplite = IS_UDPLITE(sk);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800636 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 /* destination address check */
639 if (sin6) {
640 if (addr_len < offsetof(struct sockaddr, sa_data))
641 return -EINVAL;
642
643 switch (sin6->sin6_family) {
644 case AF_INET6:
645 if (addr_len < SIN6_LEN_RFC2133)
646 return -EINVAL;
647 daddr = &sin6->sin6_addr;
648 break;
649 case AF_INET:
650 goto do_udp_sendmsg;
651 case AF_UNSPEC:
652 msg->msg_name = sin6 = NULL;
653 msg->msg_namelen = addr_len = 0;
654 daddr = NULL;
655 break;
656 default:
657 return -EINVAL;
658 }
659 } else if (!up->pending) {
660 if (sk->sk_state != TCP_ESTABLISHED)
661 return -EDESTADDRREQ;
662 daddr = &np->daddr;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900663 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 daddr = NULL;
665
666 if (daddr) {
Brian Haleye773e4f2007-08-24 23:16:08 -0700667 if (ipv6_addr_v4mapped(daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 struct sockaddr_in sin;
669 sin.sin_family = AF_INET;
670 sin.sin_port = sin6 ? sin6->sin6_port : inet->dport;
671 sin.sin_addr.s_addr = daddr->s6_addr32[3];
672 msg->msg_name = &sin;
673 msg->msg_namelen = sizeof(sin);
674do_udp_sendmsg:
675 if (__ipv6_only_sock(sk))
676 return -ENETUNREACH;
677 return udp_sendmsg(iocb, sk, msg, len);
678 }
679 }
680
681 if (up->pending == AF_INET)
682 return udp_sendmsg(iocb, sk, msg, len);
683
684 /* Rough check on arithmetic overflow,
YOSHIFUJI Hideakib59e1392007-03-30 14:45:35 -0700685 better check is made in ip6_append_data().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 */
687 if (len > INT_MAX - sizeof(struct udphdr))
688 return -EMSGSIZE;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (up->pending) {
691 /*
692 * There are pending frames.
693 * The socket lock must be held while it's corked.
694 */
695 lock_sock(sk);
696 if (likely(up->pending)) {
697 if (unlikely(up->pending != AF_INET6)) {
698 release_sock(sk);
699 return -EAFNOSUPPORT;
700 }
701 dst = NULL;
702 goto do_append_data;
703 }
704 release_sock(sk);
705 }
706 ulen += sizeof(struct udphdr);
707
Herbert Xu132a55f2006-10-03 14:34:00 -0700708 memset(&fl, 0, sizeof(fl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 if (sin6) {
711 if (sin6->sin6_port == 0)
712 return -EINVAL;
713
Herbert Xu132a55f2006-10-03 14:34:00 -0700714 fl.fl_ip_dport = sin6->sin6_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 daddr = &sin6->sin6_addr;
716
717 if (np->sndflow) {
Herbert Xu132a55f2006-10-03 14:34:00 -0700718 fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
719 if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
720 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (flowlabel == NULL)
722 return -EINVAL;
723 daddr = &flowlabel->dst;
724 }
725 }
726
727 /*
728 * Otherwise it will be difficult to maintain
729 * sk->sk_dst_cache.
730 */
731 if (sk->sk_state == TCP_ESTABLISHED &&
732 ipv6_addr_equal(daddr, &np->daddr))
733 daddr = &np->daddr;
734
735 if (addr_len >= sizeof(struct sockaddr_in6) &&
736 sin6->sin6_scope_id &&
737 ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
Herbert Xu132a55f2006-10-03 14:34:00 -0700738 fl.oif = sin6->sin6_scope_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 } else {
740 if (sk->sk_state != TCP_ESTABLISHED)
741 return -EDESTADDRREQ;
742
Herbert Xu132a55f2006-10-03 14:34:00 -0700743 fl.fl_ip_dport = inet->dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 daddr = &np->daddr;
Herbert Xu132a55f2006-10-03 14:34:00 -0700745 fl.fl6_flowlabel = np->flow_label;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700746 connected = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
Herbert Xu132a55f2006-10-03 14:34:00 -0700749 if (!fl.oif)
750 fl.oif = sk->sk_bound_dev_if;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 if (msg->msg_controllen) {
753 opt = &opt_space;
754 memset(opt, 0, sizeof(struct ipv6_txoptions));
755 opt->tot_len = sizeof(*opt);
756
YOSHIFUJI Hideaki91e19082008-06-04 13:02:49 +0900757 err = datagram_send_ctl(sock_net(sk), msg, &fl, opt, &hlimit, &tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (err < 0) {
759 fl6_sock_release(flowlabel);
760 return err;
761 }
Herbert Xu132a55f2006-10-03 14:34:00 -0700762 if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
763 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (flowlabel == NULL)
765 return -EINVAL;
766 }
767 if (!(opt->opt_nflen|opt->opt_flen))
768 opt = NULL;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700769 connected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771 if (opt == NULL)
772 opt = np->opt;
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900773 if (flowlabel)
774 opt = fl6_merge_options(&opt_space, flowlabel, opt);
775 opt = ipv6_fixup_options(&opt_space, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800777 fl.proto = sk->sk_protocol;
Brian Haley876c7f42008-04-11 00:38:24 -0400778 if (!ipv6_addr_any(daddr))
779 ipv6_addr_copy(&fl.fl6_dst, daddr);
780 else
781 fl.fl6_dst.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
Herbert Xu132a55f2006-10-03 14:34:00 -0700782 if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
783 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
784 fl.fl_ip_sport = inet->sport;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 /* merge ip6_build_xmit from ip6_output */
787 if (opt && opt->srcrt) {
788 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
Herbert Xu132a55f2006-10-03 14:34:00 -0700789 ipv6_addr_copy(&final, &fl.fl6_dst);
790 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 final_p = &final;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700792 connected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
794
Herbert Xu132a55f2006-10-03 14:34:00 -0700795 if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst)) {
796 fl.oif = np->mcast_oif;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700797 connected = 0;
798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Herbert Xu132a55f2006-10-03 14:34:00 -0700800 security_sk_classify_flow(sk, &fl);
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -0700801
Herbert Xu132a55f2006-10-03 14:34:00 -0700802 err = ip6_sk_dst_lookup(sk, &dst, &fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (err)
804 goto out;
805 if (final_p)
Herbert Xu132a55f2006-10-03 14:34:00 -0700806 ipv6_addr_copy(&fl.fl6_dst, final_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Herbert Xubb728452007-12-12 18:48:58 -0800808 if ((err = __xfrm_lookup(&dst, &fl, sk, XFRM_LOOKUP_WAIT)) < 0) {
David S. Miller14e50e52007-05-24 18:17:54 -0700809 if (err == -EREMOTE)
810 err = ip6_dst_blackhole(sk, &dst, &fl);
811 if (err < 0)
812 goto out;
813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 if (hlimit < 0) {
Herbert Xu132a55f2006-10-03 14:34:00 -0700816 if (ipv6_addr_is_multicast(&fl.fl6_dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 hlimit = np->mcast_hops;
818 else
819 hlimit = np->hop_limit;
820 if (hlimit < 0)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -0400821 hlimit = ip6_dst_hoplimit(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900824 if (tclass < 0) {
825 tclass = np->tclass;
826 if (tclass < 0)
827 tclass = 0;
828 }
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (msg->msg_flags&MSG_CONFIRM)
831 goto do_confirm;
832back_from_confirm:
833
834 lock_sock(sk);
835 if (unlikely(up->pending)) {
836 /* The socket is already corked while preparing it. */
837 /* ... which is an evident application bug. --ANK */
838 release_sock(sk);
839
Patrick McHardy64ce2072005-08-09 20:50:53 -0700840 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 err = -EINVAL;
842 goto out;
843 }
844
845 up->pending = AF_INET6;
846
847do_append_data:
848 up->len += ulen;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800849 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
850 err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen,
Herbert Xu132a55f2006-10-03 14:34:00 -0700851 sizeof(struct udphdr), hlimit, tclass, opt, &fl,
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900852 (struct rt6_info*)dst,
853 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (err)
855 udp_v6_flush_pending_frames(sk);
856 else if (!corkreq)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800857 err = udp_v6_push_pending_frames(sk);
Herbert Xu1e0c14f2006-10-03 14:35:49 -0700858 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
859 up->pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
David S. Millera5e7c212005-10-03 14:21:58 -0700861 if (dst) {
862 if (connected) {
863 ip6_dst_store(sk, dst,
Herbert Xu132a55f2006-10-03 14:34:00 -0700864 ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700865 &np->daddr : NULL,
866#ifdef CONFIG_IPV6_SUBTREES
Herbert Xu132a55f2006-10-03 14:34:00 -0700867 ipv6_addr_equal(&fl.fl6_src, &np->saddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700868 &np->saddr :
869#endif
870 NULL);
David S. Millera5e7c212005-10-03 14:21:58 -0700871 } else {
872 dst_release(dst);
873 }
YOSHIFUJI Hideakia3c96082008-06-04 01:30:25 +0900874 dst = NULL;
David S. Millera5e7c212005-10-03 14:21:58 -0700875 }
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (err > 0)
878 err = np->recverr ? net_xmit_errno(err) : 0;
879 release_sock(sk);
880out:
YOSHIFUJI Hideakia3c96082008-06-04 01:30:25 +0900881 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 fl6_sock_release(flowlabel);
YOSHIFUJI Hideakicd562c92007-09-14 17:15:01 -0700883 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return len;
David S. Millera18135e2006-08-15 00:00:09 -0700885 /*
886 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
887 * ENOBUFS might not be good (it's not tunable per se), but otherwise
888 * we don't have a good statistic (IpOutDiscards but it can be too many
889 * things). We could add another new stat but at least for now that
890 * seems like overkill.
891 */
892 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
Pavel Emelyanov235b9f72008-07-05 21:19:20 -0700893 UDP6_INC_STATS_USER(sock_net(sk),
894 UDP_MIB_SNDBUFERRORS, is_udplite);
David S. Millera18135e2006-08-15 00:00:09 -0700895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return err;
897
898do_confirm:
899 dst_confirm(dst);
900 if (!(msg->msg_flags&MSG_PROBE) || len)
901 goto back_from_confirm;
902 err = 0;
903 goto out;
904}
905
Brian Haley7d06b2e2008-06-14 17:04:49 -0700906void udpv6_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
908 lock_sock(sk);
909 udp_v6_flush_pending_frames(sk);
910 release_sock(sk);
911
912 inet6_destroy_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
915/*
916 * Socket option code for UDP
917 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800918int udpv6_setsockopt(struct sock *sk, int level, int optname,
919 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800920{
David S. Millerdb8dac22008-03-06 16:22:02 -0800921 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800922 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
923 udp_v6_push_pending_frames);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800924 return ipv6_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800925}
926
927#ifdef CONFIG_COMPAT
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800928int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
929 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800930{
David S. Millerdb8dac22008-03-06 16:22:02 -0800931 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800932 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
933 udp_v6_push_pending_frames);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800934 return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800935}
936#endif
937
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800938int udpv6_getsockopt(struct sock *sk, int level, int optname,
939 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800940{
David S. Millerdb8dac22008-03-06 16:22:02 -0800941 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800942 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800943 return ipv6_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800944}
945
946#ifdef CONFIG_COMPAT
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800947int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
948 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800949{
David S. Millerdb8dac22008-03-06 16:22:02 -0800950 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800951 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800952 return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800953}
954#endif
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956static struct inet6_protocol udpv6_protocol = {
957 .handler = udpv6_rcv,
958 .err_handler = udpv6_err,
959 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
960};
961
962/* ------------------------------------------------------------------------ */
963#ifdef CONFIG_PROC_FS
964
965static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
966{
967 struct inet_sock *inet = inet_sk(sp);
968 struct ipv6_pinfo *np = inet6_sk(sp);
969 struct in6_addr *dest, *src;
970 __u16 destp, srcp;
971
972 dest = &np->daddr;
973 src = &np->rcv_saddr;
974 destp = ntohs(inet->dport);
975 srcp = ntohs(inet->sport);
976 seq_printf(seq,
977 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
Eric Dumazetcb61cb92008-06-17 21:04:56 -0700978 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 bucket,
980 src->s6_addr32[0], src->s6_addr32[1],
981 src->s6_addr32[2], src->s6_addr32[3], srcp,
982 dest->s6_addr32[0], dest->s6_addr32[1],
983 dest->s6_addr32[2], dest->s6_addr32[3], destp,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900984 sp->sk_state,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 atomic_read(&sp->sk_wmem_alloc),
986 atomic_read(&sp->sk_rmem_alloc),
987 0, 0L, 0,
988 sock_i_uid(sp), 0,
989 sock_i_ino(sp),
Eric Dumazetcb61cb92008-06-17 21:04:56 -0700990 atomic_read(&sp->sk_refcnt), sp,
991 atomic_read(&sp->sk_drops));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800994int udp6_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 if (v == SEQ_START_TOKEN)
997 seq_printf(seq,
998 " sl "
999 "local_address "
1000 "remote_address "
1001 "st tx_queue rx_queue tr tm->when retrnsmt"
Eric Dumazetcb61cb92008-06-17 21:04:56 -07001002 " uid timeout inode ref pointer drops\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 else
1004 udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
1005 return 0;
1006}
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008static struct udp_seq_afinfo udp6_seq_afinfo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 .name = "udp6",
1010 .family = AF_INET6,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001011 .hashtable = udp_hash,
Denis V. Lunev4ad96d32008-03-28 18:25:53 -07001012 .seq_fops = {
1013 .owner = THIS_MODULE,
1014 },
Denis V. Lunevdda61922008-03-28 18:24:26 -07001015 .seq_ops = {
1016 .show = udp6_seq_show,
1017 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018};
1019
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001020int udp6_proc_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001022 return udp_proc_register(net, &udp6_seq_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001025void udp6_proc_exit(struct net *net) {
1026 udp_proc_unregister(net, &udp6_seq_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028#endif /* CONFIG_PROC_FS */
1029
1030/* ------------------------------------------------------------------------ */
1031
1032struct proto udpv6_prot = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001033 .name = "UDPv6",
1034 .owner = THIS_MODULE,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001035 .close = udp_lib_close,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001036 .connect = ip6_datagram_connect,
1037 .disconnect = udp_disconnect,
1038 .ioctl = udp_ioctl,
1039 .destroy = udpv6_destroy_sock,
1040 .setsockopt = udpv6_setsockopt,
1041 .getsockopt = udpv6_getsockopt,
1042 .sendmsg = udpv6_sendmsg,
1043 .recvmsg = udpv6_recvmsg,
1044 .backlog_rcv = udpv6_queue_rcv_skb,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001045 .hash = udp_lib_hash,
1046 .unhash = udp_lib_unhash,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001047 .get_port = udp_v6_get_port,
Hideo Aoki95766ff2007-12-31 00:29:24 -08001048 .memory_allocated = &udp_memory_allocated,
1049 .sysctl_mem = sysctl_udp_mem,
1050 .sysctl_wmem = &sysctl_udp_wmem_min,
1051 .sysctl_rmem = &sysctl_udp_rmem_min,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001052 .obj_size = sizeof(struct udp6_sock),
Pavel Emelyanov6ba5a3c2008-03-22 16:51:21 -07001053 .h.udp_hash = udp_hash,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001054#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001055 .compat_setsockopt = compat_udpv6_setsockopt,
1056 .compat_getsockopt = compat_udpv6_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001057#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058};
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060static struct inet_protosw udpv6_protosw = {
1061 .type = SOCK_DGRAM,
1062 .protocol = IPPROTO_UDP,
1063 .prot = &udpv6_prot,
1064 .ops = &inet6_dgram_ops,
1065 .capability =-1,
1066 .no_check = UDP_CSUM_DEFAULT,
1067 .flags = INET_PROTOSW_PERMANENT,
1068};
1069
1070
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001071int __init udpv6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001073 int ret;
1074
1075 ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1076 if (ret)
1077 goto out;
1078
1079 ret = inet6_register_protosw(&udpv6_protosw);
1080 if (ret)
1081 goto out_udpv6_protocol;
1082out:
1083 return ret;
1084
1085out_udpv6_protocol:
1086 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1087 goto out;
1088}
1089
Daniel Lezcano09f77092007-12-13 05:34:58 -08001090void udpv6_exit(void)
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001091{
1092 inet6_unregister_protosw(&udpv6_protosw);
1093 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}