blob: d1477b350f761aa474f47c7444d3327d0a19bbe1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * UDP over IPv6
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Based on linux/ipv4/udp.c
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Fixes:
11 * Hideaki YOSHIFUJI : sin6_scope_id support
12 * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
13 * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind
14 * a single port at the same time.
15 * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data
16 * YOSHIFUJI Hideaki @USAGI: convert /proc/net/udp6 to seq_file.
17 *
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version
21 * 2 of the License, or (at your option) any later version.
22 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/errno.h>
25#include <linux/types.h>
26#include <linux/socket.h>
27#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/net.h>
29#include <linux/in6.h>
30#include <linux/netdevice.h>
31#include <linux/if_arp.h>
32#include <linux/ipv6.h>
33#include <linux/icmpv6.h>
34#include <linux/init.h>
Herbert Xu1781f7f2007-12-11 11:30:32 -080035#include <linux/module.h>
Herbert Xu3305b802005-12-13 23:16:37 -080036#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/uaccess.h>
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <net/ndisc.h>
40#include <net/protocol.h>
41#include <net/transp_v6.h>
42#include <net/ip6_route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <net/raw.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070044#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <net/ip6_checksum.h>
46#include <net/xfrm.h>
47
48#include <linux/proc_fs.h>
49#include <linux/seq_file.h>
Gerrit Renkerba4e58e2006-11-27 11:10:57 -080050#include "udp_impl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Pavel Emelyanov6ba5a3c2008-03-22 16:51:21 -070052int udp_v6_get_port(struct sock *sk, unsigned short snum)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Pavel Emelyanov6ba5a3c2008-03-22 16:51:21 -070054 return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
Pavel Emelyanovfa4d3c62008-01-31 05:07:57 -080057static struct sock *__udp6_lib_lookup(struct net *net,
58 struct in6_addr *saddr, __be16 sport,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -080059 struct in6_addr *daddr, __be16 dport,
60 int dif, struct hlist_head udptable[])
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 struct sock *sk, *result = NULL;
63 struct hlist_node *node;
64 unsigned short hnum = ntohs(dport);
65 int badness = -1;
66
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090067 read_lock(&udp_hash_lock);
Pavel Emelyanov19c75782008-06-16 17:12:29 -070068 sk_for_each(sk, node, &udptable[udp_hashfn(net, hnum)]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 struct inet_sock *inet = inet_sk(sk);
70
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +090071 if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum &&
Pavel Emelyanovfa4d3c62008-01-31 05:07:57 -080072 sk->sk_family == PF_INET6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 struct ipv6_pinfo *np = inet6_sk(sk);
74 int score = 0;
75 if (inet->dport) {
76 if (inet->dport != sport)
77 continue;
78 score++;
79 }
80 if (!ipv6_addr_any(&np->rcv_saddr)) {
81 if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
82 continue;
83 score++;
84 }
85 if (!ipv6_addr_any(&np->daddr)) {
86 if (!ipv6_addr_equal(&np->daddr, saddr))
87 continue;
88 score++;
89 }
90 if (sk->sk_bound_dev_if) {
91 if (sk->sk_bound_dev_if != dif)
92 continue;
93 score++;
94 }
Stephen Hemmingeradd459a2007-03-08 20:42:35 -080095 if (score == 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 result = sk;
97 break;
Stephen Hemmingeradd459a2007-03-08 20:42:35 -080098 } else if (score > badness) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 result = sk;
100 badness = score;
101 }
102 }
103 }
104 if (result)
105 sock_hold(result);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900106 read_unlock(&udp_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return result;
108}
109
110/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * This should be easy, if there is something there we
112 * return it, otherwise we block.
113 */
114
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800115int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 struct msghdr *msg, size_t len,
117 int noblock, int flags, int *addr_len)
118{
119 struct ipv6_pinfo *np = inet6_sk(sk);
120 struct inet_sock *inet = inet_sk(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900121 struct sk_buff *skb;
Herbert Xu759e5d02007-03-25 20:10:56 -0700122 unsigned int ulen, copied;
Herbert Xua59322b2007-12-05 01:53:40 -0800123 int peeked;
Herbert Xu759e5d02007-03-25 20:10:56 -0700124 int err;
125 int is_udplite = IS_UDPLITE(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900127 if (addr_len)
128 *addr_len=sizeof(struct sockaddr_in6);
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if (flags & MSG_ERRQUEUE)
131 return ipv6_recv_error(sk, msg, len);
132
133try_again:
Herbert Xua59322b2007-12-05 01:53:40 -0800134 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
135 &peeked, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (!skb)
137 goto out;
138
Herbert Xu759e5d02007-03-25 20:10:56 -0700139 ulen = skb->len - sizeof(struct udphdr);
140 copied = len;
141 if (copied > ulen)
142 copied = ulen;
143 else if (copied < ulen)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900144 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800146 /*
Herbert Xu759e5d02007-03-25 20:10:56 -0700147 * If checksum is needed at all, try to do it while copying the
148 * data. If the data is truncated, or if we only want a partial
149 * coverage checksum (UDP-Lite), do it before the copy.
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800150 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800151
Herbert Xu759e5d02007-03-25 20:10:56 -0700152 if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
153 if (udp_lib_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 goto csum_copy_err;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800155 }
156
Herbert Xu60476372007-04-09 11:59:39 -0700157 if (skb_csum_unnecessary(skb))
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800158 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
159 msg->msg_iov, copied );
160 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
162 if (err == -EINVAL)
163 goto csum_copy_err;
164 }
165 if (err)
166 goto out_free;
167
Herbert Xua59322b2007-12-05 01:53:40 -0800168 if (!peeked)
Pavel Emelyanov235b9f72008-07-05 21:19:20 -0700169 UDP6_INC_STATS_USER(sock_net(sk),
170 UDP_MIB_INDATAGRAMS, is_udplite);
Wang Chencb759942007-12-03 22:33:28 +1100171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 sock_recv_timestamp(msg, sk, skb);
173
174 /* Copy the address. */
175 if (msg->msg_name) {
176 struct sockaddr_in6 *sin6;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 sin6 = (struct sockaddr_in6 *) msg->msg_name;
179 sin6->sin6_family = AF_INET6;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300180 sin6->sin6_port = udp_hdr(skb)->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 sin6->sin6_flowinfo = 0;
182 sin6->sin6_scope_id = 0;
183
184 if (skb->protocol == htons(ETH_P_IP))
185 ipv6_addr_set(&sin6->sin6_addr, 0, 0,
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700186 htonl(0xffff), ip_hdr(skb)->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 else {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700188 ipv6_addr_copy(&sin6->sin6_addr,
189 &ipv6_hdr(skb)->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
191 sin6->sin6_scope_id = IP6CB(skb)->iif;
192 }
193
194 }
195 if (skb->protocol == htons(ETH_P_IP)) {
196 if (inet->cmsg_flags)
197 ip_cmsg_recv(msg, skb);
198 } else {
199 if (np->rxopt.all)
200 datagram_recv_ctl(sk, msg, skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 err = copied;
204 if (flags & MSG_TRUNC)
Herbert Xu759e5d02007-03-25 20:10:56 -0700205 err = ulen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207out_free:
Hideo Aoki95766ff2007-12-31 00:29:24 -0800208 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 skb_free_datagram(sk, skb);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800210 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211out:
212 return err;
213
214csum_copy_err:
Hideo Aoki95766ff2007-12-31 00:29:24 -0800215 lock_sock(sk);
Herbert Xu27ab2562007-12-05 01:51:58 -0800216 if (!skb_kill_datagram(sk, skb, flags))
Pavel Emelyanov235b9f72008-07-05 21:19:20 -0700217 UDP6_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800218 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Mitsuru Chinen7a0ff712007-11-05 21:29:17 -0800220 if (flags & MSG_DONTWAIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 goto try_again;
223}
224
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800225void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
226 int type, int code, int offset, __be32 info,
227 struct hlist_head udptable[] )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 struct ipv6_pinfo *np;
230 struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 struct in6_addr *saddr = &hdr->saddr;
232 struct in6_addr *daddr = &hdr->daddr;
233 struct udphdr *uh = (struct udphdr*)(skb->data+offset);
234 struct sock *sk;
235 int err;
236
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900237 sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800238 saddr, uh->source, inet6_iif(skb), udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (sk == NULL)
240 return;
241
242 np = inet6_sk(sk);
243
244 if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
245 goto out;
246
247 if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
248 goto out;
249
250 if (np->recverr)
251 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
252
253 sk->sk_err = err;
254 sk->sk_error_report(sk);
255out:
256 sock_put(sk);
257}
258
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800259static __inline__ void udpv6_err(struct sk_buff *skb,
260 struct inet6_skb_parm *opt, int type,
Al Viro8e5200f2006-11-20 18:06:37 -0800261 int code, int offset, __be32 info )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
YOSHIFUJI Hideaki77d0d352008-01-22 17:09:55 +0900263 __udp6_lib_err(skb, opt, type, code, offset, info, udp_hash);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800264}
265
266int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
267{
268 struct udp_sock *up = udp_sk(sk);
David S. Millera18135e2006-08-15 00:00:09 -0700269 int rc;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100270 int is_udplite = IS_UDPLITE(sk);
David S. Millera18135e2006-08-15 00:00:09 -0700271
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800272 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
273 goto drop;
274
275 /*
276 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
277 */
Wang Chenb2bf1e22007-12-03 22:34:16 +1100278 if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800279
280 if (up->pcrlen == 0) { /* full coverage was set */
281 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage"
282 " %d while full coverage %d requested\n",
283 UDP_SKB_CB(skb)->cscov, skb->len);
284 goto drop;
285 }
286 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
287 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d "
288 "too small, need min %d\n",
289 UDP_SKB_CB(skb)->cscov, up->pcrlen);
290 goto drop;
291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293
Herbert Xu1ab6eb62007-03-06 20:29:58 -0800294 if (sk->sk_filter) {
295 if (udp_lib_checksum_complete(skb))
296 goto drop;
297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
David S. Millera18135e2006-08-15 00:00:09 -0700299 if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) {
300 /* Note that an ENOMEM error is charged twice */
Eric Dumazetcb61cb92008-06-17 21:04:56 -0700301 if (rc == -ENOMEM) {
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700302 UDP6_INC_STATS_BH(sock_net(sk),
303 UDP_MIB_RCVBUFERRORS, is_udplite);
Eric Dumazetcb61cb92008-06-17 21:04:56 -0700304 atomic_inc(&sk->sk_drops);
305 }
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800306 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
Wang Chencb759942007-12-03 22:33:28 +1100308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return 0;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800310drop:
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700311 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800312 kfree_skb(skb);
313 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
316static struct sock *udp_v6_mcast_next(struct sock *sk,
Al Viroe69a4ad2006-11-14 20:56:00 -0800317 __be16 loc_port, struct in6_addr *loc_addr,
318 __be16 rmt_port, struct in6_addr *rmt_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 int dif)
320{
321 struct hlist_node *node;
322 struct sock *s = sk;
323 unsigned short num = ntohs(loc_port);
324
325 sk_for_each_from(s, node) {
326 struct inet_sock *inet = inet_sk(s);
327
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900328 if (sock_net(s) != sock_net(sk))
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800329 continue;
330
Eric Dumazet95f30b32007-02-09 15:44:52 -0800331 if (s->sk_hash == num && s->sk_family == PF_INET6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 struct ipv6_pinfo *np = inet6_sk(s);
333 if (inet->dport) {
334 if (inet->dport != rmt_port)
335 continue;
336 }
337 if (!ipv6_addr_any(&np->daddr) &&
338 !ipv6_addr_equal(&np->daddr, rmt_addr))
339 continue;
340
341 if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
342 continue;
343
344 if (!ipv6_addr_any(&np->rcv_saddr)) {
David L Stevens40796c52005-09-14 21:10:20 -0700345 if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
346 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800348 if (!inet6_mc_check(s, loc_addr, rmt_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 continue;
350 return s;
351 }
352 }
353 return NULL;
354}
355
356/*
357 * Note: called only from the BH handler context,
358 * so we don't need to lock the hashes.
359 */
Pavel Emelyanove3163492008-06-16 17:12:11 -0700360static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
361 struct in6_addr *saddr, struct in6_addr *daddr,
362 struct hlist_head udptable[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 struct sock *sk, *sk2;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300365 const struct udphdr *uh = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 int dif;
367
368 read_lock(&udp_hash_lock);
Pavel Emelyanov19c75782008-06-16 17:12:29 -0700369 sk = sk_head(&udptable[udp_hashfn(net, ntohs(uh->dest))]);
YOSHIFUJI Hideakif2776ff2006-11-21 17:41:56 -0800370 dif = inet6_iif(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 sk = udp_v6_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
372 if (!sk) {
373 kfree_skb(skb);
374 goto out;
375 }
376
377 sk2 = sk;
378 while ((sk2 = udp_v6_mcast_next(sk_next(sk2), uh->dest, daddr,
379 uh->source, saddr, dif))) {
380 struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800381 if (buff) {
382 bh_lock_sock_nested(sk2);
383 if (!sock_owned_by_user(sk2))
384 udpv6_queue_rcv_skb(sk2, buff);
385 else
386 sk_add_backlog(sk2, buff);
387 bh_unlock_sock(sk2);
388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
Hideo Aoki95766ff2007-12-31 00:29:24 -0800390 bh_lock_sock_nested(sk);
391 if (!sock_owned_by_user(sk))
392 udpv6_queue_rcv_skb(sk, skb);
393 else
394 sk_add_backlog(sk, skb);
395 bh_unlock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396out:
397 read_unlock(&udp_hash_lock);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Herbert Xu759e5d02007-03-25 20:10:56 -0700401static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh,
402 int proto)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800403{
Herbert Xu759e5d02007-03-25 20:10:56 -0700404 int err;
405
406 UDP_SKB_CB(skb)->partial_cov = 0;
407 UDP_SKB_CB(skb)->cscov = skb->len;
408
David S. Millerdb8dac22008-03-06 16:22:02 -0800409 if (proto == IPPROTO_UDPLITE) {
Herbert Xu759e5d02007-03-25 20:10:56 -0700410 err = udplite_checksum_init(skb, uh);
411 if (err)
412 return err;
413 }
414
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800415 if (uh->check == 0) {
416 /* RFC 2460 section 8.1 says that we SHOULD log
417 this error. Well, it is reasonable.
418 */
419 LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
420 return 1;
421 }
422 if (skb->ip_summed == CHECKSUM_COMPLETE &&
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700423 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
Herbert Xu759e5d02007-03-25 20:10:56 -0700424 skb->len, proto, skb->csum))
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800425 skb->ip_summed = CHECKSUM_UNNECESSARY;
426
Herbert Xu60476372007-04-09 11:59:39 -0700427 if (!skb_csum_unnecessary(skb))
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700428 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
429 &ipv6_hdr(skb)->daddr,
Herbert Xu759e5d02007-03-25 20:10:56 -0700430 skb->len, proto, 0));
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800431
Herbert Xu759e5d02007-03-25 20:10:56 -0700432 return 0;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800433}
434
Herbert Xue5bbef22007-10-15 12:50:28 -0700435int __udp6_lib_rcv(struct sk_buff *skb, struct hlist_head udptable[],
Herbert Xu759e5d02007-03-25 20:10:56 -0700436 int proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 struct sock *sk;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900439 struct udphdr *uh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 struct net_device *dev = skb->dev;
441 struct in6_addr *saddr, *daddr;
442 u32 ulen = 0;
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700443 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
446 goto short_packet;
447
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700448 saddr = &ipv6_hdr(skb)->saddr;
449 daddr = &ipv6_hdr(skb)->daddr;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300450 uh = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 ulen = ntohs(uh->len);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800453 if (ulen > skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 goto short_packet;
455
Herbert Xu759e5d02007-03-25 20:10:56 -0700456 if (proto == IPPROTO_UDP) {
457 /* UDP validates ulen. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800459 /* Check for jumbo payload */
460 if (ulen == 0)
461 ulen = skb->len;
462
463 if (ulen < sizeof(*uh))
464 goto short_packet;
465
466 if (ulen < skb->len) {
467 if (pskb_trim_rcsum(skb, ulen))
468 goto short_packet;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700469 saddr = &ipv6_hdr(skb)->saddr;
470 daddr = &ipv6_hdr(skb)->daddr;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300471 uh = udp_hdr(skb);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474
Herbert Xu759e5d02007-03-25 20:10:56 -0700475 if (udp6_csum_init(skb, uh, proto))
476 goto discard;
477
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900478 /*
479 * Multicast receive code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800481 if (ipv6_addr_is_multicast(daddr))
Pavel Emelyanove3163492008-06-16 17:12:11 -0700482 return __udp6_lib_mcast_deliver(net, skb,
483 saddr, daddr, udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 /* Unicast */
486
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900487 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 * check socket cache ... must talk to Alan about his plans
489 * for sock caches... i'll skip this for now.
490 */
Pavel Emelyanove3163492008-06-16 17:12:11 -0700491 sk = __udp6_lib_lookup(net, saddr, uh->source,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800492 daddr, uh->dest, inet6_iif(skb), udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 if (sk == NULL) {
495 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
496 goto discard;
497
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800498 if (udp_lib_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 goto discard;
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700500 UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS,
501 proto == IPPROTO_UDPLITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev);
504
505 kfree_skb(skb);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800506 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 /* deliver */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900510
Hideo Aoki95766ff2007-12-31 00:29:24 -0800511 bh_lock_sock_nested(sk);
512 if (!sock_owned_by_user(sk))
513 udpv6_queue_rcv_skb(sk, skb);
514 else
515 sk_add_backlog(sk, skb);
516 bh_unlock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 sock_put(sk);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800518 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900520short_packet:
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800521 LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: %d/%u\n",
David S. Millerdb8dac22008-03-06 16:22:02 -0800522 proto == IPPROTO_UDPLITE ? "-Lite" : "",
Herbert Xu759e5d02007-03-25 20:10:56 -0700523 ulen, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525discard:
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700526 UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 kfree_skb(skb);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800528 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800530
Herbert Xue5bbef22007-10-15 12:50:28 -0700531static __inline__ int udpv6_rcv(struct sk_buff *skb)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800532{
Herbert Xue5bbef22007-10-15 12:50:28 -0700533 return __udp6_lib_rcv(skb, udp_hash, IPPROTO_UDP);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800534}
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536/*
537 * Throw away all pending data and cancel the corking. Socket is locked.
538 */
539static void udp_v6_flush_pending_frames(struct sock *sk)
540{
541 struct udp_sock *up = udp_sk(sk);
542
Denis V. Lunev36d926b2008-06-04 15:49:07 +0400543 if (up->pending == AF_INET)
544 udp_flush_pending_frames(sk);
545 else if (up->pending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 up->len = 0;
547 up->pending = 0;
548 ip6_flush_pending_frames(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
552/*
553 * Sending
554 */
555
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800556static int udp_v6_push_pending_frames(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 struct sk_buff *skb;
559 struct udphdr *uh;
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800560 struct udp_sock *up = udp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 struct inet_sock *inet = inet_sk(sk);
562 struct flowi *fl = &inet->cork.fl;
563 int err = 0;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100564 int is_udplite = IS_UDPLITE(sk);
Al Viro868c86b2006-11-14 21:35:48 -0800565 __wsum csum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 /* Grab the skbuff where UDP header space exists. */
568 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
569 goto out;
570
571 /*
572 * Create a UDP header
573 */
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300574 uh = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 uh->source = fl->fl_ip_sport;
576 uh->dest = fl->fl_ip_dport;
577 uh->len = htons(up->len);
578 uh->check = 0;
579
Wang Chenb2bf1e22007-12-03 22:34:16 +1100580 if (is_udplite)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800581 csum = udplite_csum_outgoing(sk, skb);
582 else
583 csum = udp_csum_outgoing(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800585 /* add protocol-dependent pseudo-header */
586 uh->check = csum_ipv6_magic(&fl->fl6_src, &fl->fl6_dst,
587 up->len, fl->proto, csum );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (uh->check == 0)
Al Virof6ab0282006-11-16 02:36:50 -0800589 uh->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 err = ip6_push_pending_frames(sk);
592out:
593 up->len = 0;
594 up->pending = 0;
YOSHIFUJI Hideakicd562c92007-09-14 17:15:01 -0700595 if (!err)
Pavel Emelyanov235b9f72008-07-05 21:19:20 -0700596 UDP6_INC_STATS_USER(sock_net(sk),
597 UDP_MIB_OUTDATAGRAMS, is_udplite);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return err;
599}
600
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800601int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 struct msghdr *msg, size_t len)
603{
604 struct ipv6_txoptions opt_space;
605 struct udp_sock *up = udp_sk(sk);
606 struct inet_sock *inet = inet_sk(sk);
607 struct ipv6_pinfo *np = inet6_sk(sk);
608 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
609 struct in6_addr *daddr, *final_p = NULL, final;
610 struct ipv6_txoptions *opt = NULL;
611 struct ip6_flowlabel *flowlabel = NULL;
Herbert Xu132a55f2006-10-03 14:34:00 -0700612 struct flowi fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 struct dst_entry *dst;
614 int addr_len = msg->msg_namelen;
615 int ulen = len;
616 int hlimit = -1;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900617 int tclass = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
619 int err;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700620 int connected = 0;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100621 int is_udplite = IS_UDPLITE(sk);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800622 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 /* destination address check */
625 if (sin6) {
626 if (addr_len < offsetof(struct sockaddr, sa_data))
627 return -EINVAL;
628
629 switch (sin6->sin6_family) {
630 case AF_INET6:
631 if (addr_len < SIN6_LEN_RFC2133)
632 return -EINVAL;
633 daddr = &sin6->sin6_addr;
634 break;
635 case AF_INET:
636 goto do_udp_sendmsg;
637 case AF_UNSPEC:
638 msg->msg_name = sin6 = NULL;
639 msg->msg_namelen = addr_len = 0;
640 daddr = NULL;
641 break;
642 default:
643 return -EINVAL;
644 }
645 } else if (!up->pending) {
646 if (sk->sk_state != TCP_ESTABLISHED)
647 return -EDESTADDRREQ;
648 daddr = &np->daddr;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900649 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 daddr = NULL;
651
652 if (daddr) {
Brian Haleye773e4f2007-08-24 23:16:08 -0700653 if (ipv6_addr_v4mapped(daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 struct sockaddr_in sin;
655 sin.sin_family = AF_INET;
656 sin.sin_port = sin6 ? sin6->sin6_port : inet->dport;
657 sin.sin_addr.s_addr = daddr->s6_addr32[3];
658 msg->msg_name = &sin;
659 msg->msg_namelen = sizeof(sin);
660do_udp_sendmsg:
661 if (__ipv6_only_sock(sk))
662 return -ENETUNREACH;
663 return udp_sendmsg(iocb, sk, msg, len);
664 }
665 }
666
667 if (up->pending == AF_INET)
668 return udp_sendmsg(iocb, sk, msg, len);
669
670 /* Rough check on arithmetic overflow,
YOSHIFUJI Hideakib59e1392007-03-30 14:45:35 -0700671 better check is made in ip6_append_data().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 */
673 if (len > INT_MAX - sizeof(struct udphdr))
674 return -EMSGSIZE;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (up->pending) {
677 /*
678 * There are pending frames.
679 * The socket lock must be held while it's corked.
680 */
681 lock_sock(sk);
682 if (likely(up->pending)) {
683 if (unlikely(up->pending != AF_INET6)) {
684 release_sock(sk);
685 return -EAFNOSUPPORT;
686 }
687 dst = NULL;
688 goto do_append_data;
689 }
690 release_sock(sk);
691 }
692 ulen += sizeof(struct udphdr);
693
Herbert Xu132a55f2006-10-03 14:34:00 -0700694 memset(&fl, 0, sizeof(fl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 if (sin6) {
697 if (sin6->sin6_port == 0)
698 return -EINVAL;
699
Herbert Xu132a55f2006-10-03 14:34:00 -0700700 fl.fl_ip_dport = sin6->sin6_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 daddr = &sin6->sin6_addr;
702
703 if (np->sndflow) {
Herbert Xu132a55f2006-10-03 14:34:00 -0700704 fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
705 if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
706 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (flowlabel == NULL)
708 return -EINVAL;
709 daddr = &flowlabel->dst;
710 }
711 }
712
713 /*
714 * Otherwise it will be difficult to maintain
715 * sk->sk_dst_cache.
716 */
717 if (sk->sk_state == TCP_ESTABLISHED &&
718 ipv6_addr_equal(daddr, &np->daddr))
719 daddr = &np->daddr;
720
721 if (addr_len >= sizeof(struct sockaddr_in6) &&
722 sin6->sin6_scope_id &&
723 ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
Herbert Xu132a55f2006-10-03 14:34:00 -0700724 fl.oif = sin6->sin6_scope_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 } else {
726 if (sk->sk_state != TCP_ESTABLISHED)
727 return -EDESTADDRREQ;
728
Herbert Xu132a55f2006-10-03 14:34:00 -0700729 fl.fl_ip_dport = inet->dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 daddr = &np->daddr;
Herbert Xu132a55f2006-10-03 14:34:00 -0700731 fl.fl6_flowlabel = np->flow_label;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700732 connected = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734
Herbert Xu132a55f2006-10-03 14:34:00 -0700735 if (!fl.oif)
736 fl.oif = sk->sk_bound_dev_if;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 if (msg->msg_controllen) {
739 opt = &opt_space;
740 memset(opt, 0, sizeof(struct ipv6_txoptions));
741 opt->tot_len = sizeof(*opt);
742
YOSHIFUJI Hideaki91e19082008-06-04 13:02:49 +0900743 err = datagram_send_ctl(sock_net(sk), msg, &fl, opt, &hlimit, &tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (err < 0) {
745 fl6_sock_release(flowlabel);
746 return err;
747 }
Herbert Xu132a55f2006-10-03 14:34:00 -0700748 if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
749 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (flowlabel == NULL)
751 return -EINVAL;
752 }
753 if (!(opt->opt_nflen|opt->opt_flen))
754 opt = NULL;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700755 connected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
757 if (opt == NULL)
758 opt = np->opt;
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900759 if (flowlabel)
760 opt = fl6_merge_options(&opt_space, flowlabel, opt);
761 opt = ipv6_fixup_options(&opt_space, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800763 fl.proto = sk->sk_protocol;
Brian Haley876c7f42008-04-11 00:38:24 -0400764 if (!ipv6_addr_any(daddr))
765 ipv6_addr_copy(&fl.fl6_dst, daddr);
766 else
767 fl.fl6_dst.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
Herbert Xu132a55f2006-10-03 14:34:00 -0700768 if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
769 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
770 fl.fl_ip_sport = inet->sport;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 /* merge ip6_build_xmit from ip6_output */
773 if (opt && opt->srcrt) {
774 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
Herbert Xu132a55f2006-10-03 14:34:00 -0700775 ipv6_addr_copy(&final, &fl.fl6_dst);
776 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 final_p = &final;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700778 connected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780
Herbert Xu132a55f2006-10-03 14:34:00 -0700781 if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst)) {
782 fl.oif = np->mcast_oif;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700783 connected = 0;
784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Herbert Xu132a55f2006-10-03 14:34:00 -0700786 security_sk_classify_flow(sk, &fl);
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -0700787
Herbert Xu132a55f2006-10-03 14:34:00 -0700788 err = ip6_sk_dst_lookup(sk, &dst, &fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (err)
790 goto out;
791 if (final_p)
Herbert Xu132a55f2006-10-03 14:34:00 -0700792 ipv6_addr_copy(&fl.fl6_dst, final_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Herbert Xubb728452007-12-12 18:48:58 -0800794 if ((err = __xfrm_lookup(&dst, &fl, sk, XFRM_LOOKUP_WAIT)) < 0) {
David S. Miller14e50e52007-05-24 18:17:54 -0700795 if (err == -EREMOTE)
796 err = ip6_dst_blackhole(sk, &dst, &fl);
797 if (err < 0)
798 goto out;
799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 if (hlimit < 0) {
Herbert Xu132a55f2006-10-03 14:34:00 -0700802 if (ipv6_addr_is_multicast(&fl.fl6_dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 hlimit = np->mcast_hops;
804 else
805 hlimit = np->hop_limit;
806 if (hlimit < 0)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -0400807 hlimit = ip6_dst_hoplimit(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900810 if (tclass < 0) {
811 tclass = np->tclass;
812 if (tclass < 0)
813 tclass = 0;
814 }
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (msg->msg_flags&MSG_CONFIRM)
817 goto do_confirm;
818back_from_confirm:
819
820 lock_sock(sk);
821 if (unlikely(up->pending)) {
822 /* The socket is already corked while preparing it. */
823 /* ... which is an evident application bug. --ANK */
824 release_sock(sk);
825
Patrick McHardy64ce2072005-08-09 20:50:53 -0700826 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 err = -EINVAL;
828 goto out;
829 }
830
831 up->pending = AF_INET6;
832
833do_append_data:
834 up->len += ulen;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800835 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
836 err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen,
Herbert Xu132a55f2006-10-03 14:34:00 -0700837 sizeof(struct udphdr), hlimit, tclass, opt, &fl,
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900838 (struct rt6_info*)dst,
839 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (err)
841 udp_v6_flush_pending_frames(sk);
842 else if (!corkreq)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800843 err = udp_v6_push_pending_frames(sk);
Herbert Xu1e0c14f2006-10-03 14:35:49 -0700844 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
845 up->pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
David S. Millera5e7c212005-10-03 14:21:58 -0700847 if (dst) {
848 if (connected) {
849 ip6_dst_store(sk, dst,
Herbert Xu132a55f2006-10-03 14:34:00 -0700850 ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700851 &np->daddr : NULL,
852#ifdef CONFIG_IPV6_SUBTREES
Herbert Xu132a55f2006-10-03 14:34:00 -0700853 ipv6_addr_equal(&fl.fl6_src, &np->saddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700854 &np->saddr :
855#endif
856 NULL);
David S. Millera5e7c212005-10-03 14:21:58 -0700857 } else {
858 dst_release(dst);
859 }
YOSHIFUJI Hideakia3c96082008-06-04 01:30:25 +0900860 dst = NULL;
David S. Millera5e7c212005-10-03 14:21:58 -0700861 }
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (err > 0)
864 err = np->recverr ? net_xmit_errno(err) : 0;
865 release_sock(sk);
866out:
YOSHIFUJI Hideakia3c96082008-06-04 01:30:25 +0900867 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 fl6_sock_release(flowlabel);
YOSHIFUJI Hideakicd562c92007-09-14 17:15:01 -0700869 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return len;
David S. Millera18135e2006-08-15 00:00:09 -0700871 /*
872 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
873 * ENOBUFS might not be good (it's not tunable per se), but otherwise
874 * we don't have a good statistic (IpOutDiscards but it can be too many
875 * things). We could add another new stat but at least for now that
876 * seems like overkill.
877 */
878 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
Pavel Emelyanov235b9f72008-07-05 21:19:20 -0700879 UDP6_INC_STATS_USER(sock_net(sk),
880 UDP_MIB_SNDBUFERRORS, is_udplite);
David S. Millera18135e2006-08-15 00:00:09 -0700881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return err;
883
884do_confirm:
885 dst_confirm(dst);
886 if (!(msg->msg_flags&MSG_PROBE) || len)
887 goto back_from_confirm;
888 err = 0;
889 goto out;
890}
891
Brian Haley7d06b2e2008-06-14 17:04:49 -0700892void udpv6_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 lock_sock(sk);
895 udp_v6_flush_pending_frames(sk);
896 release_sock(sk);
897
898 inet6_destroy_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
901/*
902 * Socket option code for UDP
903 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800904int udpv6_setsockopt(struct sock *sk, int level, int optname,
905 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800906{
David S. Millerdb8dac22008-03-06 16:22:02 -0800907 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800908 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
909 udp_v6_push_pending_frames);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800910 return ipv6_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800911}
912
913#ifdef CONFIG_COMPAT
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800914int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
915 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800916{
David S. Millerdb8dac22008-03-06 16:22:02 -0800917 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800918 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
919 udp_v6_push_pending_frames);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800920 return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800921}
922#endif
923
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800924int udpv6_getsockopt(struct sock *sk, int level, int optname,
925 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800926{
David S. Millerdb8dac22008-03-06 16:22:02 -0800927 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800928 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800929 return ipv6_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800930}
931
932#ifdef CONFIG_COMPAT
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800933int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
934 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800935{
David S. Millerdb8dac22008-03-06 16:22:02 -0800936 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800937 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800938 return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800939}
940#endif
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942static struct inet6_protocol udpv6_protocol = {
943 .handler = udpv6_rcv,
944 .err_handler = udpv6_err,
945 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
946};
947
948/* ------------------------------------------------------------------------ */
949#ifdef CONFIG_PROC_FS
950
951static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
952{
953 struct inet_sock *inet = inet_sk(sp);
954 struct ipv6_pinfo *np = inet6_sk(sp);
955 struct in6_addr *dest, *src;
956 __u16 destp, srcp;
957
958 dest = &np->daddr;
959 src = &np->rcv_saddr;
960 destp = ntohs(inet->dport);
961 srcp = ntohs(inet->sport);
962 seq_printf(seq,
963 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
Eric Dumazetcb61cb92008-06-17 21:04:56 -0700964 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 bucket,
966 src->s6_addr32[0], src->s6_addr32[1],
967 src->s6_addr32[2], src->s6_addr32[3], srcp,
968 dest->s6_addr32[0], dest->s6_addr32[1],
969 dest->s6_addr32[2], dest->s6_addr32[3], destp,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900970 sp->sk_state,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 atomic_read(&sp->sk_wmem_alloc),
972 atomic_read(&sp->sk_rmem_alloc),
973 0, 0L, 0,
974 sock_i_uid(sp), 0,
975 sock_i_ino(sp),
Eric Dumazetcb61cb92008-06-17 21:04:56 -0700976 atomic_read(&sp->sk_refcnt), sp,
977 atomic_read(&sp->sk_drops));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800980int udp6_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
982 if (v == SEQ_START_TOKEN)
983 seq_printf(seq,
984 " sl "
985 "local_address "
986 "remote_address "
987 "st tx_queue rx_queue tr tm->when retrnsmt"
Eric Dumazetcb61cb92008-06-17 21:04:56 -0700988 " uid timeout inode ref pointer drops\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 else
990 udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
991 return 0;
992}
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994static struct udp_seq_afinfo udp6_seq_afinfo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 .name = "udp6",
996 .family = AF_INET6,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800997 .hashtable = udp_hash,
Denis V. Lunev4ad96d32008-03-28 18:25:53 -0700998 .seq_fops = {
999 .owner = THIS_MODULE,
1000 },
Denis V. Lunevdda61922008-03-28 18:24:26 -07001001 .seq_ops = {
1002 .show = udp6_seq_show,
1003 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004};
1005
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001006int udp6_proc_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001008 return udp_proc_register(net, &udp6_seq_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001011void udp6_proc_exit(struct net *net) {
1012 udp_proc_unregister(net, &udp6_seq_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014#endif /* CONFIG_PROC_FS */
1015
1016/* ------------------------------------------------------------------------ */
1017
1018struct proto udpv6_prot = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001019 .name = "UDPv6",
1020 .owner = THIS_MODULE,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001021 .close = udp_lib_close,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001022 .connect = ip6_datagram_connect,
1023 .disconnect = udp_disconnect,
1024 .ioctl = udp_ioctl,
1025 .destroy = udpv6_destroy_sock,
1026 .setsockopt = udpv6_setsockopt,
1027 .getsockopt = udpv6_getsockopt,
1028 .sendmsg = udpv6_sendmsg,
1029 .recvmsg = udpv6_recvmsg,
1030 .backlog_rcv = udpv6_queue_rcv_skb,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001031 .hash = udp_lib_hash,
1032 .unhash = udp_lib_unhash,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001033 .get_port = udp_v6_get_port,
Hideo Aoki95766ff2007-12-31 00:29:24 -08001034 .memory_allocated = &udp_memory_allocated,
1035 .sysctl_mem = sysctl_udp_mem,
1036 .sysctl_wmem = &sysctl_udp_wmem_min,
1037 .sysctl_rmem = &sysctl_udp_rmem_min,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001038 .obj_size = sizeof(struct udp6_sock),
Pavel Emelyanov6ba5a3c2008-03-22 16:51:21 -07001039 .h.udp_hash = udp_hash,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001040#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001041 .compat_setsockopt = compat_udpv6_setsockopt,
1042 .compat_getsockopt = compat_udpv6_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001043#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044};
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046static struct inet_protosw udpv6_protosw = {
1047 .type = SOCK_DGRAM,
1048 .protocol = IPPROTO_UDP,
1049 .prot = &udpv6_prot,
1050 .ops = &inet6_dgram_ops,
1051 .capability =-1,
1052 .no_check = UDP_CSUM_DEFAULT,
1053 .flags = INET_PROTOSW_PERMANENT,
1054};
1055
1056
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001057int __init udpv6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001059 int ret;
1060
1061 ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1062 if (ret)
1063 goto out;
1064
1065 ret = inet6_register_protosw(&udpv6_protosw);
1066 if (ret)
1067 goto out_udpv6_protocol;
1068out:
1069 return ret;
1070
1071out_udpv6_protocol:
1072 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1073 goto out;
1074}
1075
Daniel Lezcano09f77092007-12-13 05:34:58 -08001076void udpv6_exit(void)
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001077{
1078 inet6_unregister_protosw(&udpv6_protosw);
1079 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}