blob: 32d914db6c4fa90e5d773fc405242ee349e83ce4 [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{
Eric Dumazet271b72c2008-10-29 02:11:14 -0700100 struct sock *sk, *result;
Eric Dumazet96631ed2008-10-29 11:19:58 -0700101 struct hlist_node *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 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];
Eric Dumazet271b72c2008-10-29 02:11:14 -0700105 int score, badness;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Eric Dumazet271b72c2008-10-29 02:11:14 -0700107 rcu_read_lock();
108begin:
109 result = NULL;
110 badness = -1;
Eric Dumazet96631ed2008-10-29 11:19:58 -0700111 sk_for_each_rcu_safenext(sk, node, &hslot->head, next) {
Eric Dumazet271b72c2008-10-29 02:11:14 -0700112 /*
113 * lockless reader, and SLAB_DESTROY_BY_RCU items:
114 * We must check this item was not moved to another chain
115 */
116 if (udp_hashfn(net, sk->sk_hash) != hash)
117 goto begin;
Eric Dumazet645ca702008-10-29 01:41:45 -0700118 score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif);
119 if (score > badness) {
120 result = sk;
121 badness = score;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123 }
Eric Dumazet271b72c2008-10-29 02:11:14 -0700124 if (result) {
125 if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt)))
126 result = NULL;
127 else if (unlikely(compute_score(result, net, hnum, saddr, sport,
128 daddr, dport, dif) < badness)) {
129 sock_put(result);
130 goto begin;
131 }
132 }
133 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return result;
135}
136
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700137static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
138 __be16 sport, __be16 dport,
Eric Dumazet645ca702008-10-29 01:41:45 -0700139 struct udp_table *udptable)
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700140{
KOVACS Krisztian23542612008-10-07 12:41:01 -0700141 struct sock *sk;
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700142 struct ipv6hdr *iph = ipv6_hdr(skb);
143
KOVACS Krisztian23542612008-10-07 12:41:01 -0700144 if (unlikely(sk = skb_steal_sock(skb)))
145 return sk;
146 else
147 return __udp6_lib_lookup(dev_net(skb->dst->dev), &iph->saddr, sport,
148 &iph->daddr, dport, inet6_iif(skb),
149 udptable);
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 * This should be easy, if there is something there we
154 * return it, otherwise we block.
155 */
156
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800157int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 struct msghdr *msg, size_t len,
159 int noblock, int flags, int *addr_len)
160{
161 struct ipv6_pinfo *np = inet6_sk(sk);
162 struct inet_sock *inet = inet_sk(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900163 struct sk_buff *skb;
Herbert Xu759e5d02007-03-25 20:10:56 -0700164 unsigned int ulen, copied;
Herbert Xua59322b2007-12-05 01:53:40 -0800165 int peeked;
Herbert Xu759e5d02007-03-25 20:10:56 -0700166 int err;
167 int is_udplite = IS_UDPLITE(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900169 if (addr_len)
170 *addr_len=sizeof(struct sockaddr_in6);
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (flags & MSG_ERRQUEUE)
173 return ipv6_recv_error(sk, msg, len);
174
175try_again:
Herbert Xua59322b2007-12-05 01:53:40 -0800176 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
177 &peeked, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (!skb)
179 goto out;
180
Herbert Xu759e5d02007-03-25 20:10:56 -0700181 ulen = skb->len - sizeof(struct udphdr);
182 copied = len;
183 if (copied > ulen)
184 copied = ulen;
185 else if (copied < ulen)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900186 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800188 /*
Herbert Xu759e5d02007-03-25 20:10:56 -0700189 * If checksum is needed at all, try to do it while copying the
190 * data. If the data is truncated, or if we only want a partial
191 * coverage checksum (UDP-Lite), do it before the copy.
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800192 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800193
Herbert Xu759e5d02007-03-25 20:10:56 -0700194 if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
195 if (udp_lib_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 goto csum_copy_err;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800197 }
198
Herbert Xu60476372007-04-09 11:59:39 -0700199 if (skb_csum_unnecessary(skb))
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800200 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
201 msg->msg_iov, copied );
202 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
204 if (err == -EINVAL)
205 goto csum_copy_err;
206 }
207 if (err)
208 goto out_free;
209
Herbert Xua59322b2007-12-05 01:53:40 -0800210 if (!peeked)
Pavel Emelyanov235b9f72008-07-05 21:19:20 -0700211 UDP6_INC_STATS_USER(sock_net(sk),
212 UDP_MIB_INDATAGRAMS, is_udplite);
Wang Chencb759942007-12-03 22:33:28 +1100213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 sock_recv_timestamp(msg, sk, skb);
215
216 /* Copy the address. */
217 if (msg->msg_name) {
218 struct sockaddr_in6 *sin6;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 sin6 = (struct sockaddr_in6 *) msg->msg_name;
221 sin6->sin6_family = AF_INET6;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300222 sin6->sin6_port = udp_hdr(skb)->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 sin6->sin6_flowinfo = 0;
224 sin6->sin6_scope_id = 0;
225
226 if (skb->protocol == htons(ETH_P_IP))
227 ipv6_addr_set(&sin6->sin6_addr, 0, 0,
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700228 htonl(0xffff), ip_hdr(skb)->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 else {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700230 ipv6_addr_copy(&sin6->sin6_addr,
231 &ipv6_hdr(skb)->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
233 sin6->sin6_scope_id = IP6CB(skb)->iif;
234 }
235
236 }
237 if (skb->protocol == htons(ETH_P_IP)) {
238 if (inet->cmsg_flags)
239 ip_cmsg_recv(msg, skb);
240 } else {
241 if (np->rxopt.all)
242 datagram_recv_ctl(sk, msg, skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 err = copied;
246 if (flags & MSG_TRUNC)
Herbert Xu759e5d02007-03-25 20:10:56 -0700247 err = ulen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249out_free:
Hideo Aoki95766ff2007-12-31 00:29:24 -0800250 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 skb_free_datagram(sk, skb);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800252 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253out:
254 return err;
255
256csum_copy_err:
Hideo Aoki95766ff2007-12-31 00:29:24 -0800257 lock_sock(sk);
Herbert Xu27ab2562007-12-05 01:51:58 -0800258 if (!skb_kill_datagram(sk, skb, flags))
Pavel Emelyanov235b9f72008-07-05 21:19:20 -0700259 UDP6_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800260 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Mitsuru Chinen7a0ff712007-11-05 21:29:17 -0800262 if (flags & MSG_DONTWAIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 goto try_again;
265}
266
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800267void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
268 int type, int code, int offset, __be32 info,
Eric Dumazet645ca702008-10-29 01:41:45 -0700269 struct udp_table *udptable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 struct ipv6_pinfo *np;
272 struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 struct in6_addr *saddr = &hdr->saddr;
274 struct in6_addr *daddr = &hdr->daddr;
275 struct udphdr *uh = (struct udphdr*)(skb->data+offset);
276 struct sock *sk;
277 int err;
278
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900279 sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800280 saddr, uh->source, inet6_iif(skb), udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (sk == NULL)
282 return;
283
284 np = inet6_sk(sk);
285
286 if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
287 goto out;
288
289 if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
290 goto out;
291
292 if (np->recverr)
293 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
294
295 sk->sk_err = err;
296 sk->sk_error_report(sk);
297out:
298 sock_put(sk);
299}
300
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800301static __inline__ void udpv6_err(struct sk_buff *skb,
302 struct inet6_skb_parm *opt, int type,
Al Viro8e5200f2006-11-20 18:06:37 -0800303 int code, int offset, __be32 info )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Eric Dumazet645ca702008-10-29 01:41:45 -0700305 __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800306}
307
308int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
309{
310 struct udp_sock *up = udp_sk(sk);
David S. Millera18135e2006-08-15 00:00:09 -0700311 int rc;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100312 int is_udplite = IS_UDPLITE(sk);
David S. Millera18135e2006-08-15 00:00:09 -0700313
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800314 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
315 goto drop;
316
317 /*
318 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
319 */
Wang Chenb2bf1e22007-12-03 22:34:16 +1100320 if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800321
322 if (up->pcrlen == 0) { /* full coverage was set */
323 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage"
324 " %d while full coverage %d requested\n",
325 UDP_SKB_CB(skb)->cscov, skb->len);
326 goto drop;
327 }
328 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
329 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d "
330 "too small, need min %d\n",
331 UDP_SKB_CB(skb)->cscov, up->pcrlen);
332 goto drop;
333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
Herbert Xu1ab6eb62007-03-06 20:29:58 -0800336 if (sk->sk_filter) {
337 if (udp_lib_checksum_complete(skb))
338 goto drop;
339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
David S. Millera18135e2006-08-15 00:00:09 -0700341 if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) {
342 /* Note that an ENOMEM error is charged twice */
Eric Dumazetcb61cb92008-06-17 21:04:56 -0700343 if (rc == -ENOMEM) {
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700344 UDP6_INC_STATS_BH(sock_net(sk),
345 UDP_MIB_RCVBUFERRORS, is_udplite);
Eric Dumazetcb61cb92008-06-17 21:04:56 -0700346 atomic_inc(&sk->sk_drops);
347 }
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800348 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Wang Chencb759942007-12-03 22:33:28 +1100350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return 0;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800352drop:
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700353 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800354 kfree_skb(skb);
355 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
358static struct sock *udp_v6_mcast_next(struct sock *sk,
Al Viroe69a4ad2006-11-14 20:56:00 -0800359 __be16 loc_port, struct in6_addr *loc_addr,
360 __be16 rmt_port, struct in6_addr *rmt_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 int dif)
362{
363 struct hlist_node *node;
364 struct sock *s = sk;
365 unsigned short num = ntohs(loc_port);
366
367 sk_for_each_from(s, node) {
368 struct inet_sock *inet = inet_sk(s);
369
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900370 if (sock_net(s) != sock_net(sk))
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800371 continue;
372
Eric Dumazet95f30b32007-02-09 15:44:52 -0800373 if (s->sk_hash == num && s->sk_family == PF_INET6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 struct ipv6_pinfo *np = inet6_sk(s);
375 if (inet->dport) {
376 if (inet->dport != rmt_port)
377 continue;
378 }
379 if (!ipv6_addr_any(&np->daddr) &&
380 !ipv6_addr_equal(&np->daddr, rmt_addr))
381 continue;
382
383 if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
384 continue;
385
386 if (!ipv6_addr_any(&np->rcv_saddr)) {
David L Stevens40796c52005-09-14 21:10:20 -0700387 if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
388 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800390 if (!inet6_mc_check(s, loc_addr, rmt_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 continue;
392 return s;
393 }
394 }
395 return NULL;
396}
397
398/*
399 * Note: called only from the BH handler context,
400 * so we don't need to lock the hashes.
401 */
Pavel Emelyanove3163492008-06-16 17:12:11 -0700402static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
403 struct in6_addr *saddr, struct in6_addr *daddr,
Eric Dumazet645ca702008-10-29 01:41:45 -0700404 struct udp_table *udptable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 struct sock *sk, *sk2;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300407 const struct udphdr *uh = udp_hdr(skb);
Eric Dumazet645ca702008-10-29 01:41:45 -0700408 struct udp_hslot *hslot = &udptable->hash[udp_hashfn(net, ntohs(uh->dest))];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 int dif;
410
Eric Dumazet645ca702008-10-29 01:41:45 -0700411 spin_lock(&hslot->lock);
412 sk = sk_head(&hslot->head);
YOSHIFUJI Hideakif2776ff2006-11-21 17:41:56 -0800413 dif = inet6_iif(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 sk = udp_v6_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
415 if (!sk) {
416 kfree_skb(skb);
417 goto out;
418 }
419
420 sk2 = sk;
421 while ((sk2 = udp_v6_mcast_next(sk_next(sk2), uh->dest, daddr,
422 uh->source, saddr, dif))) {
423 struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800424 if (buff) {
Herbert Xud97106e2008-08-09 00:35:05 -0700425 bh_lock_sock(sk2);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800426 if (!sock_owned_by_user(sk2))
427 udpv6_queue_rcv_skb(sk2, buff);
428 else
429 sk_add_backlog(sk2, buff);
430 bh_unlock_sock(sk2);
431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
Herbert Xud97106e2008-08-09 00:35:05 -0700433 bh_lock_sock(sk);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800434 if (!sock_owned_by_user(sk))
435 udpv6_queue_rcv_skb(sk, skb);
436 else
437 sk_add_backlog(sk, skb);
438 bh_unlock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439out:
Eric Dumazet645ca702008-10-29 01:41:45 -0700440 spin_unlock(&hslot->lock);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
Herbert Xu759e5d02007-03-25 20:10:56 -0700444static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh,
445 int proto)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800446{
Herbert Xu759e5d02007-03-25 20:10:56 -0700447 int err;
448
449 UDP_SKB_CB(skb)->partial_cov = 0;
450 UDP_SKB_CB(skb)->cscov = skb->len;
451
David S. Millerdb8dac22008-03-06 16:22:02 -0800452 if (proto == IPPROTO_UDPLITE) {
Herbert Xu759e5d02007-03-25 20:10:56 -0700453 err = udplite_checksum_init(skb, uh);
454 if (err)
455 return err;
456 }
457
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800458 if (uh->check == 0) {
459 /* RFC 2460 section 8.1 says that we SHOULD log
460 this error. Well, it is reasonable.
461 */
462 LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
463 return 1;
464 }
465 if (skb->ip_summed == CHECKSUM_COMPLETE &&
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700466 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
Herbert Xu759e5d02007-03-25 20:10:56 -0700467 skb->len, proto, skb->csum))
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800468 skb->ip_summed = CHECKSUM_UNNECESSARY;
469
Herbert Xu60476372007-04-09 11:59:39 -0700470 if (!skb_csum_unnecessary(skb))
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700471 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
472 &ipv6_hdr(skb)->daddr,
Herbert Xu759e5d02007-03-25 20:10:56 -0700473 skb->len, proto, 0));
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800474
Herbert Xu759e5d02007-03-25 20:10:56 -0700475 return 0;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800476}
477
Eric Dumazet645ca702008-10-29 01:41:45 -0700478int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
Herbert Xu759e5d02007-03-25 20:10:56 -0700479 int proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 struct sock *sk;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900482 struct udphdr *uh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 struct net_device *dev = skb->dev;
484 struct in6_addr *saddr, *daddr;
485 u32 ulen = 0;
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700486 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
489 goto short_packet;
490
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700491 saddr = &ipv6_hdr(skb)->saddr;
492 daddr = &ipv6_hdr(skb)->daddr;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300493 uh = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 ulen = ntohs(uh->len);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800496 if (ulen > skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 goto short_packet;
498
Herbert Xu759e5d02007-03-25 20:10:56 -0700499 if (proto == IPPROTO_UDP) {
500 /* UDP validates ulen. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800502 /* Check for jumbo payload */
503 if (ulen == 0)
504 ulen = skb->len;
505
506 if (ulen < sizeof(*uh))
507 goto short_packet;
508
509 if (ulen < skb->len) {
510 if (pskb_trim_rcsum(skb, ulen))
511 goto short_packet;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700512 saddr = &ipv6_hdr(skb)->saddr;
513 daddr = &ipv6_hdr(skb)->daddr;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300514 uh = udp_hdr(skb);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517
Herbert Xu759e5d02007-03-25 20:10:56 -0700518 if (udp6_csum_init(skb, uh, proto))
519 goto discard;
520
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900521 /*
522 * Multicast receive code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800524 if (ipv6_addr_is_multicast(daddr))
Pavel Emelyanove3163492008-06-16 17:12:11 -0700525 return __udp6_lib_mcast_deliver(net, skb,
526 saddr, daddr, udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 /* Unicast */
529
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900530 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 * check socket cache ... must talk to Alan about his plans
532 * for sock caches... i'll skip this for now.
533 */
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700534 sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 if (sk == NULL) {
537 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
538 goto discard;
539
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800540 if (udp_lib_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 goto discard;
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700542 UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS,
543 proto == IPPROTO_UDPLITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev);
546
547 kfree_skb(skb);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800548 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /* deliver */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900552
Herbert Xud97106e2008-08-09 00:35:05 -0700553 bh_lock_sock(sk);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800554 if (!sock_owned_by_user(sk))
555 udpv6_queue_rcv_skb(sk, skb);
556 else
557 sk_add_backlog(sk, skb);
558 bh_unlock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 sock_put(sk);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800560 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900562short_packet:
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800563 LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: %d/%u\n",
David S. Millerdb8dac22008-03-06 16:22:02 -0800564 proto == IPPROTO_UDPLITE ? "-Lite" : "",
Herbert Xu759e5d02007-03-25 20:10:56 -0700565 ulen, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567discard:
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700568 UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 kfree_skb(skb);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800570 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800572
Herbert Xue5bbef22007-10-15 12:50:28 -0700573static __inline__ int udpv6_rcv(struct sk_buff *skb)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800574{
Eric Dumazet645ca702008-10-29 01:41:45 -0700575 return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800576}
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578/*
579 * Throw away all pending data and cancel the corking. Socket is locked.
580 */
581static void udp_v6_flush_pending_frames(struct sock *sk)
582{
583 struct udp_sock *up = udp_sk(sk);
584
Denis V. Lunev36d926b2008-06-04 15:49:07 +0400585 if (up->pending == AF_INET)
586 udp_flush_pending_frames(sk);
587 else if (up->pending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 up->len = 0;
589 up->pending = 0;
590 ip6_flush_pending_frames(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
594/*
595 * Sending
596 */
597
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800598static int udp_v6_push_pending_frames(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 struct sk_buff *skb;
601 struct udphdr *uh;
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800602 struct udp_sock *up = udp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 struct inet_sock *inet = inet_sk(sk);
604 struct flowi *fl = &inet->cork.fl;
605 int err = 0;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100606 int is_udplite = IS_UDPLITE(sk);
Al Viro868c86b2006-11-14 21:35:48 -0800607 __wsum csum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 /* Grab the skbuff where UDP header space exists. */
610 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
611 goto out;
612
613 /*
614 * Create a UDP header
615 */
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300616 uh = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 uh->source = fl->fl_ip_sport;
618 uh->dest = fl->fl_ip_dport;
619 uh->len = htons(up->len);
620 uh->check = 0;
621
Wang Chenb2bf1e22007-12-03 22:34:16 +1100622 if (is_udplite)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800623 csum = udplite_csum_outgoing(sk, skb);
624 else
625 csum = udp_csum_outgoing(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800627 /* add protocol-dependent pseudo-header */
628 uh->check = csum_ipv6_magic(&fl->fl6_src, &fl->fl6_dst,
629 up->len, fl->proto, csum );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (uh->check == 0)
Al Virof6ab0282006-11-16 02:36:50 -0800631 uh->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 err = ip6_push_pending_frames(sk);
634out:
635 up->len = 0;
636 up->pending = 0;
YOSHIFUJI Hideakicd562c92007-09-14 17:15:01 -0700637 if (!err)
Pavel Emelyanov235b9f72008-07-05 21:19:20 -0700638 UDP6_INC_STATS_USER(sock_net(sk),
639 UDP_MIB_OUTDATAGRAMS, is_udplite);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return err;
641}
642
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800643int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 struct msghdr *msg, size_t len)
645{
646 struct ipv6_txoptions opt_space;
647 struct udp_sock *up = udp_sk(sk);
648 struct inet_sock *inet = inet_sk(sk);
649 struct ipv6_pinfo *np = inet6_sk(sk);
650 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
651 struct in6_addr *daddr, *final_p = NULL, final;
652 struct ipv6_txoptions *opt = NULL;
653 struct ip6_flowlabel *flowlabel = NULL;
Herbert Xu132a55f2006-10-03 14:34:00 -0700654 struct flowi fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 struct dst_entry *dst;
656 int addr_len = msg->msg_namelen;
657 int ulen = len;
658 int hlimit = -1;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900659 int tclass = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
661 int err;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700662 int connected = 0;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100663 int is_udplite = IS_UDPLITE(sk);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800664 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 /* destination address check */
667 if (sin6) {
668 if (addr_len < offsetof(struct sockaddr, sa_data))
669 return -EINVAL;
670
671 switch (sin6->sin6_family) {
672 case AF_INET6:
673 if (addr_len < SIN6_LEN_RFC2133)
674 return -EINVAL;
675 daddr = &sin6->sin6_addr;
676 break;
677 case AF_INET:
678 goto do_udp_sendmsg;
679 case AF_UNSPEC:
680 msg->msg_name = sin6 = NULL;
681 msg->msg_namelen = addr_len = 0;
682 daddr = NULL;
683 break;
684 default:
685 return -EINVAL;
686 }
687 } else if (!up->pending) {
688 if (sk->sk_state != TCP_ESTABLISHED)
689 return -EDESTADDRREQ;
690 daddr = &np->daddr;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900691 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 daddr = NULL;
693
694 if (daddr) {
Brian Haleye773e4f2007-08-24 23:16:08 -0700695 if (ipv6_addr_v4mapped(daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 struct sockaddr_in sin;
697 sin.sin_family = AF_INET;
698 sin.sin_port = sin6 ? sin6->sin6_port : inet->dport;
699 sin.sin_addr.s_addr = daddr->s6_addr32[3];
700 msg->msg_name = &sin;
701 msg->msg_namelen = sizeof(sin);
702do_udp_sendmsg:
703 if (__ipv6_only_sock(sk))
704 return -ENETUNREACH;
705 return udp_sendmsg(iocb, sk, msg, len);
706 }
707 }
708
709 if (up->pending == AF_INET)
710 return udp_sendmsg(iocb, sk, msg, len);
711
712 /* Rough check on arithmetic overflow,
YOSHIFUJI Hideakib59e1392007-03-30 14:45:35 -0700713 better check is made in ip6_append_data().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 */
715 if (len > INT_MAX - sizeof(struct udphdr))
716 return -EMSGSIZE;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (up->pending) {
719 /*
720 * There are pending frames.
721 * The socket lock must be held while it's corked.
722 */
723 lock_sock(sk);
724 if (likely(up->pending)) {
725 if (unlikely(up->pending != AF_INET6)) {
726 release_sock(sk);
727 return -EAFNOSUPPORT;
728 }
729 dst = NULL;
730 goto do_append_data;
731 }
732 release_sock(sk);
733 }
734 ulen += sizeof(struct udphdr);
735
Herbert Xu132a55f2006-10-03 14:34:00 -0700736 memset(&fl, 0, sizeof(fl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 if (sin6) {
739 if (sin6->sin6_port == 0)
740 return -EINVAL;
741
Herbert Xu132a55f2006-10-03 14:34:00 -0700742 fl.fl_ip_dport = sin6->sin6_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 daddr = &sin6->sin6_addr;
744
745 if (np->sndflow) {
Herbert Xu132a55f2006-10-03 14:34:00 -0700746 fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
747 if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
748 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (flowlabel == NULL)
750 return -EINVAL;
751 daddr = &flowlabel->dst;
752 }
753 }
754
755 /*
756 * Otherwise it will be difficult to maintain
757 * sk->sk_dst_cache.
758 */
759 if (sk->sk_state == TCP_ESTABLISHED &&
760 ipv6_addr_equal(daddr, &np->daddr))
761 daddr = &np->daddr;
762
763 if (addr_len >= sizeof(struct sockaddr_in6) &&
764 sin6->sin6_scope_id &&
765 ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
Herbert Xu132a55f2006-10-03 14:34:00 -0700766 fl.oif = sin6->sin6_scope_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 } else {
768 if (sk->sk_state != TCP_ESTABLISHED)
769 return -EDESTADDRREQ;
770
Herbert Xu132a55f2006-10-03 14:34:00 -0700771 fl.fl_ip_dport = inet->dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 daddr = &np->daddr;
Herbert Xu132a55f2006-10-03 14:34:00 -0700773 fl.fl6_flowlabel = np->flow_label;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700774 connected = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776
Herbert Xu132a55f2006-10-03 14:34:00 -0700777 if (!fl.oif)
778 fl.oif = sk->sk_bound_dev_if;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 if (msg->msg_controllen) {
781 opt = &opt_space;
782 memset(opt, 0, sizeof(struct ipv6_txoptions));
783 opt->tot_len = sizeof(*opt);
784
YOSHIFUJI Hideaki91e19082008-06-04 13:02:49 +0900785 err = datagram_send_ctl(sock_net(sk), msg, &fl, opt, &hlimit, &tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (err < 0) {
787 fl6_sock_release(flowlabel);
788 return err;
789 }
Herbert Xu132a55f2006-10-03 14:34:00 -0700790 if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
791 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (flowlabel == NULL)
793 return -EINVAL;
794 }
795 if (!(opt->opt_nflen|opt->opt_flen))
796 opt = NULL;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700797 connected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799 if (opt == NULL)
800 opt = np->opt;
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900801 if (flowlabel)
802 opt = fl6_merge_options(&opt_space, flowlabel, opt);
803 opt = ipv6_fixup_options(&opt_space, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800805 fl.proto = sk->sk_protocol;
Brian Haley876c7f42008-04-11 00:38:24 -0400806 if (!ipv6_addr_any(daddr))
807 ipv6_addr_copy(&fl.fl6_dst, daddr);
808 else
809 fl.fl6_dst.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
Herbert Xu132a55f2006-10-03 14:34:00 -0700810 if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
811 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
812 fl.fl_ip_sport = inet->sport;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 /* merge ip6_build_xmit from ip6_output */
815 if (opt && opt->srcrt) {
816 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
Herbert Xu132a55f2006-10-03 14:34:00 -0700817 ipv6_addr_copy(&final, &fl.fl6_dst);
818 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 final_p = &final;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700820 connected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822
Herbert Xu132a55f2006-10-03 14:34:00 -0700823 if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst)) {
824 fl.oif = np->mcast_oif;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700825 connected = 0;
826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Herbert Xu132a55f2006-10-03 14:34:00 -0700828 security_sk_classify_flow(sk, &fl);
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -0700829
Herbert Xu132a55f2006-10-03 14:34:00 -0700830 err = ip6_sk_dst_lookup(sk, &dst, &fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (err)
832 goto out;
833 if (final_p)
Herbert Xu132a55f2006-10-03 14:34:00 -0700834 ipv6_addr_copy(&fl.fl6_dst, final_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Herbert Xubb728452007-12-12 18:48:58 -0800836 if ((err = __xfrm_lookup(&dst, &fl, sk, XFRM_LOOKUP_WAIT)) < 0) {
David S. Miller14e50e52007-05-24 18:17:54 -0700837 if (err == -EREMOTE)
838 err = ip6_dst_blackhole(sk, &dst, &fl);
839 if (err < 0)
840 goto out;
841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 if (hlimit < 0) {
Herbert Xu132a55f2006-10-03 14:34:00 -0700844 if (ipv6_addr_is_multicast(&fl.fl6_dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 hlimit = np->mcast_hops;
846 else
847 hlimit = np->hop_limit;
848 if (hlimit < 0)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -0400849 hlimit = ip6_dst_hoplimit(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
851
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900852 if (tclass < 0) {
853 tclass = np->tclass;
854 if (tclass < 0)
855 tclass = 0;
856 }
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (msg->msg_flags&MSG_CONFIRM)
859 goto do_confirm;
860back_from_confirm:
861
862 lock_sock(sk);
863 if (unlikely(up->pending)) {
864 /* The socket is already corked while preparing it. */
865 /* ... which is an evident application bug. --ANK */
866 release_sock(sk);
867
Patrick McHardy64ce2072005-08-09 20:50:53 -0700868 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 err = -EINVAL;
870 goto out;
871 }
872
873 up->pending = AF_INET6;
874
875do_append_data:
876 up->len += ulen;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800877 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
878 err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen,
Herbert Xu132a55f2006-10-03 14:34:00 -0700879 sizeof(struct udphdr), hlimit, tclass, opt, &fl,
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900880 (struct rt6_info*)dst,
881 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (err)
883 udp_v6_flush_pending_frames(sk);
884 else if (!corkreq)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800885 err = udp_v6_push_pending_frames(sk);
Herbert Xu1e0c14f2006-10-03 14:35:49 -0700886 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
887 up->pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
David S. Millera5e7c212005-10-03 14:21:58 -0700889 if (dst) {
890 if (connected) {
891 ip6_dst_store(sk, dst,
Herbert Xu132a55f2006-10-03 14:34:00 -0700892 ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700893 &np->daddr : NULL,
894#ifdef CONFIG_IPV6_SUBTREES
Herbert Xu132a55f2006-10-03 14:34:00 -0700895 ipv6_addr_equal(&fl.fl6_src, &np->saddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700896 &np->saddr :
897#endif
898 NULL);
David S. Millera5e7c212005-10-03 14:21:58 -0700899 } else {
900 dst_release(dst);
901 }
YOSHIFUJI Hideakia3c96082008-06-04 01:30:25 +0900902 dst = NULL;
David S. Millera5e7c212005-10-03 14:21:58 -0700903 }
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (err > 0)
906 err = np->recverr ? net_xmit_errno(err) : 0;
907 release_sock(sk);
908out:
YOSHIFUJI Hideakia3c96082008-06-04 01:30:25 +0900909 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 fl6_sock_release(flowlabel);
YOSHIFUJI Hideakicd562c92007-09-14 17:15:01 -0700911 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return len;
David S. Millera18135e2006-08-15 00:00:09 -0700913 /*
914 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
915 * ENOBUFS might not be good (it's not tunable per se), but otherwise
916 * we don't have a good statistic (IpOutDiscards but it can be too many
917 * things). We could add another new stat but at least for now that
918 * seems like overkill.
919 */
920 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
Pavel Emelyanov235b9f72008-07-05 21:19:20 -0700921 UDP6_INC_STATS_USER(sock_net(sk),
922 UDP_MIB_SNDBUFERRORS, is_udplite);
David S. Millera18135e2006-08-15 00:00:09 -0700923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return err;
925
926do_confirm:
927 dst_confirm(dst);
928 if (!(msg->msg_flags&MSG_PROBE) || len)
929 goto back_from_confirm;
930 err = 0;
931 goto out;
932}
933
Brian Haley7d06b2e2008-06-14 17:04:49 -0700934void udpv6_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936 lock_sock(sk);
937 udp_v6_flush_pending_frames(sk);
938 release_sock(sk);
939
940 inet6_destroy_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
943/*
944 * Socket option code for UDP
945 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800946int udpv6_setsockopt(struct sock *sk, int level, int optname,
947 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800948{
David S. Millerdb8dac22008-03-06 16:22:02 -0800949 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800950 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
951 udp_v6_push_pending_frames);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800952 return ipv6_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800953}
954
955#ifdef CONFIG_COMPAT
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800956int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
957 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800958{
David S. Millerdb8dac22008-03-06 16:22:02 -0800959 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800960 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
961 udp_v6_push_pending_frames);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800962 return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800963}
964#endif
965
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800966int udpv6_getsockopt(struct sock *sk, int level, int optname,
967 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800968{
David S. Millerdb8dac22008-03-06 16:22:02 -0800969 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800970 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800971 return ipv6_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800972}
973
974#ifdef CONFIG_COMPAT
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800975int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
976 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800977{
David S. Millerdb8dac22008-03-06 16:22:02 -0800978 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800979 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800980 return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800981}
982#endif
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984static struct inet6_protocol udpv6_protocol = {
985 .handler = udpv6_rcv,
986 .err_handler = udpv6_err,
987 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
988};
989
990/* ------------------------------------------------------------------------ */
991#ifdef CONFIG_PROC_FS
992
993static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
994{
995 struct inet_sock *inet = inet_sk(sp);
996 struct ipv6_pinfo *np = inet6_sk(sp);
997 struct in6_addr *dest, *src;
998 __u16 destp, srcp;
999
1000 dest = &np->daddr;
1001 src = &np->rcv_saddr;
1002 destp = ntohs(inet->dport);
1003 srcp = ntohs(inet->sport);
1004 seq_printf(seq,
1005 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
Eric Dumazetcb61cb92008-06-17 21:04:56 -07001006 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 bucket,
1008 src->s6_addr32[0], src->s6_addr32[1],
1009 src->s6_addr32[2], src->s6_addr32[3], srcp,
1010 dest->s6_addr32[0], dest->s6_addr32[1],
1011 dest->s6_addr32[2], dest->s6_addr32[3], destp,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001012 sp->sk_state,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 atomic_read(&sp->sk_wmem_alloc),
1014 atomic_read(&sp->sk_rmem_alloc),
1015 0, 0L, 0,
1016 sock_i_uid(sp), 0,
1017 sock_i_ino(sp),
Eric Dumazetcb61cb92008-06-17 21:04:56 -07001018 atomic_read(&sp->sk_refcnt), sp,
1019 atomic_read(&sp->sk_drops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001022int udp6_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 if (v == SEQ_START_TOKEN)
1025 seq_printf(seq,
1026 " sl "
1027 "local_address "
1028 "remote_address "
1029 "st tx_queue rx_queue tr tm->when retrnsmt"
Eric Dumazetcb61cb92008-06-17 21:04:56 -07001030 " uid timeout inode ref pointer drops\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 else
1032 udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
1033 return 0;
1034}
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036static struct udp_seq_afinfo udp6_seq_afinfo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 .name = "udp6",
1038 .family = AF_INET6,
Eric Dumazet645ca702008-10-29 01:41:45 -07001039 .udp_table = &udp_table,
Denis V. Lunev4ad96d32008-03-28 18:25:53 -07001040 .seq_fops = {
1041 .owner = THIS_MODULE,
1042 },
Denis V. Lunevdda61922008-03-28 18:24:26 -07001043 .seq_ops = {
1044 .show = udp6_seq_show,
1045 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046};
1047
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001048int udp6_proc_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001050 return udp_proc_register(net, &udp6_seq_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001053void udp6_proc_exit(struct net *net) {
1054 udp_proc_unregister(net, &udp6_seq_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056#endif /* CONFIG_PROC_FS */
1057
1058/* ------------------------------------------------------------------------ */
1059
1060struct proto udpv6_prot = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001061 .name = "UDPv6",
1062 .owner = THIS_MODULE,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001063 .close = udp_lib_close,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001064 .connect = ip6_datagram_connect,
1065 .disconnect = udp_disconnect,
1066 .ioctl = udp_ioctl,
1067 .destroy = udpv6_destroy_sock,
1068 .setsockopt = udpv6_setsockopt,
1069 .getsockopt = udpv6_getsockopt,
1070 .sendmsg = udpv6_sendmsg,
1071 .recvmsg = udpv6_recvmsg,
1072 .backlog_rcv = udpv6_queue_rcv_skb,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001073 .hash = udp_lib_hash,
1074 .unhash = udp_lib_unhash,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001075 .get_port = udp_v6_get_port,
Hideo Aoki95766ff2007-12-31 00:29:24 -08001076 .memory_allocated = &udp_memory_allocated,
1077 .sysctl_mem = sysctl_udp_mem,
1078 .sysctl_wmem = &sysctl_udp_wmem_min,
1079 .sysctl_rmem = &sysctl_udp_rmem_min,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001080 .obj_size = sizeof(struct udp6_sock),
Eric Dumazet271b72c2008-10-29 02:11:14 -07001081 .slab_flags = SLAB_DESTROY_BY_RCU,
Eric Dumazet645ca702008-10-29 01:41:45 -07001082 .h.udp_table = &udp_table,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001083#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001084 .compat_setsockopt = compat_udpv6_setsockopt,
1085 .compat_getsockopt = compat_udpv6_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001086#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087};
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089static struct inet_protosw udpv6_protosw = {
1090 .type = SOCK_DGRAM,
1091 .protocol = IPPROTO_UDP,
1092 .prot = &udpv6_prot,
1093 .ops = &inet6_dgram_ops,
1094 .capability =-1,
1095 .no_check = UDP_CSUM_DEFAULT,
1096 .flags = INET_PROTOSW_PERMANENT,
1097};
1098
1099
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001100int __init udpv6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001102 int ret;
1103
1104 ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1105 if (ret)
1106 goto out;
1107
1108 ret = inet6_register_protosw(&udpv6_protosw);
1109 if (ret)
1110 goto out_udpv6_protocol;
1111out:
1112 return ret;
1113
1114out_udpv6_protocol:
1115 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1116 goto out;
1117}
1118
Daniel Lezcano09f77092007-12-13 05:34:58 -08001119void udpv6_exit(void)
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001120{
1121 inet6_unregister_protosw(&udpv6_protosw);
1122 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123}