blob: fa640765385e995350a4dc2a1fe9b4c19552ea3b [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 *
10 * $Id: udp.c,v 1.65 2002/02/01 22:01:04 davem Exp $
11 *
12 * Fixes:
13 * Hideaki YOSHIFUJI : sin6_scope_id support
14 * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
15 * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind
16 * a single port at the same time.
17 * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data
18 * YOSHIFUJI Hideaki @USAGI: convert /proc/net/udp6 to seq_file.
19 *
20 * This program is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU General Public License
22 * as published by the Free Software Foundation; either version
23 * 2 of the License, or (at your option) any later version.
24 */
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/errno.h>
27#include <linux/types.h>
28#include <linux/socket.h>
29#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/net.h>
31#include <linux/in6.h>
32#include <linux/netdevice.h>
33#include <linux/if_arp.h>
34#include <linux/ipv6.h>
35#include <linux/icmpv6.h>
36#include <linux/init.h>
Herbert Xu1781f7f2007-12-11 11:30:32 -080037#include <linux/module.h>
Herbert Xu3305b802005-12-13 23:16:37 -080038#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/uaccess.h>
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <net/ndisc.h>
42#include <net/protocol.h>
43#include <net/transp_v6.h>
44#include <net/ip6_route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <net/raw.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070046#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <net/ip6_checksum.h>
48#include <net/xfrm.h>
49
50#include <linux/proc_fs.h>
51#include <linux/seq_file.h>
Gerrit Renkerba4e58e2006-11-27 11:10:57 -080052#include "udp_impl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Eric Dumazetba899662005-08-26 12:05:31 -070054DEFINE_SNMP_STAT(struct udp_mib, udp_stats_in6) __read_mostly;
Herbert Xu1781f7f2007-12-11 11:30:32 -080055EXPORT_SYMBOL(udp_stats_in6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Gerrit Renker25030a72006-08-26 20:06:05 -070057static inline int udp_v6_get_port(struct sock *sk, unsigned short snum)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
David S. Millerdf2bc452007-06-05 15:18:43 -070059 return udp_get_port(sk, snum, ipv6_rcv_saddr_equal);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
Gerrit Renkerba4e58e2006-11-27 11:10:57 -080062static struct sock *__udp6_lib_lookup(struct in6_addr *saddr, __be16 sport,
63 struct in6_addr *daddr, __be16 dport,
64 int dif, struct hlist_head udptable[])
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 struct sock *sk, *result = NULL;
67 struct hlist_node *node;
68 unsigned short hnum = ntohs(dport);
69 int badness = -1;
70
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090071 read_lock(&udp_hash_lock);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -080072 sk_for_each(sk, node, &udptable[hnum & (UDP_HTABLE_SIZE - 1)]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 struct inet_sock *inet = inet_sk(sk);
74
Eric Dumazet95f30b32007-02-09 15:44:52 -080075 if (sk->sk_hash == hnum && sk->sk_family == PF_INET6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 struct ipv6_pinfo *np = inet6_sk(sk);
77 int score = 0;
78 if (inet->dport) {
79 if (inet->dport != sport)
80 continue;
81 score++;
82 }
83 if (!ipv6_addr_any(&np->rcv_saddr)) {
84 if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
85 continue;
86 score++;
87 }
88 if (!ipv6_addr_any(&np->daddr)) {
89 if (!ipv6_addr_equal(&np->daddr, saddr))
90 continue;
91 score++;
92 }
93 if (sk->sk_bound_dev_if) {
94 if (sk->sk_bound_dev_if != dif)
95 continue;
96 score++;
97 }
Stephen Hemmingeradd459a2007-03-08 20:42:35 -080098 if (score == 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 result = sk;
100 break;
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800101 } else if (score > badness) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 result = sk;
103 badness = score;
104 }
105 }
106 }
107 if (result)
108 sock_hold(result);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900109 read_unlock(&udp_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return result;
111}
112
113/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * This should be easy, if there is something there we
115 * return it, otherwise we block.
116 */
117
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800118int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 struct msghdr *msg, size_t len,
120 int noblock, int flags, int *addr_len)
121{
122 struct ipv6_pinfo *np = inet6_sk(sk);
123 struct inet_sock *inet = inet_sk(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900124 struct sk_buff *skb;
Herbert Xu759e5d02007-03-25 20:10:56 -0700125 unsigned int ulen, copied;
Herbert Xua59322b2007-12-05 01:53:40 -0800126 int peeked;
Herbert Xu759e5d02007-03-25 20:10:56 -0700127 int err;
128 int is_udplite = IS_UDPLITE(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900130 if (addr_len)
131 *addr_len=sizeof(struct sockaddr_in6);
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (flags & MSG_ERRQUEUE)
134 return ipv6_recv_error(sk, msg, len);
135
136try_again:
Herbert Xua59322b2007-12-05 01:53:40 -0800137 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
138 &peeked, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (!skb)
140 goto out;
141
Herbert Xu759e5d02007-03-25 20:10:56 -0700142 ulen = skb->len - sizeof(struct udphdr);
143 copied = len;
144 if (copied > ulen)
145 copied = ulen;
146 else if (copied < ulen)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900147 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800149 /*
Herbert Xu759e5d02007-03-25 20:10:56 -0700150 * If checksum is needed at all, try to do it while copying the
151 * data. If the data is truncated, or if we only want a partial
152 * coverage checksum (UDP-Lite), do it before the copy.
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800153 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800154
Herbert Xu759e5d02007-03-25 20:10:56 -0700155 if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
156 if (udp_lib_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 goto csum_copy_err;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800158 }
159
Herbert Xu60476372007-04-09 11:59:39 -0700160 if (skb_csum_unnecessary(skb))
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800161 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
162 msg->msg_iov, copied );
163 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
165 if (err == -EINVAL)
166 goto csum_copy_err;
167 }
168 if (err)
169 goto out_free;
170
Herbert Xua59322b2007-12-05 01:53:40 -0800171 if (!peeked)
172 UDP6_INC_STATS_USER(UDP_MIB_INDATAGRAMS, is_udplite);
Wang Chencb759942007-12-03 22:33:28 +1100173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 sock_recv_timestamp(msg, sk, skb);
175
176 /* Copy the address. */
177 if (msg->msg_name) {
178 struct sockaddr_in6 *sin6;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 sin6 = (struct sockaddr_in6 *) msg->msg_name;
181 sin6->sin6_family = AF_INET6;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300182 sin6->sin6_port = udp_hdr(skb)->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 sin6->sin6_flowinfo = 0;
184 sin6->sin6_scope_id = 0;
185
186 if (skb->protocol == htons(ETH_P_IP))
187 ipv6_addr_set(&sin6->sin6_addr, 0, 0,
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700188 htonl(0xffff), ip_hdr(skb)->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 else {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700190 ipv6_addr_copy(&sin6->sin6_addr,
191 &ipv6_hdr(skb)->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
193 sin6->sin6_scope_id = IP6CB(skb)->iif;
194 }
195
196 }
197 if (skb->protocol == htons(ETH_P_IP)) {
198 if (inet->cmsg_flags)
199 ip_cmsg_recv(msg, skb);
200 } else {
201 if (np->rxopt.all)
202 datagram_recv_ctl(sk, msg, skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 err = copied;
206 if (flags & MSG_TRUNC)
Herbert Xu759e5d02007-03-25 20:10:56 -0700207 err = ulen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209out_free:
210 skb_free_datagram(sk, skb);
211out:
212 return err;
213
214csum_copy_err:
Herbert Xu27ab2562007-12-05 01:51:58 -0800215 if (!skb_kill_datagram(sk, skb, flags))
216 UDP6_INC_STATS_USER(UDP_MIB_INERRORS, is_udplite);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Mitsuru Chinen7a0ff7162007-11-05 21:29:17 -0800218 if (flags & MSG_DONTWAIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 goto try_again;
221}
222
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800223void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
224 int type, int code, int offset, __be32 info,
225 struct hlist_head udptable[] )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 struct ipv6_pinfo *np;
228 struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 struct in6_addr *saddr = &hdr->saddr;
230 struct in6_addr *daddr = &hdr->daddr;
231 struct udphdr *uh = (struct udphdr*)(skb->data+offset);
232 struct sock *sk;
233 int err;
234
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800235 sk = __udp6_lib_lookup(daddr, uh->dest,
236 saddr, uh->source, inet6_iif(skb), udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (sk == NULL)
238 return;
239
240 np = inet6_sk(sk);
241
242 if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
243 goto out;
244
245 if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
246 goto out;
247
248 if (np->recverr)
249 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
250
251 sk->sk_err = err;
252 sk->sk_error_report(sk);
253out:
254 sock_put(sk);
255}
256
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800257static __inline__ void udpv6_err(struct sk_buff *skb,
258 struct inet6_skb_parm *opt, int type,
Al Viro8e5200f2006-11-20 18:06:37 -0800259 int code, int offset, __be32 info )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800261 return __udp6_lib_err(skb, opt, type, code, offset, info, udp_hash);
262}
263
264int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
265{
266 struct udp_sock *up = udp_sk(sk);
David S. Millera18135e2006-08-15 00:00:09 -0700267 int rc;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100268 int is_udplite = IS_UDPLITE(sk);
David S. Millera18135e2006-08-15 00:00:09 -0700269
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800270 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
271 goto drop;
272
273 /*
274 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
275 */
Wang Chenb2bf1e22007-12-03 22:34:16 +1100276 if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800277
278 if (up->pcrlen == 0) { /* full coverage was set */
279 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage"
280 " %d while full coverage %d requested\n",
281 UDP_SKB_CB(skb)->cscov, skb->len);
282 goto drop;
283 }
284 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
285 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d "
286 "too small, need min %d\n",
287 UDP_SKB_CB(skb)->cscov, up->pcrlen);
288 goto drop;
289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291
Herbert Xu1ab6eb62007-03-06 20:29:58 -0800292 if (sk->sk_filter) {
293 if (udp_lib_checksum_complete(skb))
294 goto drop;
295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
David S. Millera18135e2006-08-15 00:00:09 -0700297 if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) {
298 /* Note that an ENOMEM error is charged twice */
299 if (rc == -ENOMEM)
Wang Chenb2bf1e22007-12-03 22:34:16 +1100300 UDP6_INC_STATS_BH(UDP_MIB_RCVBUFERRORS, is_udplite);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800301 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
Wang Chencb759942007-12-03 22:33:28 +1100303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return 0;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800305drop:
Wang Chenb2bf1e22007-12-03 22:34:16 +1100306 UDP6_INC_STATS_BH(UDP_MIB_INERRORS, is_udplite);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800307 kfree_skb(skb);
308 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
311static struct sock *udp_v6_mcast_next(struct sock *sk,
Al Viroe69a4ad2006-11-14 20:56:00 -0800312 __be16 loc_port, struct in6_addr *loc_addr,
313 __be16 rmt_port, struct in6_addr *rmt_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 int dif)
315{
316 struct hlist_node *node;
317 struct sock *s = sk;
318 unsigned short num = ntohs(loc_port);
319
320 sk_for_each_from(s, node) {
321 struct inet_sock *inet = inet_sk(s);
322
Eric Dumazet95f30b32007-02-09 15:44:52 -0800323 if (s->sk_hash == num && s->sk_family == PF_INET6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 struct ipv6_pinfo *np = inet6_sk(s);
325 if (inet->dport) {
326 if (inet->dport != rmt_port)
327 continue;
328 }
329 if (!ipv6_addr_any(&np->daddr) &&
330 !ipv6_addr_equal(&np->daddr, rmt_addr))
331 continue;
332
333 if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
334 continue;
335
336 if (!ipv6_addr_any(&np->rcv_saddr)) {
David L Stevens40796c52005-09-14 21:10:20 -0700337 if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
338 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800340 if (!inet6_mc_check(s, loc_addr, rmt_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 continue;
342 return s;
343 }
344 }
345 return NULL;
346}
347
348/*
349 * Note: called only from the BH handler context,
350 * so we don't need to lock the hashes.
351 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800352static int __udp6_lib_mcast_deliver(struct sk_buff *skb, struct in6_addr *saddr,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900353 struct in6_addr *daddr, struct hlist_head udptable[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 struct sock *sk, *sk2;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300356 const struct udphdr *uh = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 int dif;
358
359 read_lock(&udp_hash_lock);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800360 sk = sk_head(&udptable[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]);
YOSHIFUJI Hideakif2776ff2006-11-21 17:41:56 -0800361 dif = inet6_iif(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 sk = udp_v6_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
363 if (!sk) {
364 kfree_skb(skb);
365 goto out;
366 }
367
368 sk2 = sk;
369 while ((sk2 = udp_v6_mcast_next(sk_next(sk2), uh->dest, daddr,
370 uh->source, saddr, dif))) {
371 struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC);
372 if (buff)
373 udpv6_queue_rcv_skb(sk2, buff);
374 }
375 udpv6_queue_rcv_skb(sk, skb);
376out:
377 read_unlock(&udp_hash_lock);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800378 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
Herbert Xu759e5d02007-03-25 20:10:56 -0700381static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh,
382 int proto)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800383{
Herbert Xu759e5d02007-03-25 20:10:56 -0700384 int err;
385
386 UDP_SKB_CB(skb)->partial_cov = 0;
387 UDP_SKB_CB(skb)->cscov = skb->len;
388
389 if (proto == IPPROTO_UDPLITE) {
390 err = udplite_checksum_init(skb, uh);
391 if (err)
392 return err;
393 }
394
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800395 if (uh->check == 0) {
396 /* RFC 2460 section 8.1 says that we SHOULD log
397 this error. Well, it is reasonable.
398 */
399 LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
400 return 1;
401 }
402 if (skb->ip_summed == CHECKSUM_COMPLETE &&
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700403 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
Herbert Xu759e5d02007-03-25 20:10:56 -0700404 skb->len, proto, skb->csum))
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800405 skb->ip_summed = CHECKSUM_UNNECESSARY;
406
Herbert Xu60476372007-04-09 11:59:39 -0700407 if (!skb_csum_unnecessary(skb))
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700408 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
409 &ipv6_hdr(skb)->daddr,
Herbert Xu759e5d02007-03-25 20:10:56 -0700410 skb->len, proto, 0));
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800411
Herbert Xu759e5d02007-03-25 20:10:56 -0700412 return 0;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800413}
414
Herbert Xue5bbef22007-10-15 12:50:28 -0700415int __udp6_lib_rcv(struct sk_buff *skb, struct hlist_head udptable[],
Herbert Xu759e5d02007-03-25 20:10:56 -0700416 int proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 struct sock *sk;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900419 struct udphdr *uh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 struct net_device *dev = skb->dev;
421 struct in6_addr *saddr, *daddr;
422 u32 ulen = 0;
423
424 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
425 goto short_packet;
426
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700427 saddr = &ipv6_hdr(skb)->saddr;
428 daddr = &ipv6_hdr(skb)->daddr;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300429 uh = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 ulen = ntohs(uh->len);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800432 if (ulen > skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 goto short_packet;
434
Herbert Xu759e5d02007-03-25 20:10:56 -0700435 if (proto == IPPROTO_UDP) {
436 /* UDP validates ulen. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800438 /* Check for jumbo payload */
439 if (ulen == 0)
440 ulen = skb->len;
441
442 if (ulen < sizeof(*uh))
443 goto short_packet;
444
445 if (ulen < skb->len) {
446 if (pskb_trim_rcsum(skb, ulen))
447 goto short_packet;
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);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453
Herbert Xu759e5d02007-03-25 20:10:56 -0700454 if (udp6_csum_init(skb, uh, proto))
455 goto discard;
456
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900457 /*
458 * Multicast receive code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800460 if (ipv6_addr_is_multicast(daddr))
461 return __udp6_lib_mcast_deliver(skb, saddr, daddr, udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 /* Unicast */
464
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900465 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 * check socket cache ... must talk to Alan about his plans
467 * for sock caches... i'll skip this for now.
468 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800469 sk = __udp6_lib_lookup(saddr, uh->source,
470 daddr, uh->dest, inet6_iif(skb), udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 if (sk == NULL) {
473 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
474 goto discard;
475
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800476 if (udp_lib_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 goto discard;
Herbert Xu759e5d02007-03-25 20:10:56 -0700478 UDP6_INC_STATS_BH(UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev);
481
482 kfree_skb(skb);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800483 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 /* deliver */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 udpv6_queue_rcv_skb(sk, skb);
489 sock_put(sk);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800490 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900492short_packet:
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800493 LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: %d/%u\n",
Herbert Xu759e5d02007-03-25 20:10:56 -0700494 proto == IPPROTO_UDPLITE ? "-Lite" : "",
495 ulen, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497discard:
Herbert Xu759e5d02007-03-25 20:10:56 -0700498 UDP6_INC_STATS_BH(UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 kfree_skb(skb);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800500 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800502
Herbert Xue5bbef22007-10-15 12:50:28 -0700503static __inline__ int udpv6_rcv(struct sk_buff *skb)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800504{
Herbert Xue5bbef22007-10-15 12:50:28 -0700505 return __udp6_lib_rcv(skb, udp_hash, IPPROTO_UDP);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800506}
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508/*
509 * Throw away all pending data and cancel the corking. Socket is locked.
510 */
511static void udp_v6_flush_pending_frames(struct sock *sk)
512{
513 struct udp_sock *up = udp_sk(sk);
514
515 if (up->pending) {
516 up->len = 0;
517 up->pending = 0;
518 ip6_flush_pending_frames(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522/*
523 * Sending
524 */
525
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800526static int udp_v6_push_pending_frames(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 struct sk_buff *skb;
529 struct udphdr *uh;
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800530 struct udp_sock *up = udp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 struct inet_sock *inet = inet_sk(sk);
532 struct flowi *fl = &inet->cork.fl;
533 int err = 0;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100534 int is_udplite = IS_UDPLITE(sk);
Al Viro868c86b2006-11-14 21:35:48 -0800535 __wsum csum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 /* Grab the skbuff where UDP header space exists. */
538 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
539 goto out;
540
541 /*
542 * Create a UDP header
543 */
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300544 uh = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 uh->source = fl->fl_ip_sport;
546 uh->dest = fl->fl_ip_dport;
547 uh->len = htons(up->len);
548 uh->check = 0;
549
Wang Chenb2bf1e22007-12-03 22:34:16 +1100550 if (is_udplite)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800551 csum = udplite_csum_outgoing(sk, skb);
552 else
553 csum = udp_csum_outgoing(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800555 /* add protocol-dependent pseudo-header */
556 uh->check = csum_ipv6_magic(&fl->fl6_src, &fl->fl6_dst,
557 up->len, fl->proto, csum );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (uh->check == 0)
Al Virof6ab0282006-11-16 02:36:50 -0800559 uh->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 err = ip6_push_pending_frames(sk);
562out:
563 up->len = 0;
564 up->pending = 0;
YOSHIFUJI Hideakicd562c92007-09-14 17:15:01 -0700565 if (!err)
Wang Chenb2bf1e22007-12-03 22:34:16 +1100566 UDP6_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS, is_udplite);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return err;
568}
569
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800570int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 struct msghdr *msg, size_t len)
572{
573 struct ipv6_txoptions opt_space;
574 struct udp_sock *up = udp_sk(sk);
575 struct inet_sock *inet = inet_sk(sk);
576 struct ipv6_pinfo *np = inet6_sk(sk);
577 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
578 struct in6_addr *daddr, *final_p = NULL, final;
579 struct ipv6_txoptions *opt = NULL;
580 struct ip6_flowlabel *flowlabel = NULL;
Herbert Xu132a55f2006-10-03 14:34:00 -0700581 struct flowi fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 struct dst_entry *dst;
583 int addr_len = msg->msg_namelen;
584 int ulen = len;
585 int hlimit = -1;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900586 int tclass = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
588 int err;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700589 int connected = 0;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100590 int is_udplite = IS_UDPLITE(sk);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800591 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 /* destination address check */
594 if (sin6) {
595 if (addr_len < offsetof(struct sockaddr, sa_data))
596 return -EINVAL;
597
598 switch (sin6->sin6_family) {
599 case AF_INET6:
600 if (addr_len < SIN6_LEN_RFC2133)
601 return -EINVAL;
602 daddr = &sin6->sin6_addr;
603 break;
604 case AF_INET:
605 goto do_udp_sendmsg;
606 case AF_UNSPEC:
607 msg->msg_name = sin6 = NULL;
608 msg->msg_namelen = addr_len = 0;
609 daddr = NULL;
610 break;
611 default:
612 return -EINVAL;
613 }
614 } else if (!up->pending) {
615 if (sk->sk_state != TCP_ESTABLISHED)
616 return -EDESTADDRREQ;
617 daddr = &np->daddr;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900618 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 daddr = NULL;
620
621 if (daddr) {
Brian Haleye773e4f2007-08-24 23:16:08 -0700622 if (ipv6_addr_v4mapped(daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 struct sockaddr_in sin;
624 sin.sin_family = AF_INET;
625 sin.sin_port = sin6 ? sin6->sin6_port : inet->dport;
626 sin.sin_addr.s_addr = daddr->s6_addr32[3];
627 msg->msg_name = &sin;
628 msg->msg_namelen = sizeof(sin);
629do_udp_sendmsg:
630 if (__ipv6_only_sock(sk))
631 return -ENETUNREACH;
632 return udp_sendmsg(iocb, sk, msg, len);
633 }
634 }
635
636 if (up->pending == AF_INET)
637 return udp_sendmsg(iocb, sk, msg, len);
638
639 /* Rough check on arithmetic overflow,
YOSHIFUJI Hideakib59e1392007-03-30 14:45:35 -0700640 better check is made in ip6_append_data().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 */
642 if (len > INT_MAX - sizeof(struct udphdr))
643 return -EMSGSIZE;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (up->pending) {
646 /*
647 * There are pending frames.
648 * The socket lock must be held while it's corked.
649 */
650 lock_sock(sk);
651 if (likely(up->pending)) {
652 if (unlikely(up->pending != AF_INET6)) {
653 release_sock(sk);
654 return -EAFNOSUPPORT;
655 }
656 dst = NULL;
657 goto do_append_data;
658 }
659 release_sock(sk);
660 }
661 ulen += sizeof(struct udphdr);
662
Herbert Xu132a55f2006-10-03 14:34:00 -0700663 memset(&fl, 0, sizeof(fl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 if (sin6) {
666 if (sin6->sin6_port == 0)
667 return -EINVAL;
668
Herbert Xu132a55f2006-10-03 14:34:00 -0700669 fl.fl_ip_dport = sin6->sin6_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 daddr = &sin6->sin6_addr;
671
672 if (np->sndflow) {
Herbert Xu132a55f2006-10-03 14:34:00 -0700673 fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
674 if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
675 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (flowlabel == NULL)
677 return -EINVAL;
678 daddr = &flowlabel->dst;
679 }
680 }
681
682 /*
683 * Otherwise it will be difficult to maintain
684 * sk->sk_dst_cache.
685 */
686 if (sk->sk_state == TCP_ESTABLISHED &&
687 ipv6_addr_equal(daddr, &np->daddr))
688 daddr = &np->daddr;
689
690 if (addr_len >= sizeof(struct sockaddr_in6) &&
691 sin6->sin6_scope_id &&
692 ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
Herbert Xu132a55f2006-10-03 14:34:00 -0700693 fl.oif = sin6->sin6_scope_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 } else {
695 if (sk->sk_state != TCP_ESTABLISHED)
696 return -EDESTADDRREQ;
697
Herbert Xu132a55f2006-10-03 14:34:00 -0700698 fl.fl_ip_dport = inet->dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 daddr = &np->daddr;
Herbert Xu132a55f2006-10-03 14:34:00 -0700700 fl.fl6_flowlabel = np->flow_label;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700701 connected = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
703
Herbert Xu132a55f2006-10-03 14:34:00 -0700704 if (!fl.oif)
705 fl.oif = sk->sk_bound_dev_if;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 if (msg->msg_controllen) {
708 opt = &opt_space;
709 memset(opt, 0, sizeof(struct ipv6_txoptions));
710 opt->tot_len = sizeof(*opt);
711
Herbert Xu132a55f2006-10-03 14:34:00 -0700712 err = datagram_send_ctl(msg, &fl, opt, &hlimit, &tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (err < 0) {
714 fl6_sock_release(flowlabel);
715 return err;
716 }
Herbert Xu132a55f2006-10-03 14:34:00 -0700717 if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
718 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (flowlabel == NULL)
720 return -EINVAL;
721 }
722 if (!(opt->opt_nflen|opt->opt_flen))
723 opt = NULL;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700724 connected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726 if (opt == NULL)
727 opt = np->opt;
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900728 if (flowlabel)
729 opt = fl6_merge_options(&opt_space, flowlabel, opt);
730 opt = ipv6_fixup_options(&opt_space, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800732 fl.proto = sk->sk_protocol;
Herbert Xu132a55f2006-10-03 14:34:00 -0700733 ipv6_addr_copy(&fl.fl6_dst, daddr);
734 if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
735 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
736 fl.fl_ip_sport = inet->sport;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 /* merge ip6_build_xmit from ip6_output */
739 if (opt && opt->srcrt) {
740 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
Herbert Xu132a55f2006-10-03 14:34:00 -0700741 ipv6_addr_copy(&final, &fl.fl6_dst);
742 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 final_p = &final;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700744 connected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746
Herbert Xu132a55f2006-10-03 14:34:00 -0700747 if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst)) {
748 fl.oif = np->mcast_oif;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700749 connected = 0;
750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Herbert Xu132a55f2006-10-03 14:34:00 -0700752 security_sk_classify_flow(sk, &fl);
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -0700753
Herbert Xu132a55f2006-10-03 14:34:00 -0700754 err = ip6_sk_dst_lookup(sk, &dst, &fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (err)
756 goto out;
757 if (final_p)
Herbert Xu132a55f2006-10-03 14:34:00 -0700758 ipv6_addr_copy(&fl.fl6_dst, final_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
David S. Miller14e50e52007-05-24 18:17:54 -0700760 if ((err = __xfrm_lookup(&dst, &fl, sk, 1)) < 0) {
761 if (err == -EREMOTE)
762 err = ip6_dst_blackhole(sk, &dst, &fl);
763 if (err < 0)
764 goto out;
765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 if (hlimit < 0) {
Herbert Xu132a55f2006-10-03 14:34:00 -0700768 if (ipv6_addr_is_multicast(&fl.fl6_dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 hlimit = np->mcast_hops;
770 else
771 hlimit = np->hop_limit;
772 if (hlimit < 0)
773 hlimit = dst_metric(dst, RTAX_HOPLIMIT);
774 if (hlimit < 0)
775 hlimit = ipv6_get_hoplimit(dst->dev);
776 }
777
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900778 if (tclass < 0) {
779 tclass = np->tclass;
780 if (tclass < 0)
781 tclass = 0;
782 }
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (msg->msg_flags&MSG_CONFIRM)
785 goto do_confirm;
786back_from_confirm:
787
788 lock_sock(sk);
789 if (unlikely(up->pending)) {
790 /* The socket is already corked while preparing it. */
791 /* ... which is an evident application bug. --ANK */
792 release_sock(sk);
793
Patrick McHardy64ce2072005-08-09 20:50:53 -0700794 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 err = -EINVAL;
796 goto out;
797 }
798
799 up->pending = AF_INET6;
800
801do_append_data:
802 up->len += ulen;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800803 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
804 err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen,
Herbert Xu132a55f2006-10-03 14:34:00 -0700805 sizeof(struct udphdr), hlimit, tclass, opt, &fl,
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900806 (struct rt6_info*)dst,
807 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (err)
809 udp_v6_flush_pending_frames(sk);
810 else if (!corkreq)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800811 err = udp_v6_push_pending_frames(sk);
Herbert Xu1e0c14f2006-10-03 14:35:49 -0700812 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
813 up->pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
David S. Millera5e7c212005-10-03 14:21:58 -0700815 if (dst) {
816 if (connected) {
817 ip6_dst_store(sk, dst,
Herbert Xu132a55f2006-10-03 14:34:00 -0700818 ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700819 &np->daddr : NULL,
820#ifdef CONFIG_IPV6_SUBTREES
Herbert Xu132a55f2006-10-03 14:34:00 -0700821 ipv6_addr_equal(&fl.fl6_src, &np->saddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700822 &np->saddr :
823#endif
824 NULL);
David S. Millera5e7c212005-10-03 14:21:58 -0700825 } else {
826 dst_release(dst);
827 }
828 }
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (err > 0)
831 err = np->recverr ? net_xmit_errno(err) : 0;
832 release_sock(sk);
833out:
834 fl6_sock_release(flowlabel);
YOSHIFUJI Hideakicd562c92007-09-14 17:15:01 -0700835 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return len;
David S. Millera18135e2006-08-15 00:00:09 -0700837 /*
838 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
839 * ENOBUFS might not be good (it's not tunable per se), but otherwise
840 * we don't have a good statistic (IpOutDiscards but it can be too many
841 * things). We could add another new stat but at least for now that
842 * seems like overkill.
843 */
844 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800845 UDP6_INC_STATS_USER(UDP_MIB_SNDBUFERRORS, is_udplite);
David S. Millera18135e2006-08-15 00:00:09 -0700846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return err;
848
849do_confirm:
850 dst_confirm(dst);
851 if (!(msg->msg_flags&MSG_PROBE) || len)
852 goto back_from_confirm;
853 err = 0;
854 goto out;
855}
856
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800857int udpv6_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
859 lock_sock(sk);
860 udp_v6_flush_pending_frames(sk);
861 release_sock(sk);
862
863 inet6_destroy_sock(sk);
864
865 return 0;
866}
867
868/*
869 * Socket option code for UDP
870 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800871int udpv6_setsockopt(struct sock *sk, int level, int optname,
872 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800873{
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800874 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800875 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
876 udp_v6_push_pending_frames);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800877 return ipv6_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800878}
879
880#ifdef CONFIG_COMPAT
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800881int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
882 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800883{
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800884 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800885 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
886 udp_v6_push_pending_frames);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800887 return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800888}
889#endif
890
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800891int udpv6_getsockopt(struct sock *sk, int level, int optname,
892 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800893{
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800894 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800895 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800896 return ipv6_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800897}
898
899#ifdef CONFIG_COMPAT
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800900int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
901 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800902{
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800903 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800904 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800905 return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800906}
907#endif
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909static struct inet6_protocol udpv6_protocol = {
910 .handler = udpv6_rcv,
911 .err_handler = udpv6_err,
912 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
913};
914
915/* ------------------------------------------------------------------------ */
916#ifdef CONFIG_PROC_FS
917
918static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
919{
920 struct inet_sock *inet = inet_sk(sp);
921 struct ipv6_pinfo *np = inet6_sk(sp);
922 struct in6_addr *dest, *src;
923 __u16 destp, srcp;
924
925 dest = &np->daddr;
926 src = &np->rcv_saddr;
927 destp = ntohs(inet->dport);
928 srcp = ntohs(inet->sport);
929 seq_printf(seq,
930 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
931 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p\n",
932 bucket,
933 src->s6_addr32[0], src->s6_addr32[1],
934 src->s6_addr32[2], src->s6_addr32[3], srcp,
935 dest->s6_addr32[0], dest->s6_addr32[1],
936 dest->s6_addr32[2], dest->s6_addr32[3], destp,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900937 sp->sk_state,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 atomic_read(&sp->sk_wmem_alloc),
939 atomic_read(&sp->sk_rmem_alloc),
940 0, 0L, 0,
941 sock_i_uid(sp), 0,
942 sock_i_ino(sp),
943 atomic_read(&sp->sk_refcnt), sp);
944}
945
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800946int udp6_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
948 if (v == SEQ_START_TOKEN)
949 seq_printf(seq,
950 " sl "
951 "local_address "
952 "remote_address "
953 "st tx_queue rx_queue tr tm->when retrnsmt"
954 " uid timeout inode\n");
955 else
956 udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
957 return 0;
958}
959
960static struct file_operations udp6_seq_fops;
961static struct udp_seq_afinfo udp6_seq_afinfo = {
962 .owner = THIS_MODULE,
963 .name = "udp6",
964 .family = AF_INET6,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800965 .hashtable = udp_hash,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 .seq_show = udp6_seq_show,
967 .seq_fops = &udp6_seq_fops,
968};
969
970int __init udp6_proc_init(void)
971{
972 return udp_proc_register(&udp6_seq_afinfo);
973}
974
975void udp6_proc_exit(void) {
976 udp_proc_unregister(&udp6_seq_afinfo);
977}
978#endif /* CONFIG_PROC_FS */
979
980/* ------------------------------------------------------------------------ */
981
Eric Dumazetc5a432f2007-11-05 23:39:51 -0800982DEFINE_PROTO_INUSE(udpv6)
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984struct proto udpv6_prot = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800985 .name = "UDPv6",
986 .owner = THIS_MODULE,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800987 .close = udp_lib_close,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800988 .connect = ip6_datagram_connect,
989 .disconnect = udp_disconnect,
990 .ioctl = udp_ioctl,
991 .destroy = udpv6_destroy_sock,
992 .setsockopt = udpv6_setsockopt,
993 .getsockopt = udpv6_getsockopt,
994 .sendmsg = udpv6_sendmsg,
995 .recvmsg = udpv6_recvmsg,
996 .backlog_rcv = udpv6_queue_rcv_skb,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800997 .hash = udp_lib_hash,
998 .unhash = udp_lib_unhash,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800999 .get_port = udp_v6_get_port,
1000 .obj_size = sizeof(struct udp6_sock),
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001001#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001002 .compat_setsockopt = compat_udpv6_setsockopt,
1003 .compat_getsockopt = compat_udpv6_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001004#endif
Eric Dumazetc5a432f2007-11-05 23:39:51 -08001005 REF_PROTO_INUSE(udpv6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006};
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008static struct inet_protosw udpv6_protosw = {
1009 .type = SOCK_DGRAM,
1010 .protocol = IPPROTO_UDP,
1011 .prot = &udpv6_prot,
1012 .ops = &inet6_dgram_ops,
1013 .capability =-1,
1014 .no_check = UDP_CSUM_DEFAULT,
1015 .flags = INET_PROTOSW_PERMANENT,
1016};
1017
1018
1019void __init udpv6_init(void)
1020{
1021 if (inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP) < 0)
1022 printk(KERN_ERR "udpv6_init: Could not register protocol\n");
1023 inet6_register_protosw(&udpv6_protosw);
1024}