1db8dac20SDavid S. Miller /* 2db8dac20SDavid S. Miller * UDP over IPv6 3db8dac20SDavid S. Miller * Linux INET6 implementation 4db8dac20SDavid S. Miller * 5db8dac20SDavid S. Miller * Authors: 6db8dac20SDavid S. Miller * Pedro Roque <roque@di.fc.ul.pt> 7db8dac20SDavid S. Miller * 8db8dac20SDavid S. Miller * Based on linux/ipv4/udp.c 9db8dac20SDavid S. Miller * 10db8dac20SDavid S. Miller * Fixes: 11db8dac20SDavid S. Miller * Hideaki YOSHIFUJI : sin6_scope_id support 12db8dac20SDavid S. Miller * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which 13db8dac20SDavid S. Miller * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind 14db8dac20SDavid S. Miller * a single port at the same time. 15db8dac20SDavid S. Miller * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data 16db8dac20SDavid S. Miller * YOSHIFUJI Hideaki @USAGI: convert /proc/net/udp6 to seq_file. 17db8dac20SDavid S. Miller * 18db8dac20SDavid S. Miller * This program is free software; you can redistribute it and/or 19db8dac20SDavid S. Miller * modify it under the terms of the GNU General Public License 20db8dac20SDavid S. Miller * as published by the Free Software Foundation; either version 21db8dac20SDavid S. Miller * 2 of the License, or (at your option) any later version. 22db8dac20SDavid S. Miller */ 23db8dac20SDavid S. Miller 24db8dac20SDavid S. Miller #include <linux/errno.h> 25db8dac20SDavid S. Miller #include <linux/types.h> 26db8dac20SDavid S. Miller #include <linux/socket.h> 27db8dac20SDavid S. Miller #include <linux/sockios.h> 28db8dac20SDavid S. Miller #include <linux/net.h> 29db8dac20SDavid S. Miller #include <linux/in6.h> 30db8dac20SDavid S. Miller #include <linux/netdevice.h> 31db8dac20SDavid S. Miller #include <linux/if_arp.h> 32db8dac20SDavid S. Miller #include <linux/ipv6.h> 33db8dac20SDavid S. Miller #include <linux/icmpv6.h> 34db8dac20SDavid S. Miller #include <linux/init.h> 35db8dac20SDavid S. Miller #include <linux/module.h> 36db8dac20SDavid S. Miller #include <linux/skbuff.h> 37db8dac20SDavid S. Miller #include <asm/uaccess.h> 38db8dac20SDavid S. Miller 39db8dac20SDavid S. Miller #include <net/ndisc.h> 40db8dac20SDavid S. Miller #include <net/protocol.h> 41db8dac20SDavid S. Miller #include <net/transp_v6.h> 42db8dac20SDavid S. Miller #include <net/ip6_route.h> 43db8dac20SDavid S. Miller #include <net/raw.h> 44db8dac20SDavid S. Miller #include <net/tcp_states.h> 45db8dac20SDavid S. Miller #include <net/ip6_checksum.h> 46db8dac20SDavid S. Miller #include <net/xfrm.h> 47db8dac20SDavid S. Miller 48db8dac20SDavid S. Miller #include <linux/proc_fs.h> 49db8dac20SDavid S. Miller #include <linux/seq_file.h> 50db8dac20SDavid S. Miller #include "udp_impl.h" 51db8dac20SDavid S. Miller 52b2f5e7cdSVlad Yasevich int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2) 53b2f5e7cdSVlad Yasevich { 54b2f5e7cdSVlad Yasevich const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr; 55b2f5e7cdSVlad Yasevich const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2); 56499923c7SVlad Yasevich __be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr; 57499923c7SVlad Yasevich __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2); 58b2f5e7cdSVlad Yasevich int sk_ipv6only = ipv6_only_sock(sk); 59b2f5e7cdSVlad Yasevich int sk2_ipv6only = inet_v6_ipv6only(sk2); 60b2f5e7cdSVlad Yasevich int addr_type = ipv6_addr_type(sk_rcv_saddr6); 61b2f5e7cdSVlad Yasevich int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED; 62b2f5e7cdSVlad Yasevich 63b2f5e7cdSVlad Yasevich /* if both are mapped, treat as IPv4 */ 64b2f5e7cdSVlad Yasevich if (addr_type == IPV6_ADDR_MAPPED && addr_type2 == IPV6_ADDR_MAPPED) 65499923c7SVlad Yasevich return (!sk2_ipv6only && 66499923c7SVlad Yasevich (!sk_rcv_saddr || !sk2_rcv_saddr || 67499923c7SVlad Yasevich sk_rcv_saddr == sk2_rcv_saddr)); 68b2f5e7cdSVlad Yasevich 69b2f5e7cdSVlad Yasevich if (addr_type2 == IPV6_ADDR_ANY && 70b2f5e7cdSVlad Yasevich !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED)) 71b2f5e7cdSVlad Yasevich return 1; 72b2f5e7cdSVlad Yasevich 73b2f5e7cdSVlad Yasevich if (addr_type == IPV6_ADDR_ANY && 74b2f5e7cdSVlad Yasevich !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED)) 75b2f5e7cdSVlad Yasevich return 1; 76b2f5e7cdSVlad Yasevich 77b2f5e7cdSVlad Yasevich if (sk2_rcv_saddr6 && 78b2f5e7cdSVlad Yasevich ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6)) 79b2f5e7cdSVlad Yasevich return 1; 80b2f5e7cdSVlad Yasevich 81b2f5e7cdSVlad Yasevich return 0; 82b2f5e7cdSVlad Yasevich } 83b2f5e7cdSVlad Yasevich 846ba5a3c5SPavel Emelyanov int udp_v6_get_port(struct sock *sk, unsigned short snum) 85db8dac20SDavid S. Miller { 866ba5a3c5SPavel Emelyanov return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal); 87db8dac20SDavid S. Miller } 88db8dac20SDavid S. Miller 89645ca708SEric Dumazet static inline int compute_score(struct sock *sk, struct net *net, 90645ca708SEric Dumazet unsigned short hnum, 91db8dac20SDavid S. Miller struct in6_addr *saddr, __be16 sport, 92db8dac20SDavid S. Miller struct in6_addr *daddr, __be16 dport, 93645ca708SEric Dumazet int dif) 94db8dac20SDavid S. Miller { 95645ca708SEric Dumazet int score = -1; 96db8dac20SDavid S. Miller 97878628fbSYOSHIFUJI Hideaki if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum && 98db8dac20SDavid S. Miller sk->sk_family == PF_INET6) { 99db8dac20SDavid S. Miller struct ipv6_pinfo *np = inet6_sk(sk); 100645ca708SEric Dumazet struct inet_sock *inet = inet_sk(sk); 101645ca708SEric Dumazet 102645ca708SEric Dumazet score = 0; 103db8dac20SDavid S. Miller if (inet->dport) { 104db8dac20SDavid S. Miller if (inet->dport != sport) 105645ca708SEric Dumazet return -1; 106db8dac20SDavid S. Miller score++; 107db8dac20SDavid S. Miller } 108db8dac20SDavid S. Miller if (!ipv6_addr_any(&np->rcv_saddr)) { 109db8dac20SDavid S. Miller if (!ipv6_addr_equal(&np->rcv_saddr, daddr)) 110645ca708SEric Dumazet return -1; 111db8dac20SDavid S. Miller score++; 112db8dac20SDavid S. Miller } 113db8dac20SDavid S. Miller if (!ipv6_addr_any(&np->daddr)) { 114db8dac20SDavid S. Miller if (!ipv6_addr_equal(&np->daddr, saddr)) 115645ca708SEric Dumazet return -1; 116db8dac20SDavid S. Miller score++; 117db8dac20SDavid S. Miller } 118db8dac20SDavid S. Miller if (sk->sk_bound_dev_if) { 119db8dac20SDavid S. Miller if (sk->sk_bound_dev_if != dif) 120645ca708SEric Dumazet return -1; 121db8dac20SDavid S. Miller score++; 122db8dac20SDavid S. Miller } 123645ca708SEric Dumazet } 124645ca708SEric Dumazet return score; 125645ca708SEric Dumazet } 126645ca708SEric Dumazet 127645ca708SEric Dumazet static struct sock *__udp6_lib_lookup(struct net *net, 128645ca708SEric Dumazet struct in6_addr *saddr, __be16 sport, 129645ca708SEric Dumazet struct in6_addr *daddr, __be16 dport, 130645ca708SEric Dumazet int dif, struct udp_table *udptable) 131645ca708SEric Dumazet { 132271b72c7SEric Dumazet struct sock *sk, *result; 13388ab1932SEric Dumazet struct hlist_nulls_node *node; 134645ca708SEric Dumazet unsigned short hnum = ntohs(dport); 135645ca708SEric Dumazet unsigned int hash = udp_hashfn(net, hnum); 136645ca708SEric Dumazet struct udp_hslot *hslot = &udptable->hash[hash]; 137271b72c7SEric Dumazet int score, badness; 138645ca708SEric Dumazet 139271b72c7SEric Dumazet rcu_read_lock(); 140271b72c7SEric Dumazet begin: 141271b72c7SEric Dumazet result = NULL; 142271b72c7SEric Dumazet badness = -1; 14388ab1932SEric Dumazet sk_nulls_for_each_rcu(sk, node, &hslot->head) { 144645ca708SEric Dumazet score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif); 145645ca708SEric Dumazet if (score > badness) { 146db8dac20SDavid S. Miller result = sk; 147db8dac20SDavid S. Miller badness = score; 148db8dac20SDavid S. Miller } 149db8dac20SDavid S. Miller } 15088ab1932SEric Dumazet /* 15188ab1932SEric Dumazet * if the nulls value we got at the end of this lookup is 15288ab1932SEric Dumazet * not the expected one, we must restart lookup. 15388ab1932SEric Dumazet * We probably met an item that was moved to another chain. 15488ab1932SEric Dumazet */ 15588ab1932SEric Dumazet if (get_nulls_value(node) != hash) 15688ab1932SEric Dumazet goto begin; 15788ab1932SEric Dumazet 158271b72c7SEric Dumazet if (result) { 159271b72c7SEric Dumazet if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt))) 160271b72c7SEric Dumazet result = NULL; 161271b72c7SEric Dumazet else if (unlikely(compute_score(result, net, hnum, saddr, sport, 162271b72c7SEric Dumazet daddr, dport, dif) < badness)) { 163271b72c7SEric Dumazet sock_put(result); 164271b72c7SEric Dumazet goto begin; 165271b72c7SEric Dumazet } 166271b72c7SEric Dumazet } 167271b72c7SEric Dumazet rcu_read_unlock(); 168db8dac20SDavid S. Miller return result; 169db8dac20SDavid S. Miller } 170db8dac20SDavid S. Miller 171607c4aafSKOVACS Krisztian static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb, 172607c4aafSKOVACS Krisztian __be16 sport, __be16 dport, 173645ca708SEric Dumazet struct udp_table *udptable) 174607c4aafSKOVACS Krisztian { 17523542618SKOVACS Krisztian struct sock *sk; 176607c4aafSKOVACS Krisztian struct ipv6hdr *iph = ipv6_hdr(skb); 177607c4aafSKOVACS Krisztian 17823542618SKOVACS Krisztian if (unlikely(sk = skb_steal_sock(skb))) 17923542618SKOVACS Krisztian return sk; 180adf30907SEric Dumazet return __udp6_lib_lookup(dev_net(skb_dst(skb)->dev), &iph->saddr, sport, 181607c4aafSKOVACS Krisztian &iph->daddr, dport, inet6_iif(skb), 182607c4aafSKOVACS Krisztian udptable); 183607c4aafSKOVACS Krisztian } 184607c4aafSKOVACS Krisztian 185db8dac20SDavid S. Miller /* 186db8dac20SDavid S. Miller * This should be easy, if there is something there we 187db8dac20SDavid S. Miller * return it, otherwise we block. 188db8dac20SDavid S. Miller */ 189db8dac20SDavid S. Miller 190db8dac20SDavid S. Miller int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk, 191db8dac20SDavid S. Miller struct msghdr *msg, size_t len, 192db8dac20SDavid S. Miller int noblock, int flags, int *addr_len) 193db8dac20SDavid S. Miller { 194db8dac20SDavid S. Miller struct ipv6_pinfo *np = inet6_sk(sk); 195db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 196db8dac20SDavid S. Miller struct sk_buff *skb; 197db8dac20SDavid S. Miller unsigned int ulen, copied; 198db8dac20SDavid S. Miller int peeked; 199db8dac20SDavid S. Miller int err; 200db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 201f26ba175SWei Yongjun int is_udp4; 202db8dac20SDavid S. Miller 203db8dac20SDavid S. Miller if (addr_len) 204db8dac20SDavid S. Miller *addr_len=sizeof(struct sockaddr_in6); 205db8dac20SDavid S. Miller 206db8dac20SDavid S. Miller if (flags & MSG_ERRQUEUE) 207db8dac20SDavid S. Miller return ipv6_recv_error(sk, msg, len); 208db8dac20SDavid S. Miller 209db8dac20SDavid S. Miller try_again: 210db8dac20SDavid S. Miller skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), 211db8dac20SDavid S. Miller &peeked, &err); 212db8dac20SDavid S. Miller if (!skb) 213db8dac20SDavid S. Miller goto out; 214db8dac20SDavid S. Miller 215db8dac20SDavid S. Miller ulen = skb->len - sizeof(struct udphdr); 216db8dac20SDavid S. Miller copied = len; 217db8dac20SDavid S. Miller if (copied > ulen) 218db8dac20SDavid S. Miller copied = ulen; 219db8dac20SDavid S. Miller else if (copied < ulen) 220db8dac20SDavid S. Miller msg->msg_flags |= MSG_TRUNC; 221db8dac20SDavid S. Miller 222f26ba175SWei Yongjun is_udp4 = (skb->protocol == htons(ETH_P_IP)); 223f26ba175SWei Yongjun 224db8dac20SDavid S. Miller /* 225db8dac20SDavid S. Miller * If checksum is needed at all, try to do it while copying the 226db8dac20SDavid S. Miller * data. If the data is truncated, or if we only want a partial 227db8dac20SDavid S. Miller * coverage checksum (UDP-Lite), do it before the copy. 228db8dac20SDavid S. Miller */ 229db8dac20SDavid S. Miller 230db8dac20SDavid S. Miller if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) { 231db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 232db8dac20SDavid S. Miller goto csum_copy_err; 233db8dac20SDavid S. Miller } 234db8dac20SDavid S. Miller 235db8dac20SDavid S. Miller if (skb_csum_unnecessary(skb)) 236db8dac20SDavid S. Miller err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), 237db8dac20SDavid S. Miller msg->msg_iov, copied ); 238db8dac20SDavid S. Miller else { 239db8dac20SDavid S. Miller err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov); 240db8dac20SDavid S. Miller if (err == -EINVAL) 241db8dac20SDavid S. Miller goto csum_copy_err; 242db8dac20SDavid S. Miller } 243db8dac20SDavid S. Miller if (err) 244db8dac20SDavid S. Miller goto out_free; 245db8dac20SDavid S. Miller 246f26ba175SWei Yongjun if (!peeked) { 247f26ba175SWei Yongjun if (is_udp4) 248f26ba175SWei Yongjun UDP_INC_STATS_USER(sock_net(sk), 249f26ba175SWei Yongjun UDP_MIB_INDATAGRAMS, is_udplite); 250f26ba175SWei Yongjun else 251235b9f7aSPavel Emelyanov UDP6_INC_STATS_USER(sock_net(sk), 252235b9f7aSPavel Emelyanov UDP_MIB_INDATAGRAMS, is_udplite); 253f26ba175SWei Yongjun } 254db8dac20SDavid S. Miller 255db8dac20SDavid S. Miller sock_recv_timestamp(msg, sk, skb); 256db8dac20SDavid S. Miller 257db8dac20SDavid S. Miller /* Copy the address. */ 258db8dac20SDavid S. Miller if (msg->msg_name) { 259db8dac20SDavid S. Miller struct sockaddr_in6 *sin6; 260db8dac20SDavid S. Miller 261db8dac20SDavid S. Miller sin6 = (struct sockaddr_in6 *) msg->msg_name; 262db8dac20SDavid S. Miller sin6->sin6_family = AF_INET6; 263db8dac20SDavid S. Miller sin6->sin6_port = udp_hdr(skb)->source; 264db8dac20SDavid S. Miller sin6->sin6_flowinfo = 0; 265db8dac20SDavid S. Miller sin6->sin6_scope_id = 0; 266db8dac20SDavid S. Miller 267f26ba175SWei Yongjun if (is_udp4) 268db8dac20SDavid S. Miller ipv6_addr_set(&sin6->sin6_addr, 0, 0, 269db8dac20SDavid S. Miller htonl(0xffff), ip_hdr(skb)->saddr); 270db8dac20SDavid S. Miller else { 271db8dac20SDavid S. Miller ipv6_addr_copy(&sin6->sin6_addr, 272db8dac20SDavid S. Miller &ipv6_hdr(skb)->saddr); 273db8dac20SDavid S. Miller if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) 274db8dac20SDavid S. Miller sin6->sin6_scope_id = IP6CB(skb)->iif; 275db8dac20SDavid S. Miller } 276db8dac20SDavid S. Miller 277db8dac20SDavid S. Miller } 278f26ba175SWei Yongjun if (is_udp4) { 279db8dac20SDavid S. Miller if (inet->cmsg_flags) 280db8dac20SDavid S. Miller ip_cmsg_recv(msg, skb); 281db8dac20SDavid S. Miller } else { 282db8dac20SDavid S. Miller if (np->rxopt.all) 283db8dac20SDavid S. Miller datagram_recv_ctl(sk, msg, skb); 284db8dac20SDavid S. Miller } 285db8dac20SDavid S. Miller 286db8dac20SDavid S. Miller err = copied; 287db8dac20SDavid S. Miller if (flags & MSG_TRUNC) 288db8dac20SDavid S. Miller err = ulen; 289db8dac20SDavid S. Miller 290db8dac20SDavid S. Miller out_free: 291db8dac20SDavid S. Miller lock_sock(sk); 292db8dac20SDavid S. Miller skb_free_datagram(sk, skb); 293db8dac20SDavid S. Miller release_sock(sk); 294db8dac20SDavid S. Miller out: 295db8dac20SDavid S. Miller return err; 296db8dac20SDavid S. Miller 297db8dac20SDavid S. Miller csum_copy_err: 298db8dac20SDavid S. Miller lock_sock(sk); 2990856f939SWei Yongjun if (!skb_kill_datagram(sk, skb, flags)) { 3000856f939SWei Yongjun if (is_udp4) 3010856f939SWei Yongjun UDP_INC_STATS_USER(sock_net(sk), 3020856f939SWei Yongjun UDP_MIB_INERRORS, is_udplite); 3030856f939SWei Yongjun else 3040856f939SWei Yongjun UDP6_INC_STATS_USER(sock_net(sk), 3050856f939SWei Yongjun UDP_MIB_INERRORS, is_udplite); 3060856f939SWei Yongjun } 307db8dac20SDavid S. Miller release_sock(sk); 308db8dac20SDavid S. Miller 309db8dac20SDavid S. Miller if (flags & MSG_DONTWAIT) 310db8dac20SDavid S. Miller return -EAGAIN; 311db8dac20SDavid S. Miller goto try_again; 312db8dac20SDavid S. Miller } 313db8dac20SDavid S. Miller 314db8dac20SDavid S. Miller void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt, 315d5fdd6baSBrian Haley u8 type, u8 code, int offset, __be32 info, 316645ca708SEric Dumazet struct udp_table *udptable) 317db8dac20SDavid S. Miller { 318db8dac20SDavid S. Miller struct ipv6_pinfo *np; 319db8dac20SDavid S. Miller struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data; 320db8dac20SDavid S. Miller struct in6_addr *saddr = &hdr->saddr; 321db8dac20SDavid S. Miller struct in6_addr *daddr = &hdr->daddr; 322db8dac20SDavid S. Miller struct udphdr *uh = (struct udphdr*)(skb->data+offset); 323db8dac20SDavid S. Miller struct sock *sk; 324db8dac20SDavid S. Miller int err; 325db8dac20SDavid S. Miller 326c346dca1SYOSHIFUJI Hideaki sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest, 327db8dac20SDavid S. Miller saddr, uh->source, inet6_iif(skb), udptable); 328db8dac20SDavid S. Miller if (sk == NULL) 329db8dac20SDavid S. Miller return; 330db8dac20SDavid S. Miller 331db8dac20SDavid S. Miller np = inet6_sk(sk); 332db8dac20SDavid S. Miller 333db8dac20SDavid S. Miller if (!icmpv6_err_convert(type, code, &err) && !np->recverr) 334db8dac20SDavid S. Miller goto out; 335db8dac20SDavid S. Miller 336db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED && !np->recverr) 337db8dac20SDavid S. Miller goto out; 338db8dac20SDavid S. Miller 339db8dac20SDavid S. Miller if (np->recverr) 340db8dac20SDavid S. Miller ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1)); 341db8dac20SDavid S. Miller 342db8dac20SDavid S. Miller sk->sk_err = err; 343db8dac20SDavid S. Miller sk->sk_error_report(sk); 344db8dac20SDavid S. Miller out: 345db8dac20SDavid S. Miller sock_put(sk); 346db8dac20SDavid S. Miller } 347db8dac20SDavid S. Miller 348db8dac20SDavid S. Miller static __inline__ void udpv6_err(struct sk_buff *skb, 349d5fdd6baSBrian Haley struct inet6_skb_parm *opt, u8 type, 350d5fdd6baSBrian Haley u8 code, int offset, __be32 info ) 351db8dac20SDavid S. Miller { 352645ca708SEric Dumazet __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table); 353db8dac20SDavid S. Miller } 354db8dac20SDavid S. Miller 355db8dac20SDavid S. Miller int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb) 356db8dac20SDavid S. Miller { 357db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 358db8dac20SDavid S. Miller int rc; 359db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 360db8dac20SDavid S. Miller 361db8dac20SDavid S. Miller if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) 362db8dac20SDavid S. Miller goto drop; 363db8dac20SDavid S. Miller 364db8dac20SDavid S. Miller /* 365db8dac20SDavid S. Miller * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c). 366db8dac20SDavid S. Miller */ 367db8dac20SDavid S. Miller if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 368db8dac20SDavid S. Miller 369db8dac20SDavid S. Miller if (up->pcrlen == 0) { /* full coverage was set */ 370db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage" 371db8dac20SDavid S. Miller " %d while full coverage %d requested\n", 372db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, skb->len); 373db8dac20SDavid S. Miller goto drop; 374db8dac20SDavid S. Miller } 375db8dac20SDavid S. Miller if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 376db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d " 377db8dac20SDavid S. Miller "too small, need min %d\n", 378db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, up->pcrlen); 379db8dac20SDavid S. Miller goto drop; 380db8dac20SDavid S. Miller } 381db8dac20SDavid S. Miller } 382db8dac20SDavid S. Miller 383db8dac20SDavid S. Miller if (sk->sk_filter) { 384db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 385db8dac20SDavid S. Miller goto drop; 386db8dac20SDavid S. Miller } 387db8dac20SDavid S. Miller 388db8dac20SDavid S. Miller if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) { 389db8dac20SDavid S. Miller /* Note that an ENOMEM error is charged twice */ 390cb61cb9bSEric Dumazet if (rc == -ENOMEM) { 391ef28d1a2SPavel Emelyanov UDP6_INC_STATS_BH(sock_net(sk), 392ef28d1a2SPavel Emelyanov UDP_MIB_RCVBUFERRORS, is_udplite); 393cb61cb9bSEric Dumazet atomic_inc(&sk->sk_drops); 394cb61cb9bSEric Dumazet } 395db8dac20SDavid S. Miller goto drop; 396db8dac20SDavid S. Miller } 397db8dac20SDavid S. Miller 398db8dac20SDavid S. Miller return 0; 399db8dac20SDavid S. Miller drop: 400ef28d1a2SPavel Emelyanov UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 401db8dac20SDavid S. Miller kfree_skb(skb); 402db8dac20SDavid S. Miller return -1; 403db8dac20SDavid S. Miller } 404db8dac20SDavid S. Miller 405920a4611SEric Dumazet static struct sock *udp_v6_mcast_next(struct net *net, struct sock *sk, 406db8dac20SDavid S. Miller __be16 loc_port, struct in6_addr *loc_addr, 407db8dac20SDavid S. Miller __be16 rmt_port, struct in6_addr *rmt_addr, 408db8dac20SDavid S. Miller int dif) 409db8dac20SDavid S. Miller { 41088ab1932SEric Dumazet struct hlist_nulls_node *node; 411db8dac20SDavid S. Miller struct sock *s = sk; 412db8dac20SDavid S. Miller unsigned short num = ntohs(loc_port); 413db8dac20SDavid S. Miller 41488ab1932SEric Dumazet sk_nulls_for_each_from(s, node) { 415db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(s); 416db8dac20SDavid S. Miller 417920a4611SEric Dumazet if (!net_eq(sock_net(s), net)) 418b8ad0cbcSDaniel Lezcano continue; 419b8ad0cbcSDaniel Lezcano 420db8dac20SDavid S. Miller if (s->sk_hash == num && s->sk_family == PF_INET6) { 421db8dac20SDavid S. Miller struct ipv6_pinfo *np = inet6_sk(s); 422db8dac20SDavid S. Miller if (inet->dport) { 423db8dac20SDavid S. Miller if (inet->dport != rmt_port) 424db8dac20SDavid S. Miller continue; 425db8dac20SDavid S. Miller } 426db8dac20SDavid S. Miller if (!ipv6_addr_any(&np->daddr) && 427db8dac20SDavid S. Miller !ipv6_addr_equal(&np->daddr, rmt_addr)) 428db8dac20SDavid S. Miller continue; 429db8dac20SDavid S. Miller 430db8dac20SDavid S. Miller if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif) 431db8dac20SDavid S. Miller continue; 432db8dac20SDavid S. Miller 433db8dac20SDavid S. Miller if (!ipv6_addr_any(&np->rcv_saddr)) { 434db8dac20SDavid S. Miller if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr)) 435db8dac20SDavid S. Miller continue; 436db8dac20SDavid S. Miller } 437db8dac20SDavid S. Miller if (!inet6_mc_check(s, loc_addr, rmt_addr)) 438db8dac20SDavid S. Miller continue; 439db8dac20SDavid S. Miller return s; 440db8dac20SDavid S. Miller } 441db8dac20SDavid S. Miller } 442db8dac20SDavid S. Miller return NULL; 443db8dac20SDavid S. Miller } 444db8dac20SDavid S. Miller 445db8dac20SDavid S. Miller /* 446db8dac20SDavid S. Miller * Note: called only from the BH handler context, 447db8dac20SDavid S. Miller * so we don't need to lock the hashes. 448db8dac20SDavid S. Miller */ 449e3163493SPavel Emelyanov static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb, 450e3163493SPavel Emelyanov struct in6_addr *saddr, struct in6_addr *daddr, 451645ca708SEric Dumazet struct udp_table *udptable) 452db8dac20SDavid S. Miller { 453db8dac20SDavid S. Miller struct sock *sk, *sk2; 454db8dac20SDavid S. Miller const struct udphdr *uh = udp_hdr(skb); 455645ca708SEric Dumazet struct udp_hslot *hslot = &udptable->hash[udp_hashfn(net, ntohs(uh->dest))]; 456db8dac20SDavid S. Miller int dif; 457db8dac20SDavid S. Miller 458645ca708SEric Dumazet spin_lock(&hslot->lock); 45988ab1932SEric Dumazet sk = sk_nulls_head(&hslot->head); 460db8dac20SDavid S. Miller dif = inet6_iif(skb); 461920a4611SEric Dumazet sk = udp_v6_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif); 462db8dac20SDavid S. Miller if (!sk) { 463db8dac20SDavid S. Miller kfree_skb(skb); 464db8dac20SDavid S. Miller goto out; 465db8dac20SDavid S. Miller } 466db8dac20SDavid S. Miller 467db8dac20SDavid S. Miller sk2 = sk; 46888ab1932SEric Dumazet while ((sk2 = udp_v6_mcast_next(net, sk_nulls_next(sk2), uh->dest, daddr, 469db8dac20SDavid S. Miller uh->source, saddr, dif))) { 470db8dac20SDavid S. Miller struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC); 471db8dac20SDavid S. Miller if (buff) { 472d97106eaSHerbert Xu bh_lock_sock(sk2); 473db8dac20SDavid S. Miller if (!sock_owned_by_user(sk2)) 474db8dac20SDavid S. Miller udpv6_queue_rcv_skb(sk2, buff); 475db8dac20SDavid S. Miller else 476db8dac20SDavid S. Miller sk_add_backlog(sk2, buff); 477db8dac20SDavid S. Miller bh_unlock_sock(sk2); 478db8dac20SDavid S. Miller } 479db8dac20SDavid S. Miller } 480d97106eaSHerbert Xu bh_lock_sock(sk); 481db8dac20SDavid S. Miller if (!sock_owned_by_user(sk)) 482db8dac20SDavid S. Miller udpv6_queue_rcv_skb(sk, skb); 483db8dac20SDavid S. Miller else 484db8dac20SDavid S. Miller sk_add_backlog(sk, skb); 485db8dac20SDavid S. Miller bh_unlock_sock(sk); 486db8dac20SDavid S. Miller out: 487645ca708SEric Dumazet spin_unlock(&hslot->lock); 488db8dac20SDavid S. Miller return 0; 489db8dac20SDavid S. Miller } 490db8dac20SDavid S. Miller 491db8dac20SDavid S. Miller static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh, 492db8dac20SDavid S. Miller int proto) 493db8dac20SDavid S. Miller { 494db8dac20SDavid S. Miller int err; 495db8dac20SDavid S. Miller 496db8dac20SDavid S. Miller UDP_SKB_CB(skb)->partial_cov = 0; 497db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov = skb->len; 498db8dac20SDavid S. Miller 499db8dac20SDavid S. Miller if (proto == IPPROTO_UDPLITE) { 500db8dac20SDavid S. Miller err = udplite_checksum_init(skb, uh); 501db8dac20SDavid S. Miller if (err) 502db8dac20SDavid S. Miller return err; 503db8dac20SDavid S. Miller } 504db8dac20SDavid S. Miller 505db8dac20SDavid S. Miller if (uh->check == 0) { 506db8dac20SDavid S. Miller /* RFC 2460 section 8.1 says that we SHOULD log 507db8dac20SDavid S. Miller this error. Well, it is reasonable. 508db8dac20SDavid S. Miller */ 509db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n"); 510db8dac20SDavid S. Miller return 1; 511db8dac20SDavid S. Miller } 512db8dac20SDavid S. Miller if (skb->ip_summed == CHECKSUM_COMPLETE && 513db8dac20SDavid S. Miller !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr, 514db8dac20SDavid S. Miller skb->len, proto, skb->csum)) 515db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_UNNECESSARY; 516db8dac20SDavid S. Miller 517db8dac20SDavid S. Miller if (!skb_csum_unnecessary(skb)) 518db8dac20SDavid S. Miller skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 519db8dac20SDavid S. Miller &ipv6_hdr(skb)->daddr, 520db8dac20SDavid S. Miller skb->len, proto, 0)); 521db8dac20SDavid S. Miller 522db8dac20SDavid S. Miller return 0; 523db8dac20SDavid S. Miller } 524db8dac20SDavid S. Miller 525645ca708SEric Dumazet int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable, 526db8dac20SDavid S. Miller int proto) 527db8dac20SDavid S. Miller { 528db8dac20SDavid S. Miller struct sock *sk; 529db8dac20SDavid S. Miller struct udphdr *uh; 530db8dac20SDavid S. Miller struct net_device *dev = skb->dev; 531db8dac20SDavid S. Miller struct in6_addr *saddr, *daddr; 532db8dac20SDavid S. Miller u32 ulen = 0; 533ef28d1a2SPavel Emelyanov struct net *net = dev_net(skb->dev); 534db8dac20SDavid S. Miller 535db8dac20SDavid S. Miller if (!pskb_may_pull(skb, sizeof(struct udphdr))) 536db8dac20SDavid S. Miller goto short_packet; 537db8dac20SDavid S. Miller 538db8dac20SDavid S. Miller saddr = &ipv6_hdr(skb)->saddr; 539db8dac20SDavid S. Miller daddr = &ipv6_hdr(skb)->daddr; 540db8dac20SDavid S. Miller uh = udp_hdr(skb); 541db8dac20SDavid S. Miller 542db8dac20SDavid S. Miller ulen = ntohs(uh->len); 543db8dac20SDavid S. Miller if (ulen > skb->len) 544db8dac20SDavid S. Miller goto short_packet; 545db8dac20SDavid S. Miller 546db8dac20SDavid S. Miller if (proto == IPPROTO_UDP) { 547db8dac20SDavid S. Miller /* UDP validates ulen. */ 548db8dac20SDavid S. Miller 549db8dac20SDavid S. Miller /* Check for jumbo payload */ 550db8dac20SDavid S. Miller if (ulen == 0) 551db8dac20SDavid S. Miller ulen = skb->len; 552db8dac20SDavid S. Miller 553db8dac20SDavid S. Miller if (ulen < sizeof(*uh)) 554db8dac20SDavid S. Miller goto short_packet; 555db8dac20SDavid S. Miller 556db8dac20SDavid S. Miller if (ulen < skb->len) { 557db8dac20SDavid S. Miller if (pskb_trim_rcsum(skb, ulen)) 558db8dac20SDavid S. Miller goto short_packet; 559db8dac20SDavid S. Miller saddr = &ipv6_hdr(skb)->saddr; 560db8dac20SDavid S. Miller daddr = &ipv6_hdr(skb)->daddr; 561db8dac20SDavid S. Miller uh = udp_hdr(skb); 562db8dac20SDavid S. Miller } 563db8dac20SDavid S. Miller } 564db8dac20SDavid S. Miller 565db8dac20SDavid S. Miller if (udp6_csum_init(skb, uh, proto)) 566db8dac20SDavid S. Miller goto discard; 567db8dac20SDavid S. Miller 568db8dac20SDavid S. Miller /* 569db8dac20SDavid S. Miller * Multicast receive code 570db8dac20SDavid S. Miller */ 571db8dac20SDavid S. Miller if (ipv6_addr_is_multicast(daddr)) 572e3163493SPavel Emelyanov return __udp6_lib_mcast_deliver(net, skb, 573e3163493SPavel Emelyanov saddr, daddr, udptable); 574db8dac20SDavid S. Miller 575db8dac20SDavid S. Miller /* Unicast */ 576db8dac20SDavid S. Miller 577db8dac20SDavid S. Miller /* 578db8dac20SDavid S. Miller * check socket cache ... must talk to Alan about his plans 579db8dac20SDavid S. Miller * for sock caches... i'll skip this for now. 580db8dac20SDavid S. Miller */ 581607c4aafSKOVACS Krisztian sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable); 582db8dac20SDavid S. Miller 583db8dac20SDavid S. Miller if (sk == NULL) { 584db8dac20SDavid S. Miller if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) 585db8dac20SDavid S. Miller goto discard; 586db8dac20SDavid S. Miller 587db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 588db8dac20SDavid S. Miller goto discard; 589ef28d1a2SPavel Emelyanov UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS, 590ef28d1a2SPavel Emelyanov proto == IPPROTO_UDPLITE); 591db8dac20SDavid S. Miller 592db8dac20SDavid S. Miller icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev); 593db8dac20SDavid S. Miller 594db8dac20SDavid S. Miller kfree_skb(skb); 595db8dac20SDavid S. Miller return 0; 596db8dac20SDavid S. Miller } 597db8dac20SDavid S. Miller 598db8dac20SDavid S. Miller /* deliver */ 599db8dac20SDavid S. Miller 600d97106eaSHerbert Xu bh_lock_sock(sk); 601db8dac20SDavid S. Miller if (!sock_owned_by_user(sk)) 602db8dac20SDavid S. Miller udpv6_queue_rcv_skb(sk, skb); 603db8dac20SDavid S. Miller else 604db8dac20SDavid S. Miller sk_add_backlog(sk, skb); 605db8dac20SDavid S. Miller bh_unlock_sock(sk); 606db8dac20SDavid S. Miller sock_put(sk); 607db8dac20SDavid S. Miller return 0; 608db8dac20SDavid S. Miller 609db8dac20SDavid S. Miller short_packet: 610db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: %d/%u\n", 611db8dac20SDavid S. Miller proto == IPPROTO_UDPLITE ? "-Lite" : "", 612db8dac20SDavid S. Miller ulen, skb->len); 613db8dac20SDavid S. Miller 614db8dac20SDavid S. Miller discard: 615ef28d1a2SPavel Emelyanov UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 616db8dac20SDavid S. Miller kfree_skb(skb); 617db8dac20SDavid S. Miller return 0; 618db8dac20SDavid S. Miller } 619db8dac20SDavid S. Miller 620db8dac20SDavid S. Miller static __inline__ int udpv6_rcv(struct sk_buff *skb) 621db8dac20SDavid S. Miller { 622645ca708SEric Dumazet return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP); 623db8dac20SDavid S. Miller } 624db8dac20SDavid S. Miller 625db8dac20SDavid S. Miller /* 626db8dac20SDavid S. Miller * Throw away all pending data and cancel the corking. Socket is locked. 627db8dac20SDavid S. Miller */ 628db8dac20SDavid S. Miller static void udp_v6_flush_pending_frames(struct sock *sk) 629db8dac20SDavid S. Miller { 630db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 631db8dac20SDavid S. Miller 63236d926b9SDenis V. Lunev if (up->pending == AF_INET) 63336d926b9SDenis V. Lunev udp_flush_pending_frames(sk); 63436d926b9SDenis V. Lunev else if (up->pending) { 635db8dac20SDavid S. Miller up->len = 0; 636db8dac20SDavid S. Miller up->pending = 0; 637db8dac20SDavid S. Miller ip6_flush_pending_frames(sk); 638db8dac20SDavid S. Miller } 639db8dac20SDavid S. Miller } 640db8dac20SDavid S. Miller 641493c6be3SSridhar Samudrala /** 642493c6be3SSridhar Samudrala * udp6_hwcsum_outgoing - handle outgoing HW checksumming 643493c6be3SSridhar Samudrala * @sk: socket we are sending on 644493c6be3SSridhar Samudrala * @skb: sk_buff containing the filled-in UDP header 645493c6be3SSridhar Samudrala * (checksum field must be zeroed out) 646493c6be3SSridhar Samudrala */ 647493c6be3SSridhar Samudrala static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb, 648493c6be3SSridhar Samudrala const struct in6_addr *saddr, 649493c6be3SSridhar Samudrala const struct in6_addr *daddr, int len) 650493c6be3SSridhar Samudrala { 651493c6be3SSridhar Samudrala unsigned int offset; 652493c6be3SSridhar Samudrala struct udphdr *uh = udp_hdr(skb); 653493c6be3SSridhar Samudrala __wsum csum = 0; 654493c6be3SSridhar Samudrala 655493c6be3SSridhar Samudrala if (skb_queue_len(&sk->sk_write_queue) == 1) { 656493c6be3SSridhar Samudrala /* Only one fragment on the socket. */ 657493c6be3SSridhar Samudrala skb->csum_start = skb_transport_header(skb) - skb->head; 658493c6be3SSridhar Samudrala skb->csum_offset = offsetof(struct udphdr, check); 659493c6be3SSridhar Samudrala uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0); 660493c6be3SSridhar Samudrala } else { 661493c6be3SSridhar Samudrala /* 662493c6be3SSridhar Samudrala * HW-checksum won't work as there are two or more 663493c6be3SSridhar Samudrala * fragments on the socket so that all csums of sk_buffs 664493c6be3SSridhar Samudrala * should be together 665493c6be3SSridhar Samudrala */ 666493c6be3SSridhar Samudrala offset = skb_transport_offset(skb); 667493c6be3SSridhar Samudrala skb->csum = skb_checksum(skb, offset, skb->len - offset, 0); 668493c6be3SSridhar Samudrala 669493c6be3SSridhar Samudrala skb->ip_summed = CHECKSUM_NONE; 670493c6be3SSridhar Samudrala 671493c6be3SSridhar Samudrala skb_queue_walk(&sk->sk_write_queue, skb) { 672493c6be3SSridhar Samudrala csum = csum_add(csum, skb->csum); 673493c6be3SSridhar Samudrala } 674493c6be3SSridhar Samudrala 675493c6be3SSridhar Samudrala uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 676493c6be3SSridhar Samudrala csum); 677493c6be3SSridhar Samudrala if (uh->check == 0) 678493c6be3SSridhar Samudrala uh->check = CSUM_MANGLED_0; 679493c6be3SSridhar Samudrala } 680493c6be3SSridhar Samudrala } 681493c6be3SSridhar Samudrala 682db8dac20SDavid S. Miller /* 683db8dac20SDavid S. Miller * Sending 684db8dac20SDavid S. Miller */ 685db8dac20SDavid S. Miller 686db8dac20SDavid S. Miller static int udp_v6_push_pending_frames(struct sock *sk) 687db8dac20SDavid S. Miller { 688db8dac20SDavid S. Miller struct sk_buff *skb; 689db8dac20SDavid S. Miller struct udphdr *uh; 690db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 691db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 692db8dac20SDavid S. Miller struct flowi *fl = &inet->cork.fl; 693db8dac20SDavid S. Miller int err = 0; 694db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 695db8dac20SDavid S. Miller __wsum csum = 0; 696db8dac20SDavid S. Miller 697db8dac20SDavid S. Miller /* Grab the skbuff where UDP header space exists. */ 698db8dac20SDavid S. Miller if ((skb = skb_peek(&sk->sk_write_queue)) == NULL) 699db8dac20SDavid S. Miller goto out; 700db8dac20SDavid S. Miller 701db8dac20SDavid S. Miller /* 702db8dac20SDavid S. Miller * Create a UDP header 703db8dac20SDavid S. Miller */ 704db8dac20SDavid S. Miller uh = udp_hdr(skb); 705db8dac20SDavid S. Miller uh->source = fl->fl_ip_sport; 706db8dac20SDavid S. Miller uh->dest = fl->fl_ip_dport; 707db8dac20SDavid S. Miller uh->len = htons(up->len); 708db8dac20SDavid S. Miller uh->check = 0; 709db8dac20SDavid S. Miller 710db8dac20SDavid S. Miller if (is_udplite) 711db8dac20SDavid S. Miller csum = udplite_csum_outgoing(sk, skb); 712493c6be3SSridhar Samudrala else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 713493c6be3SSridhar Samudrala udp6_hwcsum_outgoing(sk, skb, &fl->fl6_src, &fl->fl6_dst, 714493c6be3SSridhar Samudrala up->len); 715493c6be3SSridhar Samudrala goto send; 716493c6be3SSridhar Samudrala } else 717db8dac20SDavid S. Miller csum = udp_csum_outgoing(sk, skb); 718db8dac20SDavid S. Miller 719db8dac20SDavid S. Miller /* add protocol-dependent pseudo-header */ 720db8dac20SDavid S. Miller uh->check = csum_ipv6_magic(&fl->fl6_src, &fl->fl6_dst, 721db8dac20SDavid S. Miller up->len, fl->proto, csum ); 722db8dac20SDavid S. Miller if (uh->check == 0) 723db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 724db8dac20SDavid S. Miller 725493c6be3SSridhar Samudrala send: 726db8dac20SDavid S. Miller err = ip6_push_pending_frames(sk); 7276ce9e7b5SEric Dumazet if (err) { 7286ce9e7b5SEric Dumazet if (err == -ENOBUFS && !inet6_sk(sk)->recverr) { 7296ce9e7b5SEric Dumazet UDP6_INC_STATS_USER(sock_net(sk), 7306ce9e7b5SEric Dumazet UDP_MIB_SNDBUFERRORS, is_udplite); 7316ce9e7b5SEric Dumazet err = 0; 7326ce9e7b5SEric Dumazet } 7336ce9e7b5SEric Dumazet } else 7346ce9e7b5SEric Dumazet UDP6_INC_STATS_USER(sock_net(sk), 7356ce9e7b5SEric Dumazet UDP_MIB_OUTDATAGRAMS, is_udplite); 736db8dac20SDavid S. Miller out: 737db8dac20SDavid S. Miller up->len = 0; 738db8dac20SDavid S. Miller up->pending = 0; 739db8dac20SDavid S. Miller return err; 740db8dac20SDavid S. Miller } 741db8dac20SDavid S. Miller 742db8dac20SDavid S. Miller int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk, 743db8dac20SDavid S. Miller struct msghdr *msg, size_t len) 744db8dac20SDavid S. Miller { 745db8dac20SDavid S. Miller struct ipv6_txoptions opt_space; 746db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 747db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 748db8dac20SDavid S. Miller struct ipv6_pinfo *np = inet6_sk(sk); 749db8dac20SDavid S. Miller struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name; 750db8dac20SDavid S. Miller struct in6_addr *daddr, *final_p = NULL, final; 751db8dac20SDavid S. Miller struct ipv6_txoptions *opt = NULL; 752db8dac20SDavid S. Miller struct ip6_flowlabel *flowlabel = NULL; 753db8dac20SDavid S. Miller struct flowi fl; 754db8dac20SDavid S. Miller struct dst_entry *dst; 755db8dac20SDavid S. Miller int addr_len = msg->msg_namelen; 756db8dac20SDavid S. Miller int ulen = len; 757db8dac20SDavid S. Miller int hlimit = -1; 758db8dac20SDavid S. Miller int tclass = -1; 759db8dac20SDavid S. Miller int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; 760db8dac20SDavid S. Miller int err; 761db8dac20SDavid S. Miller int connected = 0; 762db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 763db8dac20SDavid S. Miller int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 764db8dac20SDavid S. Miller 765db8dac20SDavid S. Miller /* destination address check */ 766db8dac20SDavid S. Miller if (sin6) { 767db8dac20SDavid S. Miller if (addr_len < offsetof(struct sockaddr, sa_data)) 768db8dac20SDavid S. Miller return -EINVAL; 769db8dac20SDavid S. Miller 770db8dac20SDavid S. Miller switch (sin6->sin6_family) { 771db8dac20SDavid S. Miller case AF_INET6: 772db8dac20SDavid S. Miller if (addr_len < SIN6_LEN_RFC2133) 773db8dac20SDavid S. Miller return -EINVAL; 774db8dac20SDavid S. Miller daddr = &sin6->sin6_addr; 775db8dac20SDavid S. Miller break; 776db8dac20SDavid S. Miller case AF_INET: 777db8dac20SDavid S. Miller goto do_udp_sendmsg; 778db8dac20SDavid S. Miller case AF_UNSPEC: 779db8dac20SDavid S. Miller msg->msg_name = sin6 = NULL; 780db8dac20SDavid S. Miller msg->msg_namelen = addr_len = 0; 781db8dac20SDavid S. Miller daddr = NULL; 782db8dac20SDavid S. Miller break; 783db8dac20SDavid S. Miller default: 784db8dac20SDavid S. Miller return -EINVAL; 785db8dac20SDavid S. Miller } 786db8dac20SDavid S. Miller } else if (!up->pending) { 787db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 788db8dac20SDavid S. Miller return -EDESTADDRREQ; 789db8dac20SDavid S. Miller daddr = &np->daddr; 790db8dac20SDavid S. Miller } else 791db8dac20SDavid S. Miller daddr = NULL; 792db8dac20SDavid S. Miller 793db8dac20SDavid S. Miller if (daddr) { 794db8dac20SDavid S. Miller if (ipv6_addr_v4mapped(daddr)) { 795db8dac20SDavid S. Miller struct sockaddr_in sin; 796db8dac20SDavid S. Miller sin.sin_family = AF_INET; 797db8dac20SDavid S. Miller sin.sin_port = sin6 ? sin6->sin6_port : inet->dport; 798db8dac20SDavid S. Miller sin.sin_addr.s_addr = daddr->s6_addr32[3]; 799db8dac20SDavid S. Miller msg->msg_name = &sin; 800db8dac20SDavid S. Miller msg->msg_namelen = sizeof(sin); 801db8dac20SDavid S. Miller do_udp_sendmsg: 802db8dac20SDavid S. Miller if (__ipv6_only_sock(sk)) 803db8dac20SDavid S. Miller return -ENETUNREACH; 804db8dac20SDavid S. Miller return udp_sendmsg(iocb, sk, msg, len); 805db8dac20SDavid S. Miller } 806db8dac20SDavid S. Miller } 807db8dac20SDavid S. Miller 808db8dac20SDavid S. Miller if (up->pending == AF_INET) 809db8dac20SDavid S. Miller return udp_sendmsg(iocb, sk, msg, len); 810db8dac20SDavid S. Miller 811db8dac20SDavid S. Miller /* Rough check on arithmetic overflow, 812db8dac20SDavid S. Miller better check is made in ip6_append_data(). 813db8dac20SDavid S. Miller */ 814db8dac20SDavid S. Miller if (len > INT_MAX - sizeof(struct udphdr)) 815db8dac20SDavid S. Miller return -EMSGSIZE; 816db8dac20SDavid S. Miller 817db8dac20SDavid S. Miller if (up->pending) { 818db8dac20SDavid S. Miller /* 819db8dac20SDavid S. Miller * There are pending frames. 820db8dac20SDavid S. Miller * The socket lock must be held while it's corked. 821db8dac20SDavid S. Miller */ 822db8dac20SDavid S. Miller lock_sock(sk); 823db8dac20SDavid S. Miller if (likely(up->pending)) { 824db8dac20SDavid S. Miller if (unlikely(up->pending != AF_INET6)) { 825db8dac20SDavid S. Miller release_sock(sk); 826db8dac20SDavid S. Miller return -EAFNOSUPPORT; 827db8dac20SDavid S. Miller } 828db8dac20SDavid S. Miller dst = NULL; 829db8dac20SDavid S. Miller goto do_append_data; 830db8dac20SDavid S. Miller } 831db8dac20SDavid S. Miller release_sock(sk); 832db8dac20SDavid S. Miller } 833db8dac20SDavid S. Miller ulen += sizeof(struct udphdr); 834db8dac20SDavid S. Miller 835db8dac20SDavid S. Miller memset(&fl, 0, sizeof(fl)); 836db8dac20SDavid S. Miller 837db8dac20SDavid S. Miller if (sin6) { 838db8dac20SDavid S. Miller if (sin6->sin6_port == 0) 839db8dac20SDavid S. Miller return -EINVAL; 840db8dac20SDavid S. Miller 841db8dac20SDavid S. Miller fl.fl_ip_dport = sin6->sin6_port; 842db8dac20SDavid S. Miller daddr = &sin6->sin6_addr; 843db8dac20SDavid S. Miller 844db8dac20SDavid S. Miller if (np->sndflow) { 845db8dac20SDavid S. Miller fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK; 846db8dac20SDavid S. Miller if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) { 847db8dac20SDavid S. Miller flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel); 848db8dac20SDavid S. Miller if (flowlabel == NULL) 849db8dac20SDavid S. Miller return -EINVAL; 850db8dac20SDavid S. Miller daddr = &flowlabel->dst; 851db8dac20SDavid S. Miller } 852db8dac20SDavid S. Miller } 853db8dac20SDavid S. Miller 854db8dac20SDavid S. Miller /* 855db8dac20SDavid S. Miller * Otherwise it will be difficult to maintain 856db8dac20SDavid S. Miller * sk->sk_dst_cache. 857db8dac20SDavid S. Miller */ 858db8dac20SDavid S. Miller if (sk->sk_state == TCP_ESTABLISHED && 859db8dac20SDavid S. Miller ipv6_addr_equal(daddr, &np->daddr)) 860db8dac20SDavid S. Miller daddr = &np->daddr; 861db8dac20SDavid S. Miller 862db8dac20SDavid S. Miller if (addr_len >= sizeof(struct sockaddr_in6) && 863db8dac20SDavid S. Miller sin6->sin6_scope_id && 864db8dac20SDavid S. Miller ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL) 865db8dac20SDavid S. Miller fl.oif = sin6->sin6_scope_id; 866db8dac20SDavid S. Miller } else { 867db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 868db8dac20SDavid S. Miller return -EDESTADDRREQ; 869db8dac20SDavid S. Miller 870db8dac20SDavid S. Miller fl.fl_ip_dport = inet->dport; 871db8dac20SDavid S. Miller daddr = &np->daddr; 872db8dac20SDavid S. Miller fl.fl6_flowlabel = np->flow_label; 873db8dac20SDavid S. Miller connected = 1; 874db8dac20SDavid S. Miller } 875db8dac20SDavid S. Miller 876db8dac20SDavid S. Miller if (!fl.oif) 877db8dac20SDavid S. Miller fl.oif = sk->sk_bound_dev_if; 878db8dac20SDavid S. Miller 8799f690db7SYang Hongyang if (!fl.oif) 8809f690db7SYang Hongyang fl.oif = np->sticky_pktinfo.ipi6_ifindex; 8819f690db7SYang Hongyang 88251953d5bSBrian Haley fl.mark = sk->sk_mark; 88351953d5bSBrian Haley 884db8dac20SDavid S. Miller if (msg->msg_controllen) { 885db8dac20SDavid S. Miller opt = &opt_space; 886db8dac20SDavid S. Miller memset(opt, 0, sizeof(struct ipv6_txoptions)); 887db8dac20SDavid S. Miller opt->tot_len = sizeof(*opt); 888db8dac20SDavid S. Miller 88991e1908fSYOSHIFUJI Hideaki err = datagram_send_ctl(sock_net(sk), msg, &fl, opt, &hlimit, &tclass); 890db8dac20SDavid S. Miller if (err < 0) { 891db8dac20SDavid S. Miller fl6_sock_release(flowlabel); 892db8dac20SDavid S. Miller return err; 893db8dac20SDavid S. Miller } 894db8dac20SDavid S. Miller if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) { 895db8dac20SDavid S. Miller flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel); 896db8dac20SDavid S. Miller if (flowlabel == NULL) 897db8dac20SDavid S. Miller return -EINVAL; 898db8dac20SDavid S. Miller } 899db8dac20SDavid S. Miller if (!(opt->opt_nflen|opt->opt_flen)) 900db8dac20SDavid S. Miller opt = NULL; 901db8dac20SDavid S. Miller connected = 0; 902db8dac20SDavid S. Miller } 903db8dac20SDavid S. Miller if (opt == NULL) 904db8dac20SDavid S. Miller opt = np->opt; 905db8dac20SDavid S. Miller if (flowlabel) 906db8dac20SDavid S. Miller opt = fl6_merge_options(&opt_space, flowlabel, opt); 907db8dac20SDavid S. Miller opt = ipv6_fixup_options(&opt_space, opt); 908db8dac20SDavid S. Miller 909db8dac20SDavid S. Miller fl.proto = sk->sk_protocol; 910876c7f41SBrian Haley if (!ipv6_addr_any(daddr)) 911db8dac20SDavid S. Miller ipv6_addr_copy(&fl.fl6_dst, daddr); 912876c7f41SBrian Haley else 913876c7f41SBrian Haley fl.fl6_dst.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */ 914db8dac20SDavid S. Miller if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr)) 915db8dac20SDavid S. Miller ipv6_addr_copy(&fl.fl6_src, &np->saddr); 916db8dac20SDavid S. Miller fl.fl_ip_sport = inet->sport; 917db8dac20SDavid S. Miller 918db8dac20SDavid S. Miller /* merge ip6_build_xmit from ip6_output */ 919db8dac20SDavid S. Miller if (opt && opt->srcrt) { 920db8dac20SDavid S. Miller struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt; 921db8dac20SDavid S. Miller ipv6_addr_copy(&final, &fl.fl6_dst); 922db8dac20SDavid S. Miller ipv6_addr_copy(&fl.fl6_dst, rt0->addr); 923db8dac20SDavid S. Miller final_p = &final; 924db8dac20SDavid S. Miller connected = 0; 925db8dac20SDavid S. Miller } 926db8dac20SDavid S. Miller 927db8dac20SDavid S. Miller if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst)) { 928db8dac20SDavid S. Miller fl.oif = np->mcast_oif; 929db8dac20SDavid S. Miller connected = 0; 930db8dac20SDavid S. Miller } 931db8dac20SDavid S. Miller 932db8dac20SDavid S. Miller security_sk_classify_flow(sk, &fl); 933db8dac20SDavid S. Miller 934db8dac20SDavid S. Miller err = ip6_sk_dst_lookup(sk, &dst, &fl); 935db8dac20SDavid S. Miller if (err) 936db8dac20SDavid S. Miller goto out; 937db8dac20SDavid S. Miller if (final_p) 938db8dac20SDavid S. Miller ipv6_addr_copy(&fl.fl6_dst, final_p); 939db8dac20SDavid S. Miller 94052479b62SAlexey Dobriyan err = __xfrm_lookup(sock_net(sk), &dst, &fl, sk, XFRM_LOOKUP_WAIT); 94152479b62SAlexey Dobriyan if (err < 0) { 942db8dac20SDavid S. Miller if (err == -EREMOTE) 943db8dac20SDavid S. Miller err = ip6_dst_blackhole(sk, &dst, &fl); 944db8dac20SDavid S. Miller if (err < 0) 945db8dac20SDavid S. Miller goto out; 946db8dac20SDavid S. Miller } 947db8dac20SDavid S. Miller 948db8dac20SDavid S. Miller if (hlimit < 0) { 949db8dac20SDavid S. Miller if (ipv6_addr_is_multicast(&fl.fl6_dst)) 950db8dac20SDavid S. Miller hlimit = np->mcast_hops; 951db8dac20SDavid S. Miller else 952db8dac20SDavid S. Miller hlimit = np->hop_limit; 953db8dac20SDavid S. Miller if (hlimit < 0) 9546b75d090SYOSHIFUJI Hideaki hlimit = ip6_dst_hoplimit(dst); 955db8dac20SDavid S. Miller } 956db8dac20SDavid S. Miller 957db8dac20SDavid S. Miller if (tclass < 0) 958e651f03aSGerrit Renker tclass = np->tclass; 959db8dac20SDavid S. Miller 960db8dac20SDavid S. Miller if (msg->msg_flags&MSG_CONFIRM) 961db8dac20SDavid S. Miller goto do_confirm; 962db8dac20SDavid S. Miller back_from_confirm: 963db8dac20SDavid S. Miller 964db8dac20SDavid S. Miller lock_sock(sk); 965db8dac20SDavid S. Miller if (unlikely(up->pending)) { 966db8dac20SDavid S. Miller /* The socket is already corked while preparing it. */ 967db8dac20SDavid S. Miller /* ... which is an evident application bug. --ANK */ 968db8dac20SDavid S. Miller release_sock(sk); 969db8dac20SDavid S. Miller 970db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n"); 971db8dac20SDavid S. Miller err = -EINVAL; 972db8dac20SDavid S. Miller goto out; 973db8dac20SDavid S. Miller } 974db8dac20SDavid S. Miller 975db8dac20SDavid S. Miller up->pending = AF_INET6; 976db8dac20SDavid S. Miller 977db8dac20SDavid S. Miller do_append_data: 978db8dac20SDavid S. Miller up->len += ulen; 979db8dac20SDavid S. Miller getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 980db8dac20SDavid S. Miller err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen, 981db8dac20SDavid S. Miller sizeof(struct udphdr), hlimit, tclass, opt, &fl, 982db8dac20SDavid S. Miller (struct rt6_info*)dst, 983db8dac20SDavid S. Miller corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); 984db8dac20SDavid S. Miller if (err) 985db8dac20SDavid S. Miller udp_v6_flush_pending_frames(sk); 986db8dac20SDavid S. Miller else if (!corkreq) 987db8dac20SDavid S. Miller err = udp_v6_push_pending_frames(sk); 988db8dac20SDavid S. Miller else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 989db8dac20SDavid S. Miller up->pending = 0; 990db8dac20SDavid S. Miller 991db8dac20SDavid S. Miller if (dst) { 992db8dac20SDavid S. Miller if (connected) { 993db8dac20SDavid S. Miller ip6_dst_store(sk, dst, 994db8dac20SDavid S. Miller ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ? 995db8dac20SDavid S. Miller &np->daddr : NULL, 996db8dac20SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 997db8dac20SDavid S. Miller ipv6_addr_equal(&fl.fl6_src, &np->saddr) ? 998db8dac20SDavid S. Miller &np->saddr : 999db8dac20SDavid S. Miller #endif 1000db8dac20SDavid S. Miller NULL); 1001db8dac20SDavid S. Miller } else { 1002db8dac20SDavid S. Miller dst_release(dst); 1003db8dac20SDavid S. Miller } 1004a3c96089SYOSHIFUJI Hideaki dst = NULL; 1005db8dac20SDavid S. Miller } 1006db8dac20SDavid S. Miller 1007db8dac20SDavid S. Miller if (err > 0) 1008db8dac20SDavid S. Miller err = np->recverr ? net_xmit_errno(err) : 0; 1009db8dac20SDavid S. Miller release_sock(sk); 1010db8dac20SDavid S. Miller out: 1011a3c96089SYOSHIFUJI Hideaki dst_release(dst); 1012db8dac20SDavid S. Miller fl6_sock_release(flowlabel); 1013db8dac20SDavid S. Miller if (!err) 1014db8dac20SDavid S. Miller return len; 1015db8dac20SDavid S. Miller /* 1016db8dac20SDavid S. Miller * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 1017db8dac20SDavid S. Miller * ENOBUFS might not be good (it's not tunable per se), but otherwise 1018db8dac20SDavid S. Miller * we don't have a good statistic (IpOutDiscards but it can be too many 1019db8dac20SDavid S. Miller * things). We could add another new stat but at least for now that 1020db8dac20SDavid S. Miller * seems like overkill. 1021db8dac20SDavid S. Miller */ 1022db8dac20SDavid S. Miller if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 1023235b9f7aSPavel Emelyanov UDP6_INC_STATS_USER(sock_net(sk), 1024235b9f7aSPavel Emelyanov UDP_MIB_SNDBUFERRORS, is_udplite); 1025db8dac20SDavid S. Miller } 1026db8dac20SDavid S. Miller return err; 1027db8dac20SDavid S. Miller 1028db8dac20SDavid S. Miller do_confirm: 1029db8dac20SDavid S. Miller dst_confirm(dst); 1030db8dac20SDavid S. Miller if (!(msg->msg_flags&MSG_PROBE) || len) 1031db8dac20SDavid S. Miller goto back_from_confirm; 1032db8dac20SDavid S. Miller err = 0; 1033db8dac20SDavid S. Miller goto out; 1034db8dac20SDavid S. Miller } 1035db8dac20SDavid S. Miller 10367d06b2e0SBrian Haley void udpv6_destroy_sock(struct sock *sk) 1037db8dac20SDavid S. Miller { 1038db8dac20SDavid S. Miller lock_sock(sk); 1039db8dac20SDavid S. Miller udp_v6_flush_pending_frames(sk); 1040db8dac20SDavid S. Miller release_sock(sk); 1041db8dac20SDavid S. Miller 1042db8dac20SDavid S. Miller inet6_destroy_sock(sk); 1043db8dac20SDavid S. Miller } 1044db8dac20SDavid S. Miller 1045db8dac20SDavid S. Miller /* 1046db8dac20SDavid S. Miller * Socket option code for UDP 1047db8dac20SDavid S. Miller */ 1048db8dac20SDavid S. Miller int udpv6_setsockopt(struct sock *sk, int level, int optname, 1049b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 1050db8dac20SDavid S. Miller { 1051db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1052db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1053db8dac20SDavid S. Miller udp_v6_push_pending_frames); 1054db8dac20SDavid S. Miller return ipv6_setsockopt(sk, level, optname, optval, optlen); 1055db8dac20SDavid S. Miller } 1056db8dac20SDavid S. Miller 1057db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1058db8dac20SDavid S. Miller int compat_udpv6_setsockopt(struct sock *sk, int level, int optname, 1059b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 1060db8dac20SDavid S. Miller { 1061db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1062db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1063db8dac20SDavid S. Miller udp_v6_push_pending_frames); 1064db8dac20SDavid S. Miller return compat_ipv6_setsockopt(sk, level, optname, optval, optlen); 1065db8dac20SDavid S. Miller } 1066db8dac20SDavid S. Miller #endif 1067db8dac20SDavid S. Miller 1068db8dac20SDavid S. Miller int udpv6_getsockopt(struct sock *sk, int level, int optname, 1069db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 1070db8dac20SDavid S. Miller { 1071db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1072db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1073db8dac20SDavid S. Miller return ipv6_getsockopt(sk, level, optname, optval, optlen); 1074db8dac20SDavid S. Miller } 1075db8dac20SDavid S. Miller 1076db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1077db8dac20SDavid S. Miller int compat_udpv6_getsockopt(struct sock *sk, int level, int optname, 1078db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 1079db8dac20SDavid S. Miller { 1080db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1081db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1082db8dac20SDavid S. Miller return compat_ipv6_getsockopt(sk, level, optname, optval, optlen); 1083db8dac20SDavid S. Miller } 1084db8dac20SDavid S. Miller #endif 1085db8dac20SDavid S. Miller 1086ba735425SSridhar Samudrala static int udp6_ufo_send_check(struct sk_buff *skb) 1087ba735425SSridhar Samudrala { 1088ba735425SSridhar Samudrala struct ipv6hdr *ipv6h; 1089ba735425SSridhar Samudrala struct udphdr *uh; 1090ba735425SSridhar Samudrala 1091ba735425SSridhar Samudrala if (!pskb_may_pull(skb, sizeof(*uh))) 1092ba735425SSridhar Samudrala return -EINVAL; 1093ba735425SSridhar Samudrala 1094ba735425SSridhar Samudrala ipv6h = ipv6_hdr(skb); 1095ba735425SSridhar Samudrala uh = udp_hdr(skb); 1096ba735425SSridhar Samudrala 1097ba735425SSridhar Samudrala uh->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, skb->len, 1098ba735425SSridhar Samudrala IPPROTO_UDP, 0); 1099ba735425SSridhar Samudrala skb->csum_start = skb_transport_header(skb) - skb->head; 1100ba735425SSridhar Samudrala skb->csum_offset = offsetof(struct udphdr, check); 1101ba735425SSridhar Samudrala skb->ip_summed = CHECKSUM_PARTIAL; 1102ba735425SSridhar Samudrala return 0; 1103ba735425SSridhar Samudrala } 1104ba735425SSridhar Samudrala 1105ba735425SSridhar Samudrala static struct sk_buff *udp6_ufo_fragment(struct sk_buff *skb, int features) 1106ba735425SSridhar Samudrala { 1107ba735425SSridhar Samudrala struct sk_buff *segs = ERR_PTR(-EINVAL); 1108ba735425SSridhar Samudrala unsigned int mss; 1109ba735425SSridhar Samudrala unsigned int unfrag_ip6hlen, unfrag_len; 1110ba735425SSridhar Samudrala struct frag_hdr *fptr; 1111ba735425SSridhar Samudrala u8 *mac_start, *prevhdr; 1112ba735425SSridhar Samudrala u8 nexthdr; 1113ba735425SSridhar Samudrala u8 frag_hdr_sz = sizeof(struct frag_hdr); 1114ba735425SSridhar Samudrala int offset; 1115ba735425SSridhar Samudrala __wsum csum; 1116ba735425SSridhar Samudrala 1117ba735425SSridhar Samudrala mss = skb_shinfo(skb)->gso_size; 1118ba735425SSridhar Samudrala if (unlikely(skb->len <= mss)) 1119ba735425SSridhar Samudrala goto out; 1120ba735425SSridhar Samudrala 1121ba735425SSridhar Samudrala if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) { 1122ba735425SSridhar Samudrala /* Packet is from an untrusted source, reset gso_segs. */ 1123ba735425SSridhar Samudrala int type = skb_shinfo(skb)->gso_type; 1124ba735425SSridhar Samudrala 1125ba735425SSridhar Samudrala if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY) || 1126ba735425SSridhar Samudrala !(type & (SKB_GSO_UDP)))) 1127ba735425SSridhar Samudrala goto out; 1128ba735425SSridhar Samudrala 1129ba735425SSridhar Samudrala skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss); 1130ba735425SSridhar Samudrala 1131ba735425SSridhar Samudrala segs = NULL; 1132ba735425SSridhar Samudrala goto out; 1133ba735425SSridhar Samudrala } 1134ba735425SSridhar Samudrala 1135ba735425SSridhar Samudrala /* Do software UFO. Complete and fill in the UDP checksum as HW cannot 1136ba735425SSridhar Samudrala * do checksum of UDP packets sent as multiple IP fragments. 1137ba735425SSridhar Samudrala */ 1138ba735425SSridhar Samudrala offset = skb->csum_start - skb_headroom(skb); 1139ba735425SSridhar Samudrala csum = skb_checksum(skb, offset, skb->len- offset, 0); 1140ba735425SSridhar Samudrala offset += skb->csum_offset; 1141ba735425SSridhar Samudrala *(__sum16 *)(skb->data + offset) = csum_fold(csum); 1142ba735425SSridhar Samudrala skb->ip_summed = CHECKSUM_NONE; 1143ba735425SSridhar Samudrala 1144ba735425SSridhar Samudrala /* Check if there is enough headroom to insert fragment header. */ 1145ba735425SSridhar Samudrala if ((skb_headroom(skb) < frag_hdr_sz) && 1146ba735425SSridhar Samudrala pskb_expand_head(skb, frag_hdr_sz, 0, GFP_ATOMIC)) 1147ba735425SSridhar Samudrala goto out; 1148ba735425SSridhar Samudrala 1149ba735425SSridhar Samudrala /* Find the unfragmentable header and shift it left by frag_hdr_sz 1150ba735425SSridhar Samudrala * bytes to insert fragment header. 1151ba735425SSridhar Samudrala */ 1152ba735425SSridhar Samudrala unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr); 1153ba735425SSridhar Samudrala nexthdr = *prevhdr; 1154ba735425SSridhar Samudrala *prevhdr = NEXTHDR_FRAGMENT; 1155ba735425SSridhar Samudrala unfrag_len = skb_network_header(skb) - skb_mac_header(skb) + 1156ba735425SSridhar Samudrala unfrag_ip6hlen; 1157ba735425SSridhar Samudrala mac_start = skb_mac_header(skb); 1158ba735425SSridhar Samudrala memmove(mac_start-frag_hdr_sz, mac_start, unfrag_len); 1159ba735425SSridhar Samudrala 1160ba735425SSridhar Samudrala skb->mac_header -= frag_hdr_sz; 1161ba735425SSridhar Samudrala skb->network_header -= frag_hdr_sz; 1162ba735425SSridhar Samudrala 1163ba735425SSridhar Samudrala fptr = (struct frag_hdr *)(skb_network_header(skb) + unfrag_ip6hlen); 1164ba735425SSridhar Samudrala fptr->nexthdr = nexthdr; 1165ba735425SSridhar Samudrala fptr->reserved = 0; 1166ba735425SSridhar Samudrala ipv6_select_ident(fptr); 1167ba735425SSridhar Samudrala 1168ba735425SSridhar Samudrala /* Fragment the skb. ipv6 header and the remaining fields of the 1169ba735425SSridhar Samudrala * fragment header are updated in ipv6_gso_segment() 1170ba735425SSridhar Samudrala */ 1171ba735425SSridhar Samudrala segs = skb_segment(skb, features); 1172ba735425SSridhar Samudrala 1173ba735425SSridhar Samudrala out: 1174ba735425SSridhar Samudrala return segs; 1175ba735425SSridhar Samudrala } 1176ba735425SSridhar Samudrala 117741135cc8SAlexey Dobriyan static const struct inet6_protocol udpv6_protocol = { 1178db8dac20SDavid S. Miller .handler = udpv6_rcv, 1179db8dac20SDavid S. Miller .err_handler = udpv6_err, 1180ba735425SSridhar Samudrala .gso_send_check = udp6_ufo_send_check, 1181ba735425SSridhar Samudrala .gso_segment = udp6_ufo_fragment, 1182db8dac20SDavid S. Miller .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, 1183db8dac20SDavid S. Miller }; 1184db8dac20SDavid S. Miller 1185db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 1186db8dac20SDavid S. Miller #ifdef CONFIG_PROC_FS 1187db8dac20SDavid S. Miller 1188db8dac20SDavid S. Miller static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket) 1189db8dac20SDavid S. Miller { 1190db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sp); 1191db8dac20SDavid S. Miller struct ipv6_pinfo *np = inet6_sk(sp); 1192db8dac20SDavid S. Miller struct in6_addr *dest, *src; 1193db8dac20SDavid S. Miller __u16 destp, srcp; 1194db8dac20SDavid S. Miller 1195db8dac20SDavid S. Miller dest = &np->daddr; 1196db8dac20SDavid S. Miller src = &np->rcv_saddr; 1197db8dac20SDavid S. Miller destp = ntohs(inet->dport); 1198db8dac20SDavid S. Miller srcp = ntohs(inet->sport); 1199db8dac20SDavid S. Miller seq_printf(seq, 1200db8dac20SDavid S. Miller "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X " 1201cb61cb9bSEric Dumazet "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n", 1202db8dac20SDavid S. Miller bucket, 1203db8dac20SDavid S. Miller src->s6_addr32[0], src->s6_addr32[1], 1204db8dac20SDavid S. Miller src->s6_addr32[2], src->s6_addr32[3], srcp, 1205db8dac20SDavid S. Miller dest->s6_addr32[0], dest->s6_addr32[1], 1206db8dac20SDavid S. Miller dest->s6_addr32[2], dest->s6_addr32[3], destp, 1207db8dac20SDavid S. Miller sp->sk_state, 120831e6d363SEric Dumazet sk_wmem_alloc_get(sp), 120931e6d363SEric Dumazet sk_rmem_alloc_get(sp), 1210db8dac20SDavid S. Miller 0, 0L, 0, 1211db8dac20SDavid S. Miller sock_i_uid(sp), 0, 1212db8dac20SDavid S. Miller sock_i_ino(sp), 1213cb61cb9bSEric Dumazet atomic_read(&sp->sk_refcnt), sp, 1214cb61cb9bSEric Dumazet atomic_read(&sp->sk_drops)); 1215db8dac20SDavid S. Miller } 1216db8dac20SDavid S. Miller 1217db8dac20SDavid S. Miller int udp6_seq_show(struct seq_file *seq, void *v) 1218db8dac20SDavid S. Miller { 1219db8dac20SDavid S. Miller if (v == SEQ_START_TOKEN) 1220db8dac20SDavid S. Miller seq_printf(seq, 1221db8dac20SDavid S. Miller " sl " 1222db8dac20SDavid S. Miller "local_address " 1223db8dac20SDavid S. Miller "remote_address " 1224db8dac20SDavid S. Miller "st tx_queue rx_queue tr tm->when retrnsmt" 1225cb61cb9bSEric Dumazet " uid timeout inode ref pointer drops\n"); 1226db8dac20SDavid S. Miller else 1227db8dac20SDavid S. Miller udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket); 1228db8dac20SDavid S. Miller return 0; 1229db8dac20SDavid S. Miller } 1230db8dac20SDavid S. Miller 1231db8dac20SDavid S. Miller static struct udp_seq_afinfo udp6_seq_afinfo = { 1232db8dac20SDavid S. Miller .name = "udp6", 1233db8dac20SDavid S. Miller .family = AF_INET6, 1234645ca708SEric Dumazet .udp_table = &udp_table, 12354ad96d39SDenis V. Lunev .seq_fops = { 12364ad96d39SDenis V. Lunev .owner = THIS_MODULE, 12374ad96d39SDenis V. Lunev }, 1238dda61925SDenis V. Lunev .seq_ops = { 1239dda61925SDenis V. Lunev .show = udp6_seq_show, 1240dda61925SDenis V. Lunev }, 1241db8dac20SDavid S. Miller }; 1242db8dac20SDavid S. Miller 12430c96d8c5SDaniel Lezcano int udp6_proc_init(struct net *net) 1244db8dac20SDavid S. Miller { 12450c96d8c5SDaniel Lezcano return udp_proc_register(net, &udp6_seq_afinfo); 1246db8dac20SDavid S. Miller } 1247db8dac20SDavid S. Miller 12480c96d8c5SDaniel Lezcano void udp6_proc_exit(struct net *net) { 12490c96d8c5SDaniel Lezcano udp_proc_unregister(net, &udp6_seq_afinfo); 1250db8dac20SDavid S. Miller } 1251db8dac20SDavid S. Miller #endif /* CONFIG_PROC_FS */ 1252db8dac20SDavid S. Miller 1253db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 1254db8dac20SDavid S. Miller 1255db8dac20SDavid S. Miller struct proto udpv6_prot = { 1256db8dac20SDavid S. Miller .name = "UDPv6", 1257db8dac20SDavid S. Miller .owner = THIS_MODULE, 1258db8dac20SDavid S. Miller .close = udp_lib_close, 1259db8dac20SDavid S. Miller .connect = ip6_datagram_connect, 1260db8dac20SDavid S. Miller .disconnect = udp_disconnect, 1261db8dac20SDavid S. Miller .ioctl = udp_ioctl, 1262db8dac20SDavid S. Miller .destroy = udpv6_destroy_sock, 1263db8dac20SDavid S. Miller .setsockopt = udpv6_setsockopt, 1264db8dac20SDavid S. Miller .getsockopt = udpv6_getsockopt, 1265db8dac20SDavid S. Miller .sendmsg = udpv6_sendmsg, 1266db8dac20SDavid S. Miller .recvmsg = udpv6_recvmsg, 1267db8dac20SDavid S. Miller .backlog_rcv = udpv6_queue_rcv_skb, 1268db8dac20SDavid S. Miller .hash = udp_lib_hash, 1269db8dac20SDavid S. Miller .unhash = udp_lib_unhash, 1270db8dac20SDavid S. Miller .get_port = udp_v6_get_port, 1271db8dac20SDavid S. Miller .memory_allocated = &udp_memory_allocated, 1272db8dac20SDavid S. Miller .sysctl_mem = sysctl_udp_mem, 1273db8dac20SDavid S. Miller .sysctl_wmem = &sysctl_udp_wmem_min, 1274db8dac20SDavid S. Miller .sysctl_rmem = &sysctl_udp_rmem_min, 1275db8dac20SDavid S. Miller .obj_size = sizeof(struct udp6_sock), 1276271b72c7SEric Dumazet .slab_flags = SLAB_DESTROY_BY_RCU, 1277645ca708SEric Dumazet .h.udp_table = &udp_table, 1278db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1279db8dac20SDavid S. Miller .compat_setsockopt = compat_udpv6_setsockopt, 1280db8dac20SDavid S. Miller .compat_getsockopt = compat_udpv6_getsockopt, 1281db8dac20SDavid S. Miller #endif 1282db8dac20SDavid S. Miller }; 1283db8dac20SDavid S. Miller 1284db8dac20SDavid S. Miller static struct inet_protosw udpv6_protosw = { 1285db8dac20SDavid S. Miller .type = SOCK_DGRAM, 1286db8dac20SDavid S. Miller .protocol = IPPROTO_UDP, 1287db8dac20SDavid S. Miller .prot = &udpv6_prot, 1288db8dac20SDavid S. Miller .ops = &inet6_dgram_ops, 1289db8dac20SDavid S. Miller .capability =-1, 1290db8dac20SDavid S. Miller .no_check = UDP_CSUM_DEFAULT, 1291db8dac20SDavid S. Miller .flags = INET_PROTOSW_PERMANENT, 1292db8dac20SDavid S. Miller }; 1293db8dac20SDavid S. Miller 1294db8dac20SDavid S. Miller 1295db8dac20SDavid S. Miller int __init udpv6_init(void) 1296db8dac20SDavid S. Miller { 1297db8dac20SDavid S. Miller int ret; 1298db8dac20SDavid S. Miller 1299db8dac20SDavid S. Miller ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP); 1300db8dac20SDavid S. Miller if (ret) 1301db8dac20SDavid S. Miller goto out; 1302db8dac20SDavid S. Miller 1303db8dac20SDavid S. Miller ret = inet6_register_protosw(&udpv6_protosw); 1304db8dac20SDavid S. Miller if (ret) 1305db8dac20SDavid S. Miller goto out_udpv6_protocol; 1306db8dac20SDavid S. Miller out: 1307db8dac20SDavid S. Miller return ret; 1308db8dac20SDavid S. Miller 1309db8dac20SDavid S. Miller out_udpv6_protocol: 1310db8dac20SDavid S. Miller inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP); 1311db8dac20SDavid S. Miller goto out; 1312db8dac20SDavid S. Miller } 1313db8dac20SDavid S. Miller 1314db8dac20SDavid S. Miller void udpv6_exit(void) 1315db8dac20SDavid S. Miller { 1316db8dac20SDavid S. Miller inet6_unregister_protosw(&udpv6_protosw); 1317db8dac20SDavid S. Miller inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP); 1318db8dac20SDavid S. Miller } 1319