blob: ccee7244ca0ffe4c7429f22e12d52e0b262db77a [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
Eric Dumazet645ca702008-10-29 01:41:45 -070057static inline int compute_score(struct sock *sk, struct net *net,
58 unsigned short hnum,
59 struct in6_addr *saddr, __be16 sport,
60 struct in6_addr *daddr, __be16 dport,
61 int dif)
62{
63 int score = -1;
64
65 if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum &&
66 sk->sk_family == PF_INET6) {
67 struct ipv6_pinfo *np = inet6_sk(sk);
68 struct inet_sock *inet = inet_sk(sk);
69
70 score = 0;
71 if (inet->dport) {
72 if (inet->dport != sport)
73 return -1;
74 score++;
75 }
76 if (!ipv6_addr_any(&np->rcv_saddr)) {
77 if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
78 return -1;
79 score++;
80 }
81 if (!ipv6_addr_any(&np->daddr)) {
82 if (!ipv6_addr_equal(&np->daddr, saddr))
83 return -1;
84 score++;
85 }
86 if (sk->sk_bound_dev_if) {
87 if (sk->sk_bound_dev_if != dif)
88 return -1;
89 score++;
90 }
91 }
92 return score;
93}
94
Pavel Emelyanovfa4d3c62008-01-31 05:07:57 -080095static struct sock *__udp6_lib_lookup(struct net *net,
96 struct in6_addr *saddr, __be16 sport,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -080097 struct in6_addr *daddr, __be16 dport,
Eric Dumazet645ca702008-10-29 01:41:45 -070098 int dif, struct udp_table *udptable)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 struct sock *sk, *result = NULL;
101 struct hlist_node *node;
102 unsigned short hnum = ntohs(dport);
Eric Dumazet645ca702008-10-29 01:41:45 -0700103 unsigned int hash = udp_hashfn(net, hnum);
104 struct udp_hslot *hslot = &udptable->hash[hash];
105 int score, badness = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Eric Dumazet645ca702008-10-29 01:41:45 -0700107 spin_lock(&hslot->lock);
108 sk_for_each(sk, node, &hslot->head) {
109 score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif);
110 if (score > badness) {
111 result = sk;
112 badness = score;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
114 }
115 if (result)
116 sock_hold(result);
Eric Dumazet645ca702008-10-29 01:41:45 -0700117 spin_unlock(&hslot->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return result;
119}
120
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700121static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
122 __be16 sport, __be16 dport,
Eric Dumazet645ca702008-10-29 01:41:45 -0700123 struct udp_table *udptable)
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700124{
KOVACS Krisztian23542612008-10-07 12:41:01 -0700125 struct sock *sk;
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700126 struct ipv6hdr *iph = ipv6_hdr(skb);
127
KOVACS Krisztian23542612008-10-07 12:41:01 -0700128 if (unlikely(sk = skb_steal_sock(skb)))
129 return sk;
130 else
131 return __udp6_lib_lookup(dev_net(skb->dst->dev), &iph->saddr, sport,
132 &iph->daddr, dport, inet6_iif(skb),
133 udptable);
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 * This should be easy, if there is something there we
138 * return it, otherwise we block.
139 */
140
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800141int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 struct msghdr *msg, size_t len,
143 int noblock, int flags, int *addr_len)
144{
145 struct ipv6_pinfo *np = inet6_sk(sk);
146 struct inet_sock *inet = inet_sk(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900147 struct sk_buff *skb;
Herbert Xu759e5d02007-03-25 20:10:56 -0700148 unsigned int ulen, copied;
Herbert Xua59322b2007-12-05 01:53:40 -0800149 int peeked;
Herbert Xu759e5d02007-03-25 20:10:56 -0700150 int err;
151 int is_udplite = IS_UDPLITE(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900153 if (addr_len)
154 *addr_len=sizeof(struct sockaddr_in6);
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if (flags & MSG_ERRQUEUE)
157 return ipv6_recv_error(sk, msg, len);
158
159try_again:
Herbert Xua59322b2007-12-05 01:53:40 -0800160 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
161 &peeked, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (!skb)
163 goto out;
164
Herbert Xu759e5d02007-03-25 20:10:56 -0700165 ulen = skb->len - sizeof(struct udphdr);
166 copied = len;
167 if (copied > ulen)
168 copied = ulen;
169 else if (copied < ulen)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900170 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800172 /*
Herbert Xu759e5d02007-03-25 20:10:56 -0700173 * If checksum is needed at all, try to do it while copying the
174 * data. If the data is truncated, or if we only want a partial
175 * coverage checksum (UDP-Lite), do it before the copy.
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800176 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800177
Herbert Xu759e5d02007-03-25 20:10:56 -0700178 if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
179 if (udp_lib_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 goto csum_copy_err;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800181 }
182
Herbert Xu60476372007-04-09 11:59:39 -0700183 if (skb_csum_unnecessary(skb))
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800184 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
185 msg->msg_iov, copied );
186 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
188 if (err == -EINVAL)
189 goto csum_copy_err;
190 }
191 if (err)
192 goto out_free;
193
Herbert Xua59322b2007-12-05 01:53:40 -0800194 if (!peeked)
Pavel Emelyanov235b9f72008-07-05 21:19:20 -0700195 UDP6_INC_STATS_USER(sock_net(sk),
196 UDP_MIB_INDATAGRAMS, is_udplite);
Wang Chencb759942007-12-03 22:33:28 +1100197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 sock_recv_timestamp(msg, sk, skb);
199
200 /* Copy the address. */
201 if (msg->msg_name) {
202 struct sockaddr_in6 *sin6;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 sin6 = (struct sockaddr_in6 *) msg->msg_name;
205 sin6->sin6_family = AF_INET6;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300206 sin6->sin6_port = udp_hdr(skb)->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 sin6->sin6_flowinfo = 0;
208 sin6->sin6_scope_id = 0;
209
210 if (skb->protocol == htons(ETH_P_IP))
211 ipv6_addr_set(&sin6->sin6_addr, 0, 0,
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700212 htonl(0xffff), ip_hdr(skb)->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 else {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700214 ipv6_addr_copy(&sin6->sin6_addr,
215 &ipv6_hdr(skb)->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
217 sin6->sin6_scope_id = IP6CB(skb)->iif;
218 }
219
220 }
221 if (skb->protocol == htons(ETH_P_IP)) {
222 if (inet->cmsg_flags)
223 ip_cmsg_recv(msg, skb);
224 } else {
225 if (np->rxopt.all)
226 datagram_recv_ctl(sk, msg, skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 err = copied;
230 if (flags & MSG_TRUNC)
Herbert Xu759e5d02007-03-25 20:10:56 -0700231 err = ulen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233out_free:
Hideo Aoki95766ff2007-12-31 00:29:24 -0800234 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 skb_free_datagram(sk, skb);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800236 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237out:
238 return err;
239
240csum_copy_err:
Hideo Aoki95766ff2007-12-31 00:29:24 -0800241 lock_sock(sk);
Herbert Xu27ab2562007-12-05 01:51:58 -0800242 if (!skb_kill_datagram(sk, skb, flags))
Pavel Emelyanov235b9f72008-07-05 21:19:20 -0700243 UDP6_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800244 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Mitsuru Chinen7a0ff7162007-11-05 21:29:17 -0800246 if (flags & MSG_DONTWAIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 goto try_again;
249}
250
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800251void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
252 int type, int code, int offset, __be32 info,
Eric Dumazet645ca702008-10-29 01:41:45 -0700253 struct udp_table *udptable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 struct ipv6_pinfo *np;
256 struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 struct in6_addr *saddr = &hdr->saddr;
258 struct in6_addr *daddr = &hdr->daddr;
259 struct udphdr *uh = (struct udphdr*)(skb->data+offset);
260 struct sock *sk;
261 int err;
262
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900263 sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800264 saddr, uh->source, inet6_iif(skb), udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (sk == NULL)
266 return;
267
268 np = inet6_sk(sk);
269
270 if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
271 goto out;
272
273 if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
274 goto out;
275
276 if (np->recverr)
277 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
278
279 sk->sk_err = err;
280 sk->sk_error_report(sk);
281out:
282 sock_put(sk);
283}
284
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800285static __inline__ void udpv6_err(struct sk_buff *skb,
286 struct inet6_skb_parm *opt, int type,
Al Viro8e5200f2006-11-20 18:06:37 -0800287 int code, int offset, __be32 info )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Eric Dumazet645ca702008-10-29 01:41:45 -0700289 __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800290}
291
292int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
293{
294 struct udp_sock *up = udp_sk(sk);
David S. Millera18135e2006-08-15 00:00:09 -0700295 int rc;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100296 int is_udplite = IS_UDPLITE(sk);
David S. Millera18135e2006-08-15 00:00:09 -0700297
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800298 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
299 goto drop;
300
301 /*
302 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
303 */
Wang Chenb2bf1e22007-12-03 22:34:16 +1100304 if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800305
306 if (up->pcrlen == 0) { /* full coverage was set */
307 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage"
308 " %d while full coverage %d requested\n",
309 UDP_SKB_CB(skb)->cscov, skb->len);
310 goto drop;
311 }
312 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
313 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d "
314 "too small, need min %d\n",
315 UDP_SKB_CB(skb)->cscov, up->pcrlen);
316 goto drop;
317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319
Herbert Xu1ab6eb62007-03-06 20:29:58 -0800320 if (sk->sk_filter) {
321 if (udp_lib_checksum_complete(skb))
322 goto drop;
323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
David S. Millera18135e2006-08-15 00:00:09 -0700325 if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) {
326 /* Note that an ENOMEM error is charged twice */
Eric Dumazetcb61cb92008-06-17 21:04:56 -0700327 if (rc == -ENOMEM) {
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700328 UDP6_INC_STATS_BH(sock_net(sk),
329 UDP_MIB_RCVBUFERRORS, is_udplite);
Eric Dumazetcb61cb92008-06-17 21:04:56 -0700330 atomic_inc(&sk->sk_drops);
331 }
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800332 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
Wang Chencb759942007-12-03 22:33:28 +1100334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return 0;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800336drop:
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700337 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800338 kfree_skb(skb);
339 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
342static struct sock *udp_v6_mcast_next(struct sock *sk,
Al Viroe69a4ad2006-11-14 20:56:00 -0800343 __be16 loc_port, struct in6_addr *loc_addr,
344 __be16 rmt_port, struct in6_addr *rmt_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 int dif)
346{
347 struct hlist_node *node;
348 struct sock *s = sk;
349 unsigned short num = ntohs(loc_port);
350
351 sk_for_each_from(s, node) {
352 struct inet_sock *inet = inet_sk(s);
353
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900354 if (sock_net(s) != sock_net(sk))
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800355 continue;
356
Eric Dumazet95f30b32007-02-09 15:44:52 -0800357 if (s->sk_hash == num && s->sk_family == PF_INET6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 struct ipv6_pinfo *np = inet6_sk(s);
359 if (inet->dport) {
360 if (inet->dport != rmt_port)
361 continue;
362 }
363 if (!ipv6_addr_any(&np->daddr) &&
364 !ipv6_addr_equal(&np->daddr, rmt_addr))
365 continue;
366
367 if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
368 continue;
369
370 if (!ipv6_addr_any(&np->rcv_saddr)) {
David L Stevens40796c52005-09-14 21:10:20 -0700371 if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
372 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800374 if (!inet6_mc_check(s, loc_addr, rmt_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 continue;
376 return s;
377 }
378 }
379 return NULL;
380}
381
382/*
383 * Note: called only from the BH handler context,
384 * so we don't need to lock the hashes.
385 */
Pavel Emelyanove3163492008-06-16 17:12:11 -0700386static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
387 struct in6_addr *saddr, struct in6_addr *daddr,
Eric Dumazet645ca702008-10-29 01:41:45 -0700388 struct udp_table *udptable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 struct sock *sk, *sk2;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300391 const struct udphdr *uh = udp_hdr(skb);
Eric Dumazet645ca702008-10-29 01:41:45 -0700392 struct udp_hslot *hslot = &udptable->hash[udp_hashfn(net, ntohs(uh->dest))];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 int dif;
394
Eric Dumazet645ca702008-10-29 01:41:45 -0700395 spin_lock(&hslot->lock);
396 sk = sk_head(&hslot->head);
YOSHIFUJI Hideakif2776ff2006-11-21 17:41:56 -0800397 dif = inet6_iif(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 sk = udp_v6_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
399 if (!sk) {
400 kfree_skb(skb);
401 goto out;
402 }
403
404 sk2 = sk;
405 while ((sk2 = udp_v6_mcast_next(sk_next(sk2), uh->dest, daddr,
406 uh->source, saddr, dif))) {
407 struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800408 if (buff) {
Herbert Xud97106e2008-08-09 00:35:05 -0700409 bh_lock_sock(sk2);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800410 if (!sock_owned_by_user(sk2))
411 udpv6_queue_rcv_skb(sk2, buff);
412 else
413 sk_add_backlog(sk2, buff);
414 bh_unlock_sock(sk2);
415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
Herbert Xud97106e2008-08-09 00:35:05 -0700417 bh_lock_sock(sk);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800418 if (!sock_owned_by_user(sk))
419 udpv6_queue_rcv_skb(sk, skb);
420 else
421 sk_add_backlog(sk, skb);
422 bh_unlock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423out:
Eric Dumazet645ca702008-10-29 01:41:45 -0700424 spin_unlock(&hslot->lock);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
Herbert Xu759e5d02007-03-25 20:10:56 -0700428static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh,
429 int proto)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800430{
Herbert Xu759e5d02007-03-25 20:10:56 -0700431 int err;
432
433 UDP_SKB_CB(skb)->partial_cov = 0;
434 UDP_SKB_CB(skb)->cscov = skb->len;
435
David S. Millerdb8dac22008-03-06 16:22:02 -0800436 if (proto == IPPROTO_UDPLITE) {
Herbert Xu759e5d02007-03-25 20:10:56 -0700437 err = udplite_checksum_init(skb, uh);
438 if (err)
439 return err;
440 }
441
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800442 if (uh->check == 0) {
443 /* RFC 2460 section 8.1 says that we SHOULD log
444 this error. Well, it is reasonable.
445 */
446 LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
447 return 1;
448 }
449 if (skb->ip_summed == CHECKSUM_COMPLETE &&
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700450 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
Herbert Xu759e5d02007-03-25 20:10:56 -0700451 skb->len, proto, skb->csum))
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800452 skb->ip_summed = CHECKSUM_UNNECESSARY;
453
Herbert Xu60476372007-04-09 11:59:39 -0700454 if (!skb_csum_unnecessary(skb))
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700455 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
456 &ipv6_hdr(skb)->daddr,
Herbert Xu759e5d02007-03-25 20:10:56 -0700457 skb->len, proto, 0));
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800458
Herbert Xu759e5d02007-03-25 20:10:56 -0700459 return 0;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800460}
461
Eric Dumazet645ca702008-10-29 01:41:45 -0700462int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
Herbert Xu759e5d02007-03-25 20:10:56 -0700463 int proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 struct sock *sk;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900466 struct udphdr *uh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 struct net_device *dev = skb->dev;
468 struct in6_addr *saddr, *daddr;
469 u32 ulen = 0;
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700470 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
473 goto short_packet;
474
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700475 saddr = &ipv6_hdr(skb)->saddr;
476 daddr = &ipv6_hdr(skb)->daddr;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300477 uh = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 ulen = ntohs(uh->len);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800480 if (ulen > skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 goto short_packet;
482
Herbert Xu759e5d02007-03-25 20:10:56 -0700483 if (proto == IPPROTO_UDP) {
484 /* UDP validates ulen. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800486 /* Check for jumbo payload */
487 if (ulen == 0)
488 ulen = skb->len;
489
490 if (ulen < sizeof(*uh))
491 goto short_packet;
492
493 if (ulen < skb->len) {
494 if (pskb_trim_rcsum(skb, ulen))
495 goto short_packet;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700496 saddr = &ipv6_hdr(skb)->saddr;
497 daddr = &ipv6_hdr(skb)->daddr;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300498 uh = udp_hdr(skb);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501
Herbert Xu759e5d02007-03-25 20:10:56 -0700502 if (udp6_csum_init(skb, uh, proto))
503 goto discard;
504
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900505 /*
506 * Multicast receive code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800508 if (ipv6_addr_is_multicast(daddr))
Pavel Emelyanove3163492008-06-16 17:12:11 -0700509 return __udp6_lib_mcast_deliver(net, skb,
510 saddr, daddr, udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 /* Unicast */
513
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900514 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * check socket cache ... must talk to Alan about his plans
516 * for sock caches... i'll skip this for now.
517 */
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700518 sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 if (sk == NULL) {
521 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
522 goto discard;
523
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800524 if (udp_lib_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 goto discard;
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700526 UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS,
527 proto == IPPROTO_UDPLITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev);
530
531 kfree_skb(skb);
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 +0900534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /* deliver */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900536
Herbert Xud97106e2008-08-09 00:35:05 -0700537 bh_lock_sock(sk);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800538 if (!sock_owned_by_user(sk))
539 udpv6_queue_rcv_skb(sk, skb);
540 else
541 sk_add_backlog(sk, skb);
542 bh_unlock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 sock_put(sk);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800544 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900546short_packet:
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800547 LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: %d/%u\n",
David S. Millerdb8dac22008-03-06 16:22:02 -0800548 proto == IPPROTO_UDPLITE ? "-Lite" : "",
Herbert Xu759e5d02007-03-25 20:10:56 -0700549 ulen, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551discard:
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700552 UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 kfree_skb(skb);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800554 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800556
Herbert Xue5bbef22007-10-15 12:50:28 -0700557static __inline__ int udpv6_rcv(struct sk_buff *skb)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800558{
Eric Dumazet645ca702008-10-29 01:41:45 -0700559 return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800560}
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562/*
563 * Throw away all pending data and cancel the corking. Socket is locked.
564 */
565static void udp_v6_flush_pending_frames(struct sock *sk)
566{
567 struct udp_sock *up = udp_sk(sk);
568
Denis V. Lunev36d926b2008-06-04 15:49:07 +0400569 if (up->pending == AF_INET)
570 udp_flush_pending_frames(sk);
571 else if (up->pending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 up->len = 0;
573 up->pending = 0;
574 ip6_flush_pending_frames(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
578/*
579 * Sending
580 */
581
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800582static int udp_v6_push_pending_frames(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 struct sk_buff *skb;
585 struct udphdr *uh;
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800586 struct udp_sock *up = udp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 struct inet_sock *inet = inet_sk(sk);
588 struct flowi *fl = &inet->cork.fl;
589 int err = 0;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100590 int is_udplite = IS_UDPLITE(sk);
Al Viro868c86b2006-11-14 21:35:48 -0800591 __wsum csum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 /* Grab the skbuff where UDP header space exists. */
594 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
595 goto out;
596
597 /*
598 * Create a UDP header
599 */
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300600 uh = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 uh->source = fl->fl_ip_sport;
602 uh->dest = fl->fl_ip_dport;
603 uh->len = htons(up->len);
604 uh->check = 0;
605
Wang Chenb2bf1e22007-12-03 22:34:16 +1100606 if (is_udplite)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800607 csum = udplite_csum_outgoing(sk, skb);
608 else
609 csum = udp_csum_outgoing(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800611 /* add protocol-dependent pseudo-header */
612 uh->check = csum_ipv6_magic(&fl->fl6_src, &fl->fl6_dst,
613 up->len, fl->proto, csum );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (uh->check == 0)
Al Virof6ab0282006-11-16 02:36:50 -0800615 uh->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 err = ip6_push_pending_frames(sk);
618out:
619 up->len = 0;
620 up->pending = 0;
YOSHIFUJI Hideakicd562c92007-09-14 17:15:01 -0700621 if (!err)
Pavel Emelyanov235b9f72008-07-05 21:19:20 -0700622 UDP6_INC_STATS_USER(sock_net(sk),
623 UDP_MIB_OUTDATAGRAMS, is_udplite);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return err;
625}
626
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800627int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 struct msghdr *msg, size_t len)
629{
630 struct ipv6_txoptions opt_space;
631 struct udp_sock *up = udp_sk(sk);
632 struct inet_sock *inet = inet_sk(sk);
633 struct ipv6_pinfo *np = inet6_sk(sk);
634 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
635 struct in6_addr *daddr, *final_p = NULL, final;
636 struct ipv6_txoptions *opt = NULL;
637 struct ip6_flowlabel *flowlabel = NULL;
Herbert Xu132a55f2006-10-03 14:34:00 -0700638 struct flowi fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 struct dst_entry *dst;
640 int addr_len = msg->msg_namelen;
641 int ulen = len;
642 int hlimit = -1;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900643 int tclass = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
645 int err;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700646 int connected = 0;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100647 int is_udplite = IS_UDPLITE(sk);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800648 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 /* destination address check */
651 if (sin6) {
652 if (addr_len < offsetof(struct sockaddr, sa_data))
653 return -EINVAL;
654
655 switch (sin6->sin6_family) {
656 case AF_INET6:
657 if (addr_len < SIN6_LEN_RFC2133)
658 return -EINVAL;
659 daddr = &sin6->sin6_addr;
660 break;
661 case AF_INET:
662 goto do_udp_sendmsg;
663 case AF_UNSPEC:
664 msg->msg_name = sin6 = NULL;
665 msg->msg_namelen = addr_len = 0;
666 daddr = NULL;
667 break;
668 default:
669 return -EINVAL;
670 }
671 } else if (!up->pending) {
672 if (sk->sk_state != TCP_ESTABLISHED)
673 return -EDESTADDRREQ;
674 daddr = &np->daddr;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900675 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 daddr = NULL;
677
678 if (daddr) {
Brian Haleye773e4f2007-08-24 23:16:08 -0700679 if (ipv6_addr_v4mapped(daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 struct sockaddr_in sin;
681 sin.sin_family = AF_INET;
682 sin.sin_port = sin6 ? sin6->sin6_port : inet->dport;
683 sin.sin_addr.s_addr = daddr->s6_addr32[3];
684 msg->msg_name = &sin;
685 msg->msg_namelen = sizeof(sin);
686do_udp_sendmsg:
687 if (__ipv6_only_sock(sk))
688 return -ENETUNREACH;
689 return udp_sendmsg(iocb, sk, msg, len);
690 }
691 }
692
693 if (up->pending == AF_INET)
694 return udp_sendmsg(iocb, sk, msg, len);
695
696 /* Rough check on arithmetic overflow,
YOSHIFUJI Hideakib59e1392007-03-30 14:45:35 -0700697 better check is made in ip6_append_data().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 */
699 if (len > INT_MAX - sizeof(struct udphdr))
700 return -EMSGSIZE;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (up->pending) {
703 /*
704 * There are pending frames.
705 * The socket lock must be held while it's corked.
706 */
707 lock_sock(sk);
708 if (likely(up->pending)) {
709 if (unlikely(up->pending != AF_INET6)) {
710 release_sock(sk);
711 return -EAFNOSUPPORT;
712 }
713 dst = NULL;
714 goto do_append_data;
715 }
716 release_sock(sk);
717 }
718 ulen += sizeof(struct udphdr);
719
Herbert Xu132a55f2006-10-03 14:34:00 -0700720 memset(&fl, 0, sizeof(fl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 if (sin6) {
723 if (sin6->sin6_port == 0)
724 return -EINVAL;
725
Herbert Xu132a55f2006-10-03 14:34:00 -0700726 fl.fl_ip_dport = sin6->sin6_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 daddr = &sin6->sin6_addr;
728
729 if (np->sndflow) {
Herbert Xu132a55f2006-10-03 14:34:00 -0700730 fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
731 if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
732 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (flowlabel == NULL)
734 return -EINVAL;
735 daddr = &flowlabel->dst;
736 }
737 }
738
739 /*
740 * Otherwise it will be difficult to maintain
741 * sk->sk_dst_cache.
742 */
743 if (sk->sk_state == TCP_ESTABLISHED &&
744 ipv6_addr_equal(daddr, &np->daddr))
745 daddr = &np->daddr;
746
747 if (addr_len >= sizeof(struct sockaddr_in6) &&
748 sin6->sin6_scope_id &&
749 ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
Herbert Xu132a55f2006-10-03 14:34:00 -0700750 fl.oif = sin6->sin6_scope_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 } else {
752 if (sk->sk_state != TCP_ESTABLISHED)
753 return -EDESTADDRREQ;
754
Herbert Xu132a55f2006-10-03 14:34:00 -0700755 fl.fl_ip_dport = inet->dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 daddr = &np->daddr;
Herbert Xu132a55f2006-10-03 14:34:00 -0700757 fl.fl6_flowlabel = np->flow_label;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700758 connected = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
Herbert Xu132a55f2006-10-03 14:34:00 -0700761 if (!fl.oif)
762 fl.oif = sk->sk_bound_dev_if;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 if (msg->msg_controllen) {
765 opt = &opt_space;
766 memset(opt, 0, sizeof(struct ipv6_txoptions));
767 opt->tot_len = sizeof(*opt);
768
YOSHIFUJI Hideaki91e19082008-06-04 13:02:49 +0900769 err = datagram_send_ctl(sock_net(sk), msg, &fl, opt, &hlimit, &tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (err < 0) {
771 fl6_sock_release(flowlabel);
772 return err;
773 }
Herbert Xu132a55f2006-10-03 14:34:00 -0700774 if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
775 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (flowlabel == NULL)
777 return -EINVAL;
778 }
779 if (!(opt->opt_nflen|opt->opt_flen))
780 opt = NULL;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700781 connected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
783 if (opt == NULL)
784 opt = np->opt;
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900785 if (flowlabel)
786 opt = fl6_merge_options(&opt_space, flowlabel, opt);
787 opt = ipv6_fixup_options(&opt_space, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800789 fl.proto = sk->sk_protocol;
Brian Haley876c7f42008-04-11 00:38:24 -0400790 if (!ipv6_addr_any(daddr))
791 ipv6_addr_copy(&fl.fl6_dst, daddr);
792 else
793 fl.fl6_dst.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
Herbert Xu132a55f2006-10-03 14:34:00 -0700794 if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
795 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
796 fl.fl_ip_sport = inet->sport;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 /* merge ip6_build_xmit from ip6_output */
799 if (opt && opt->srcrt) {
800 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
Herbert Xu132a55f2006-10-03 14:34:00 -0700801 ipv6_addr_copy(&final, &fl.fl6_dst);
802 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 final_p = &final;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700804 connected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806
Herbert Xu132a55f2006-10-03 14:34:00 -0700807 if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst)) {
808 fl.oif = np->mcast_oif;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700809 connected = 0;
810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Herbert Xu132a55f2006-10-03 14:34:00 -0700812 security_sk_classify_flow(sk, &fl);
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -0700813
Herbert Xu132a55f2006-10-03 14:34:00 -0700814 err = ip6_sk_dst_lookup(sk, &dst, &fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (err)
816 goto out;
817 if (final_p)
Herbert Xu132a55f2006-10-03 14:34:00 -0700818 ipv6_addr_copy(&fl.fl6_dst, final_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Herbert Xubb728452007-12-12 18:48:58 -0800820 if ((err = __xfrm_lookup(&dst, &fl, sk, XFRM_LOOKUP_WAIT)) < 0) {
David S. Miller14e50e52007-05-24 18:17:54 -0700821 if (err == -EREMOTE)
822 err = ip6_dst_blackhole(sk, &dst, &fl);
823 if (err < 0)
824 goto out;
825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 if (hlimit < 0) {
Herbert Xu132a55f2006-10-03 14:34:00 -0700828 if (ipv6_addr_is_multicast(&fl.fl6_dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 hlimit = np->mcast_hops;
830 else
831 hlimit = np->hop_limit;
832 if (hlimit < 0)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -0400833 hlimit = ip6_dst_hoplimit(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900836 if (tclass < 0) {
837 tclass = np->tclass;
838 if (tclass < 0)
839 tclass = 0;
840 }
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (msg->msg_flags&MSG_CONFIRM)
843 goto do_confirm;
844back_from_confirm:
845
846 lock_sock(sk);
847 if (unlikely(up->pending)) {
848 /* The socket is already corked while preparing it. */
849 /* ... which is an evident application bug. --ANK */
850 release_sock(sk);
851
Patrick McHardy64ce2072005-08-09 20:50:53 -0700852 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 err = -EINVAL;
854 goto out;
855 }
856
857 up->pending = AF_INET6;
858
859do_append_data:
860 up->len += ulen;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800861 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
862 err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen,
Herbert Xu132a55f2006-10-03 14:34:00 -0700863 sizeof(struct udphdr), hlimit, tclass, opt, &fl,
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900864 (struct rt6_info*)dst,
865 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (err)
867 udp_v6_flush_pending_frames(sk);
868 else if (!corkreq)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800869 err = udp_v6_push_pending_frames(sk);
Herbert Xu1e0c14f2006-10-03 14:35:49 -0700870 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
871 up->pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
David S. Millera5e7c212005-10-03 14:21:58 -0700873 if (dst) {
874 if (connected) {
875 ip6_dst_store(sk, dst,
Herbert Xu132a55f2006-10-03 14:34:00 -0700876 ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700877 &np->daddr : NULL,
878#ifdef CONFIG_IPV6_SUBTREES
Herbert Xu132a55f2006-10-03 14:34:00 -0700879 ipv6_addr_equal(&fl.fl6_src, &np->saddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700880 &np->saddr :
881#endif
882 NULL);
David S. Millera5e7c212005-10-03 14:21:58 -0700883 } else {
884 dst_release(dst);
885 }
YOSHIFUJI Hideakia3c96082008-06-04 01:30:25 +0900886 dst = NULL;
David S. Millera5e7c212005-10-03 14:21:58 -0700887 }
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (err > 0)
890 err = np->recverr ? net_xmit_errno(err) : 0;
891 release_sock(sk);
892out:
YOSHIFUJI Hideakia3c96082008-06-04 01:30:25 +0900893 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 fl6_sock_release(flowlabel);
YOSHIFUJI Hideakicd562c92007-09-14 17:15:01 -0700895 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return len;
David S. Millera18135e2006-08-15 00:00:09 -0700897 /*
898 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
899 * ENOBUFS might not be good (it's not tunable per se), but otherwise
900 * we don't have a good statistic (IpOutDiscards but it can be too many
901 * things). We could add another new stat but at least for now that
902 * seems like overkill.
903 */
904 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
Pavel Emelyanov235b9f72008-07-05 21:19:20 -0700905 UDP6_INC_STATS_USER(sock_net(sk),
906 UDP_MIB_SNDBUFERRORS, is_udplite);
David S. Millera18135e2006-08-15 00:00:09 -0700907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return err;
909
910do_confirm:
911 dst_confirm(dst);
912 if (!(msg->msg_flags&MSG_PROBE) || len)
913 goto back_from_confirm;
914 err = 0;
915 goto out;
916}
917
Brian Haley7d06b2e2008-06-14 17:04:49 -0700918void udpv6_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
920 lock_sock(sk);
921 udp_v6_flush_pending_frames(sk);
922 release_sock(sk);
923
924 inet6_destroy_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
927/*
928 * Socket option code for UDP
929 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800930int udpv6_setsockopt(struct sock *sk, int level, int optname,
931 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800932{
David S. Millerdb8dac22008-03-06 16:22:02 -0800933 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800934 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
935 udp_v6_push_pending_frames);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800936 return ipv6_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800937}
938
939#ifdef CONFIG_COMPAT
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800940int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
941 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800942{
David S. Millerdb8dac22008-03-06 16:22:02 -0800943 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800944 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
945 udp_v6_push_pending_frames);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800946 return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800947}
948#endif
949
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800950int udpv6_getsockopt(struct sock *sk, int level, int optname,
951 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800952{
David S. Millerdb8dac22008-03-06 16:22:02 -0800953 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800954 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800955 return ipv6_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800956}
957
958#ifdef CONFIG_COMPAT
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800959int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
960 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800961{
David S. Millerdb8dac22008-03-06 16:22:02 -0800962 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800963 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800964 return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800965}
966#endif
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968static struct inet6_protocol udpv6_protocol = {
969 .handler = udpv6_rcv,
970 .err_handler = udpv6_err,
971 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
972};
973
974/* ------------------------------------------------------------------------ */
975#ifdef CONFIG_PROC_FS
976
977static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
978{
979 struct inet_sock *inet = inet_sk(sp);
980 struct ipv6_pinfo *np = inet6_sk(sp);
981 struct in6_addr *dest, *src;
982 __u16 destp, srcp;
983
984 dest = &np->daddr;
985 src = &np->rcv_saddr;
986 destp = ntohs(inet->dport);
987 srcp = ntohs(inet->sport);
988 seq_printf(seq,
989 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
Eric Dumazetcb61cb92008-06-17 21:04:56 -0700990 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 bucket,
992 src->s6_addr32[0], src->s6_addr32[1],
993 src->s6_addr32[2], src->s6_addr32[3], srcp,
994 dest->s6_addr32[0], dest->s6_addr32[1],
995 dest->s6_addr32[2], dest->s6_addr32[3], destp,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900996 sp->sk_state,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 atomic_read(&sp->sk_wmem_alloc),
998 atomic_read(&sp->sk_rmem_alloc),
999 0, 0L, 0,
1000 sock_i_uid(sp), 0,
1001 sock_i_ino(sp),
Eric Dumazetcb61cb92008-06-17 21:04:56 -07001002 atomic_read(&sp->sk_refcnt), sp,
1003 atomic_read(&sp->sk_drops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001006int udp6_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
1008 if (v == SEQ_START_TOKEN)
1009 seq_printf(seq,
1010 " sl "
1011 "local_address "
1012 "remote_address "
1013 "st tx_queue rx_queue tr tm->when retrnsmt"
Eric Dumazetcb61cb92008-06-17 21:04:56 -07001014 " uid timeout inode ref pointer drops\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 else
1016 udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
1017 return 0;
1018}
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020static struct udp_seq_afinfo udp6_seq_afinfo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 .name = "udp6",
1022 .family = AF_INET6,
Eric Dumazet645ca702008-10-29 01:41:45 -07001023 .udp_table = &udp_table,
Denis V. Lunev4ad96d32008-03-28 18:25:53 -07001024 .seq_fops = {
1025 .owner = THIS_MODULE,
1026 },
Denis V. Lunevdda61922008-03-28 18:24:26 -07001027 .seq_ops = {
1028 .show = udp6_seq_show,
1029 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030};
1031
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001032int udp6_proc_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001034 return udp_proc_register(net, &udp6_seq_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001037void udp6_proc_exit(struct net *net) {
1038 udp_proc_unregister(net, &udp6_seq_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040#endif /* CONFIG_PROC_FS */
1041
1042/* ------------------------------------------------------------------------ */
1043
1044struct proto udpv6_prot = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001045 .name = "UDPv6",
1046 .owner = THIS_MODULE,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001047 .close = udp_lib_close,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001048 .connect = ip6_datagram_connect,
1049 .disconnect = udp_disconnect,
1050 .ioctl = udp_ioctl,
1051 .destroy = udpv6_destroy_sock,
1052 .setsockopt = udpv6_setsockopt,
1053 .getsockopt = udpv6_getsockopt,
1054 .sendmsg = udpv6_sendmsg,
1055 .recvmsg = udpv6_recvmsg,
1056 .backlog_rcv = udpv6_queue_rcv_skb,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001057 .hash = udp_lib_hash,
1058 .unhash = udp_lib_unhash,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001059 .get_port = udp_v6_get_port,
Hideo Aoki95766ff2007-12-31 00:29:24 -08001060 .memory_allocated = &udp_memory_allocated,
1061 .sysctl_mem = sysctl_udp_mem,
1062 .sysctl_wmem = &sysctl_udp_wmem_min,
1063 .sysctl_rmem = &sysctl_udp_rmem_min,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001064 .obj_size = sizeof(struct udp6_sock),
Eric Dumazet645ca702008-10-29 01:41:45 -07001065 .h.udp_table = &udp_table,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001066#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001067 .compat_setsockopt = compat_udpv6_setsockopt,
1068 .compat_getsockopt = compat_udpv6_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001069#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070};
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072static struct inet_protosw udpv6_protosw = {
1073 .type = SOCK_DGRAM,
1074 .protocol = IPPROTO_UDP,
1075 .prot = &udpv6_prot,
1076 .ops = &inet6_dgram_ops,
1077 .capability =-1,
1078 .no_check = UDP_CSUM_DEFAULT,
1079 .flags = INET_PROTOSW_PERMANENT,
1080};
1081
1082
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001083int __init udpv6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001085 int ret;
1086
1087 ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1088 if (ret)
1089 goto out;
1090
1091 ret = inet6_register_protosw(&udpv6_protosw);
1092 if (ret)
1093 goto out_udpv6_protocol;
1094out:
1095 return ret;
1096
1097out_udpv6_protocol:
1098 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1099 goto out;
1100}
1101
Daniel Lezcano09f77092007-12-13 05:34:58 -08001102void udpv6_exit(void)
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001103{
1104 inet6_unregister_protosw(&udpv6_protosw);
1105 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}