blob: c83f23e51c469a11bd388eab97b9d80a345db876 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * UDP over IPv6
3 * Linux INET6 implementation
4 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 *
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>
30#include <linux/sched.h>
31#include <linux/net.h>
32#include <linux/in6.h>
33#include <linux/netdevice.h>
34#include <linux/if_arp.h>
35#include <linux/ipv6.h>
36#include <linux/icmpv6.h>
37#include <linux/init.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
41#include <net/sock.h>
42#include <net/snmp.h>
43
44#include <net/ipv6.h>
45#include <net/ndisc.h>
46#include <net/protocol.h>
47#include <net/transp_v6.h>
48#include <net/ip6_route.h>
49#include <net/addrconf.h>
50#include <net/ip.h>
51#include <net/udp.h>
52#include <net/raw.h>
53#include <net/inet_common.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070054#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#include <net/ip6_checksum.h>
57#include <net/xfrm.h>
58
59#include <linux/proc_fs.h>
60#include <linux/seq_file.h>
61
Eric Dumazetba899662005-08-26 12:05:31 -070062DEFINE_SNMP_STAT(struct udp_mib, udp_stats_in6) __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Gerrit Renker25030a72006-08-26 20:06:05 -070064static inline int udp_v6_get_port(struct sock *sk, unsigned short snum)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Gerrit Renker25030a72006-08-26 20:06:05 -070066 return udp_get_port(sk, snum, ipv6_rcv_saddr_equal);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
69static void udp_v6_hash(struct sock *sk)
70{
71 BUG();
72}
73
74static void udp_v6_unhash(struct sock *sk)
75{
76 write_lock_bh(&udp_hash_lock);
77 if (sk_del_node_init(sk)) {
78 inet_sk(sk)->num = 0;
79 sock_prot_dec_use(sk->sk_prot);
80 }
81 write_unlock_bh(&udp_hash_lock);
82}
83
84static struct sock *udp_v6_lookup(struct in6_addr *saddr, u16 sport,
85 struct in6_addr *daddr, u16 dport, int dif)
86{
87 struct sock *sk, *result = NULL;
88 struct hlist_node *node;
89 unsigned short hnum = ntohs(dport);
90 int badness = -1;
91
92 read_lock(&udp_hash_lock);
93 sk_for_each(sk, node, &udp_hash[hnum & (UDP_HTABLE_SIZE - 1)]) {
94 struct inet_sock *inet = inet_sk(sk);
95
96 if (inet->num == hnum && sk->sk_family == PF_INET6) {
97 struct ipv6_pinfo *np = inet6_sk(sk);
98 int score = 0;
99 if (inet->dport) {
100 if (inet->dport != sport)
101 continue;
102 score++;
103 }
104 if (!ipv6_addr_any(&np->rcv_saddr)) {
105 if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
106 continue;
107 score++;
108 }
109 if (!ipv6_addr_any(&np->daddr)) {
110 if (!ipv6_addr_equal(&np->daddr, saddr))
111 continue;
112 score++;
113 }
114 if (sk->sk_bound_dev_if) {
115 if (sk->sk_bound_dev_if != dif)
116 continue;
117 score++;
118 }
119 if(score == 4) {
120 result = sk;
121 break;
122 } else if(score > badness) {
123 result = sk;
124 badness = score;
125 }
126 }
127 }
128 if (result)
129 sock_hold(result);
130 read_unlock(&udp_hash_lock);
131 return result;
132}
133
134/*
135 *
136 */
137
138static void udpv6_close(struct sock *sk, long timeout)
139{
140 sk_common_release(sk);
141}
142
143/*
144 * This should be easy, if there is something there we
145 * return it, otherwise we block.
146 */
147
148static int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
149 struct msghdr *msg, size_t len,
150 int noblock, int flags, int *addr_len)
151{
152 struct ipv6_pinfo *np = inet6_sk(sk);
153 struct inet_sock *inet = inet_sk(sk);
154 struct sk_buff *skb;
155 size_t copied;
156 int err;
157
158 if (addr_len)
159 *addr_len=sizeof(struct sockaddr_in6);
160
161 if (flags & MSG_ERRQUEUE)
162 return ipv6_recv_error(sk, msg, len);
163
164try_again:
165 skb = skb_recv_datagram(sk, flags, noblock, &err);
166 if (!skb)
167 goto out;
168
169 copied = skb->len - sizeof(struct udphdr);
170 if (copied > len) {
171 copied = len;
172 msg->msg_flags |= MSG_TRUNC;
173 }
174
175 if (skb->ip_summed==CHECKSUM_UNNECESSARY) {
176 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
177 copied);
178 } else if (msg->msg_flags&MSG_TRUNC) {
Herbert Xufb286bb2005-11-10 13:01:24 -0800179 if (__skb_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 goto csum_copy_err;
181 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
182 copied);
183 } else {
184 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
185 if (err == -EINVAL)
186 goto csum_copy_err;
187 }
188 if (err)
189 goto out_free;
190
191 sock_recv_timestamp(msg, sk, skb);
192
193 /* Copy the address. */
194 if (msg->msg_name) {
195 struct sockaddr_in6 *sin6;
196
197 sin6 = (struct sockaddr_in6 *) msg->msg_name;
198 sin6->sin6_family = AF_INET6;
199 sin6->sin6_port = skb->h.uh->source;
200 sin6->sin6_flowinfo = 0;
201 sin6->sin6_scope_id = 0;
202
203 if (skb->protocol == htons(ETH_P_IP))
204 ipv6_addr_set(&sin6->sin6_addr, 0, 0,
205 htonl(0xffff), skb->nh.iph->saddr);
206 else {
207 ipv6_addr_copy(&sin6->sin6_addr, &skb->nh.ipv6h->saddr);
208 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
209 sin6->sin6_scope_id = IP6CB(skb)->iif;
210 }
211
212 }
213 if (skb->protocol == htons(ETH_P_IP)) {
214 if (inet->cmsg_flags)
215 ip_cmsg_recv(msg, skb);
216 } else {
217 if (np->rxopt.all)
218 datagram_recv_ctl(sk, msg, skb);
219 }
220
221 err = copied;
222 if (flags & MSG_TRUNC)
223 err = skb->len - sizeof(struct udphdr);
224
225out_free:
226 skb_free_datagram(sk, skb);
227out:
228 return err;
229
230csum_copy_err:
Herbert Xu3305b802005-12-13 23:16:37 -0800231 skb_kill_datagram(sk, skb, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 if (flags & MSG_DONTWAIT) {
234 UDP6_INC_STATS_USER(UDP_MIB_INERRORS);
235 return -EAGAIN;
236 }
237 goto try_again;
238}
239
240static void udpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
241 int type, int code, int offset, __u32 info)
242{
243 struct ipv6_pinfo *np;
244 struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 struct in6_addr *saddr = &hdr->saddr;
246 struct in6_addr *daddr = &hdr->daddr;
247 struct udphdr *uh = (struct udphdr*)(skb->data+offset);
248 struct sock *sk;
249 int err;
250
YOSHIFUJI Hideakif2776ff2006-11-21 17:41:56 -0800251 sk = udp_v6_lookup(daddr, uh->dest, saddr, uh->source, inet6_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 if (sk == NULL)
254 return;
255
256 np = inet6_sk(sk);
257
258 if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
259 goto out;
260
261 if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
262 goto out;
263
264 if (np->recverr)
265 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
266
267 sk->sk_err = err;
268 sk->sk_error_report(sk);
269out:
270 sock_put(sk);
271}
272
273static inline int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
274{
David S. Millera18135e2006-08-15 00:00:09 -0700275 int rc;
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
278 kfree_skb(skb);
279 return -1;
280 }
281
Herbert Xufb286bb2005-11-10 13:01:24 -0800282 if (skb_checksum_complete(skb)) {
283 UDP6_INC_STATS_BH(UDP_MIB_INERRORS);
284 kfree_skb(skb);
285 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287
David S. Millera18135e2006-08-15 00:00:09 -0700288 if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) {
289 /* Note that an ENOMEM error is charged twice */
290 if (rc == -ENOMEM)
David S. Millerf8d8fda2006-08-15 00:15:41 -0700291 UDP6_INC_STATS_BH(UDP_MIB_RCVBUFERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 UDP6_INC_STATS_BH(UDP_MIB_INERRORS);
293 kfree_skb(skb);
294 return 0;
295 }
296 UDP6_INC_STATS_BH(UDP_MIB_INDATAGRAMS);
297 return 0;
298}
299
300static struct sock *udp_v6_mcast_next(struct sock *sk,
301 u16 loc_port, struct in6_addr *loc_addr,
302 u16 rmt_port, struct in6_addr *rmt_addr,
303 int dif)
304{
305 struct hlist_node *node;
306 struct sock *s = sk;
307 unsigned short num = ntohs(loc_port);
308
309 sk_for_each_from(s, node) {
310 struct inet_sock *inet = inet_sk(s);
311
312 if (inet->num == num && s->sk_family == PF_INET6) {
313 struct ipv6_pinfo *np = inet6_sk(s);
314 if (inet->dport) {
315 if (inet->dport != rmt_port)
316 continue;
317 }
318 if (!ipv6_addr_any(&np->daddr) &&
319 !ipv6_addr_equal(&np->daddr, rmt_addr))
320 continue;
321
322 if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
323 continue;
324
325 if (!ipv6_addr_any(&np->rcv_saddr)) {
David L Stevens40796c52005-09-14 21:10:20 -0700326 if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
327 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329 if(!inet6_mc_check(s, loc_addr, rmt_addr))
330 continue;
331 return s;
332 }
333 }
334 return NULL;
335}
336
337/*
338 * Note: called only from the BH handler context,
339 * so we don't need to lock the hashes.
340 */
341static void udpv6_mcast_deliver(struct udphdr *uh,
342 struct in6_addr *saddr, struct in6_addr *daddr,
343 struct sk_buff *skb)
344{
345 struct sock *sk, *sk2;
346 int dif;
347
348 read_lock(&udp_hash_lock);
349 sk = sk_head(&udp_hash[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]);
YOSHIFUJI Hideakif2776ff2006-11-21 17:41:56 -0800350 dif = inet6_iif(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 sk = udp_v6_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
352 if (!sk) {
353 kfree_skb(skb);
354 goto out;
355 }
356
357 sk2 = sk;
358 while ((sk2 = udp_v6_mcast_next(sk_next(sk2), uh->dest, daddr,
359 uh->source, saddr, dif))) {
360 struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC);
361 if (buff)
362 udpv6_queue_rcv_skb(sk2, buff);
363 }
364 udpv6_queue_rcv_skb(sk, skb);
365out:
366 read_unlock(&udp_hash_lock);
367}
368
Patrick McHardy951dbc82006-01-06 23:02:34 -0800369static int udpv6_rcv(struct sk_buff **pskb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 struct sk_buff *skb = *pskb;
372 struct sock *sk;
373 struct udphdr *uh;
374 struct net_device *dev = skb->dev;
375 struct in6_addr *saddr, *daddr;
376 u32 ulen = 0;
377
378 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
379 goto short_packet;
380
381 saddr = &skb->nh.ipv6h->saddr;
382 daddr = &skb->nh.ipv6h->daddr;
383 uh = skb->h.uh;
384
385 ulen = ntohs(uh->len);
386
387 /* Check for jumbo payload */
388 if (ulen == 0)
389 ulen = skb->len;
390
391 if (ulen > skb->len || ulen < sizeof(*uh))
392 goto short_packet;
393
394 if (uh->check == 0) {
395 /* RFC 2460 section 8.1 says that we SHOULD log
396 this error. Well, it is reasonable.
397 */
Patrick McHardy64ce2072005-08-09 20:50:53 -0700398 LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 goto discard;
400 }
401
402 if (ulen < skb->len) {
Stephen Hemminger42ca89c2005-09-08 12:57:43 -0700403 if (pskb_trim_rcsum(skb, ulen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 goto discard;
405 saddr = &skb->nh.ipv6h->saddr;
406 daddr = &skb->nh.ipv6h->daddr;
407 uh = skb->h.uh;
408 }
409
Patrick McHardy84fa7932006-08-29 16:44:56 -0700410 if (skb->ip_summed == CHECKSUM_COMPLETE &&
Herbert Xufb286bb2005-11-10 13:01:24 -0800411 !csum_ipv6_magic(saddr, daddr, ulen, IPPROTO_UDP, skb->csum))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 skb->ip_summed = CHECKSUM_UNNECESSARY;
Herbert Xufb286bb2005-11-10 13:01:24 -0800413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
415 skb->csum = ~csum_ipv6_magic(saddr, daddr, ulen, IPPROTO_UDP, 0);
416
417 /*
418 * Multicast receive code
419 */
420 if (ipv6_addr_is_multicast(daddr)) {
421 udpv6_mcast_deliver(uh, saddr, daddr, skb);
422 return 0;
423 }
424
425 /* Unicast */
426
427 /*
428 * check socket cache ... must talk to Alan about his plans
429 * for sock caches... i'll skip this for now.
430 */
YOSHIFUJI Hideakif2776ff2006-11-21 17:41:56 -0800431 sk = udp_v6_lookup(saddr, uh->source, daddr, uh->dest, inet6_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 if (sk == NULL) {
434 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
435 goto discard;
436
Herbert Xufb286bb2005-11-10 13:01:24 -0800437 if (skb_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 goto discard;
439 UDP6_INC_STATS_BH(UDP_MIB_NOPORTS);
440
441 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev);
442
443 kfree_skb(skb);
444 return(0);
445 }
446
447 /* deliver */
448
449 udpv6_queue_rcv_skb(sk, skb);
450 sock_put(sk);
451 return(0);
452
453short_packet:
454 if (net_ratelimit())
455 printk(KERN_DEBUG "UDP: short packet: %d/%u\n", ulen, skb->len);
456
457discard:
458 UDP6_INC_STATS_BH(UDP_MIB_INERRORS);
459 kfree_skb(skb);
460 return(0);
461}
462/*
463 * Throw away all pending data and cancel the corking. Socket is locked.
464 */
465static void udp_v6_flush_pending_frames(struct sock *sk)
466{
467 struct udp_sock *up = udp_sk(sk);
468
469 if (up->pending) {
470 up->len = 0;
471 up->pending = 0;
472 ip6_flush_pending_frames(sk);
473 }
474}
475
476/*
477 * Sending
478 */
479
480static int udp_v6_push_pending_frames(struct sock *sk, struct udp_sock *up)
481{
482 struct sk_buff *skb;
483 struct udphdr *uh;
484 struct inet_sock *inet = inet_sk(sk);
485 struct flowi *fl = &inet->cork.fl;
486 int err = 0;
487
488 /* Grab the skbuff where UDP header space exists. */
489 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
490 goto out;
491
492 /*
493 * Create a UDP header
494 */
495 uh = skb->h.uh;
496 uh->source = fl->fl_ip_sport;
497 uh->dest = fl->fl_ip_dport;
498 uh->len = htons(up->len);
499 uh->check = 0;
500
501 if (sk->sk_no_check == UDP_CSUM_NOXMIT) {
502 skb->ip_summed = CHECKSUM_NONE;
503 goto send;
504 }
505
506 if (skb_queue_len(&sk->sk_write_queue) == 1) {
507 skb->csum = csum_partial((char *)uh,
508 sizeof(struct udphdr), skb->csum);
509 uh->check = csum_ipv6_magic(&fl->fl6_src,
510 &fl->fl6_dst,
511 up->len, fl->proto, skb->csum);
512 } else {
513 u32 tmp_csum = 0;
514
515 skb_queue_walk(&sk->sk_write_queue, skb) {
516 tmp_csum = csum_add(tmp_csum, skb->csum);
517 }
518 tmp_csum = csum_partial((char *)uh,
519 sizeof(struct udphdr), tmp_csum);
520 tmp_csum = csum_ipv6_magic(&fl->fl6_src,
521 &fl->fl6_dst,
522 up->len, fl->proto, tmp_csum);
523 uh->check = tmp_csum;
524
525 }
526 if (uh->check == 0)
527 uh->check = -1;
528
529send:
530 err = ip6_push_pending_frames(sk);
531out:
532 up->len = 0;
533 up->pending = 0;
534 return err;
535}
536
537static int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
538 struct msghdr *msg, size_t len)
539{
540 struct ipv6_txoptions opt_space;
541 struct udp_sock *up = udp_sk(sk);
542 struct inet_sock *inet = inet_sk(sk);
543 struct ipv6_pinfo *np = inet6_sk(sk);
544 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
545 struct in6_addr *daddr, *final_p = NULL, final;
546 struct ipv6_txoptions *opt = NULL;
547 struct ip6_flowlabel *flowlabel = NULL;
Herbert Xu132a55f2006-10-03 14:34:00 -0700548 struct flowi fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 struct dst_entry *dst;
550 int addr_len = msg->msg_namelen;
551 int ulen = len;
552 int hlimit = -1;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900553 int tclass = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
555 int err;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700556 int connected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 /* destination address check */
559 if (sin6) {
560 if (addr_len < offsetof(struct sockaddr, sa_data))
561 return -EINVAL;
562
563 switch (sin6->sin6_family) {
564 case AF_INET6:
565 if (addr_len < SIN6_LEN_RFC2133)
566 return -EINVAL;
567 daddr = &sin6->sin6_addr;
568 break;
569 case AF_INET:
570 goto do_udp_sendmsg;
571 case AF_UNSPEC:
572 msg->msg_name = sin6 = NULL;
573 msg->msg_namelen = addr_len = 0;
574 daddr = NULL;
575 break;
576 default:
577 return -EINVAL;
578 }
579 } else if (!up->pending) {
580 if (sk->sk_state != TCP_ESTABLISHED)
581 return -EDESTADDRREQ;
582 daddr = &np->daddr;
583 } else
584 daddr = NULL;
585
586 if (daddr) {
587 if (ipv6_addr_type(daddr) == IPV6_ADDR_MAPPED) {
588 struct sockaddr_in sin;
589 sin.sin_family = AF_INET;
590 sin.sin_port = sin6 ? sin6->sin6_port : inet->dport;
591 sin.sin_addr.s_addr = daddr->s6_addr32[3];
592 msg->msg_name = &sin;
593 msg->msg_namelen = sizeof(sin);
594do_udp_sendmsg:
595 if (__ipv6_only_sock(sk))
596 return -ENETUNREACH;
597 return udp_sendmsg(iocb, sk, msg, len);
598 }
599 }
600
601 if (up->pending == AF_INET)
602 return udp_sendmsg(iocb, sk, msg, len);
603
604 /* Rough check on arithmetic overflow,
605 better check is made in ip6_build_xmit
606 */
607 if (len > INT_MAX - sizeof(struct udphdr))
608 return -EMSGSIZE;
609
610 if (up->pending) {
611 /*
612 * There are pending frames.
613 * The socket lock must be held while it's corked.
614 */
615 lock_sock(sk);
616 if (likely(up->pending)) {
617 if (unlikely(up->pending != AF_INET6)) {
618 release_sock(sk);
619 return -EAFNOSUPPORT;
620 }
621 dst = NULL;
622 goto do_append_data;
623 }
624 release_sock(sk);
625 }
626 ulen += sizeof(struct udphdr);
627
Herbert Xu132a55f2006-10-03 14:34:00 -0700628 memset(&fl, 0, sizeof(fl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 if (sin6) {
631 if (sin6->sin6_port == 0)
632 return -EINVAL;
633
Herbert Xu132a55f2006-10-03 14:34:00 -0700634 fl.fl_ip_dport = sin6->sin6_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 daddr = &sin6->sin6_addr;
636
637 if (np->sndflow) {
Herbert Xu132a55f2006-10-03 14:34:00 -0700638 fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
639 if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
640 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (flowlabel == NULL)
642 return -EINVAL;
643 daddr = &flowlabel->dst;
644 }
645 }
646
647 /*
648 * Otherwise it will be difficult to maintain
649 * sk->sk_dst_cache.
650 */
651 if (sk->sk_state == TCP_ESTABLISHED &&
652 ipv6_addr_equal(daddr, &np->daddr))
653 daddr = &np->daddr;
654
655 if (addr_len >= sizeof(struct sockaddr_in6) &&
656 sin6->sin6_scope_id &&
657 ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
Herbert Xu132a55f2006-10-03 14:34:00 -0700658 fl.oif = sin6->sin6_scope_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 } else {
660 if (sk->sk_state != TCP_ESTABLISHED)
661 return -EDESTADDRREQ;
662
Herbert Xu132a55f2006-10-03 14:34:00 -0700663 fl.fl_ip_dport = inet->dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 daddr = &np->daddr;
Herbert Xu132a55f2006-10-03 14:34:00 -0700665 fl.fl6_flowlabel = np->flow_label;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700666 connected = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668
Herbert Xu132a55f2006-10-03 14:34:00 -0700669 if (!fl.oif)
670 fl.oif = sk->sk_bound_dev_if;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 if (msg->msg_controllen) {
673 opt = &opt_space;
674 memset(opt, 0, sizeof(struct ipv6_txoptions));
675 opt->tot_len = sizeof(*opt);
676
Herbert Xu132a55f2006-10-03 14:34:00 -0700677 err = datagram_send_ctl(msg, &fl, opt, &hlimit, &tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (err < 0) {
679 fl6_sock_release(flowlabel);
680 return err;
681 }
Herbert Xu132a55f2006-10-03 14:34:00 -0700682 if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
683 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (flowlabel == NULL)
685 return -EINVAL;
686 }
687 if (!(opt->opt_nflen|opt->opt_flen))
688 opt = NULL;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700689 connected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691 if (opt == NULL)
692 opt = np->opt;
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900693 if (flowlabel)
694 opt = fl6_merge_options(&opt_space, flowlabel, opt);
695 opt = ipv6_fixup_options(&opt_space, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Herbert Xu132a55f2006-10-03 14:34:00 -0700697 fl.proto = IPPROTO_UDP;
698 ipv6_addr_copy(&fl.fl6_dst, daddr);
699 if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
700 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
701 fl.fl_ip_sport = inet->sport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 /* merge ip6_build_xmit from ip6_output */
704 if (opt && opt->srcrt) {
705 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
Herbert Xu132a55f2006-10-03 14:34:00 -0700706 ipv6_addr_copy(&final, &fl.fl6_dst);
707 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 final_p = &final;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700709 connected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711
Herbert Xu132a55f2006-10-03 14:34:00 -0700712 if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst)) {
713 fl.oif = np->mcast_oif;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700714 connected = 0;
715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Herbert Xu132a55f2006-10-03 14:34:00 -0700717 security_sk_classify_flow(sk, &fl);
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -0700718
Herbert Xu132a55f2006-10-03 14:34:00 -0700719 err = ip6_sk_dst_lookup(sk, &dst, &fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (err)
721 goto out;
722 if (final_p)
Herbert Xu132a55f2006-10-03 14:34:00 -0700723 ipv6_addr_copy(&fl.fl6_dst, final_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Herbert Xu132a55f2006-10-03 14:34:00 -0700725 if ((err = xfrm_lookup(&dst, &fl, sk, 0)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 if (hlimit < 0) {
Herbert Xu132a55f2006-10-03 14:34:00 -0700729 if (ipv6_addr_is_multicast(&fl.fl6_dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 hlimit = np->mcast_hops;
731 else
732 hlimit = np->hop_limit;
733 if (hlimit < 0)
734 hlimit = dst_metric(dst, RTAX_HOPLIMIT);
735 if (hlimit < 0)
736 hlimit = ipv6_get_hoplimit(dst->dev);
737 }
738
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900739 if (tclass < 0) {
740 tclass = np->tclass;
741 if (tclass < 0)
742 tclass = 0;
743 }
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (msg->msg_flags&MSG_CONFIRM)
746 goto do_confirm;
747back_from_confirm:
748
749 lock_sock(sk);
750 if (unlikely(up->pending)) {
751 /* The socket is already corked while preparing it. */
752 /* ... which is an evident application bug. --ANK */
753 release_sock(sk);
754
Patrick McHardy64ce2072005-08-09 20:50:53 -0700755 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 err = -EINVAL;
757 goto out;
758 }
759
760 up->pending = AF_INET6;
761
762do_append_data:
763 up->len += ulen;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900764 err = ip6_append_data(sk, ip_generic_getfrag, msg->msg_iov, ulen,
Herbert Xu132a55f2006-10-03 14:34:00 -0700765 sizeof(struct udphdr), hlimit, tclass, opt, &fl,
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900766 (struct rt6_info*)dst,
767 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (err)
769 udp_v6_flush_pending_frames(sk);
770 else if (!corkreq)
771 err = udp_v6_push_pending_frames(sk, up);
Herbert Xu1e0c14f2006-10-03 14:35:49 -0700772 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
773 up->pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
David S. Millera5e7c212005-10-03 14:21:58 -0700775 if (dst) {
776 if (connected) {
777 ip6_dst_store(sk, dst,
Herbert Xu132a55f2006-10-03 14:34:00 -0700778 ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700779 &np->daddr : NULL,
780#ifdef CONFIG_IPV6_SUBTREES
Herbert Xu132a55f2006-10-03 14:34:00 -0700781 ipv6_addr_equal(&fl.fl6_src, &np->saddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700782 &np->saddr :
783#endif
784 NULL);
David S. Millera5e7c212005-10-03 14:21:58 -0700785 } else {
786 dst_release(dst);
787 }
788 }
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (err > 0)
791 err = np->recverr ? net_xmit_errno(err) : 0;
792 release_sock(sk);
793out:
794 fl6_sock_release(flowlabel);
795 if (!err) {
796 UDP6_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS);
797 return len;
798 }
David S. Millera18135e2006-08-15 00:00:09 -0700799 /*
800 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
801 * ENOBUFS might not be good (it's not tunable per se), but otherwise
802 * we don't have a good statistic (IpOutDiscards but it can be too many
803 * things). We could add another new stat but at least for now that
804 * seems like overkill.
805 */
806 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
David S. Millerf8d8fda2006-08-15 00:15:41 -0700807 UDP6_INC_STATS_USER(UDP_MIB_SNDBUFERRORS);
David S. Millera18135e2006-08-15 00:00:09 -0700808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return err;
810
811do_confirm:
812 dst_confirm(dst);
813 if (!(msg->msg_flags&MSG_PROBE) || len)
814 goto back_from_confirm;
815 err = 0;
816 goto out;
817}
818
819static int udpv6_destroy_sock(struct sock *sk)
820{
821 lock_sock(sk);
822 udp_v6_flush_pending_frames(sk);
823 release_sock(sk);
824
825 inet6_destroy_sock(sk);
826
827 return 0;
828}
829
830/*
831 * Socket option code for UDP
832 */
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800833static int do_udpv6_setsockopt(struct sock *sk, int level, int optname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 char __user *optval, int optlen)
835{
836 struct udp_sock *up = udp_sk(sk);
837 int val;
838 int err = 0;
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if(optlen<sizeof(int))
841 return -EINVAL;
842
843 if (get_user(val, (int __user *)optval))
844 return -EFAULT;
845
846 switch(optname) {
847 case UDP_CORK:
848 if (val != 0) {
849 up->corkflag = 1;
850 } else {
851 up->corkflag = 0;
852 lock_sock(sk);
853 udp_v6_push_pending_frames(sk, up);
854 release_sock(sk);
855 }
856 break;
857
858 case UDP_ENCAP:
859 switch (val) {
860 case 0:
861 up->encap_type = val;
862 break;
863 default:
864 err = -ENOPROTOOPT;
865 break;
866 }
867 break;
868
869 default:
870 err = -ENOPROTOOPT;
871 break;
872 };
873
874 return err;
875}
876
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800877static int udpv6_setsockopt(struct sock *sk, int level, int optname,
878 char __user *optval, int optlen)
879{
880 if (level != SOL_UDP)
881 return ipv6_setsockopt(sk, level, optname, optval, optlen);
882 return do_udpv6_setsockopt(sk, level, optname, optval, optlen);
883}
884
885#ifdef CONFIG_COMPAT
886static int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800887 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800888{
889 if (level != SOL_UDP)
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800890 return compat_ipv6_setsockopt(sk, level, optname,
891 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800892 return do_udpv6_setsockopt(sk, level, optname, optval, optlen);
893}
894#endif
895
896static int do_udpv6_getsockopt(struct sock *sk, int level, int optname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 char __user *optval, int __user *optlen)
898{
899 struct udp_sock *up = udp_sk(sk);
900 int val, len;
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if(get_user(len,optlen))
903 return -EFAULT;
904
905 len = min_t(unsigned int, len, sizeof(int));
906
907 if(len < 0)
908 return -EINVAL;
909
910 switch(optname) {
911 case UDP_CORK:
912 val = up->corkflag;
913 break;
914
915 case UDP_ENCAP:
916 val = up->encap_type;
917 break;
918
919 default:
920 return -ENOPROTOOPT;
921 };
922
923 if(put_user(len, optlen))
924 return -EFAULT;
925 if(copy_to_user(optval, &val,len))
926 return -EFAULT;
927 return 0;
928}
929
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800930static int udpv6_getsockopt(struct sock *sk, int level, int optname,
931 char __user *optval, int __user *optlen)
932{
933 if (level != SOL_UDP)
934 return ipv6_getsockopt(sk, level, optname, optval, optlen);
935 return do_udpv6_getsockopt(sk, level, optname, optval, optlen);
936}
937
938#ifdef CONFIG_COMPAT
939static int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800940 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800941{
942 if (level != SOL_UDP)
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800943 return compat_ipv6_getsockopt(sk, level, optname,
944 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800945 return do_udpv6_getsockopt(sk, level, optname, optval, optlen);
946}
947#endif
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949static struct inet6_protocol udpv6_protocol = {
950 .handler = udpv6_rcv,
951 .err_handler = udpv6_err,
952 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
953};
954
955/* ------------------------------------------------------------------------ */
956#ifdef CONFIG_PROC_FS
957
958static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
959{
960 struct inet_sock *inet = inet_sk(sp);
961 struct ipv6_pinfo *np = inet6_sk(sp);
962 struct in6_addr *dest, *src;
963 __u16 destp, srcp;
964
965 dest = &np->daddr;
966 src = &np->rcv_saddr;
967 destp = ntohs(inet->dport);
968 srcp = ntohs(inet->sport);
969 seq_printf(seq,
970 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
971 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p\n",
972 bucket,
973 src->s6_addr32[0], src->s6_addr32[1],
974 src->s6_addr32[2], src->s6_addr32[3], srcp,
975 dest->s6_addr32[0], dest->s6_addr32[1],
976 dest->s6_addr32[2], dest->s6_addr32[3], destp,
977 sp->sk_state,
978 atomic_read(&sp->sk_wmem_alloc),
979 atomic_read(&sp->sk_rmem_alloc),
980 0, 0L, 0,
981 sock_i_uid(sp), 0,
982 sock_i_ino(sp),
983 atomic_read(&sp->sk_refcnt), sp);
984}
985
986static int udp6_seq_show(struct seq_file *seq, void *v)
987{
988 if (v == SEQ_START_TOKEN)
989 seq_printf(seq,
990 " sl "
991 "local_address "
992 "remote_address "
993 "st tx_queue rx_queue tr tm->when retrnsmt"
994 " uid timeout inode\n");
995 else
996 udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
997 return 0;
998}
999
1000static struct file_operations udp6_seq_fops;
1001static struct udp_seq_afinfo udp6_seq_afinfo = {
1002 .owner = THIS_MODULE,
1003 .name = "udp6",
1004 .family = AF_INET6,
1005 .seq_show = udp6_seq_show,
1006 .seq_fops = &udp6_seq_fops,
1007};
1008
1009int __init udp6_proc_init(void)
1010{
1011 return udp_proc_register(&udp6_seq_afinfo);
1012}
1013
1014void udp6_proc_exit(void) {
1015 udp_proc_unregister(&udp6_seq_afinfo);
1016}
1017#endif /* CONFIG_PROC_FS */
1018
1019/* ------------------------------------------------------------------------ */
1020
1021struct proto udpv6_prot = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001022 .name = "UDPv6",
1023 .owner = THIS_MODULE,
1024 .close = udpv6_close,
1025 .connect = ip6_datagram_connect,
1026 .disconnect = udp_disconnect,
1027 .ioctl = udp_ioctl,
1028 .destroy = udpv6_destroy_sock,
1029 .setsockopt = udpv6_setsockopt,
1030 .getsockopt = udpv6_getsockopt,
1031 .sendmsg = udpv6_sendmsg,
1032 .recvmsg = udpv6_recvmsg,
1033 .backlog_rcv = udpv6_queue_rcv_skb,
1034 .hash = udp_v6_hash,
1035 .unhash = udp_v6_unhash,
1036 .get_port = udp_v6_get_port,
1037 .obj_size = sizeof(struct udp6_sock),
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001038#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001039 .compat_setsockopt = compat_udpv6_setsockopt,
1040 .compat_getsockopt = compat_udpv6_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001041#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042};
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044static struct inet_protosw udpv6_protosw = {
1045 .type = SOCK_DGRAM,
1046 .protocol = IPPROTO_UDP,
1047 .prot = &udpv6_prot,
1048 .ops = &inet6_dgram_ops,
1049 .capability =-1,
1050 .no_check = UDP_CSUM_DEFAULT,
1051 .flags = INET_PROTOSW_PERMANENT,
1052};
1053
1054
1055void __init udpv6_init(void)
1056{
1057 if (inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP) < 0)
1058 printk(KERN_ERR "udpv6_init: Could not register protocol\n");
1059 inet6_register_protosw(&udpv6_protosw);
1060}