blob: 432edaa882f615efa039ed05b0af9de0ac68ab52 [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
110/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * This should be easy, if there is something there we
112 * return it, otherwise we block.
113 */
114
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800115int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 struct msghdr *msg, size_t len,
117 int noblock, int flags, int *addr_len)
118{
119 struct ipv6_pinfo *np = inet6_sk(sk);
120 struct inet_sock *inet = inet_sk(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900121 struct sk_buff *skb;
Herbert Xu759e5d02007-03-25 20:10:56 -0700122 unsigned int ulen, copied;
Herbert Xua59322b2007-12-05 01:53:40 -0800123 int peeked;
Herbert Xu759e5d02007-03-25 20:10:56 -0700124 int err;
125 int is_udplite = IS_UDPLITE(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900127 if (addr_len)
128 *addr_len=sizeof(struct sockaddr_in6);
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if (flags & MSG_ERRQUEUE)
131 return ipv6_recv_error(sk, msg, len);
132
133try_again:
Herbert Xua59322b2007-12-05 01:53:40 -0800134 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
135 &peeked, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (!skb)
137 goto out;
138
Herbert Xu759e5d02007-03-25 20:10:56 -0700139 ulen = skb->len - sizeof(struct udphdr);
140 copied = len;
141 if (copied > ulen)
142 copied = ulen;
143 else if (copied < ulen)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900144 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800146 /*
Herbert Xu759e5d02007-03-25 20:10:56 -0700147 * If checksum is needed at all, try to do it while copying the
148 * data. If the data is truncated, or if we only want a partial
149 * coverage checksum (UDP-Lite), do it before the copy.
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800150 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800151
Herbert Xu759e5d02007-03-25 20:10:56 -0700152 if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
153 if (udp_lib_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 goto csum_copy_err;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800155 }
156
Herbert Xu60476372007-04-09 11:59:39 -0700157 if (skb_csum_unnecessary(skb))
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800158 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
159 msg->msg_iov, copied );
160 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
162 if (err == -EINVAL)
163 goto csum_copy_err;
164 }
165 if (err)
166 goto out_free;
167
Herbert Xua59322b2007-12-05 01:53:40 -0800168 if (!peeked)
169 UDP6_INC_STATS_USER(UDP_MIB_INDATAGRAMS, is_udplite);
Wang Chencb759942007-12-03 22:33:28 +1100170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 sock_recv_timestamp(msg, sk, skb);
172
173 /* Copy the address. */
174 if (msg->msg_name) {
175 struct sockaddr_in6 *sin6;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 sin6 = (struct sockaddr_in6 *) msg->msg_name;
178 sin6->sin6_family = AF_INET6;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300179 sin6->sin6_port = udp_hdr(skb)->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 sin6->sin6_flowinfo = 0;
181 sin6->sin6_scope_id = 0;
182
183 if (skb->protocol == htons(ETH_P_IP))
184 ipv6_addr_set(&sin6->sin6_addr, 0, 0,
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700185 htonl(0xffff), ip_hdr(skb)->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 else {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700187 ipv6_addr_copy(&sin6->sin6_addr,
188 &ipv6_hdr(skb)->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
190 sin6->sin6_scope_id = IP6CB(skb)->iif;
191 }
192
193 }
194 if (skb->protocol == htons(ETH_P_IP)) {
195 if (inet->cmsg_flags)
196 ip_cmsg_recv(msg, skb);
197 } else {
198 if (np->rxopt.all)
199 datagram_recv_ctl(sk, msg, skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 err = copied;
203 if (flags & MSG_TRUNC)
Herbert Xu759e5d02007-03-25 20:10:56 -0700204 err = ulen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206out_free:
Hideo Aoki95766ff2007-12-31 00:29:24 -0800207 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 skb_free_datagram(sk, skb);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800209 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210out:
211 return err;
212
213csum_copy_err:
Hideo Aoki95766ff2007-12-31 00:29:24 -0800214 lock_sock(sk);
Herbert Xu27ab2562007-12-05 01:51:58 -0800215 if (!skb_kill_datagram(sk, skb, flags))
216 UDP6_INC_STATS_USER(UDP_MIB_INERRORS, is_udplite);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800217 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Mitsuru Chinen7a0ff712007-11-05 21:29:17 -0800219 if (flags & MSG_DONTWAIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 goto try_again;
222}
223
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800224void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
225 int type, int code, int offset, __be32 info,
226 struct hlist_head udptable[] )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 struct ipv6_pinfo *np;
229 struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 struct in6_addr *saddr = &hdr->saddr;
231 struct in6_addr *daddr = &hdr->daddr;
232 struct udphdr *uh = (struct udphdr*)(skb->data+offset);
233 struct sock *sk;
234 int err;
235
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900236 sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800237 saddr, uh->source, inet6_iif(skb), udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (sk == NULL)
239 return;
240
241 np = inet6_sk(sk);
242
243 if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
244 goto out;
245
246 if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
247 goto out;
248
249 if (np->recverr)
250 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
251
252 sk->sk_err = err;
253 sk->sk_error_report(sk);
254out:
255 sock_put(sk);
256}
257
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800258static __inline__ void udpv6_err(struct sk_buff *skb,
259 struct inet6_skb_parm *opt, int type,
Al Viro8e5200f2006-11-20 18:06:37 -0800260 int code, int offset, __be32 info )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
YOSHIFUJI Hideaki77d0d352008-01-22 17:09:55 +0900262 __udp6_lib_err(skb, opt, type, code, offset, info, udp_hash);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800263}
264
265int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
266{
267 struct udp_sock *up = udp_sk(sk);
David S. Millera18135e2006-08-15 00:00:09 -0700268 int rc;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100269 int is_udplite = IS_UDPLITE(sk);
David S. Millera18135e2006-08-15 00:00:09 -0700270
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800271 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
272 goto drop;
273
274 /*
275 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
276 */
Wang Chenb2bf1e22007-12-03 22:34:16 +1100277 if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800278
279 if (up->pcrlen == 0) { /* full coverage was set */
280 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage"
281 " %d while full coverage %d requested\n",
282 UDP_SKB_CB(skb)->cscov, skb->len);
283 goto drop;
284 }
285 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
286 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d "
287 "too small, need min %d\n",
288 UDP_SKB_CB(skb)->cscov, up->pcrlen);
289 goto drop;
290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292
Herbert Xu1ab6eb62007-03-06 20:29:58 -0800293 if (sk->sk_filter) {
294 if (udp_lib_checksum_complete(skb))
295 goto drop;
296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
David S. Millera18135e2006-08-15 00:00:09 -0700298 if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) {
299 /* Note that an ENOMEM error is charged twice */
300 if (rc == -ENOMEM)
Wang Chenb2bf1e22007-12-03 22:34:16 +1100301 UDP6_INC_STATS_BH(UDP_MIB_RCVBUFERRORS, is_udplite);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800302 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
Wang Chencb759942007-12-03 22:33:28 +1100304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return 0;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800306drop:
Wang Chenb2bf1e22007-12-03 22:34:16 +1100307 UDP6_INC_STATS_BH(UDP_MIB_INERRORS, is_udplite);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800308 kfree_skb(skb);
309 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
312static struct sock *udp_v6_mcast_next(struct sock *sk,
Al Viroe69a4ad2006-11-14 20:56:00 -0800313 __be16 loc_port, struct in6_addr *loc_addr,
314 __be16 rmt_port, struct in6_addr *rmt_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 int dif)
316{
317 struct hlist_node *node;
318 struct sock *s = sk;
319 unsigned short num = ntohs(loc_port);
320
321 sk_for_each_from(s, node) {
322 struct inet_sock *inet = inet_sk(s);
323
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900324 if (sock_net(s) != sock_net(sk))
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800325 continue;
326
Eric Dumazet95f30b32007-02-09 15:44:52 -0800327 if (s->sk_hash == num && s->sk_family == PF_INET6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 struct ipv6_pinfo *np = inet6_sk(s);
329 if (inet->dport) {
330 if (inet->dport != rmt_port)
331 continue;
332 }
333 if (!ipv6_addr_any(&np->daddr) &&
334 !ipv6_addr_equal(&np->daddr, rmt_addr))
335 continue;
336
337 if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
338 continue;
339
340 if (!ipv6_addr_any(&np->rcv_saddr)) {
David L Stevens40796c52005-09-14 21:10:20 -0700341 if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
342 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800344 if (!inet6_mc_check(s, loc_addr, rmt_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 continue;
346 return s;
347 }
348 }
349 return NULL;
350}
351
352/*
353 * Note: called only from the BH handler context,
354 * so we don't need to lock the hashes.
355 */
Pavel Emelyanove3163492008-06-16 17:12:11 -0700356static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
357 struct in6_addr *saddr, struct in6_addr *daddr,
358 struct hlist_head udptable[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 struct sock *sk, *sk2;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300361 const struct udphdr *uh = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 int dif;
363
364 read_lock(&udp_hash_lock);
Pavel Emelyanov19c75782008-06-16 17:12:29 -0700365 sk = sk_head(&udptable[udp_hashfn(net, ntohs(uh->dest))]);
YOSHIFUJI Hideakif2776ff2006-11-21 17:41:56 -0800366 dif = inet6_iif(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 sk = udp_v6_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
368 if (!sk) {
369 kfree_skb(skb);
370 goto out;
371 }
372
373 sk2 = sk;
374 while ((sk2 = udp_v6_mcast_next(sk_next(sk2), uh->dest, daddr,
375 uh->source, saddr, dif))) {
376 struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800377 if (buff) {
378 bh_lock_sock_nested(sk2);
379 if (!sock_owned_by_user(sk2))
380 udpv6_queue_rcv_skb(sk2, buff);
381 else
382 sk_add_backlog(sk2, buff);
383 bh_unlock_sock(sk2);
384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
Hideo Aoki95766ff2007-12-31 00:29:24 -0800386 bh_lock_sock_nested(sk);
387 if (!sock_owned_by_user(sk))
388 udpv6_queue_rcv_skb(sk, skb);
389 else
390 sk_add_backlog(sk, skb);
391 bh_unlock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392out:
393 read_unlock(&udp_hash_lock);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
Herbert Xu759e5d02007-03-25 20:10:56 -0700397static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh,
398 int proto)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800399{
Herbert Xu759e5d02007-03-25 20:10:56 -0700400 int err;
401
402 UDP_SKB_CB(skb)->partial_cov = 0;
403 UDP_SKB_CB(skb)->cscov = skb->len;
404
David S. Millerdb8dac22008-03-06 16:22:02 -0800405 if (proto == IPPROTO_UDPLITE) {
Herbert Xu759e5d02007-03-25 20:10:56 -0700406 err = udplite_checksum_init(skb, uh);
407 if (err)
408 return err;
409 }
410
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800411 if (uh->check == 0) {
412 /* RFC 2460 section 8.1 says that we SHOULD log
413 this error. Well, it is reasonable.
414 */
415 LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
416 return 1;
417 }
418 if (skb->ip_summed == CHECKSUM_COMPLETE &&
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700419 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
Herbert Xu759e5d02007-03-25 20:10:56 -0700420 skb->len, proto, skb->csum))
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800421 skb->ip_summed = CHECKSUM_UNNECESSARY;
422
Herbert Xu60476372007-04-09 11:59:39 -0700423 if (!skb_csum_unnecessary(skb))
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700424 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
425 &ipv6_hdr(skb)->daddr,
Herbert Xu759e5d02007-03-25 20:10:56 -0700426 skb->len, proto, 0));
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800427
Herbert Xu759e5d02007-03-25 20:10:56 -0700428 return 0;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800429}
430
Herbert Xue5bbef22007-10-15 12:50:28 -0700431int __udp6_lib_rcv(struct sk_buff *skb, struct hlist_head udptable[],
Herbert Xu759e5d02007-03-25 20:10:56 -0700432 int proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 struct sock *sk;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900435 struct udphdr *uh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 struct net_device *dev = skb->dev;
437 struct in6_addr *saddr, *daddr;
438 u32 ulen = 0;
Pavel Emelyanove3163492008-06-16 17:12:11 -0700439 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
442 goto short_packet;
443
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700444 saddr = &ipv6_hdr(skb)->saddr;
445 daddr = &ipv6_hdr(skb)->daddr;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300446 uh = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 ulen = ntohs(uh->len);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800449 if (ulen > skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 goto short_packet;
451
Herbert Xu759e5d02007-03-25 20:10:56 -0700452 if (proto == IPPROTO_UDP) {
453 /* UDP validates ulen. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800455 /* Check for jumbo payload */
456 if (ulen == 0)
457 ulen = skb->len;
458
459 if (ulen < sizeof(*uh))
460 goto short_packet;
461
462 if (ulen < skb->len) {
463 if (pskb_trim_rcsum(skb, ulen))
464 goto short_packet;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700465 saddr = &ipv6_hdr(skb)->saddr;
466 daddr = &ipv6_hdr(skb)->daddr;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300467 uh = udp_hdr(skb);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470
Herbert Xu759e5d02007-03-25 20:10:56 -0700471 if (udp6_csum_init(skb, uh, proto))
472 goto discard;
473
Pavel Emelyanove3163492008-06-16 17:12:11 -0700474 net = dev_net(skb->dev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900475 /*
476 * Multicast receive code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800478 if (ipv6_addr_is_multicast(daddr))
Pavel Emelyanove3163492008-06-16 17:12:11 -0700479 return __udp6_lib_mcast_deliver(net, skb,
480 saddr, daddr, udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 /* Unicast */
483
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900484 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 * check socket cache ... must talk to Alan about his plans
486 * for sock caches... i'll skip this for now.
487 */
Pavel Emelyanove3163492008-06-16 17:12:11 -0700488 sk = __udp6_lib_lookup(net, saddr, uh->source,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800489 daddr, uh->dest, inet6_iif(skb), udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 if (sk == NULL) {
492 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
493 goto discard;
494
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800495 if (udp_lib_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 goto discard;
David S. Millerdb8dac22008-03-06 16:22:02 -0800497 UDP6_INC_STATS_BH(UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev);
500
501 kfree_skb(skb);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 /* deliver */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900506
Hideo Aoki95766ff2007-12-31 00:29:24 -0800507 bh_lock_sock_nested(sk);
508 if (!sock_owned_by_user(sk))
509 udpv6_queue_rcv_skb(sk, skb);
510 else
511 sk_add_backlog(sk, skb);
512 bh_unlock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 sock_put(sk);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800514 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900516short_packet:
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800517 LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: %d/%u\n",
David S. Millerdb8dac22008-03-06 16:22:02 -0800518 proto == IPPROTO_UDPLITE ? "-Lite" : "",
Herbert Xu759e5d02007-03-25 20:10:56 -0700519 ulen, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521discard:
David S. Millerdb8dac22008-03-06 16:22:02 -0800522 UDP6_INC_STATS_BH(UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 kfree_skb(skb);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800524 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800526
Herbert Xue5bbef22007-10-15 12:50:28 -0700527static __inline__ int udpv6_rcv(struct sk_buff *skb)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800528{
Herbert Xue5bbef22007-10-15 12:50:28 -0700529 return __udp6_lib_rcv(skb, udp_hash, IPPROTO_UDP);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800530}
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532/*
533 * Throw away all pending data and cancel the corking. Socket is locked.
534 */
535static void udp_v6_flush_pending_frames(struct sock *sk)
536{
537 struct udp_sock *up = udp_sk(sk);
538
Denis V. Lunev36d926b2008-06-04 15:49:07 +0400539 if (up->pending == AF_INET)
540 udp_flush_pending_frames(sk);
541 else if (up->pending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 up->len = 0;
543 up->pending = 0;
544 ip6_flush_pending_frames(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
548/*
549 * Sending
550 */
551
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800552static int udp_v6_push_pending_frames(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 struct sk_buff *skb;
555 struct udphdr *uh;
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800556 struct udp_sock *up = udp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 struct inet_sock *inet = inet_sk(sk);
558 struct flowi *fl = &inet->cork.fl;
559 int err = 0;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100560 int is_udplite = IS_UDPLITE(sk);
Al Viro868c86b2006-11-14 21:35:48 -0800561 __wsum csum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 /* Grab the skbuff where UDP header space exists. */
564 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
565 goto out;
566
567 /*
568 * Create a UDP header
569 */
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300570 uh = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 uh->source = fl->fl_ip_sport;
572 uh->dest = fl->fl_ip_dport;
573 uh->len = htons(up->len);
574 uh->check = 0;
575
Wang Chenb2bf1e22007-12-03 22:34:16 +1100576 if (is_udplite)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800577 csum = udplite_csum_outgoing(sk, skb);
578 else
579 csum = udp_csum_outgoing(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800581 /* add protocol-dependent pseudo-header */
582 uh->check = csum_ipv6_magic(&fl->fl6_src, &fl->fl6_dst,
583 up->len, fl->proto, csum );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (uh->check == 0)
Al Virof6ab0282006-11-16 02:36:50 -0800585 uh->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 err = ip6_push_pending_frames(sk);
588out:
589 up->len = 0;
590 up->pending = 0;
YOSHIFUJI Hideakicd562c92007-09-14 17:15:01 -0700591 if (!err)
Wang Chenb2bf1e22007-12-03 22:34:16 +1100592 UDP6_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS, is_udplite);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return err;
594}
595
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800596int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 struct msghdr *msg, size_t len)
598{
599 struct ipv6_txoptions opt_space;
600 struct udp_sock *up = udp_sk(sk);
601 struct inet_sock *inet = inet_sk(sk);
602 struct ipv6_pinfo *np = inet6_sk(sk);
603 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
604 struct in6_addr *daddr, *final_p = NULL, final;
605 struct ipv6_txoptions *opt = NULL;
606 struct ip6_flowlabel *flowlabel = NULL;
Herbert Xu132a55f2006-10-03 14:34:00 -0700607 struct flowi fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 struct dst_entry *dst;
609 int addr_len = msg->msg_namelen;
610 int ulen = len;
611 int hlimit = -1;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900612 int tclass = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
614 int err;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700615 int connected = 0;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100616 int is_udplite = IS_UDPLITE(sk);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800617 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 /* destination address check */
620 if (sin6) {
621 if (addr_len < offsetof(struct sockaddr, sa_data))
622 return -EINVAL;
623
624 switch (sin6->sin6_family) {
625 case AF_INET6:
626 if (addr_len < SIN6_LEN_RFC2133)
627 return -EINVAL;
628 daddr = &sin6->sin6_addr;
629 break;
630 case AF_INET:
631 goto do_udp_sendmsg;
632 case AF_UNSPEC:
633 msg->msg_name = sin6 = NULL;
634 msg->msg_namelen = addr_len = 0;
635 daddr = NULL;
636 break;
637 default:
638 return -EINVAL;
639 }
640 } else if (!up->pending) {
641 if (sk->sk_state != TCP_ESTABLISHED)
642 return -EDESTADDRREQ;
643 daddr = &np->daddr;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900644 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 daddr = NULL;
646
647 if (daddr) {
Brian Haleye773e4f2007-08-24 23:16:08 -0700648 if (ipv6_addr_v4mapped(daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 struct sockaddr_in sin;
650 sin.sin_family = AF_INET;
651 sin.sin_port = sin6 ? sin6->sin6_port : inet->dport;
652 sin.sin_addr.s_addr = daddr->s6_addr32[3];
653 msg->msg_name = &sin;
654 msg->msg_namelen = sizeof(sin);
655do_udp_sendmsg:
656 if (__ipv6_only_sock(sk))
657 return -ENETUNREACH;
658 return udp_sendmsg(iocb, sk, msg, len);
659 }
660 }
661
662 if (up->pending == AF_INET)
663 return udp_sendmsg(iocb, sk, msg, len);
664
665 /* Rough check on arithmetic overflow,
YOSHIFUJI Hideakib59e1392007-03-30 14:45:35 -0700666 better check is made in ip6_append_data().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 */
668 if (len > INT_MAX - sizeof(struct udphdr))
669 return -EMSGSIZE;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (up->pending) {
672 /*
673 * There are pending frames.
674 * The socket lock must be held while it's corked.
675 */
676 lock_sock(sk);
677 if (likely(up->pending)) {
678 if (unlikely(up->pending != AF_INET6)) {
679 release_sock(sk);
680 return -EAFNOSUPPORT;
681 }
682 dst = NULL;
683 goto do_append_data;
684 }
685 release_sock(sk);
686 }
687 ulen += sizeof(struct udphdr);
688
Herbert Xu132a55f2006-10-03 14:34:00 -0700689 memset(&fl, 0, sizeof(fl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 if (sin6) {
692 if (sin6->sin6_port == 0)
693 return -EINVAL;
694
Herbert Xu132a55f2006-10-03 14:34:00 -0700695 fl.fl_ip_dport = sin6->sin6_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 daddr = &sin6->sin6_addr;
697
698 if (np->sndflow) {
Herbert Xu132a55f2006-10-03 14:34:00 -0700699 fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
700 if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
701 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (flowlabel == NULL)
703 return -EINVAL;
704 daddr = &flowlabel->dst;
705 }
706 }
707
708 /*
709 * Otherwise it will be difficult to maintain
710 * sk->sk_dst_cache.
711 */
712 if (sk->sk_state == TCP_ESTABLISHED &&
713 ipv6_addr_equal(daddr, &np->daddr))
714 daddr = &np->daddr;
715
716 if (addr_len >= sizeof(struct sockaddr_in6) &&
717 sin6->sin6_scope_id &&
718 ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
Herbert Xu132a55f2006-10-03 14:34:00 -0700719 fl.oif = sin6->sin6_scope_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 } else {
721 if (sk->sk_state != TCP_ESTABLISHED)
722 return -EDESTADDRREQ;
723
Herbert Xu132a55f2006-10-03 14:34:00 -0700724 fl.fl_ip_dport = inet->dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 daddr = &np->daddr;
Herbert Xu132a55f2006-10-03 14:34:00 -0700726 fl.fl6_flowlabel = np->flow_label;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700727 connected = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729
Herbert Xu132a55f2006-10-03 14:34:00 -0700730 if (!fl.oif)
731 fl.oif = sk->sk_bound_dev_if;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 if (msg->msg_controllen) {
734 opt = &opt_space;
735 memset(opt, 0, sizeof(struct ipv6_txoptions));
736 opt->tot_len = sizeof(*opt);
737
YOSHIFUJI Hideaki91e19082008-06-04 13:02:49 +0900738 err = datagram_send_ctl(sock_net(sk), msg, &fl, opt, &hlimit, &tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (err < 0) {
740 fl6_sock_release(flowlabel);
741 return err;
742 }
Herbert Xu132a55f2006-10-03 14:34:00 -0700743 if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
744 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (flowlabel == NULL)
746 return -EINVAL;
747 }
748 if (!(opt->opt_nflen|opt->opt_flen))
749 opt = NULL;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700750 connected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752 if (opt == NULL)
753 opt = np->opt;
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900754 if (flowlabel)
755 opt = fl6_merge_options(&opt_space, flowlabel, opt);
756 opt = ipv6_fixup_options(&opt_space, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800758 fl.proto = sk->sk_protocol;
Brian Haley876c7f42008-04-11 00:38:24 -0400759 if (!ipv6_addr_any(daddr))
760 ipv6_addr_copy(&fl.fl6_dst, daddr);
761 else
762 fl.fl6_dst.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
Herbert Xu132a55f2006-10-03 14:34:00 -0700763 if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
764 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
765 fl.fl_ip_sport = inet->sport;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /* merge ip6_build_xmit from ip6_output */
768 if (opt && opt->srcrt) {
769 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
Herbert Xu132a55f2006-10-03 14:34:00 -0700770 ipv6_addr_copy(&final, &fl.fl6_dst);
771 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 final_p = &final;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700773 connected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775
Herbert Xu132a55f2006-10-03 14:34:00 -0700776 if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst)) {
777 fl.oif = np->mcast_oif;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700778 connected = 0;
779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Herbert Xu132a55f2006-10-03 14:34:00 -0700781 security_sk_classify_flow(sk, &fl);
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -0700782
Herbert Xu132a55f2006-10-03 14:34:00 -0700783 err = ip6_sk_dst_lookup(sk, &dst, &fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (err)
785 goto out;
786 if (final_p)
Herbert Xu132a55f2006-10-03 14:34:00 -0700787 ipv6_addr_copy(&fl.fl6_dst, final_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Herbert Xubb728452007-12-12 18:48:58 -0800789 if ((err = __xfrm_lookup(&dst, &fl, sk, XFRM_LOOKUP_WAIT)) < 0) {
David S. Miller14e50e52007-05-24 18:17:54 -0700790 if (err == -EREMOTE)
791 err = ip6_dst_blackhole(sk, &dst, &fl);
792 if (err < 0)
793 goto out;
794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 if (hlimit < 0) {
Herbert Xu132a55f2006-10-03 14:34:00 -0700797 if (ipv6_addr_is_multicast(&fl.fl6_dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 hlimit = np->mcast_hops;
799 else
800 hlimit = np->hop_limit;
801 if (hlimit < 0)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -0400802 hlimit = ip6_dst_hoplimit(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900805 if (tclass < 0) {
806 tclass = np->tclass;
807 if (tclass < 0)
808 tclass = 0;
809 }
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (msg->msg_flags&MSG_CONFIRM)
812 goto do_confirm;
813back_from_confirm:
814
815 lock_sock(sk);
816 if (unlikely(up->pending)) {
817 /* The socket is already corked while preparing it. */
818 /* ... which is an evident application bug. --ANK */
819 release_sock(sk);
820
Patrick McHardy64ce2072005-08-09 20:50:53 -0700821 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 err = -EINVAL;
823 goto out;
824 }
825
826 up->pending = AF_INET6;
827
828do_append_data:
829 up->len += ulen;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800830 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
831 err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen,
Herbert Xu132a55f2006-10-03 14:34:00 -0700832 sizeof(struct udphdr), hlimit, tclass, opt, &fl,
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900833 (struct rt6_info*)dst,
834 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (err)
836 udp_v6_flush_pending_frames(sk);
837 else if (!corkreq)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800838 err = udp_v6_push_pending_frames(sk);
Herbert Xu1e0c14f2006-10-03 14:35:49 -0700839 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
840 up->pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
David S. Millera5e7c212005-10-03 14:21:58 -0700842 if (dst) {
843 if (connected) {
844 ip6_dst_store(sk, dst,
Herbert Xu132a55f2006-10-03 14:34:00 -0700845 ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700846 &np->daddr : NULL,
847#ifdef CONFIG_IPV6_SUBTREES
Herbert Xu132a55f2006-10-03 14:34:00 -0700848 ipv6_addr_equal(&fl.fl6_src, &np->saddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700849 &np->saddr :
850#endif
851 NULL);
David S. Millera5e7c212005-10-03 14:21:58 -0700852 } else {
853 dst_release(dst);
854 }
YOSHIFUJI Hideakia3c96082008-06-04 01:30:25 +0900855 dst = NULL;
David S. Millera5e7c212005-10-03 14:21:58 -0700856 }
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (err > 0)
859 err = np->recverr ? net_xmit_errno(err) : 0;
860 release_sock(sk);
861out:
YOSHIFUJI Hideakia3c96082008-06-04 01:30:25 +0900862 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 fl6_sock_release(flowlabel);
YOSHIFUJI Hideakicd562c92007-09-14 17:15:01 -0700864 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return len;
David S. Millera18135e2006-08-15 00:00:09 -0700866 /*
867 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
868 * ENOBUFS might not be good (it's not tunable per se), but otherwise
869 * we don't have a good statistic (IpOutDiscards but it can be too many
870 * things). We could add another new stat but at least for now that
871 * seems like overkill.
872 */
873 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800874 UDP6_INC_STATS_USER(UDP_MIB_SNDBUFERRORS, is_udplite);
David S. Millera18135e2006-08-15 00:00:09 -0700875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return err;
877
878do_confirm:
879 dst_confirm(dst);
880 if (!(msg->msg_flags&MSG_PROBE) || len)
881 goto back_from_confirm;
882 err = 0;
883 goto out;
884}
885
Brian Haley7d06b2e2008-06-14 17:04:49 -0700886void udpv6_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888 lock_sock(sk);
889 udp_v6_flush_pending_frames(sk);
890 release_sock(sk);
891
892 inet6_destroy_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
895/*
896 * Socket option code for UDP
897 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800898int udpv6_setsockopt(struct sock *sk, int level, int optname,
899 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800900{
David S. Millerdb8dac22008-03-06 16:22:02 -0800901 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800902 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
903 udp_v6_push_pending_frames);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800904 return ipv6_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800905}
906
907#ifdef CONFIG_COMPAT
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800908int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
909 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800910{
David S. Millerdb8dac22008-03-06 16:22:02 -0800911 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800912 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
913 udp_v6_push_pending_frames);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800914 return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800915}
916#endif
917
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800918int udpv6_getsockopt(struct sock *sk, int level, int optname,
919 char __user *optval, int __user *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_getsockopt(sk, level, optname, optval, optlen);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800923 return ipv6_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800924}
925
926#ifdef CONFIG_COMPAT
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800927int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
928 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800929{
David S. Millerdb8dac22008-03-06 16:22:02 -0800930 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800931 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800932 return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800933}
934#endif
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936static struct inet6_protocol udpv6_protocol = {
937 .handler = udpv6_rcv,
938 .err_handler = udpv6_err,
939 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
940};
941
942/* ------------------------------------------------------------------------ */
943#ifdef CONFIG_PROC_FS
944
945static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
946{
947 struct inet_sock *inet = inet_sk(sp);
948 struct ipv6_pinfo *np = inet6_sk(sp);
949 struct in6_addr *dest, *src;
950 __u16 destp, srcp;
951
952 dest = &np->daddr;
953 src = &np->rcv_saddr;
954 destp = ntohs(inet->dport);
955 srcp = ntohs(inet->sport);
956 seq_printf(seq,
957 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
958 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p\n",
959 bucket,
960 src->s6_addr32[0], src->s6_addr32[1],
961 src->s6_addr32[2], src->s6_addr32[3], srcp,
962 dest->s6_addr32[0], dest->s6_addr32[1],
963 dest->s6_addr32[2], dest->s6_addr32[3], destp,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900964 sp->sk_state,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 atomic_read(&sp->sk_wmem_alloc),
966 atomic_read(&sp->sk_rmem_alloc),
967 0, 0L, 0,
968 sock_i_uid(sp), 0,
969 sock_i_ino(sp),
970 atomic_read(&sp->sk_refcnt), sp);
971}
972
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800973int udp6_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
975 if (v == SEQ_START_TOKEN)
976 seq_printf(seq,
977 " sl "
978 "local_address "
979 "remote_address "
980 "st tx_queue rx_queue tr tm->when retrnsmt"
981 " uid timeout inode\n");
982 else
983 udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
984 return 0;
985}
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987static struct udp_seq_afinfo udp6_seq_afinfo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 .name = "udp6",
989 .family = AF_INET6,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800990 .hashtable = udp_hash,
Denis V. Lunev4ad96d32008-03-28 18:25:53 -0700991 .seq_fops = {
992 .owner = THIS_MODULE,
993 },
Denis V. Lunevdda61922008-03-28 18:24:26 -0700994 .seq_ops = {
995 .show = udp6_seq_show,
996 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997};
998
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -0700999int udp6_proc_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001001 return udp_proc_register(net, &udp6_seq_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001004void udp6_proc_exit(struct net *net) {
1005 udp_proc_unregister(net, &udp6_seq_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007#endif /* CONFIG_PROC_FS */
1008
1009/* ------------------------------------------------------------------------ */
1010
1011struct proto udpv6_prot = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001012 .name = "UDPv6",
1013 .owner = THIS_MODULE,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001014 .close = udp_lib_close,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001015 .connect = ip6_datagram_connect,
1016 .disconnect = udp_disconnect,
1017 .ioctl = udp_ioctl,
1018 .destroy = udpv6_destroy_sock,
1019 .setsockopt = udpv6_setsockopt,
1020 .getsockopt = udpv6_getsockopt,
1021 .sendmsg = udpv6_sendmsg,
1022 .recvmsg = udpv6_recvmsg,
1023 .backlog_rcv = udpv6_queue_rcv_skb,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001024 .hash = udp_lib_hash,
1025 .unhash = udp_lib_unhash,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001026 .get_port = udp_v6_get_port,
Hideo Aoki95766ff2007-12-31 00:29:24 -08001027 .memory_allocated = &udp_memory_allocated,
1028 .sysctl_mem = sysctl_udp_mem,
1029 .sysctl_wmem = &sysctl_udp_wmem_min,
1030 .sysctl_rmem = &sysctl_udp_rmem_min,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001031 .obj_size = sizeof(struct udp6_sock),
Pavel Emelyanov6ba5a3c2008-03-22 16:51:21 -07001032 .h.udp_hash = udp_hash,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001033#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001034 .compat_setsockopt = compat_udpv6_setsockopt,
1035 .compat_getsockopt = compat_udpv6_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001036#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037};
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039static struct inet_protosw udpv6_protosw = {
1040 .type = SOCK_DGRAM,
1041 .protocol = IPPROTO_UDP,
1042 .prot = &udpv6_prot,
1043 .ops = &inet6_dgram_ops,
1044 .capability =-1,
1045 .no_check = UDP_CSUM_DEFAULT,
1046 .flags = INET_PROTOSW_PERMANENT,
1047};
1048
1049
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001050int __init udpv6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001052 int ret;
1053
1054 ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1055 if (ret)
1056 goto out;
1057
1058 ret = inet6_register_protosw(&udpv6_protosw);
1059 if (ret)
1060 goto out_udpv6_protocol;
1061out:
1062 return ret;
1063
1064out_udpv6_protocol:
1065 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1066 goto out;
1067}
1068
Daniel Lezcano09f77092007-12-13 05:34:58 -08001069void udpv6_exit(void)
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001070{
1071 inet6_unregister_protosw(&udpv6_protosw);
1072 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}