mfd: Correct WM835x ISINK ramp time defines
[linux/fpc-iii.git] / net / ipv6 / udp.c
blobcf538ed5ef6aadf849d42cdc4e046f00a0f8f51a
1 /*
2 * UDP over IPv6
3 * Linux INET6 implementation
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * Based on linux/ipv4/udp.c
10 * 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.
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.
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/socket.h>
27 #include <linux/sockios.h>
28 #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>
35 #include <linux/module.h>
36 #include <linux/skbuff.h>
37 #include <asm/uaccess.h>
39 #include <net/ndisc.h>
40 #include <net/protocol.h>
41 #include <net/transp_v6.h>
42 #include <net/ip6_route.h>
43 #include <net/raw.h>
44 #include <net/tcp_states.h>
45 #include <net/ip6_checksum.h>
46 #include <net/xfrm.h>
48 #include <linux/proc_fs.h>
49 #include <linux/seq_file.h>
50 #include "udp_impl.h"
52 int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
54 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
55 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
56 __be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
57 __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
58 int sk_ipv6only = ipv6_only_sock(sk);
59 int sk2_ipv6only = inet_v6_ipv6only(sk2);
60 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
61 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
63 /* if both are mapped, treat as IPv4 */
64 if (addr_type == IPV6_ADDR_MAPPED && addr_type2 == IPV6_ADDR_MAPPED)
65 return (!sk2_ipv6only &&
66 (!sk_rcv_saddr || !sk2_rcv_saddr ||
67 sk_rcv_saddr == sk2_rcv_saddr));
69 if (addr_type2 == IPV6_ADDR_ANY &&
70 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
71 return 1;
73 if (addr_type == IPV6_ADDR_ANY &&
74 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
75 return 1;
77 if (sk2_rcv_saddr6 &&
78 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
79 return 1;
81 return 0;
84 int udp_v6_get_port(struct sock *sk, unsigned short snum)
86 return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal);
89 static inline int compute_score(struct sock *sk, struct net *net,
90 unsigned short hnum,
91 struct in6_addr *saddr, __be16 sport,
92 struct in6_addr *daddr, __be16 dport,
93 int dif)
95 int score = -1;
97 if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum &&
98 sk->sk_family == PF_INET6) {
99 struct ipv6_pinfo *np = inet6_sk(sk);
100 struct inet_sock *inet = inet_sk(sk);
102 score = 0;
103 if (inet->dport) {
104 if (inet->dport != sport)
105 return -1;
106 score++;
108 if (!ipv6_addr_any(&np->rcv_saddr)) {
109 if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
110 return -1;
111 score++;
113 if (!ipv6_addr_any(&np->daddr)) {
114 if (!ipv6_addr_equal(&np->daddr, saddr))
115 return -1;
116 score++;
118 if (sk->sk_bound_dev_if) {
119 if (sk->sk_bound_dev_if != dif)
120 return -1;
121 score++;
124 return score;
127 static struct sock *__udp6_lib_lookup(struct net *net,
128 struct in6_addr *saddr, __be16 sport,
129 struct in6_addr *daddr, __be16 dport,
130 int dif, struct udp_table *udptable)
132 struct sock *sk, *result;
133 struct hlist_nulls_node *node;
134 unsigned short hnum = ntohs(dport);
135 unsigned int hash = udp_hashfn(net, hnum);
136 struct udp_hslot *hslot = &udptable->hash[hash];
137 int score, badness;
139 rcu_read_lock();
140 begin:
141 result = NULL;
142 badness = -1;
143 sk_nulls_for_each_rcu(sk, node, &hslot->head) {
144 score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif);
145 if (score > badness) {
146 result = sk;
147 badness = score;
151 * if the nulls value we got at the end of this lookup is
152 * not the expected one, we must restart lookup.
153 * We probably met an item that was moved to another chain.
155 if (get_nulls_value(node) != hash)
156 goto begin;
158 if (result) {
159 if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt)))
160 result = NULL;
161 else if (unlikely(compute_score(result, net, hnum, saddr, sport,
162 daddr, dport, dif) < badness)) {
163 sock_put(result);
164 goto begin;
167 rcu_read_unlock();
168 return result;
171 static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
172 __be16 sport, __be16 dport,
173 struct udp_table *udptable)
175 struct sock *sk;
176 struct ipv6hdr *iph = ipv6_hdr(skb);
178 if (unlikely(sk = skb_steal_sock(skb)))
179 return sk;
180 return __udp6_lib_lookup(dev_net(skb_dst(skb)->dev), &iph->saddr, sport,
181 &iph->daddr, dport, inet6_iif(skb),
182 udptable);
186 * This should be easy, if there is something there we
187 * return it, otherwise we block.
190 int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
191 struct msghdr *msg, size_t len,
192 int noblock, int flags, int *addr_len)
194 struct ipv6_pinfo *np = inet6_sk(sk);
195 struct inet_sock *inet = inet_sk(sk);
196 struct sk_buff *skb;
197 unsigned int ulen, copied;
198 int peeked;
199 int err;
200 int is_udplite = IS_UDPLITE(sk);
201 int is_udp4;
203 if (addr_len)
204 *addr_len=sizeof(struct sockaddr_in6);
206 if (flags & MSG_ERRQUEUE)
207 return ipv6_recv_error(sk, msg, len);
209 try_again:
210 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
211 &peeked, &err);
212 if (!skb)
213 goto out;
215 ulen = skb->len - sizeof(struct udphdr);
216 copied = len;
217 if (copied > ulen)
218 copied = ulen;
219 else if (copied < ulen)
220 msg->msg_flags |= MSG_TRUNC;
222 is_udp4 = (skb->protocol == htons(ETH_P_IP));
225 * If checksum is needed at all, try to do it while copying the
226 * data. If the data is truncated, or if we only want a partial
227 * coverage checksum (UDP-Lite), do it before the copy.
230 if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
231 if (udp_lib_checksum_complete(skb))
232 goto csum_copy_err;
235 if (skb_csum_unnecessary(skb))
236 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
237 msg->msg_iov, copied );
238 else {
239 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
240 if (err == -EINVAL)
241 goto csum_copy_err;
243 if (err)
244 goto out_free;
246 if (!peeked) {
247 if (is_udp4)
248 UDP_INC_STATS_USER(sock_net(sk),
249 UDP_MIB_INDATAGRAMS, is_udplite);
250 else
251 UDP6_INC_STATS_USER(sock_net(sk),
252 UDP_MIB_INDATAGRAMS, is_udplite);
255 sock_recv_timestamp(msg, sk, skb);
257 /* Copy the address. */
258 if (msg->msg_name) {
259 struct sockaddr_in6 *sin6;
261 sin6 = (struct sockaddr_in6 *) msg->msg_name;
262 sin6->sin6_family = AF_INET6;
263 sin6->sin6_port = udp_hdr(skb)->source;
264 sin6->sin6_flowinfo = 0;
265 sin6->sin6_scope_id = 0;
267 if (is_udp4)
268 ipv6_addr_set(&sin6->sin6_addr, 0, 0,
269 htonl(0xffff), ip_hdr(skb)->saddr);
270 else {
271 ipv6_addr_copy(&sin6->sin6_addr,
272 &ipv6_hdr(skb)->saddr);
273 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
274 sin6->sin6_scope_id = IP6CB(skb)->iif;
278 if (is_udp4) {
279 if (inet->cmsg_flags)
280 ip_cmsg_recv(msg, skb);
281 } else {
282 if (np->rxopt.all)
283 datagram_recv_ctl(sk, msg, skb);
286 err = copied;
287 if (flags & MSG_TRUNC)
288 err = ulen;
290 out_free:
291 skb_free_datagram_locked(sk, skb);
292 out:
293 return err;
295 csum_copy_err:
296 lock_sock(sk);
297 if (!skb_kill_datagram(sk, skb, flags)) {
298 if (is_udp4)
299 UDP_INC_STATS_USER(sock_net(sk),
300 UDP_MIB_INERRORS, is_udplite);
301 else
302 UDP6_INC_STATS_USER(sock_net(sk),
303 UDP_MIB_INERRORS, is_udplite);
305 release_sock(sk);
307 if (flags & MSG_DONTWAIT)
308 return -EAGAIN;
309 goto try_again;
312 void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
313 u8 type, u8 code, int offset, __be32 info,
314 struct udp_table *udptable)
316 struct ipv6_pinfo *np;
317 struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data;
318 struct in6_addr *saddr = &hdr->saddr;
319 struct in6_addr *daddr = &hdr->daddr;
320 struct udphdr *uh = (struct udphdr*)(skb->data+offset);
321 struct sock *sk;
322 int err;
324 sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest,
325 saddr, uh->source, inet6_iif(skb), udptable);
326 if (sk == NULL)
327 return;
329 np = inet6_sk(sk);
331 if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
332 goto out;
334 if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
335 goto out;
337 if (np->recverr)
338 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
340 sk->sk_err = err;
341 sk->sk_error_report(sk);
342 out:
343 sock_put(sk);
346 static __inline__ void udpv6_err(struct sk_buff *skb,
347 struct inet6_skb_parm *opt, u8 type,
348 u8 code, int offset, __be32 info )
350 __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
353 int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
355 struct udp_sock *up = udp_sk(sk);
356 int rc;
357 int is_udplite = IS_UDPLITE(sk);
359 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
360 goto drop;
363 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
365 if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
367 if (up->pcrlen == 0) { /* full coverage was set */
368 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage"
369 " %d while full coverage %d requested\n",
370 UDP_SKB_CB(skb)->cscov, skb->len);
371 goto drop;
373 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
374 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d "
375 "too small, need min %d\n",
376 UDP_SKB_CB(skb)->cscov, up->pcrlen);
377 goto drop;
381 if (sk->sk_filter) {
382 if (udp_lib_checksum_complete(skb))
383 goto drop;
386 if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) {
387 /* Note that an ENOMEM error is charged twice */
388 if (rc == -ENOMEM) {
389 UDP6_INC_STATS_BH(sock_net(sk),
390 UDP_MIB_RCVBUFERRORS, is_udplite);
391 atomic_inc(&sk->sk_drops);
393 goto drop;
396 return 0;
397 drop:
398 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
399 kfree_skb(skb);
400 return -1;
403 static struct sock *udp_v6_mcast_next(struct net *net, struct sock *sk,
404 __be16 loc_port, struct in6_addr *loc_addr,
405 __be16 rmt_port, struct in6_addr *rmt_addr,
406 int dif)
408 struct hlist_nulls_node *node;
409 struct sock *s = sk;
410 unsigned short num = ntohs(loc_port);
412 sk_nulls_for_each_from(s, node) {
413 struct inet_sock *inet = inet_sk(s);
415 if (!net_eq(sock_net(s), net))
416 continue;
418 if (s->sk_hash == num && s->sk_family == PF_INET6) {
419 struct ipv6_pinfo *np = inet6_sk(s);
420 if (inet->dport) {
421 if (inet->dport != rmt_port)
422 continue;
424 if (!ipv6_addr_any(&np->daddr) &&
425 !ipv6_addr_equal(&np->daddr, rmt_addr))
426 continue;
428 if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
429 continue;
431 if (!ipv6_addr_any(&np->rcv_saddr)) {
432 if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
433 continue;
435 if (!inet6_mc_check(s, loc_addr, rmt_addr))
436 continue;
437 return s;
440 return NULL;
444 * Note: called only from the BH handler context,
445 * so we don't need to lock the hashes.
447 static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
448 struct in6_addr *saddr, struct in6_addr *daddr,
449 struct udp_table *udptable)
451 struct sock *sk, *sk2;
452 const struct udphdr *uh = udp_hdr(skb);
453 struct udp_hslot *hslot = &udptable->hash[udp_hashfn(net, ntohs(uh->dest))];
454 int dif;
456 spin_lock(&hslot->lock);
457 sk = sk_nulls_head(&hslot->head);
458 dif = inet6_iif(skb);
459 sk = udp_v6_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
460 if (!sk) {
461 kfree_skb(skb);
462 goto out;
465 sk2 = sk;
466 while ((sk2 = udp_v6_mcast_next(net, sk_nulls_next(sk2), uh->dest, daddr,
467 uh->source, saddr, dif))) {
468 struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC);
469 if (buff) {
470 bh_lock_sock(sk2);
471 if (!sock_owned_by_user(sk2))
472 udpv6_queue_rcv_skb(sk2, buff);
473 else
474 sk_add_backlog(sk2, buff);
475 bh_unlock_sock(sk2);
478 bh_lock_sock(sk);
479 if (!sock_owned_by_user(sk))
480 udpv6_queue_rcv_skb(sk, skb);
481 else
482 sk_add_backlog(sk, skb);
483 bh_unlock_sock(sk);
484 out:
485 spin_unlock(&hslot->lock);
486 return 0;
489 static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh,
490 int proto)
492 int err;
494 UDP_SKB_CB(skb)->partial_cov = 0;
495 UDP_SKB_CB(skb)->cscov = skb->len;
497 if (proto == IPPROTO_UDPLITE) {
498 err = udplite_checksum_init(skb, uh);
499 if (err)
500 return err;
503 if (uh->check == 0) {
504 /* RFC 2460 section 8.1 says that we SHOULD log
505 this error. Well, it is reasonable.
507 LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
508 return 1;
510 if (skb->ip_summed == CHECKSUM_COMPLETE &&
511 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
512 skb->len, proto, skb->csum))
513 skb->ip_summed = CHECKSUM_UNNECESSARY;
515 if (!skb_csum_unnecessary(skb))
516 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
517 &ipv6_hdr(skb)->daddr,
518 skb->len, proto, 0));
520 return 0;
523 int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
524 int proto)
526 struct sock *sk;
527 struct udphdr *uh;
528 struct net_device *dev = skb->dev;
529 struct in6_addr *saddr, *daddr;
530 u32 ulen = 0;
531 struct net *net = dev_net(skb->dev);
533 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
534 goto short_packet;
536 saddr = &ipv6_hdr(skb)->saddr;
537 daddr = &ipv6_hdr(skb)->daddr;
538 uh = udp_hdr(skb);
540 ulen = ntohs(uh->len);
541 if (ulen > skb->len)
542 goto short_packet;
544 if (proto == IPPROTO_UDP) {
545 /* UDP validates ulen. */
547 /* Check for jumbo payload */
548 if (ulen == 0)
549 ulen = skb->len;
551 if (ulen < sizeof(*uh))
552 goto short_packet;
554 if (ulen < skb->len) {
555 if (pskb_trim_rcsum(skb, ulen))
556 goto short_packet;
557 saddr = &ipv6_hdr(skb)->saddr;
558 daddr = &ipv6_hdr(skb)->daddr;
559 uh = udp_hdr(skb);
563 if (udp6_csum_init(skb, uh, proto))
564 goto discard;
567 * Multicast receive code
569 if (ipv6_addr_is_multicast(daddr))
570 return __udp6_lib_mcast_deliver(net, skb,
571 saddr, daddr, udptable);
573 /* Unicast */
576 * check socket cache ... must talk to Alan about his plans
577 * for sock caches... i'll skip this for now.
579 sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
581 if (sk == NULL) {
582 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
583 goto discard;
585 if (udp_lib_checksum_complete(skb))
586 goto discard;
587 UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS,
588 proto == IPPROTO_UDPLITE);
590 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev);
592 kfree_skb(skb);
593 return 0;
596 /* deliver */
598 bh_lock_sock(sk);
599 if (!sock_owned_by_user(sk))
600 udpv6_queue_rcv_skb(sk, skb);
601 else
602 sk_add_backlog(sk, skb);
603 bh_unlock_sock(sk);
604 sock_put(sk);
605 return 0;
607 short_packet:
608 LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: %d/%u\n",
609 proto == IPPROTO_UDPLITE ? "-Lite" : "",
610 ulen, skb->len);
612 discard:
613 UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
614 kfree_skb(skb);
615 return 0;
618 static __inline__ int udpv6_rcv(struct sk_buff *skb)
620 return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
624 * Throw away all pending data and cancel the corking. Socket is locked.
626 static void udp_v6_flush_pending_frames(struct sock *sk)
628 struct udp_sock *up = udp_sk(sk);
630 if (up->pending == AF_INET)
631 udp_flush_pending_frames(sk);
632 else if (up->pending) {
633 up->len = 0;
634 up->pending = 0;
635 ip6_flush_pending_frames(sk);
640 * udp6_hwcsum_outgoing - handle outgoing HW checksumming
641 * @sk: socket we are sending on
642 * @skb: sk_buff containing the filled-in UDP header
643 * (checksum field must be zeroed out)
645 static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
646 const struct in6_addr *saddr,
647 const struct in6_addr *daddr, int len)
649 unsigned int offset;
650 struct udphdr *uh = udp_hdr(skb);
651 __wsum csum = 0;
653 if (skb_queue_len(&sk->sk_write_queue) == 1) {
654 /* Only one fragment on the socket. */
655 skb->csum_start = skb_transport_header(skb) - skb->head;
656 skb->csum_offset = offsetof(struct udphdr, check);
657 uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
658 } else {
660 * HW-checksum won't work as there are two or more
661 * fragments on the socket so that all csums of sk_buffs
662 * should be together
664 offset = skb_transport_offset(skb);
665 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
667 skb->ip_summed = CHECKSUM_NONE;
669 skb_queue_walk(&sk->sk_write_queue, skb) {
670 csum = csum_add(csum, skb->csum);
673 uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
674 csum);
675 if (uh->check == 0)
676 uh->check = CSUM_MANGLED_0;
681 * Sending
684 static int udp_v6_push_pending_frames(struct sock *sk)
686 struct sk_buff *skb;
687 struct udphdr *uh;
688 struct udp_sock *up = udp_sk(sk);
689 struct inet_sock *inet = inet_sk(sk);
690 struct flowi *fl = &inet->cork.fl;
691 int err = 0;
692 int is_udplite = IS_UDPLITE(sk);
693 __wsum csum = 0;
695 /* Grab the skbuff where UDP header space exists. */
696 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
697 goto out;
700 * Create a UDP header
702 uh = udp_hdr(skb);
703 uh->source = fl->fl_ip_sport;
704 uh->dest = fl->fl_ip_dport;
705 uh->len = htons(up->len);
706 uh->check = 0;
708 if (is_udplite)
709 csum = udplite_csum_outgoing(sk, skb);
710 else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
711 udp6_hwcsum_outgoing(sk, skb, &fl->fl6_src, &fl->fl6_dst,
712 up->len);
713 goto send;
714 } else
715 csum = udp_csum_outgoing(sk, skb);
717 /* add protocol-dependent pseudo-header */
718 uh->check = csum_ipv6_magic(&fl->fl6_src, &fl->fl6_dst,
719 up->len, fl->proto, csum );
720 if (uh->check == 0)
721 uh->check = CSUM_MANGLED_0;
723 send:
724 err = ip6_push_pending_frames(sk);
725 if (err) {
726 if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
727 UDP6_INC_STATS_USER(sock_net(sk),
728 UDP_MIB_SNDBUFERRORS, is_udplite);
729 err = 0;
731 } else
732 UDP6_INC_STATS_USER(sock_net(sk),
733 UDP_MIB_OUTDATAGRAMS, is_udplite);
734 out:
735 up->len = 0;
736 up->pending = 0;
737 return err;
740 int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
741 struct msghdr *msg, size_t len)
743 struct ipv6_txoptions opt_space;
744 struct udp_sock *up = udp_sk(sk);
745 struct inet_sock *inet = inet_sk(sk);
746 struct ipv6_pinfo *np = inet6_sk(sk);
747 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
748 struct in6_addr *daddr, *final_p = NULL, final;
749 struct ipv6_txoptions *opt = NULL;
750 struct ip6_flowlabel *flowlabel = NULL;
751 struct flowi fl;
752 struct dst_entry *dst;
753 int addr_len = msg->msg_namelen;
754 int ulen = len;
755 int hlimit = -1;
756 int tclass = -1;
757 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
758 int err;
759 int connected = 0;
760 int is_udplite = IS_UDPLITE(sk);
761 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
763 /* destination address check */
764 if (sin6) {
765 if (addr_len < offsetof(struct sockaddr, sa_data))
766 return -EINVAL;
768 switch (sin6->sin6_family) {
769 case AF_INET6:
770 if (addr_len < SIN6_LEN_RFC2133)
771 return -EINVAL;
772 daddr = &sin6->sin6_addr;
773 break;
774 case AF_INET:
775 goto do_udp_sendmsg;
776 case AF_UNSPEC:
777 msg->msg_name = sin6 = NULL;
778 msg->msg_namelen = addr_len = 0;
779 daddr = NULL;
780 break;
781 default:
782 return -EINVAL;
784 } else if (!up->pending) {
785 if (sk->sk_state != TCP_ESTABLISHED)
786 return -EDESTADDRREQ;
787 daddr = &np->daddr;
788 } else
789 daddr = NULL;
791 if (daddr) {
792 if (ipv6_addr_v4mapped(daddr)) {
793 struct sockaddr_in sin;
794 sin.sin_family = AF_INET;
795 sin.sin_port = sin6 ? sin6->sin6_port : inet->dport;
796 sin.sin_addr.s_addr = daddr->s6_addr32[3];
797 msg->msg_name = &sin;
798 msg->msg_namelen = sizeof(sin);
799 do_udp_sendmsg:
800 if (__ipv6_only_sock(sk))
801 return -ENETUNREACH;
802 return udp_sendmsg(iocb, sk, msg, len);
806 if (up->pending == AF_INET)
807 return udp_sendmsg(iocb, sk, msg, len);
809 /* Rough check on arithmetic overflow,
810 better check is made in ip6_append_data().
812 if (len > INT_MAX - sizeof(struct udphdr))
813 return -EMSGSIZE;
815 if (up->pending) {
817 * There are pending frames.
818 * The socket lock must be held while it's corked.
820 lock_sock(sk);
821 if (likely(up->pending)) {
822 if (unlikely(up->pending != AF_INET6)) {
823 release_sock(sk);
824 return -EAFNOSUPPORT;
826 dst = NULL;
827 goto do_append_data;
829 release_sock(sk);
831 ulen += sizeof(struct udphdr);
833 memset(&fl, 0, sizeof(fl));
835 if (sin6) {
836 if (sin6->sin6_port == 0)
837 return -EINVAL;
839 fl.fl_ip_dport = sin6->sin6_port;
840 daddr = &sin6->sin6_addr;
842 if (np->sndflow) {
843 fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
844 if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
845 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
846 if (flowlabel == NULL)
847 return -EINVAL;
848 daddr = &flowlabel->dst;
853 * Otherwise it will be difficult to maintain
854 * sk->sk_dst_cache.
856 if (sk->sk_state == TCP_ESTABLISHED &&
857 ipv6_addr_equal(daddr, &np->daddr))
858 daddr = &np->daddr;
860 if (addr_len >= sizeof(struct sockaddr_in6) &&
861 sin6->sin6_scope_id &&
862 ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
863 fl.oif = sin6->sin6_scope_id;
864 } else {
865 if (sk->sk_state != TCP_ESTABLISHED)
866 return -EDESTADDRREQ;
868 fl.fl_ip_dport = inet->dport;
869 daddr = &np->daddr;
870 fl.fl6_flowlabel = np->flow_label;
871 connected = 1;
874 if (!fl.oif)
875 fl.oif = sk->sk_bound_dev_if;
877 if (!fl.oif)
878 fl.oif = np->sticky_pktinfo.ipi6_ifindex;
880 if (msg->msg_controllen) {
881 opt = &opt_space;
882 memset(opt, 0, sizeof(struct ipv6_txoptions));
883 opt->tot_len = sizeof(*opt);
885 err = datagram_send_ctl(sock_net(sk), msg, &fl, opt, &hlimit, &tclass);
886 if (err < 0) {
887 fl6_sock_release(flowlabel);
888 return err;
890 if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
891 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
892 if (flowlabel == NULL)
893 return -EINVAL;
895 if (!(opt->opt_nflen|opt->opt_flen))
896 opt = NULL;
897 connected = 0;
899 if (opt == NULL)
900 opt = np->opt;
901 if (flowlabel)
902 opt = fl6_merge_options(&opt_space, flowlabel, opt);
903 opt = ipv6_fixup_options(&opt_space, opt);
905 fl.proto = sk->sk_protocol;
906 if (!ipv6_addr_any(daddr))
907 ipv6_addr_copy(&fl.fl6_dst, daddr);
908 else
909 fl.fl6_dst.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
910 if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
911 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
912 fl.fl_ip_sport = inet->sport;
914 /* merge ip6_build_xmit from ip6_output */
915 if (opt && opt->srcrt) {
916 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
917 ipv6_addr_copy(&final, &fl.fl6_dst);
918 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
919 final_p = &final;
920 connected = 0;
923 if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst)) {
924 fl.oif = np->mcast_oif;
925 connected = 0;
928 security_sk_classify_flow(sk, &fl);
930 err = ip6_sk_dst_lookup(sk, &dst, &fl);
931 if (err)
932 goto out;
933 if (final_p)
934 ipv6_addr_copy(&fl.fl6_dst, final_p);
936 err = __xfrm_lookup(sock_net(sk), &dst, &fl, sk, XFRM_LOOKUP_WAIT);
937 if (err < 0) {
938 if (err == -EREMOTE)
939 err = ip6_dst_blackhole(sk, &dst, &fl);
940 if (err < 0)
941 goto out;
944 if (hlimit < 0) {
945 if (ipv6_addr_is_multicast(&fl.fl6_dst))
946 hlimit = np->mcast_hops;
947 else
948 hlimit = np->hop_limit;
949 if (hlimit < 0)
950 hlimit = ip6_dst_hoplimit(dst);
953 if (tclass < 0)
954 tclass = np->tclass;
956 if (msg->msg_flags&MSG_CONFIRM)
957 goto do_confirm;
958 back_from_confirm:
960 lock_sock(sk);
961 if (unlikely(up->pending)) {
962 /* The socket is already corked while preparing it. */
963 /* ... which is an evident application bug. --ANK */
964 release_sock(sk);
966 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
967 err = -EINVAL;
968 goto out;
971 up->pending = AF_INET6;
973 do_append_data:
974 up->len += ulen;
975 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
976 err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen,
977 sizeof(struct udphdr), hlimit, tclass, opt, &fl,
978 (struct rt6_info*)dst,
979 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
980 if (err)
981 udp_v6_flush_pending_frames(sk);
982 else if (!corkreq)
983 err = udp_v6_push_pending_frames(sk);
984 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
985 up->pending = 0;
987 if (dst) {
988 if (connected) {
989 ip6_dst_store(sk, dst,
990 ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
991 &np->daddr : NULL,
992 #ifdef CONFIG_IPV6_SUBTREES
993 ipv6_addr_equal(&fl.fl6_src, &np->saddr) ?
994 &np->saddr :
995 #endif
996 NULL);
997 } else {
998 dst_release(dst);
1000 dst = NULL;
1003 if (err > 0)
1004 err = np->recverr ? net_xmit_errno(err) : 0;
1005 release_sock(sk);
1006 out:
1007 dst_release(dst);
1008 fl6_sock_release(flowlabel);
1009 if (!err)
1010 return len;
1012 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
1013 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1014 * we don't have a good statistic (IpOutDiscards but it can be too many
1015 * things). We could add another new stat but at least for now that
1016 * seems like overkill.
1018 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
1019 UDP6_INC_STATS_USER(sock_net(sk),
1020 UDP_MIB_SNDBUFERRORS, is_udplite);
1022 return err;
1024 do_confirm:
1025 dst_confirm(dst);
1026 if (!(msg->msg_flags&MSG_PROBE) || len)
1027 goto back_from_confirm;
1028 err = 0;
1029 goto out;
1032 void udpv6_destroy_sock(struct sock *sk)
1034 lock_sock(sk);
1035 udp_v6_flush_pending_frames(sk);
1036 release_sock(sk);
1038 inet6_destroy_sock(sk);
1042 * Socket option code for UDP
1044 int udpv6_setsockopt(struct sock *sk, int level, int optname,
1045 char __user *optval, unsigned int optlen)
1047 if (level == SOL_UDP || level == SOL_UDPLITE)
1048 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1049 udp_v6_push_pending_frames);
1050 return ipv6_setsockopt(sk, level, optname, optval, optlen);
1053 #ifdef CONFIG_COMPAT
1054 int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
1055 char __user *optval, unsigned int optlen)
1057 if (level == SOL_UDP || level == SOL_UDPLITE)
1058 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1059 udp_v6_push_pending_frames);
1060 return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
1062 #endif
1064 int udpv6_getsockopt(struct sock *sk, int level, int optname,
1065 char __user *optval, int __user *optlen)
1067 if (level == SOL_UDP || level == SOL_UDPLITE)
1068 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1069 return ipv6_getsockopt(sk, level, optname, optval, optlen);
1072 #ifdef CONFIG_COMPAT
1073 int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1074 char __user *optval, int __user *optlen)
1076 if (level == SOL_UDP || level == SOL_UDPLITE)
1077 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1078 return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
1080 #endif
1082 static int udp6_ufo_send_check(struct sk_buff *skb)
1084 struct ipv6hdr *ipv6h;
1085 struct udphdr *uh;
1087 if (!pskb_may_pull(skb, sizeof(*uh)))
1088 return -EINVAL;
1090 ipv6h = ipv6_hdr(skb);
1091 uh = udp_hdr(skb);
1093 uh->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, skb->len,
1094 IPPROTO_UDP, 0);
1095 skb->csum_start = skb_transport_header(skb) - skb->head;
1096 skb->csum_offset = offsetof(struct udphdr, check);
1097 skb->ip_summed = CHECKSUM_PARTIAL;
1098 return 0;
1101 static struct sk_buff *udp6_ufo_fragment(struct sk_buff *skb, int features)
1103 struct sk_buff *segs = ERR_PTR(-EINVAL);
1104 unsigned int mss;
1105 unsigned int unfrag_ip6hlen, unfrag_len;
1106 struct frag_hdr *fptr;
1107 u8 *mac_start, *prevhdr;
1108 u8 nexthdr;
1109 u8 frag_hdr_sz = sizeof(struct frag_hdr);
1110 int offset;
1111 __wsum csum;
1113 mss = skb_shinfo(skb)->gso_size;
1114 if (unlikely(skb->len <= mss))
1115 goto out;
1117 if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
1118 /* Packet is from an untrusted source, reset gso_segs. */
1119 int type = skb_shinfo(skb)->gso_type;
1121 if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY) ||
1122 !(type & (SKB_GSO_UDP))))
1123 goto out;
1125 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
1127 segs = NULL;
1128 goto out;
1131 /* Do software UFO. Complete and fill in the UDP checksum as HW cannot
1132 * do checksum of UDP packets sent as multiple IP fragments.
1134 offset = skb->csum_start - skb_headroom(skb);
1135 csum = skb_checksum(skb, offset, skb->len- offset, 0);
1136 offset += skb->csum_offset;
1137 *(__sum16 *)(skb->data + offset) = csum_fold(csum);
1138 skb->ip_summed = CHECKSUM_NONE;
1140 /* Check if there is enough headroom to insert fragment header. */
1141 if ((skb_headroom(skb) < frag_hdr_sz) &&
1142 pskb_expand_head(skb, frag_hdr_sz, 0, GFP_ATOMIC))
1143 goto out;
1145 /* Find the unfragmentable header and shift it left by frag_hdr_sz
1146 * bytes to insert fragment header.
1148 unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
1149 nexthdr = *prevhdr;
1150 *prevhdr = NEXTHDR_FRAGMENT;
1151 unfrag_len = skb_network_header(skb) - skb_mac_header(skb) +
1152 unfrag_ip6hlen;
1153 mac_start = skb_mac_header(skb);
1154 memmove(mac_start-frag_hdr_sz, mac_start, unfrag_len);
1156 skb->mac_header -= frag_hdr_sz;
1157 skb->network_header -= frag_hdr_sz;
1159 fptr = (struct frag_hdr *)(skb_network_header(skb) + unfrag_ip6hlen);
1160 fptr->nexthdr = nexthdr;
1161 fptr->reserved = 0;
1162 ipv6_select_ident(fptr);
1164 /* Fragment the skb. ipv6 header and the remaining fields of the
1165 * fragment header are updated in ipv6_gso_segment()
1167 segs = skb_segment(skb, features);
1169 out:
1170 return segs;
1173 static const struct inet6_protocol udpv6_protocol = {
1174 .handler = udpv6_rcv,
1175 .err_handler = udpv6_err,
1176 .gso_send_check = udp6_ufo_send_check,
1177 .gso_segment = udp6_ufo_fragment,
1178 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1181 /* ------------------------------------------------------------------------ */
1182 #ifdef CONFIG_PROC_FS
1184 static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
1186 struct inet_sock *inet = inet_sk(sp);
1187 struct ipv6_pinfo *np = inet6_sk(sp);
1188 struct in6_addr *dest, *src;
1189 __u16 destp, srcp;
1191 dest = &np->daddr;
1192 src = &np->rcv_saddr;
1193 destp = ntohs(inet->dport);
1194 srcp = ntohs(inet->sport);
1195 seq_printf(seq,
1196 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1197 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n",
1198 bucket,
1199 src->s6_addr32[0], src->s6_addr32[1],
1200 src->s6_addr32[2], src->s6_addr32[3], srcp,
1201 dest->s6_addr32[0], dest->s6_addr32[1],
1202 dest->s6_addr32[2], dest->s6_addr32[3], destp,
1203 sp->sk_state,
1204 sk_wmem_alloc_get(sp),
1205 sk_rmem_alloc_get(sp),
1206 0, 0L, 0,
1207 sock_i_uid(sp), 0,
1208 sock_i_ino(sp),
1209 atomic_read(&sp->sk_refcnt), sp,
1210 atomic_read(&sp->sk_drops));
1213 int udp6_seq_show(struct seq_file *seq, void *v)
1215 if (v == SEQ_START_TOKEN)
1216 seq_printf(seq,
1217 " sl "
1218 "local_address "
1219 "remote_address "
1220 "st tx_queue rx_queue tr tm->when retrnsmt"
1221 " uid timeout inode ref pointer drops\n");
1222 else
1223 udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
1224 return 0;
1227 static struct udp_seq_afinfo udp6_seq_afinfo = {
1228 .name = "udp6",
1229 .family = AF_INET6,
1230 .udp_table = &udp_table,
1231 .seq_fops = {
1232 .owner = THIS_MODULE,
1234 .seq_ops = {
1235 .show = udp6_seq_show,
1239 int udp6_proc_init(struct net *net)
1241 return udp_proc_register(net, &udp6_seq_afinfo);
1244 void udp6_proc_exit(struct net *net) {
1245 udp_proc_unregister(net, &udp6_seq_afinfo);
1247 #endif /* CONFIG_PROC_FS */
1249 /* ------------------------------------------------------------------------ */
1251 struct proto udpv6_prot = {
1252 .name = "UDPv6",
1253 .owner = THIS_MODULE,
1254 .close = udp_lib_close,
1255 .connect = ip6_datagram_connect,
1256 .disconnect = udp_disconnect,
1257 .ioctl = udp_ioctl,
1258 .destroy = udpv6_destroy_sock,
1259 .setsockopt = udpv6_setsockopt,
1260 .getsockopt = udpv6_getsockopt,
1261 .sendmsg = udpv6_sendmsg,
1262 .recvmsg = udpv6_recvmsg,
1263 .backlog_rcv = udpv6_queue_rcv_skb,
1264 .hash = udp_lib_hash,
1265 .unhash = udp_lib_unhash,
1266 .get_port = udp_v6_get_port,
1267 .memory_allocated = &udp_memory_allocated,
1268 .sysctl_mem = sysctl_udp_mem,
1269 .sysctl_wmem = &sysctl_udp_wmem_min,
1270 .sysctl_rmem = &sysctl_udp_rmem_min,
1271 .obj_size = sizeof(struct udp6_sock),
1272 .slab_flags = SLAB_DESTROY_BY_RCU,
1273 .h.udp_table = &udp_table,
1274 #ifdef CONFIG_COMPAT
1275 .compat_setsockopt = compat_udpv6_setsockopt,
1276 .compat_getsockopt = compat_udpv6_getsockopt,
1277 #endif
1280 static struct inet_protosw udpv6_protosw = {
1281 .type = SOCK_DGRAM,
1282 .protocol = IPPROTO_UDP,
1283 .prot = &udpv6_prot,
1284 .ops = &inet6_dgram_ops,
1285 .capability =-1,
1286 .no_check = UDP_CSUM_DEFAULT,
1287 .flags = INET_PROTOSW_PERMANENT,
1291 int __init udpv6_init(void)
1293 int ret;
1295 ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1296 if (ret)
1297 goto out;
1299 ret = inet6_register_protosw(&udpv6_protosw);
1300 if (ret)
1301 goto out_udpv6_protocol;
1302 out:
1303 return ret;
1305 out_udpv6_protocol:
1306 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1307 goto out;
1310 void udpv6_exit(void)
1312 inet6_unregister_protosw(&udpv6_protosw);
1313 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);