11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 31da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 41da177e4SLinus Torvalds * interface as the means of communication with the user level. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * The User Datagram Protocol (UDP). 71da177e4SLinus Torvalds * 802c30a84SJesper Juhl * Authors: Ross Biro 91da177e4SLinus Torvalds * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 101da177e4SLinus Torvalds * Arnt Gulbrandsen, <agulbra@nvg.unit.no> 11113aa838SAlan Cox * Alan Cox, <alan@lxorguk.ukuu.org.uk> 121da177e4SLinus Torvalds * Hirokazu Takahashi, <taka@valinux.co.jp> 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * Fixes: 151da177e4SLinus Torvalds * Alan Cox : verify_area() calls 161da177e4SLinus Torvalds * Alan Cox : stopped close while in use off icmp 171da177e4SLinus Torvalds * messages. Not a fix but a botch that 181da177e4SLinus Torvalds * for udp at least is 'valid'. 191da177e4SLinus Torvalds * Alan Cox : Fixed icmp handling properly 201da177e4SLinus Torvalds * Alan Cox : Correct error for oversized datagrams 211da177e4SLinus Torvalds * Alan Cox : Tidied select() semantics. 221da177e4SLinus Torvalds * Alan Cox : udp_err() fixed properly, also now 231da177e4SLinus Torvalds * select and read wake correctly on errors 241da177e4SLinus Torvalds * Alan Cox : udp_send verify_area moved to avoid mem leak 251da177e4SLinus Torvalds * Alan Cox : UDP can count its memory 261da177e4SLinus Torvalds * Alan Cox : send to an unknown connection causes 271da177e4SLinus Torvalds * an ECONNREFUSED off the icmp, but 281da177e4SLinus Torvalds * does NOT close. 291da177e4SLinus Torvalds * Alan Cox : Switched to new sk_buff handlers. No more backlog! 301da177e4SLinus Torvalds * Alan Cox : Using generic datagram code. Even smaller and the PEEK 311da177e4SLinus Torvalds * bug no longer crashes it. 321da177e4SLinus Torvalds * Fred Van Kempen : Net2e support for sk->broadcast. 331da177e4SLinus Torvalds * Alan Cox : Uses skb_free_datagram 341da177e4SLinus Torvalds * Alan Cox : Added get/set sockopt support. 351da177e4SLinus Torvalds * Alan Cox : Broadcasting without option set returns EACCES. 361da177e4SLinus Torvalds * Alan Cox : No wakeup calls. Instead we now use the callbacks. 371da177e4SLinus Torvalds * Alan Cox : Use ip_tos and ip_ttl 381da177e4SLinus Torvalds * Alan Cox : SNMP Mibs 391da177e4SLinus Torvalds * Alan Cox : MSG_DONTROUTE, and 0.0.0.0 support. 401da177e4SLinus Torvalds * Matt Dillon : UDP length checks. 411da177e4SLinus Torvalds * Alan Cox : Smarter af_inet used properly. 421da177e4SLinus Torvalds * Alan Cox : Use new kernel side addressing. 431da177e4SLinus Torvalds * Alan Cox : Incorrect return on truncated datagram receive. 441da177e4SLinus Torvalds * Arnt Gulbrandsen : New udp_send and stuff 451da177e4SLinus Torvalds * Alan Cox : Cache last socket 461da177e4SLinus Torvalds * Alan Cox : Route cache 471da177e4SLinus Torvalds * Jon Peatfield : Minor efficiency fix to sendto(). 481da177e4SLinus Torvalds * Mike Shaver : RFC1122 checks. 491da177e4SLinus Torvalds * Alan Cox : Nonblocking error fix. 501da177e4SLinus Torvalds * Willy Konynenberg : Transparent proxying support. 511da177e4SLinus Torvalds * Mike McLagan : Routing by source 521da177e4SLinus Torvalds * David S. Miller : New socket lookup architecture. 531da177e4SLinus Torvalds * Last socket cache retained as it 541da177e4SLinus Torvalds * does have a high hit rate. 551da177e4SLinus Torvalds * Olaf Kirch : Don't linearise iovec on sendmsg. 561da177e4SLinus Torvalds * Andi Kleen : Some cleanups, cache destination entry 571da177e4SLinus Torvalds * for connect. 581da177e4SLinus Torvalds * Vitaly E. Lavrov : Transparent proxy revived after year coma. 591da177e4SLinus Torvalds * Melvin Smith : Check msg_name not msg_namelen in sendto(), 601da177e4SLinus Torvalds * return ENOTCONN for unconnected sockets (POSIX) 611da177e4SLinus Torvalds * Janos Farkas : don't deliver multi/broadcasts to a different 621da177e4SLinus Torvalds * bound-to-device socket 631da177e4SLinus Torvalds * Hirokazu Takahashi : HW checksumming for outgoing UDP 641da177e4SLinus Torvalds * datagrams. 651da177e4SLinus Torvalds * Hirokazu Takahashi : sendfile() on UDP works now. 661da177e4SLinus Torvalds * Arnaldo C. Melo : convert /proc/net/udp to seq_file 671da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which 681da177e4SLinus Torvalds * Alexey Kuznetsov: allow both IPv4 and IPv6 sockets to bind 691da177e4SLinus Torvalds * a single port at the same time. 701da177e4SLinus Torvalds * Derek Atkins <derek@ihtfp.com>: Add Encapulation Support 71342f0234SJames Chapman * James Chapman : Add L2TP encapsulation type. 721da177e4SLinus Torvalds * 731da177e4SLinus Torvalds * 741da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 751da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 761da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 771da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 781da177e4SLinus Torvalds */ 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds #include <asm/system.h> 811da177e4SLinus Torvalds #include <asm/uaccess.h> 821da177e4SLinus Torvalds #include <asm/ioctls.h> 8395766fffSHideo Aoki #include <linux/bootmem.h> 848203efb3SEric Dumazet #include <linux/highmem.h> 858203efb3SEric Dumazet #include <linux/swap.h> 861da177e4SLinus Torvalds #include <linux/types.h> 871da177e4SLinus Torvalds #include <linux/fcntl.h> 881da177e4SLinus Torvalds #include <linux/module.h> 891da177e4SLinus Torvalds #include <linux/socket.h> 901da177e4SLinus Torvalds #include <linux/sockios.h> 9114c85021SArnaldo Carvalho de Melo #include <linux/igmp.h> 921da177e4SLinus Torvalds #include <linux/in.h> 931da177e4SLinus Torvalds #include <linux/errno.h> 941da177e4SLinus Torvalds #include <linux/timer.h> 951da177e4SLinus Torvalds #include <linux/mm.h> 961da177e4SLinus Torvalds #include <linux/inet.h> 971da177e4SLinus Torvalds #include <linux/netdevice.h> 98c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h> 991da177e4SLinus Torvalds #include <linux/skbuff.h> 1001da177e4SLinus Torvalds #include <linux/proc_fs.h> 1011da177e4SLinus Torvalds #include <linux/seq_file.h> 102457c4cbcSEric W. Biederman #include <net/net_namespace.h> 1031da177e4SLinus Torvalds #include <net/icmp.h> 1041da177e4SLinus Torvalds #include <net/route.h> 1051da177e4SLinus Torvalds #include <net/checksum.h> 1061da177e4SLinus Torvalds #include <net/xfrm.h> 107ba4e58ecSGerrit Renker #include "udp_impl.h" 1081da177e4SLinus Torvalds 109645ca708SEric Dumazet struct udp_table udp_table; 110645ca708SEric Dumazet EXPORT_SYMBOL(udp_table); 1111da177e4SLinus Torvalds 11295766fffSHideo Aoki int sysctl_udp_mem[3] __read_mostly; 11395766fffSHideo Aoki int sysctl_udp_rmem_min __read_mostly; 11495766fffSHideo Aoki int sysctl_udp_wmem_min __read_mostly; 11595766fffSHideo Aoki 11695766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_mem); 11795766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_rmem_min); 11895766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_wmem_min); 11995766fffSHideo Aoki 12095766fffSHideo Aoki atomic_t udp_memory_allocated; 12195766fffSHideo Aoki EXPORT_SYMBOL(udp_memory_allocated); 12295766fffSHideo Aoki 123f24d43c0SEric Dumazet static int udp_lib_lport_inuse(struct net *net, __u16 num, 124645ca708SEric Dumazet const struct udp_hslot *hslot, 125f24d43c0SEric Dumazet struct sock *sk, 126f24d43c0SEric Dumazet int (*saddr_comp)(const struct sock *sk1, 127f24d43c0SEric Dumazet const struct sock *sk2)) 12825030a7fSGerrit Renker { 129f24d43c0SEric Dumazet struct sock *sk2; 13088ab1932SEric Dumazet struct hlist_nulls_node *node; 13125030a7fSGerrit Renker 13288ab1932SEric Dumazet sk_nulls_for_each(sk2, node, &hslot->head) 133f24d43c0SEric Dumazet if (net_eq(sock_net(sk2), net) && 134f24d43c0SEric Dumazet sk2 != sk && 135f24d43c0SEric Dumazet sk2->sk_hash == num && 136f24d43c0SEric Dumazet (!sk2->sk_reuse || !sk->sk_reuse) && 137f24d43c0SEric Dumazet (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if 138f24d43c0SEric Dumazet || sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 139f24d43c0SEric Dumazet (*saddr_comp)(sk, sk2)) 140fc038410SDavid S. Miller return 1; 14125030a7fSGerrit Renker return 0; 14225030a7fSGerrit Renker } 14325030a7fSGerrit Renker 14425030a7fSGerrit Renker /** 1456ba5a3c5SPavel Emelyanov * udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6 14625030a7fSGerrit Renker * 14725030a7fSGerrit Renker * @sk: socket struct in question 14825030a7fSGerrit Renker * @snum: port number to look up 149df2bc459SDavid S. Miller * @saddr_comp: AF-dependent comparison of bound local IP addresses 15025030a7fSGerrit Renker */ 1516ba5a3c5SPavel Emelyanov int udp_lib_get_port(struct sock *sk, unsigned short snum, 152df2bc459SDavid S. Miller int (*saddr_comp)(const struct sock *sk1, 153df2bc459SDavid S. Miller const struct sock *sk2 ) ) 1541da177e4SLinus Torvalds { 155645ca708SEric Dumazet struct udp_hslot *hslot; 156645ca708SEric Dumazet struct udp_table *udptable = sk->sk_prot->h.udp_table; 15725030a7fSGerrit Renker int error = 1; 1583b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 1591da177e4SLinus Torvalds 16032c1da70SStephen Hemminger if (!snum) { 1619088c560SEric Dumazet int low, high, remaining; 1629088c560SEric Dumazet unsigned rand; 1639088c560SEric Dumazet unsigned short first; 1641da177e4SLinus Torvalds 165227b60f5SStephen Hemminger inet_get_local_port_range(&low, &high); 166a25de534SAnton Arapov remaining = (high - low) + 1; 167227b60f5SStephen Hemminger 1689088c560SEric Dumazet rand = net_random(); 1699088c560SEric Dumazet snum = first = rand % remaining + low; 1709088c560SEric Dumazet rand |= 1; 171645ca708SEric Dumazet for (;;) { 172645ca708SEric Dumazet hslot = &udptable->hash[udp_hashfn(net, snum)]; 173645ca708SEric Dumazet spin_lock_bh(&hslot->lock); 174645ca708SEric Dumazet if (!udp_lib_lport_inuse(net, snum, hslot, sk, saddr_comp)) 175645ca708SEric Dumazet break; 176645ca708SEric Dumazet spin_unlock_bh(&hslot->lock); 1779088c560SEric Dumazet do { 1789088c560SEric Dumazet snum = snum + rand; 1799088c560SEric Dumazet } while (snum < low || snum > high); 1809088c560SEric Dumazet if (snum == first) 1811da177e4SLinus Torvalds goto fail; 1829088c560SEric Dumazet } 183645ca708SEric Dumazet } else { 184645ca708SEric Dumazet hslot = &udptable->hash[udp_hashfn(net, snum)]; 185645ca708SEric Dumazet spin_lock_bh(&hslot->lock); 186645ca708SEric Dumazet if (udp_lib_lport_inuse(net, snum, hslot, sk, saddr_comp)) 187645ca708SEric Dumazet goto fail_unlock; 188645ca708SEric Dumazet } 18925030a7fSGerrit Renker inet_sk(sk)->num = snum; 190df2bc459SDavid S. Miller sk->sk_hash = snum; 1911da177e4SLinus Torvalds if (sk_unhashed(sk)) { 19288ab1932SEric Dumazet sk_nulls_add_node_rcu(sk, &hslot->head); 193c29a0bc4SPavel Emelyanov sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 1941da177e4SLinus Torvalds } 19525030a7fSGerrit Renker error = 0; 196645ca708SEric Dumazet fail_unlock: 197645ca708SEric Dumazet spin_unlock_bh(&hslot->lock); 1981da177e4SLinus Torvalds fail: 19925030a7fSGerrit Renker return error; 2001da177e4SLinus Torvalds } 2011da177e4SLinus Torvalds 2026ba5a3c5SPavel Emelyanov static int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2) 203db8dac20SDavid S. Miller { 204db8dac20SDavid S. Miller struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2); 205db8dac20SDavid S. Miller 206db8dac20SDavid S. Miller return ( !ipv6_only_sock(sk2) && 207db8dac20SDavid S. Miller (!inet1->rcv_saddr || !inet2->rcv_saddr || 208db8dac20SDavid S. Miller inet1->rcv_saddr == inet2->rcv_saddr )); 209db8dac20SDavid S. Miller } 210db8dac20SDavid S. Miller 2116ba5a3c5SPavel Emelyanov int udp_v4_get_port(struct sock *sk, unsigned short snum) 212db8dac20SDavid S. Miller { 2136ba5a3c5SPavel Emelyanov return udp_lib_get_port(sk, snum, ipv4_rcv_saddr_equal); 214db8dac20SDavid S. Miller } 215db8dac20SDavid S. Miller 216645ca708SEric Dumazet static inline int compute_score(struct sock *sk, struct net *net, __be32 saddr, 217645ca708SEric Dumazet unsigned short hnum, 218645ca708SEric Dumazet __be16 sport, __be32 daddr, __be16 dport, int dif) 219645ca708SEric Dumazet { 220645ca708SEric Dumazet int score = -1; 221645ca708SEric Dumazet 222645ca708SEric Dumazet if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum && 223645ca708SEric Dumazet !ipv6_only_sock(sk)) { 224645ca708SEric Dumazet struct inet_sock *inet = inet_sk(sk); 225645ca708SEric Dumazet 226645ca708SEric Dumazet score = (sk->sk_family == PF_INET ? 1 : 0); 227645ca708SEric Dumazet if (inet->rcv_saddr) { 228645ca708SEric Dumazet if (inet->rcv_saddr != daddr) 229645ca708SEric Dumazet return -1; 230645ca708SEric Dumazet score += 2; 231645ca708SEric Dumazet } 232645ca708SEric Dumazet if (inet->daddr) { 233645ca708SEric Dumazet if (inet->daddr != saddr) 234645ca708SEric Dumazet return -1; 235645ca708SEric Dumazet score += 2; 236645ca708SEric Dumazet } 237645ca708SEric Dumazet if (inet->dport) { 238645ca708SEric Dumazet if (inet->dport != sport) 239645ca708SEric Dumazet return -1; 240645ca708SEric Dumazet score += 2; 241645ca708SEric Dumazet } 242645ca708SEric Dumazet if (sk->sk_bound_dev_if) { 243645ca708SEric Dumazet if (sk->sk_bound_dev_if != dif) 244645ca708SEric Dumazet return -1; 245645ca708SEric Dumazet score += 2; 246645ca708SEric Dumazet } 247645ca708SEric Dumazet } 248645ca708SEric Dumazet return score; 249645ca708SEric Dumazet } 250645ca708SEric Dumazet 251db8dac20SDavid S. Miller /* UDP is nearly always wildcards out the wazoo, it makes no sense to try 252db8dac20SDavid S. Miller * harder than this. -DaveM 253db8dac20SDavid S. Miller */ 254db8dac20SDavid S. Miller static struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, 255db8dac20SDavid S. Miller __be16 sport, __be32 daddr, __be16 dport, 256645ca708SEric Dumazet int dif, struct udp_table *udptable) 257db8dac20SDavid S. Miller { 258271b72c7SEric Dumazet struct sock *sk, *result; 25988ab1932SEric Dumazet struct hlist_nulls_node *node; 260db8dac20SDavid S. Miller unsigned short hnum = ntohs(dport); 261645ca708SEric Dumazet unsigned int hash = udp_hashfn(net, hnum); 262645ca708SEric Dumazet struct udp_hslot *hslot = &udptable->hash[hash]; 263271b72c7SEric Dumazet int score, badness; 264db8dac20SDavid S. Miller 265271b72c7SEric Dumazet rcu_read_lock(); 266271b72c7SEric Dumazet begin: 267271b72c7SEric Dumazet result = NULL; 268271b72c7SEric Dumazet badness = -1; 26988ab1932SEric Dumazet sk_nulls_for_each_rcu(sk, node, &hslot->head) { 270645ca708SEric Dumazet score = compute_score(sk, net, saddr, hnum, sport, 271645ca708SEric Dumazet daddr, dport, dif); 272645ca708SEric Dumazet if (score > badness) { 273db8dac20SDavid S. Miller result = sk; 274db8dac20SDavid S. Miller badness = score; 275db8dac20SDavid S. Miller } 276db8dac20SDavid S. Miller } 27788ab1932SEric Dumazet /* 27888ab1932SEric Dumazet * if the nulls value we got at the end of this lookup is 27988ab1932SEric Dumazet * not the expected one, we must restart lookup. 28088ab1932SEric Dumazet * We probably met an item that was moved to another chain. 28188ab1932SEric Dumazet */ 28288ab1932SEric Dumazet if (get_nulls_value(node) != hash) 28388ab1932SEric Dumazet goto begin; 28488ab1932SEric Dumazet 285271b72c7SEric Dumazet if (result) { 286271b72c7SEric Dumazet if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt))) 287271b72c7SEric Dumazet result = NULL; 288271b72c7SEric Dumazet else if (unlikely(compute_score(result, net, saddr, hnum, sport, 289271b72c7SEric Dumazet daddr, dport, dif) < badness)) { 290271b72c7SEric Dumazet sock_put(result); 291271b72c7SEric Dumazet goto begin; 292271b72c7SEric Dumazet } 293271b72c7SEric Dumazet } 294271b72c7SEric Dumazet rcu_read_unlock(); 295db8dac20SDavid S. Miller return result; 296db8dac20SDavid S. Miller } 297db8dac20SDavid S. Miller 298607c4aafSKOVACS Krisztian static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb, 299607c4aafSKOVACS Krisztian __be16 sport, __be16 dport, 300645ca708SEric Dumazet struct udp_table *udptable) 301607c4aafSKOVACS Krisztian { 30223542618SKOVACS Krisztian struct sock *sk; 303607c4aafSKOVACS Krisztian const struct iphdr *iph = ip_hdr(skb); 304607c4aafSKOVACS Krisztian 30523542618SKOVACS Krisztian if (unlikely(sk = skb_steal_sock(skb))) 30623542618SKOVACS Krisztian return sk; 30723542618SKOVACS Krisztian else 308607c4aafSKOVACS Krisztian return __udp4_lib_lookup(dev_net(skb->dst->dev), iph->saddr, sport, 309607c4aafSKOVACS Krisztian iph->daddr, dport, inet_iif(skb), 310607c4aafSKOVACS Krisztian udptable); 311607c4aafSKOVACS Krisztian } 312607c4aafSKOVACS Krisztian 313bcd41303SKOVACS Krisztian struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport, 314bcd41303SKOVACS Krisztian __be32 daddr, __be16 dport, int dif) 315bcd41303SKOVACS Krisztian { 316645ca708SEric Dumazet return __udp4_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table); 317bcd41303SKOVACS Krisztian } 318bcd41303SKOVACS Krisztian EXPORT_SYMBOL_GPL(udp4_lib_lookup); 319bcd41303SKOVACS Krisztian 320920a4611SEric Dumazet static inline struct sock *udp_v4_mcast_next(struct net *net, struct sock *sk, 321db8dac20SDavid S. Miller __be16 loc_port, __be32 loc_addr, 322db8dac20SDavid S. Miller __be16 rmt_port, __be32 rmt_addr, 323db8dac20SDavid S. Miller int dif) 324db8dac20SDavid S. Miller { 32588ab1932SEric Dumazet struct hlist_nulls_node *node; 326db8dac20SDavid S. Miller struct sock *s = sk; 327db8dac20SDavid S. Miller unsigned short hnum = ntohs(loc_port); 328db8dac20SDavid S. Miller 32988ab1932SEric Dumazet sk_nulls_for_each_from(s, node) { 330db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(s); 331db8dac20SDavid S. Miller 332920a4611SEric Dumazet if (!net_eq(sock_net(s), net) || 333920a4611SEric Dumazet s->sk_hash != hnum || 334db8dac20SDavid S. Miller (inet->daddr && inet->daddr != rmt_addr) || 335db8dac20SDavid S. Miller (inet->dport != rmt_port && inet->dport) || 336db8dac20SDavid S. Miller (inet->rcv_saddr && inet->rcv_saddr != loc_addr) || 337db8dac20SDavid S. Miller ipv6_only_sock(s) || 338db8dac20SDavid S. Miller (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)) 339db8dac20SDavid S. Miller continue; 340db8dac20SDavid S. Miller if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif)) 341db8dac20SDavid S. Miller continue; 342db8dac20SDavid S. Miller goto found; 343db8dac20SDavid S. Miller } 344db8dac20SDavid S. Miller s = NULL; 345db8dac20SDavid S. Miller found: 346db8dac20SDavid S. Miller return s; 347db8dac20SDavid S. Miller } 348db8dac20SDavid S. Miller 349db8dac20SDavid S. Miller /* 350db8dac20SDavid S. Miller * This routine is called by the ICMP module when it gets some 351db8dac20SDavid S. Miller * sort of error condition. If err < 0 then the socket should 352db8dac20SDavid S. Miller * be closed and the error returned to the user. If err > 0 353db8dac20SDavid S. Miller * it's just the icmp type << 8 | icmp code. 354db8dac20SDavid S. Miller * Header points to the ip header of the error packet. We move 355db8dac20SDavid S. Miller * on past this. Then (as it used to claim before adjustment) 356db8dac20SDavid S. Miller * header points to the first 8 bytes of the udp header. We need 357db8dac20SDavid S. Miller * to find the appropriate port. 358db8dac20SDavid S. Miller */ 359db8dac20SDavid S. Miller 360645ca708SEric Dumazet void __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable) 361db8dac20SDavid S. Miller { 362db8dac20SDavid S. Miller struct inet_sock *inet; 363db8dac20SDavid S. Miller struct iphdr *iph = (struct iphdr*)skb->data; 364db8dac20SDavid S. Miller struct udphdr *uh = (struct udphdr*)(skb->data+(iph->ihl<<2)); 365db8dac20SDavid S. Miller const int type = icmp_hdr(skb)->type; 366db8dac20SDavid S. Miller const int code = icmp_hdr(skb)->code; 367db8dac20SDavid S. Miller struct sock *sk; 368db8dac20SDavid S. Miller int harderr; 369db8dac20SDavid S. Miller int err; 370fd54d716SPavel Emelyanov struct net *net = dev_net(skb->dev); 371db8dac20SDavid S. Miller 372fd54d716SPavel Emelyanov sk = __udp4_lib_lookup(net, iph->daddr, uh->dest, 373db8dac20SDavid S. Miller iph->saddr, uh->source, skb->dev->ifindex, udptable); 374db8dac20SDavid S. Miller if (sk == NULL) { 375dcfc23caSPavel Emelyanov ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS); 376db8dac20SDavid S. Miller return; /* No socket for error */ 377db8dac20SDavid S. Miller } 378db8dac20SDavid S. Miller 379db8dac20SDavid S. Miller err = 0; 380db8dac20SDavid S. Miller harderr = 0; 381db8dac20SDavid S. Miller inet = inet_sk(sk); 382db8dac20SDavid S. Miller 383db8dac20SDavid S. Miller switch (type) { 384db8dac20SDavid S. Miller default: 385db8dac20SDavid S. Miller case ICMP_TIME_EXCEEDED: 386db8dac20SDavid S. Miller err = EHOSTUNREACH; 387db8dac20SDavid S. Miller break; 388db8dac20SDavid S. Miller case ICMP_SOURCE_QUENCH: 389db8dac20SDavid S. Miller goto out; 390db8dac20SDavid S. Miller case ICMP_PARAMETERPROB: 391db8dac20SDavid S. Miller err = EPROTO; 392db8dac20SDavid S. Miller harderr = 1; 393db8dac20SDavid S. Miller break; 394db8dac20SDavid S. Miller case ICMP_DEST_UNREACH: 395db8dac20SDavid S. Miller if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */ 396db8dac20SDavid S. Miller if (inet->pmtudisc != IP_PMTUDISC_DONT) { 397db8dac20SDavid S. Miller err = EMSGSIZE; 398db8dac20SDavid S. Miller harderr = 1; 399db8dac20SDavid S. Miller break; 400db8dac20SDavid S. Miller } 401db8dac20SDavid S. Miller goto out; 402db8dac20SDavid S. Miller } 403db8dac20SDavid S. Miller err = EHOSTUNREACH; 404db8dac20SDavid S. Miller if (code <= NR_ICMP_UNREACH) { 405db8dac20SDavid S. Miller harderr = icmp_err_convert[code].fatal; 406db8dac20SDavid S. Miller err = icmp_err_convert[code].errno; 407db8dac20SDavid S. Miller } 408db8dac20SDavid S. Miller break; 409db8dac20SDavid S. Miller } 410db8dac20SDavid S. Miller 411db8dac20SDavid S. Miller /* 412db8dac20SDavid S. Miller * RFC1122: OK. Passes ICMP errors back to application, as per 413db8dac20SDavid S. Miller * 4.1.3.3. 414db8dac20SDavid S. Miller */ 415db8dac20SDavid S. Miller if (!inet->recverr) { 416db8dac20SDavid S. Miller if (!harderr || sk->sk_state != TCP_ESTABLISHED) 417db8dac20SDavid S. Miller goto out; 418db8dac20SDavid S. Miller } else { 419db8dac20SDavid S. Miller ip_icmp_error(sk, skb, err, uh->dest, info, (u8*)(uh+1)); 420db8dac20SDavid S. Miller } 421db8dac20SDavid S. Miller sk->sk_err = err; 422db8dac20SDavid S. Miller sk->sk_error_report(sk); 423db8dac20SDavid S. Miller out: 424db8dac20SDavid S. Miller sock_put(sk); 425db8dac20SDavid S. Miller } 426db8dac20SDavid S. Miller 427db8dac20SDavid S. Miller void udp_err(struct sk_buff *skb, u32 info) 428db8dac20SDavid S. Miller { 429645ca708SEric Dumazet __udp4_lib_err(skb, info, &udp_table); 430db8dac20SDavid S. Miller } 431db8dac20SDavid S. Miller 432db8dac20SDavid S. Miller /* 433db8dac20SDavid S. Miller * Throw away all pending data and cancel the corking. Socket is locked. 434db8dac20SDavid S. Miller */ 43536d926b9SDenis V. Lunev void udp_flush_pending_frames(struct sock *sk) 436db8dac20SDavid S. Miller { 437db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 438db8dac20SDavid S. Miller 439db8dac20SDavid S. Miller if (up->pending) { 440db8dac20SDavid S. Miller up->len = 0; 441db8dac20SDavid S. Miller up->pending = 0; 442db8dac20SDavid S. Miller ip_flush_pending_frames(sk); 443db8dac20SDavid S. Miller } 444db8dac20SDavid S. Miller } 44536d926b9SDenis V. Lunev EXPORT_SYMBOL(udp_flush_pending_frames); 446db8dac20SDavid S. Miller 447db8dac20SDavid S. Miller /** 448db8dac20SDavid S. Miller * udp4_hwcsum_outgoing - handle outgoing HW checksumming 449db8dac20SDavid S. Miller * @sk: socket we are sending on 450db8dac20SDavid S. Miller * @skb: sk_buff containing the filled-in UDP header 451db8dac20SDavid S. Miller * (checksum field must be zeroed out) 452db8dac20SDavid S. Miller */ 453db8dac20SDavid S. Miller static void udp4_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb, 454db8dac20SDavid S. Miller __be32 src, __be32 dst, int len ) 455db8dac20SDavid S. Miller { 456db8dac20SDavid S. Miller unsigned int offset; 457db8dac20SDavid S. Miller struct udphdr *uh = udp_hdr(skb); 458db8dac20SDavid S. Miller __wsum csum = 0; 459db8dac20SDavid S. Miller 460db8dac20SDavid S. Miller if (skb_queue_len(&sk->sk_write_queue) == 1) { 461db8dac20SDavid S. Miller /* 462db8dac20SDavid S. Miller * Only one fragment on the socket. 463db8dac20SDavid S. Miller */ 464db8dac20SDavid S. Miller skb->csum_start = skb_transport_header(skb) - skb->head; 465db8dac20SDavid S. Miller skb->csum_offset = offsetof(struct udphdr, check); 466db8dac20SDavid S. Miller uh->check = ~csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, 0); 467db8dac20SDavid S. Miller } else { 468db8dac20SDavid S. Miller /* 469db8dac20SDavid S. Miller * HW-checksum won't work as there are two or more 470db8dac20SDavid S. Miller * fragments on the socket so that all csums of sk_buffs 471db8dac20SDavid S. Miller * should be together 472db8dac20SDavid S. Miller */ 473db8dac20SDavid S. Miller offset = skb_transport_offset(skb); 474db8dac20SDavid S. Miller skb->csum = skb_checksum(skb, offset, skb->len - offset, 0); 475db8dac20SDavid S. Miller 476db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_NONE; 477db8dac20SDavid S. Miller 478db8dac20SDavid S. Miller skb_queue_walk(&sk->sk_write_queue, skb) { 479db8dac20SDavid S. Miller csum = csum_add(csum, skb->csum); 480db8dac20SDavid S. Miller } 481db8dac20SDavid S. Miller 482db8dac20SDavid S. Miller uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum); 483db8dac20SDavid S. Miller if (uh->check == 0) 484db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 485db8dac20SDavid S. Miller } 486db8dac20SDavid S. Miller } 487db8dac20SDavid S. Miller 488db8dac20SDavid S. Miller /* 489db8dac20SDavid S. Miller * Push out all pending data as one UDP datagram. Socket is locked. 490db8dac20SDavid S. Miller */ 491db8dac20SDavid S. Miller static int udp_push_pending_frames(struct sock *sk) 492db8dac20SDavid S. Miller { 493db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 494db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 495db8dac20SDavid S. Miller struct flowi *fl = &inet->cork.fl; 496db8dac20SDavid S. Miller struct sk_buff *skb; 497db8dac20SDavid S. Miller struct udphdr *uh; 498db8dac20SDavid S. Miller int err = 0; 499db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 500db8dac20SDavid S. Miller __wsum csum = 0; 501db8dac20SDavid S. Miller 502db8dac20SDavid S. Miller /* Grab the skbuff where UDP header space exists. */ 503db8dac20SDavid S. Miller if ((skb = skb_peek(&sk->sk_write_queue)) == NULL) 504db8dac20SDavid S. Miller goto out; 505db8dac20SDavid S. Miller 506db8dac20SDavid S. Miller /* 507db8dac20SDavid S. Miller * Create a UDP header 508db8dac20SDavid S. Miller */ 509db8dac20SDavid S. Miller uh = udp_hdr(skb); 510db8dac20SDavid S. Miller uh->source = fl->fl_ip_sport; 511db8dac20SDavid S. Miller uh->dest = fl->fl_ip_dport; 512db8dac20SDavid S. Miller uh->len = htons(up->len); 513db8dac20SDavid S. Miller uh->check = 0; 514db8dac20SDavid S. Miller 515db8dac20SDavid S. Miller if (is_udplite) /* UDP-Lite */ 516db8dac20SDavid S. Miller csum = udplite_csum_outgoing(sk, skb); 517db8dac20SDavid S. Miller 518db8dac20SDavid S. Miller else if (sk->sk_no_check == UDP_CSUM_NOXMIT) { /* UDP csum disabled */ 519db8dac20SDavid S. Miller 520db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_NONE; 521db8dac20SDavid S. Miller goto send; 522db8dac20SDavid S. Miller 523db8dac20SDavid S. Miller } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 524db8dac20SDavid S. Miller 525db8dac20SDavid S. Miller udp4_hwcsum_outgoing(sk, skb, fl->fl4_src,fl->fl4_dst, up->len); 526db8dac20SDavid S. Miller goto send; 527db8dac20SDavid S. Miller 528db8dac20SDavid S. Miller } else /* `normal' UDP */ 529db8dac20SDavid S. Miller csum = udp_csum_outgoing(sk, skb); 530db8dac20SDavid S. Miller 531db8dac20SDavid S. Miller /* add protocol-dependent pseudo-header */ 532db8dac20SDavid S. Miller uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst, up->len, 533db8dac20SDavid S. Miller sk->sk_protocol, csum ); 534db8dac20SDavid S. Miller if (uh->check == 0) 535db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 536db8dac20SDavid S. Miller 537db8dac20SDavid S. Miller send: 538db8dac20SDavid S. Miller err = ip_push_pending_frames(sk); 539db8dac20SDavid S. Miller out: 540db8dac20SDavid S. Miller up->len = 0; 541db8dac20SDavid S. Miller up->pending = 0; 542db8dac20SDavid S. Miller if (!err) 543629ca23cSPavel Emelyanov UDP_INC_STATS_USER(sock_net(sk), 544629ca23cSPavel Emelyanov UDP_MIB_OUTDATAGRAMS, is_udplite); 545db8dac20SDavid S. Miller return err; 546db8dac20SDavid S. Miller } 547db8dac20SDavid S. Miller 548db8dac20SDavid S. Miller int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 549db8dac20SDavid S. Miller size_t len) 550db8dac20SDavid S. Miller { 551db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 552db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 553db8dac20SDavid S. Miller int ulen = len; 554db8dac20SDavid S. Miller struct ipcm_cookie ipc; 555db8dac20SDavid S. Miller struct rtable *rt = NULL; 556db8dac20SDavid S. Miller int free = 0; 557db8dac20SDavid S. Miller int connected = 0; 558db8dac20SDavid S. Miller __be32 daddr, faddr, saddr; 559db8dac20SDavid S. Miller __be16 dport; 560db8dac20SDavid S. Miller u8 tos; 561db8dac20SDavid S. Miller int err, is_udplite = IS_UDPLITE(sk); 562db8dac20SDavid S. Miller int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; 563db8dac20SDavid S. Miller int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 564db8dac20SDavid S. Miller 565db8dac20SDavid S. Miller if (len > 0xFFFF) 566db8dac20SDavid S. Miller return -EMSGSIZE; 567db8dac20SDavid S. Miller 568db8dac20SDavid S. Miller /* 569db8dac20SDavid S. Miller * Check the flags. 570db8dac20SDavid S. Miller */ 571db8dac20SDavid S. Miller 572db8dac20SDavid S. Miller if (msg->msg_flags&MSG_OOB) /* Mirror BSD error message compatibility */ 573db8dac20SDavid S. Miller return -EOPNOTSUPP; 574db8dac20SDavid S. Miller 575db8dac20SDavid S. Miller ipc.opt = NULL; 576db8dac20SDavid S. Miller 577db8dac20SDavid S. Miller if (up->pending) { 578db8dac20SDavid S. Miller /* 579db8dac20SDavid S. Miller * There are pending frames. 580db8dac20SDavid S. Miller * The socket lock must be held while it's corked. 581db8dac20SDavid S. Miller */ 582db8dac20SDavid S. Miller lock_sock(sk); 583db8dac20SDavid S. Miller if (likely(up->pending)) { 584db8dac20SDavid S. Miller if (unlikely(up->pending != AF_INET)) { 585db8dac20SDavid S. Miller release_sock(sk); 586db8dac20SDavid S. Miller return -EINVAL; 587db8dac20SDavid S. Miller } 588db8dac20SDavid S. Miller goto do_append_data; 589db8dac20SDavid S. Miller } 590db8dac20SDavid S. Miller release_sock(sk); 591db8dac20SDavid S. Miller } 592db8dac20SDavid S. Miller ulen += sizeof(struct udphdr); 593db8dac20SDavid S. Miller 594db8dac20SDavid S. Miller /* 595db8dac20SDavid S. Miller * Get and verify the address. 596db8dac20SDavid S. Miller */ 597db8dac20SDavid S. Miller if (msg->msg_name) { 598db8dac20SDavid S. Miller struct sockaddr_in * usin = (struct sockaddr_in*)msg->msg_name; 599db8dac20SDavid S. Miller if (msg->msg_namelen < sizeof(*usin)) 600db8dac20SDavid S. Miller return -EINVAL; 601db8dac20SDavid S. Miller if (usin->sin_family != AF_INET) { 602db8dac20SDavid S. Miller if (usin->sin_family != AF_UNSPEC) 603db8dac20SDavid S. Miller return -EAFNOSUPPORT; 604db8dac20SDavid S. Miller } 605db8dac20SDavid S. Miller 606db8dac20SDavid S. Miller daddr = usin->sin_addr.s_addr; 607db8dac20SDavid S. Miller dport = usin->sin_port; 608db8dac20SDavid S. Miller if (dport == 0) 609db8dac20SDavid S. Miller return -EINVAL; 610db8dac20SDavid S. Miller } else { 611db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 612db8dac20SDavid S. Miller return -EDESTADDRREQ; 613db8dac20SDavid S. Miller daddr = inet->daddr; 614db8dac20SDavid S. Miller dport = inet->dport; 615db8dac20SDavid S. Miller /* Open fast path for connected socket. 616db8dac20SDavid S. Miller Route will not be used, if at least one option is set. 617db8dac20SDavid S. Miller */ 618db8dac20SDavid S. Miller connected = 1; 619db8dac20SDavid S. Miller } 620db8dac20SDavid S. Miller ipc.addr = inet->saddr; 621db8dac20SDavid S. Miller 622db8dac20SDavid S. Miller ipc.oif = sk->sk_bound_dev_if; 623db8dac20SDavid S. Miller if (msg->msg_controllen) { 6243b1e0a65SYOSHIFUJI Hideaki err = ip_cmsg_send(sock_net(sk), msg, &ipc); 625db8dac20SDavid S. Miller if (err) 626db8dac20SDavid S. Miller return err; 627db8dac20SDavid S. Miller if (ipc.opt) 628db8dac20SDavid S. Miller free = 1; 629db8dac20SDavid S. Miller connected = 0; 630db8dac20SDavid S. Miller } 631db8dac20SDavid S. Miller if (!ipc.opt) 632db8dac20SDavid S. Miller ipc.opt = inet->opt; 633db8dac20SDavid S. Miller 634db8dac20SDavid S. Miller saddr = ipc.addr; 635db8dac20SDavid S. Miller ipc.addr = faddr = daddr; 636db8dac20SDavid S. Miller 637db8dac20SDavid S. Miller if (ipc.opt && ipc.opt->srr) { 638db8dac20SDavid S. Miller if (!daddr) 639db8dac20SDavid S. Miller return -EINVAL; 640db8dac20SDavid S. Miller faddr = ipc.opt->faddr; 641db8dac20SDavid S. Miller connected = 0; 642db8dac20SDavid S. Miller } 643db8dac20SDavid S. Miller tos = RT_TOS(inet->tos); 644db8dac20SDavid S. Miller if (sock_flag(sk, SOCK_LOCALROUTE) || 645db8dac20SDavid S. Miller (msg->msg_flags & MSG_DONTROUTE) || 646db8dac20SDavid S. Miller (ipc.opt && ipc.opt->is_strictroute)) { 647db8dac20SDavid S. Miller tos |= RTO_ONLINK; 648db8dac20SDavid S. Miller connected = 0; 649db8dac20SDavid S. Miller } 650db8dac20SDavid S. Miller 651db8dac20SDavid S. Miller if (ipv4_is_multicast(daddr)) { 652db8dac20SDavid S. Miller if (!ipc.oif) 653db8dac20SDavid S. Miller ipc.oif = inet->mc_index; 654db8dac20SDavid S. Miller if (!saddr) 655db8dac20SDavid S. Miller saddr = inet->mc_addr; 656db8dac20SDavid S. Miller connected = 0; 657db8dac20SDavid S. Miller } 658db8dac20SDavid S. Miller 659db8dac20SDavid S. Miller if (connected) 660db8dac20SDavid S. Miller rt = (struct rtable*)sk_dst_check(sk, 0); 661db8dac20SDavid S. Miller 662db8dac20SDavid S. Miller if (rt == NULL) { 663db8dac20SDavid S. Miller struct flowi fl = { .oif = ipc.oif, 664db8dac20SDavid S. Miller .nl_u = { .ip4_u = 665db8dac20SDavid S. Miller { .daddr = faddr, 666db8dac20SDavid S. Miller .saddr = saddr, 667db8dac20SDavid S. Miller .tos = tos } }, 668db8dac20SDavid S. Miller .proto = sk->sk_protocol, 669a134f85cSBalazs Scheidler .flags = inet_sk_flowi_flags(sk), 670db8dac20SDavid S. Miller .uli_u = { .ports = 671db8dac20SDavid S. Miller { .sport = inet->sport, 672db8dac20SDavid S. Miller .dport = dport } } }; 67384a3aa00SPavel Emelyanov struct net *net = sock_net(sk); 67484a3aa00SPavel Emelyanov 675db8dac20SDavid S. Miller security_sk_classify_flow(sk, &fl); 67684a3aa00SPavel Emelyanov err = ip_route_output_flow(net, &rt, &fl, sk, 1); 677db8dac20SDavid S. Miller if (err) { 678db8dac20SDavid S. Miller if (err == -ENETUNREACH) 6797c73a6faSPavel Emelyanov IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES); 680db8dac20SDavid S. Miller goto out; 681db8dac20SDavid S. Miller } 682db8dac20SDavid S. Miller 683db8dac20SDavid S. Miller err = -EACCES; 684db8dac20SDavid S. Miller if ((rt->rt_flags & RTCF_BROADCAST) && 685db8dac20SDavid S. Miller !sock_flag(sk, SOCK_BROADCAST)) 686db8dac20SDavid S. Miller goto out; 687db8dac20SDavid S. Miller if (connected) 688db8dac20SDavid S. Miller sk_dst_set(sk, dst_clone(&rt->u.dst)); 689db8dac20SDavid S. Miller } 690db8dac20SDavid S. Miller 691db8dac20SDavid S. Miller if (msg->msg_flags&MSG_CONFIRM) 692db8dac20SDavid S. Miller goto do_confirm; 693db8dac20SDavid S. Miller back_from_confirm: 694db8dac20SDavid S. Miller 695db8dac20SDavid S. Miller saddr = rt->rt_src; 696db8dac20SDavid S. Miller if (!ipc.addr) 697db8dac20SDavid S. Miller daddr = ipc.addr = rt->rt_dst; 698db8dac20SDavid S. Miller 699db8dac20SDavid S. Miller lock_sock(sk); 700db8dac20SDavid S. Miller if (unlikely(up->pending)) { 701db8dac20SDavid S. Miller /* The socket is already corked while preparing it. */ 702db8dac20SDavid S. Miller /* ... which is an evident application bug. --ANK */ 703db8dac20SDavid S. Miller release_sock(sk); 704db8dac20SDavid S. Miller 705db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n"); 706db8dac20SDavid S. Miller err = -EINVAL; 707db8dac20SDavid S. Miller goto out; 708db8dac20SDavid S. Miller } 709db8dac20SDavid S. Miller /* 710db8dac20SDavid S. Miller * Now cork the socket to pend data. 711db8dac20SDavid S. Miller */ 712db8dac20SDavid S. Miller inet->cork.fl.fl4_dst = daddr; 713db8dac20SDavid S. Miller inet->cork.fl.fl_ip_dport = dport; 714db8dac20SDavid S. Miller inet->cork.fl.fl4_src = saddr; 715db8dac20SDavid S. Miller inet->cork.fl.fl_ip_sport = inet->sport; 716db8dac20SDavid S. Miller up->pending = AF_INET; 717db8dac20SDavid S. Miller 718db8dac20SDavid S. Miller do_append_data: 719db8dac20SDavid S. Miller up->len += ulen; 720db8dac20SDavid S. Miller getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 721db8dac20SDavid S. Miller err = ip_append_data(sk, getfrag, msg->msg_iov, ulen, 7222e77d89bSEric Dumazet sizeof(struct udphdr), &ipc, &rt, 723db8dac20SDavid S. Miller corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); 724db8dac20SDavid S. Miller if (err) 725db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 726db8dac20SDavid S. Miller else if (!corkreq) 727db8dac20SDavid S. Miller err = udp_push_pending_frames(sk); 728db8dac20SDavid S. Miller else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 729db8dac20SDavid S. Miller up->pending = 0; 730db8dac20SDavid S. Miller release_sock(sk); 731db8dac20SDavid S. Miller 732db8dac20SDavid S. Miller out: 733db8dac20SDavid S. Miller ip_rt_put(rt); 734db8dac20SDavid S. Miller if (free) 735db8dac20SDavid S. Miller kfree(ipc.opt); 736db8dac20SDavid S. Miller if (!err) 737db8dac20SDavid S. Miller return len; 738db8dac20SDavid S. Miller /* 739db8dac20SDavid S. Miller * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 740db8dac20SDavid S. Miller * ENOBUFS might not be good (it's not tunable per se), but otherwise 741db8dac20SDavid S. Miller * we don't have a good statistic (IpOutDiscards but it can be too many 742db8dac20SDavid S. Miller * things). We could add another new stat but at least for now that 743db8dac20SDavid S. Miller * seems like overkill. 744db8dac20SDavid S. Miller */ 745db8dac20SDavid S. Miller if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 746629ca23cSPavel Emelyanov UDP_INC_STATS_USER(sock_net(sk), 747629ca23cSPavel Emelyanov UDP_MIB_SNDBUFERRORS, is_udplite); 748db8dac20SDavid S. Miller } 749db8dac20SDavid S. Miller return err; 750db8dac20SDavid S. Miller 751db8dac20SDavid S. Miller do_confirm: 752db8dac20SDavid S. Miller dst_confirm(&rt->u.dst); 753db8dac20SDavid S. Miller if (!(msg->msg_flags&MSG_PROBE) || len) 754db8dac20SDavid S. Miller goto back_from_confirm; 755db8dac20SDavid S. Miller err = 0; 756db8dac20SDavid S. Miller goto out; 757db8dac20SDavid S. Miller } 758db8dac20SDavid S. Miller 759db8dac20SDavid S. Miller int udp_sendpage(struct sock *sk, struct page *page, int offset, 760db8dac20SDavid S. Miller size_t size, int flags) 761db8dac20SDavid S. Miller { 762db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 763db8dac20SDavid S. Miller int ret; 764db8dac20SDavid S. Miller 765db8dac20SDavid S. Miller if (!up->pending) { 766db8dac20SDavid S. Miller struct msghdr msg = { .msg_flags = flags|MSG_MORE }; 767db8dac20SDavid S. Miller 768db8dac20SDavid S. Miller /* Call udp_sendmsg to specify destination address which 769db8dac20SDavid S. Miller * sendpage interface can't pass. 770db8dac20SDavid S. Miller * This will succeed only when the socket is connected. 771db8dac20SDavid S. Miller */ 772db8dac20SDavid S. Miller ret = udp_sendmsg(NULL, sk, &msg, 0); 773db8dac20SDavid S. Miller if (ret < 0) 774db8dac20SDavid S. Miller return ret; 775db8dac20SDavid S. Miller } 776db8dac20SDavid S. Miller 777db8dac20SDavid S. Miller lock_sock(sk); 778db8dac20SDavid S. Miller 779db8dac20SDavid S. Miller if (unlikely(!up->pending)) { 780db8dac20SDavid S. Miller release_sock(sk); 781db8dac20SDavid S. Miller 782db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n"); 783db8dac20SDavid S. Miller return -EINVAL; 784db8dac20SDavid S. Miller } 785db8dac20SDavid S. Miller 786db8dac20SDavid S. Miller ret = ip_append_page(sk, page, offset, size, flags); 787db8dac20SDavid S. Miller if (ret == -EOPNOTSUPP) { 788db8dac20SDavid S. Miller release_sock(sk); 789db8dac20SDavid S. Miller return sock_no_sendpage(sk->sk_socket, page, offset, 790db8dac20SDavid S. Miller size, flags); 791db8dac20SDavid S. Miller } 792db8dac20SDavid S. Miller if (ret < 0) { 793db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 794db8dac20SDavid S. Miller goto out; 795db8dac20SDavid S. Miller } 796db8dac20SDavid S. Miller 797db8dac20SDavid S. Miller up->len += size; 798db8dac20SDavid S. Miller if (!(up->corkflag || (flags&MSG_MORE))) 799db8dac20SDavid S. Miller ret = udp_push_pending_frames(sk); 800db8dac20SDavid S. Miller if (!ret) 801db8dac20SDavid S. Miller ret = size; 802db8dac20SDavid S. Miller out: 803db8dac20SDavid S. Miller release_sock(sk); 804db8dac20SDavid S. Miller return ret; 805db8dac20SDavid S. Miller } 806db8dac20SDavid S. Miller 8071da177e4SLinus Torvalds /* 8081da177e4SLinus Torvalds * IOCTL requests applicable to the UDP protocol 8091da177e4SLinus Torvalds */ 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds int udp_ioctl(struct sock *sk, int cmd, unsigned long arg) 8121da177e4SLinus Torvalds { 8136516c655SStephen Hemminger switch (cmd) { 8141da177e4SLinus Torvalds case SIOCOUTQ: 8151da177e4SLinus Torvalds { 8161da177e4SLinus Torvalds int amount = atomic_read(&sk->sk_wmem_alloc); 8171da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 8181da177e4SLinus Torvalds } 8191da177e4SLinus Torvalds 8201da177e4SLinus Torvalds case SIOCINQ: 8211da177e4SLinus Torvalds { 8221da177e4SLinus Torvalds struct sk_buff *skb; 8231da177e4SLinus Torvalds unsigned long amount; 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds amount = 0; 826208d8984SHerbert Xu spin_lock_bh(&sk->sk_receive_queue.lock); 8271da177e4SLinus Torvalds skb = skb_peek(&sk->sk_receive_queue); 8281da177e4SLinus Torvalds if (skb != NULL) { 8291da177e4SLinus Torvalds /* 8301da177e4SLinus Torvalds * We will only return the amount 8311da177e4SLinus Torvalds * of this packet since that is all 8321da177e4SLinus Torvalds * that will be read. 8331da177e4SLinus Torvalds */ 8341da177e4SLinus Torvalds amount = skb->len - sizeof(struct udphdr); 8351da177e4SLinus Torvalds } 836208d8984SHerbert Xu spin_unlock_bh(&sk->sk_receive_queue.lock); 8371da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 8381da177e4SLinus Torvalds } 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds default: 8411da177e4SLinus Torvalds return -ENOIOCTLCMD; 8421da177e4SLinus Torvalds } 8436516c655SStephen Hemminger 8446516c655SStephen Hemminger return 0; 8451da177e4SLinus Torvalds } 8461da177e4SLinus Torvalds 847db8dac20SDavid S. Miller /* 848db8dac20SDavid S. Miller * This should be easy, if there is something there we 849db8dac20SDavid S. Miller * return it, otherwise we block. 850db8dac20SDavid S. Miller */ 851db8dac20SDavid S. Miller 852db8dac20SDavid S. Miller int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 853db8dac20SDavid S. Miller size_t len, int noblock, int flags, int *addr_len) 854db8dac20SDavid S. Miller { 855db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 856db8dac20SDavid S. Miller struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name; 857db8dac20SDavid S. Miller struct sk_buff *skb; 858db8dac20SDavid S. Miller unsigned int ulen, copied; 859db8dac20SDavid S. Miller int peeked; 860db8dac20SDavid S. Miller int err; 861db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 862db8dac20SDavid S. Miller 863db8dac20SDavid S. Miller /* 864db8dac20SDavid S. Miller * Check any passed addresses 865db8dac20SDavid S. Miller */ 866db8dac20SDavid S. Miller if (addr_len) 867db8dac20SDavid S. Miller *addr_len=sizeof(*sin); 868db8dac20SDavid S. Miller 869db8dac20SDavid S. Miller if (flags & MSG_ERRQUEUE) 870db8dac20SDavid S. Miller return ip_recv_error(sk, msg, len); 871db8dac20SDavid S. Miller 872db8dac20SDavid S. Miller try_again: 873db8dac20SDavid S. Miller skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), 874db8dac20SDavid S. Miller &peeked, &err); 875db8dac20SDavid S. Miller if (!skb) 876db8dac20SDavid S. Miller goto out; 877db8dac20SDavid S. Miller 878db8dac20SDavid S. Miller ulen = skb->len - sizeof(struct udphdr); 879db8dac20SDavid S. Miller copied = len; 880db8dac20SDavid S. Miller if (copied > ulen) 881db8dac20SDavid S. Miller copied = ulen; 882db8dac20SDavid S. Miller else if (copied < ulen) 883db8dac20SDavid S. Miller msg->msg_flags |= MSG_TRUNC; 884db8dac20SDavid S. Miller 885db8dac20SDavid S. Miller /* 886db8dac20SDavid S. Miller * If checksum is needed at all, try to do it while copying the 887db8dac20SDavid S. Miller * data. If the data is truncated, or if we only want a partial 888db8dac20SDavid S. Miller * coverage checksum (UDP-Lite), do it before the copy. 889db8dac20SDavid S. Miller */ 890db8dac20SDavid S. Miller 891db8dac20SDavid S. Miller if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) { 892db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 893db8dac20SDavid S. Miller goto csum_copy_err; 894db8dac20SDavid S. Miller } 895db8dac20SDavid S. Miller 896db8dac20SDavid S. Miller if (skb_csum_unnecessary(skb)) 897db8dac20SDavid S. Miller err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), 898db8dac20SDavid S. Miller msg->msg_iov, copied ); 899db8dac20SDavid S. Miller else { 900db8dac20SDavid S. Miller err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov); 901db8dac20SDavid S. Miller 902db8dac20SDavid S. Miller if (err == -EINVAL) 903db8dac20SDavid S. Miller goto csum_copy_err; 904db8dac20SDavid S. Miller } 905db8dac20SDavid S. Miller 906db8dac20SDavid S. Miller if (err) 907db8dac20SDavid S. Miller goto out_free; 908db8dac20SDavid S. Miller 909db8dac20SDavid S. Miller if (!peeked) 910629ca23cSPavel Emelyanov UDP_INC_STATS_USER(sock_net(sk), 911629ca23cSPavel Emelyanov UDP_MIB_INDATAGRAMS, is_udplite); 912db8dac20SDavid S. Miller 913db8dac20SDavid S. Miller sock_recv_timestamp(msg, sk, skb); 914db8dac20SDavid S. Miller 915db8dac20SDavid S. Miller /* Copy the address. */ 916db8dac20SDavid S. Miller if (sin) 917db8dac20SDavid S. Miller { 918db8dac20SDavid S. Miller sin->sin_family = AF_INET; 919db8dac20SDavid S. Miller sin->sin_port = udp_hdr(skb)->source; 920db8dac20SDavid S. Miller sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 921db8dac20SDavid S. Miller memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 922db8dac20SDavid S. Miller } 923db8dac20SDavid S. Miller if (inet->cmsg_flags) 924db8dac20SDavid S. Miller ip_cmsg_recv(msg, skb); 925db8dac20SDavid S. Miller 926db8dac20SDavid S. Miller err = copied; 927db8dac20SDavid S. Miller if (flags & MSG_TRUNC) 928db8dac20SDavid S. Miller err = ulen; 929db8dac20SDavid S. Miller 930db8dac20SDavid S. Miller out_free: 931db8dac20SDavid S. Miller lock_sock(sk); 932db8dac20SDavid S. Miller skb_free_datagram(sk, skb); 933db8dac20SDavid S. Miller release_sock(sk); 934db8dac20SDavid S. Miller out: 935db8dac20SDavid S. Miller return err; 936db8dac20SDavid S. Miller 937db8dac20SDavid S. Miller csum_copy_err: 938db8dac20SDavid S. Miller lock_sock(sk); 939db8dac20SDavid S. Miller if (!skb_kill_datagram(sk, skb, flags)) 940629ca23cSPavel Emelyanov UDP_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 941db8dac20SDavid S. Miller release_sock(sk); 942db8dac20SDavid S. Miller 943db8dac20SDavid S. Miller if (noblock) 944db8dac20SDavid S. Miller return -EAGAIN; 945db8dac20SDavid S. Miller goto try_again; 946db8dac20SDavid S. Miller } 947db8dac20SDavid S. Miller 948db8dac20SDavid S. Miller 9491da177e4SLinus Torvalds int udp_disconnect(struct sock *sk, int flags) 9501da177e4SLinus Torvalds { 9511da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 9521da177e4SLinus Torvalds /* 9531da177e4SLinus Torvalds * 1003.1g - break association. 9541da177e4SLinus Torvalds */ 9551da177e4SLinus Torvalds 9561da177e4SLinus Torvalds sk->sk_state = TCP_CLOSE; 9571da177e4SLinus Torvalds inet->daddr = 0; 9581da177e4SLinus Torvalds inet->dport = 0; 9591da177e4SLinus Torvalds sk->sk_bound_dev_if = 0; 9601da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) 9611da177e4SLinus Torvalds inet_reset_saddr(sk); 9621da177e4SLinus Torvalds 9631da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) { 9641da177e4SLinus Torvalds sk->sk_prot->unhash(sk); 9651da177e4SLinus Torvalds inet->sport = 0; 9661da177e4SLinus Torvalds } 9671da177e4SLinus Torvalds sk_dst_reset(sk); 9681da177e4SLinus Torvalds return 0; 9691da177e4SLinus Torvalds } 9701da177e4SLinus Torvalds 971645ca708SEric Dumazet void udp_lib_unhash(struct sock *sk) 972645ca708SEric Dumazet { 973*723b4610SEric Dumazet if (sk_hashed(sk)) { 974645ca708SEric Dumazet struct udp_table *udptable = sk->sk_prot->h.udp_table; 975645ca708SEric Dumazet unsigned int hash = udp_hashfn(sock_net(sk), sk->sk_hash); 976645ca708SEric Dumazet struct udp_hslot *hslot = &udptable->hash[hash]; 977645ca708SEric Dumazet 978c8db3fecSEric Dumazet spin_lock_bh(&hslot->lock); 97988ab1932SEric Dumazet if (sk_nulls_del_node_init_rcu(sk)) { 980645ca708SEric Dumazet inet_sk(sk)->num = 0; 981645ca708SEric Dumazet sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 982645ca708SEric Dumazet } 983c8db3fecSEric Dumazet spin_unlock_bh(&hslot->lock); 984645ca708SEric Dumazet } 985*723b4610SEric Dumazet } 986645ca708SEric Dumazet EXPORT_SYMBOL(udp_lib_unhash); 987645ca708SEric Dumazet 98893821778SHerbert Xu static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 98993821778SHerbert Xu { 99093821778SHerbert Xu int is_udplite = IS_UDPLITE(sk); 99193821778SHerbert Xu int rc; 99293821778SHerbert Xu 99393821778SHerbert Xu if ((rc = sock_queue_rcv_skb(sk, skb)) < 0) { 99493821778SHerbert Xu /* Note that an ENOMEM error is charged twice */ 99593821778SHerbert Xu if (rc == -ENOMEM) 99693821778SHerbert Xu UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS, 99793821778SHerbert Xu is_udplite); 99893821778SHerbert Xu goto drop; 99993821778SHerbert Xu } 100093821778SHerbert Xu 100193821778SHerbert Xu return 0; 100293821778SHerbert Xu 100393821778SHerbert Xu drop: 100493821778SHerbert Xu UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 100593821778SHerbert Xu kfree_skb(skb); 100693821778SHerbert Xu return -1; 100793821778SHerbert Xu } 100893821778SHerbert Xu 1009db8dac20SDavid S. Miller /* returns: 1010db8dac20SDavid S. Miller * -1: error 1011db8dac20SDavid S. Miller * 0: success 1012db8dac20SDavid S. Miller * >0: "udp encap" protocol resubmission 1013db8dac20SDavid S. Miller * 1014db8dac20SDavid S. Miller * Note that in the success and error cases, the skb is assumed to 1015db8dac20SDavid S. Miller * have either been requeued or freed. 1016db8dac20SDavid S. Miller */ 1017db8dac20SDavid S. Miller int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb) 1018db8dac20SDavid S. Miller { 1019db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 1020db8dac20SDavid S. Miller int rc; 1021db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 1022db8dac20SDavid S. Miller 1023db8dac20SDavid S. Miller /* 1024db8dac20SDavid S. Miller * Charge it to the socket, dropping if the queue is full. 1025db8dac20SDavid S. Miller */ 1026db8dac20SDavid S. Miller if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) 1027db8dac20SDavid S. Miller goto drop; 1028db8dac20SDavid S. Miller nf_reset(skb); 1029db8dac20SDavid S. Miller 1030db8dac20SDavid S. Miller if (up->encap_type) { 1031db8dac20SDavid S. Miller /* 1032db8dac20SDavid S. Miller * This is an encapsulation socket so pass the skb to 1033db8dac20SDavid S. Miller * the socket's udp_encap_rcv() hook. Otherwise, just 1034db8dac20SDavid S. Miller * fall through and pass this up the UDP socket. 1035db8dac20SDavid S. Miller * up->encap_rcv() returns the following value: 1036db8dac20SDavid S. Miller * =0 if skb was successfully passed to the encap 1037db8dac20SDavid S. Miller * handler or was discarded by it. 1038db8dac20SDavid S. Miller * >0 if skb should be passed on to UDP. 1039db8dac20SDavid S. Miller * <0 if skb should be resubmitted as proto -N 1040db8dac20SDavid S. Miller */ 1041db8dac20SDavid S. Miller 1042db8dac20SDavid S. Miller /* if we're overly short, let UDP handle it */ 1043db8dac20SDavid S. Miller if (skb->len > sizeof(struct udphdr) && 1044db8dac20SDavid S. Miller up->encap_rcv != NULL) { 1045db8dac20SDavid S. Miller int ret; 1046db8dac20SDavid S. Miller 1047db8dac20SDavid S. Miller ret = (*up->encap_rcv)(sk, skb); 1048db8dac20SDavid S. Miller if (ret <= 0) { 10490283328eSPavel Emelyanov UDP_INC_STATS_BH(sock_net(sk), 10500283328eSPavel Emelyanov UDP_MIB_INDATAGRAMS, 1051db8dac20SDavid S. Miller is_udplite); 1052db8dac20SDavid S. Miller return -ret; 1053db8dac20SDavid S. Miller } 1054db8dac20SDavid S. Miller } 1055db8dac20SDavid S. Miller 1056db8dac20SDavid S. Miller /* FALLTHROUGH -- it's a UDP Packet */ 1057db8dac20SDavid S. Miller } 1058db8dac20SDavid S. Miller 1059db8dac20SDavid S. Miller /* 1060db8dac20SDavid S. Miller * UDP-Lite specific tests, ignored on UDP sockets 1061db8dac20SDavid S. Miller */ 1062db8dac20SDavid S. Miller if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 1063db8dac20SDavid S. Miller 1064db8dac20SDavid S. Miller /* 1065db8dac20SDavid S. Miller * MIB statistics other than incrementing the error count are 1066db8dac20SDavid S. Miller * disabled for the following two types of errors: these depend 1067db8dac20SDavid S. Miller * on the application settings, not on the functioning of the 1068db8dac20SDavid S. Miller * protocol stack as such. 1069db8dac20SDavid S. Miller * 1070db8dac20SDavid S. Miller * RFC 3828 here recommends (sec 3.3): "There should also be a 1071db8dac20SDavid S. Miller * way ... to ... at least let the receiving application block 1072db8dac20SDavid S. Miller * delivery of packets with coverage values less than a value 1073db8dac20SDavid S. Miller * provided by the application." 1074db8dac20SDavid S. Miller */ 1075db8dac20SDavid S. Miller if (up->pcrlen == 0) { /* full coverage was set */ 1076db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_WARNING "UDPLITE: partial coverage " 1077db8dac20SDavid S. Miller "%d while full coverage %d requested\n", 1078db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, skb->len); 1079db8dac20SDavid S. Miller goto drop; 1080db8dac20SDavid S. Miller } 1081db8dac20SDavid S. Miller /* The next case involves violating the min. coverage requested 1082db8dac20SDavid S. Miller * by the receiver. This is subtle: if receiver wants x and x is 1083db8dac20SDavid S. Miller * greater than the buffersize/MTU then receiver will complain 1084db8dac20SDavid S. Miller * that it wants x while sender emits packets of smaller size y. 1085db8dac20SDavid S. Miller * Therefore the above ...()->partial_cov statement is essential. 1086db8dac20SDavid S. Miller */ 1087db8dac20SDavid S. Miller if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 1088db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_WARNING 1089db8dac20SDavid S. Miller "UDPLITE: coverage %d too small, need min %d\n", 1090db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, up->pcrlen); 1091db8dac20SDavid S. Miller goto drop; 1092db8dac20SDavid S. Miller } 1093db8dac20SDavid S. Miller } 1094db8dac20SDavid S. Miller 1095db8dac20SDavid S. Miller if (sk->sk_filter) { 1096db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 1097db8dac20SDavid S. Miller goto drop; 1098db8dac20SDavid S. Miller } 1099db8dac20SDavid S. Miller 110093821778SHerbert Xu rc = 0; 1101db8dac20SDavid S. Miller 110293821778SHerbert Xu bh_lock_sock(sk); 110393821778SHerbert Xu if (!sock_owned_by_user(sk)) 110493821778SHerbert Xu rc = __udp_queue_rcv_skb(sk, skb); 110593821778SHerbert Xu else 110693821778SHerbert Xu sk_add_backlog(sk, skb); 110793821778SHerbert Xu bh_unlock_sock(sk); 110893821778SHerbert Xu 110993821778SHerbert Xu return rc; 1110db8dac20SDavid S. Miller 1111db8dac20SDavid S. Miller drop: 11120283328eSPavel Emelyanov UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 1113db8dac20SDavid S. Miller kfree_skb(skb); 1114db8dac20SDavid S. Miller return -1; 1115db8dac20SDavid S. Miller } 1116db8dac20SDavid S. Miller 1117db8dac20SDavid S. Miller /* 1118db8dac20SDavid S. Miller * Multicasts and broadcasts go to each listener. 1119db8dac20SDavid S. Miller * 1120db8dac20SDavid S. Miller * Note: called only from the BH handler context, 1121db8dac20SDavid S. Miller * so we don't need to lock the hashes. 1122db8dac20SDavid S. Miller */ 1123e3163493SPavel Emelyanov static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb, 1124db8dac20SDavid S. Miller struct udphdr *uh, 1125db8dac20SDavid S. Miller __be32 saddr, __be32 daddr, 1126645ca708SEric Dumazet struct udp_table *udptable) 1127db8dac20SDavid S. Miller { 1128db8dac20SDavid S. Miller struct sock *sk; 1129645ca708SEric Dumazet struct udp_hslot *hslot = &udptable->hash[udp_hashfn(net, ntohs(uh->dest))]; 1130db8dac20SDavid S. Miller int dif; 1131db8dac20SDavid S. Miller 1132645ca708SEric Dumazet spin_lock(&hslot->lock); 113388ab1932SEric Dumazet sk = sk_nulls_head(&hslot->head); 1134db8dac20SDavid S. Miller dif = skb->dev->ifindex; 1135920a4611SEric Dumazet sk = udp_v4_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif); 1136db8dac20SDavid S. Miller if (sk) { 1137db8dac20SDavid S. Miller struct sock *sknext = NULL; 1138db8dac20SDavid S. Miller 1139db8dac20SDavid S. Miller do { 1140db8dac20SDavid S. Miller struct sk_buff *skb1 = skb; 1141db8dac20SDavid S. Miller 114288ab1932SEric Dumazet sknext = udp_v4_mcast_next(net, sk_nulls_next(sk), uh->dest, 1143920a4611SEric Dumazet daddr, uh->source, saddr, 1144920a4611SEric Dumazet dif); 1145db8dac20SDavid S. Miller if (sknext) 1146db8dac20SDavid S. Miller skb1 = skb_clone(skb, GFP_ATOMIC); 1147db8dac20SDavid S. Miller 1148db8dac20SDavid S. Miller if (skb1) { 114993821778SHerbert Xu int ret = udp_queue_rcv_skb(sk, skb1); 1150db8dac20SDavid S. Miller if (ret > 0) 1151db8dac20SDavid S. Miller /* we should probably re-process instead 1152db8dac20SDavid S. Miller * of dropping packets here. */ 1153db8dac20SDavid S. Miller kfree_skb(skb1); 1154db8dac20SDavid S. Miller } 1155db8dac20SDavid S. Miller sk = sknext; 1156db8dac20SDavid S. Miller } while (sknext); 1157db8dac20SDavid S. Miller } else 1158db8dac20SDavid S. Miller kfree_skb(skb); 1159645ca708SEric Dumazet spin_unlock(&hslot->lock); 1160db8dac20SDavid S. Miller return 0; 1161db8dac20SDavid S. Miller } 1162db8dac20SDavid S. Miller 1163db8dac20SDavid S. Miller /* Initialize UDP checksum. If exited with zero value (success), 1164db8dac20SDavid S. Miller * CHECKSUM_UNNECESSARY means, that no more checks are required. 1165db8dac20SDavid S. Miller * Otherwise, csum completion requires chacksumming packet body, 1166db8dac20SDavid S. Miller * including udp header and folding it to skb->csum. 1167db8dac20SDavid S. Miller */ 1168db8dac20SDavid S. Miller static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh, 1169db8dac20SDavid S. Miller int proto) 1170db8dac20SDavid S. Miller { 1171db8dac20SDavid S. Miller const struct iphdr *iph; 1172db8dac20SDavid S. Miller int err; 1173db8dac20SDavid S. Miller 1174db8dac20SDavid S. Miller UDP_SKB_CB(skb)->partial_cov = 0; 1175db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov = skb->len; 1176db8dac20SDavid S. Miller 1177db8dac20SDavid S. Miller if (proto == IPPROTO_UDPLITE) { 1178db8dac20SDavid S. Miller err = udplite_checksum_init(skb, uh); 1179db8dac20SDavid S. Miller if (err) 1180db8dac20SDavid S. Miller return err; 1181db8dac20SDavid S. Miller } 1182db8dac20SDavid S. Miller 1183db8dac20SDavid S. Miller iph = ip_hdr(skb); 1184db8dac20SDavid S. Miller if (uh->check == 0) { 1185db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_UNNECESSARY; 1186db8dac20SDavid S. Miller } else if (skb->ip_summed == CHECKSUM_COMPLETE) { 1187db8dac20SDavid S. Miller if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len, 1188db8dac20SDavid S. Miller proto, skb->csum)) 1189db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_UNNECESSARY; 1190db8dac20SDavid S. Miller } 1191db8dac20SDavid S. Miller if (!skb_csum_unnecessary(skb)) 1192db8dac20SDavid S. Miller skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, 1193db8dac20SDavid S. Miller skb->len, proto, 0); 1194db8dac20SDavid S. Miller /* Probably, we should checksum udp header (it should be in cache 1195db8dac20SDavid S. Miller * in any case) and data in tiny packets (< rx copybreak). 1196db8dac20SDavid S. Miller */ 1197db8dac20SDavid S. Miller 1198db8dac20SDavid S. Miller return 0; 1199db8dac20SDavid S. Miller } 1200db8dac20SDavid S. Miller 1201db8dac20SDavid S. Miller /* 1202db8dac20SDavid S. Miller * All we need to do is get the socket, and then do a checksum. 1203db8dac20SDavid S. Miller */ 1204db8dac20SDavid S. Miller 1205645ca708SEric Dumazet int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable, 1206db8dac20SDavid S. Miller int proto) 1207db8dac20SDavid S. Miller { 1208db8dac20SDavid S. Miller struct sock *sk; 1209db8dac20SDavid S. Miller struct udphdr *uh = udp_hdr(skb); 1210db8dac20SDavid S. Miller unsigned short ulen; 1211db8dac20SDavid S. Miller struct rtable *rt = (struct rtable*)skb->dst; 1212db8dac20SDavid S. Miller __be32 saddr = ip_hdr(skb)->saddr; 1213db8dac20SDavid S. Miller __be32 daddr = ip_hdr(skb)->daddr; 12140283328eSPavel Emelyanov struct net *net = dev_net(skb->dev); 1215db8dac20SDavid S. Miller 1216db8dac20SDavid S. Miller /* 1217db8dac20SDavid S. Miller * Validate the packet. 1218db8dac20SDavid S. Miller */ 1219db8dac20SDavid S. Miller if (!pskb_may_pull(skb, sizeof(struct udphdr))) 1220db8dac20SDavid S. Miller goto drop; /* No space for header. */ 1221db8dac20SDavid S. Miller 1222db8dac20SDavid S. Miller ulen = ntohs(uh->len); 1223db8dac20SDavid S. Miller if (ulen > skb->len) 1224db8dac20SDavid S. Miller goto short_packet; 1225db8dac20SDavid S. Miller 1226db8dac20SDavid S. Miller if (proto == IPPROTO_UDP) { 1227db8dac20SDavid S. Miller /* UDP validates ulen. */ 1228db8dac20SDavid S. Miller if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen)) 1229db8dac20SDavid S. Miller goto short_packet; 1230db8dac20SDavid S. Miller uh = udp_hdr(skb); 1231db8dac20SDavid S. Miller } 1232db8dac20SDavid S. Miller 1233db8dac20SDavid S. Miller if (udp4_csum_init(skb, uh, proto)) 1234db8dac20SDavid S. Miller goto csum_error; 1235db8dac20SDavid S. Miller 1236db8dac20SDavid S. Miller if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST)) 1237e3163493SPavel Emelyanov return __udp4_lib_mcast_deliver(net, skb, uh, 1238e3163493SPavel Emelyanov saddr, daddr, udptable); 1239db8dac20SDavid S. Miller 1240607c4aafSKOVACS Krisztian sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable); 1241db8dac20SDavid S. Miller 1242db8dac20SDavid S. Miller if (sk != NULL) { 124393821778SHerbert Xu int ret = udp_queue_rcv_skb(sk, skb); 1244db8dac20SDavid S. Miller sock_put(sk); 1245db8dac20SDavid S. Miller 1246db8dac20SDavid S. Miller /* a return value > 0 means to resubmit the input, but 1247db8dac20SDavid S. Miller * it wants the return to be -protocol, or 0 1248db8dac20SDavid S. Miller */ 1249db8dac20SDavid S. Miller if (ret > 0) 1250db8dac20SDavid S. Miller return -ret; 1251db8dac20SDavid S. Miller return 0; 1252db8dac20SDavid S. Miller } 1253db8dac20SDavid S. Miller 1254db8dac20SDavid S. Miller if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) 1255db8dac20SDavid S. Miller goto drop; 1256db8dac20SDavid S. Miller nf_reset(skb); 1257db8dac20SDavid S. Miller 1258db8dac20SDavid S. Miller /* No socket. Drop packet silently, if checksum is wrong */ 1259db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 1260db8dac20SDavid S. Miller goto csum_error; 1261db8dac20SDavid S. Miller 12620283328eSPavel Emelyanov UDP_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); 1263db8dac20SDavid S. Miller icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); 1264db8dac20SDavid S. Miller 1265db8dac20SDavid S. Miller /* 1266db8dac20SDavid S. Miller * Hmm. We got an UDP packet to a port to which we 1267db8dac20SDavid S. Miller * don't wanna listen. Ignore it. 1268db8dac20SDavid S. Miller */ 1269db8dac20SDavid S. Miller kfree_skb(skb); 1270db8dac20SDavid S. Miller return 0; 1271db8dac20SDavid S. Miller 1272db8dac20SDavid S. Miller short_packet: 1273673d57e7SHarvey Harrison LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n", 1274db8dac20SDavid S. Miller proto == IPPROTO_UDPLITE ? "-Lite" : "", 1275673d57e7SHarvey Harrison &saddr, 1276db8dac20SDavid S. Miller ntohs(uh->source), 1277db8dac20SDavid S. Miller ulen, 1278db8dac20SDavid S. Miller skb->len, 1279673d57e7SHarvey Harrison &daddr, 1280db8dac20SDavid S. Miller ntohs(uh->dest)); 1281db8dac20SDavid S. Miller goto drop; 1282db8dac20SDavid S. Miller 1283db8dac20SDavid S. Miller csum_error: 1284db8dac20SDavid S. Miller /* 1285db8dac20SDavid S. Miller * RFC1122: OK. Discards the bad packet silently (as far as 1286db8dac20SDavid S. Miller * the network is concerned, anyway) as per 4.1.3.4 (MUST). 1287db8dac20SDavid S. Miller */ 1288673d57e7SHarvey Harrison LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n", 1289db8dac20SDavid S. Miller proto == IPPROTO_UDPLITE ? "-Lite" : "", 1290673d57e7SHarvey Harrison &saddr, 1291db8dac20SDavid S. Miller ntohs(uh->source), 1292673d57e7SHarvey Harrison &daddr, 1293db8dac20SDavid S. Miller ntohs(uh->dest), 1294db8dac20SDavid S. Miller ulen); 1295db8dac20SDavid S. Miller drop: 12960283328eSPavel Emelyanov UDP_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 1297db8dac20SDavid S. Miller kfree_skb(skb); 1298db8dac20SDavid S. Miller return 0; 1299db8dac20SDavid S. Miller } 1300db8dac20SDavid S. Miller 1301db8dac20SDavid S. Miller int udp_rcv(struct sk_buff *skb) 1302db8dac20SDavid S. Miller { 1303645ca708SEric Dumazet return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP); 1304db8dac20SDavid S. Miller } 1305db8dac20SDavid S. Miller 13067d06b2e0SBrian Haley void udp_destroy_sock(struct sock *sk) 1307db8dac20SDavid S. Miller { 1308db8dac20SDavid S. Miller lock_sock(sk); 1309db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 1310db8dac20SDavid S. Miller release_sock(sk); 1311db8dac20SDavid S. Miller } 1312db8dac20SDavid S. Miller 13131da177e4SLinus Torvalds /* 13141da177e4SLinus Torvalds * Socket option code for UDP 13151da177e4SLinus Torvalds */ 13164c0a6cb0SGerrit Renker int udp_lib_setsockopt(struct sock *sk, int level, int optname, 13174c0a6cb0SGerrit Renker char __user *optval, int optlen, 13184c0a6cb0SGerrit Renker int (*push_pending_frames)(struct sock *)) 13191da177e4SLinus Torvalds { 13201da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 13211da177e4SLinus Torvalds int val; 13221da177e4SLinus Torvalds int err = 0; 1323b2bf1e26SWang Chen int is_udplite = IS_UDPLITE(sk); 13241da177e4SLinus Torvalds 13251da177e4SLinus Torvalds if (optlen<sizeof(int)) 13261da177e4SLinus Torvalds return -EINVAL; 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds if (get_user(val, (int __user *)optval)) 13291da177e4SLinus Torvalds return -EFAULT; 13301da177e4SLinus Torvalds 13311da177e4SLinus Torvalds switch (optname) { 13321da177e4SLinus Torvalds case UDP_CORK: 13331da177e4SLinus Torvalds if (val != 0) { 13341da177e4SLinus Torvalds up->corkflag = 1; 13351da177e4SLinus Torvalds } else { 13361da177e4SLinus Torvalds up->corkflag = 0; 13371da177e4SLinus Torvalds lock_sock(sk); 13384c0a6cb0SGerrit Renker (*push_pending_frames)(sk); 13391da177e4SLinus Torvalds release_sock(sk); 13401da177e4SLinus Torvalds } 13411da177e4SLinus Torvalds break; 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds case UDP_ENCAP: 13441da177e4SLinus Torvalds switch (val) { 13451da177e4SLinus Torvalds case 0: 13461da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP: 13471da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP_NON_IKE: 1348067b207bSJames Chapman up->encap_rcv = xfrm4_udp_encap_rcv; 1349067b207bSJames Chapman /* FALLTHROUGH */ 1350342f0234SJames Chapman case UDP_ENCAP_L2TPINUDP: 13511da177e4SLinus Torvalds up->encap_type = val; 13521da177e4SLinus Torvalds break; 13531da177e4SLinus Torvalds default: 13541da177e4SLinus Torvalds err = -ENOPROTOOPT; 13551da177e4SLinus Torvalds break; 13561da177e4SLinus Torvalds } 13571da177e4SLinus Torvalds break; 13581da177e4SLinus Torvalds 1359ba4e58ecSGerrit Renker /* 1360ba4e58ecSGerrit Renker * UDP-Lite's partial checksum coverage (RFC 3828). 1361ba4e58ecSGerrit Renker */ 1362ba4e58ecSGerrit Renker /* The sender sets actual checksum coverage length via this option. 1363ba4e58ecSGerrit Renker * The case coverage > packet length is handled by send module. */ 1364ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 1365b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 1366ba4e58ecSGerrit Renker return -ENOPROTOOPT; 1367ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Illegal coverage: use default (8) */ 1368ba4e58ecSGerrit Renker val = 8; 136947112e25SGerrit Renker else if (val > USHORT_MAX) 137047112e25SGerrit Renker val = USHORT_MAX; 1371ba4e58ecSGerrit Renker up->pcslen = val; 1372ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_SEND_CC; 1373ba4e58ecSGerrit Renker break; 1374ba4e58ecSGerrit Renker 1375ba4e58ecSGerrit Renker /* The receiver specifies a minimum checksum coverage value. To make 1376ba4e58ecSGerrit Renker * sense, this should be set to at least 8 (as done below). If zero is 1377ba4e58ecSGerrit Renker * used, this again means full checksum coverage. */ 1378ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 1379b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 1380ba4e58ecSGerrit Renker return -ENOPROTOOPT; 1381ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Avoid silly minimal values. */ 1382ba4e58ecSGerrit Renker val = 8; 138347112e25SGerrit Renker else if (val > USHORT_MAX) 138447112e25SGerrit Renker val = USHORT_MAX; 1385ba4e58ecSGerrit Renker up->pcrlen = val; 1386ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_RECV_CC; 1387ba4e58ecSGerrit Renker break; 1388ba4e58ecSGerrit Renker 13891da177e4SLinus Torvalds default: 13901da177e4SLinus Torvalds err = -ENOPROTOOPT; 13911da177e4SLinus Torvalds break; 13926516c655SStephen Hemminger } 13931da177e4SLinus Torvalds 13941da177e4SLinus Torvalds return err; 13951da177e4SLinus Torvalds } 13961da177e4SLinus Torvalds 1397db8dac20SDavid S. Miller int udp_setsockopt(struct sock *sk, int level, int optname, 1398db8dac20SDavid S. Miller char __user *optval, int optlen) 1399db8dac20SDavid S. Miller { 1400db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1401db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1402db8dac20SDavid S. Miller udp_push_pending_frames); 1403db8dac20SDavid S. Miller return ip_setsockopt(sk, level, optname, optval, optlen); 1404db8dac20SDavid S. Miller } 1405db8dac20SDavid S. Miller 1406db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1407db8dac20SDavid S. Miller int compat_udp_setsockopt(struct sock *sk, int level, int optname, 1408db8dac20SDavid S. Miller char __user *optval, int optlen) 1409db8dac20SDavid S. Miller { 1410db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1411db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1412db8dac20SDavid S. Miller udp_push_pending_frames); 1413db8dac20SDavid S. Miller return compat_ip_setsockopt(sk, level, optname, optval, optlen); 1414db8dac20SDavid S. Miller } 1415db8dac20SDavid S. Miller #endif 1416db8dac20SDavid S. Miller 14174c0a6cb0SGerrit Renker int udp_lib_getsockopt(struct sock *sk, int level, int optname, 14181da177e4SLinus Torvalds char __user *optval, int __user *optlen) 14191da177e4SLinus Torvalds { 14201da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 14211da177e4SLinus Torvalds int val, len; 14221da177e4SLinus Torvalds 14231da177e4SLinus Torvalds if (get_user(len,optlen)) 14241da177e4SLinus Torvalds return -EFAULT; 14251da177e4SLinus Torvalds 14261da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(int)); 14271da177e4SLinus Torvalds 14281da177e4SLinus Torvalds if (len < 0) 14291da177e4SLinus Torvalds return -EINVAL; 14301da177e4SLinus Torvalds 14311da177e4SLinus Torvalds switch (optname) { 14321da177e4SLinus Torvalds case UDP_CORK: 14331da177e4SLinus Torvalds val = up->corkflag; 14341da177e4SLinus Torvalds break; 14351da177e4SLinus Torvalds 14361da177e4SLinus Torvalds case UDP_ENCAP: 14371da177e4SLinus Torvalds val = up->encap_type; 14381da177e4SLinus Torvalds break; 14391da177e4SLinus Torvalds 1440ba4e58ecSGerrit Renker /* The following two cannot be changed on UDP sockets, the return is 1441ba4e58ecSGerrit Renker * always 0 (which corresponds to the full checksum coverage of UDP). */ 1442ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 1443ba4e58ecSGerrit Renker val = up->pcslen; 1444ba4e58ecSGerrit Renker break; 1445ba4e58ecSGerrit Renker 1446ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 1447ba4e58ecSGerrit Renker val = up->pcrlen; 1448ba4e58ecSGerrit Renker break; 1449ba4e58ecSGerrit Renker 14501da177e4SLinus Torvalds default: 14511da177e4SLinus Torvalds return -ENOPROTOOPT; 14526516c655SStephen Hemminger } 14531da177e4SLinus Torvalds 14541da177e4SLinus Torvalds if (put_user(len, optlen)) 14551da177e4SLinus Torvalds return -EFAULT; 14561da177e4SLinus Torvalds if (copy_to_user(optval, &val,len)) 14571da177e4SLinus Torvalds return -EFAULT; 14581da177e4SLinus Torvalds return 0; 14591da177e4SLinus Torvalds } 14601da177e4SLinus Torvalds 1461db8dac20SDavid S. Miller int udp_getsockopt(struct sock *sk, int level, int optname, 1462db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 1463db8dac20SDavid S. Miller { 1464db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1465db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1466db8dac20SDavid S. Miller return ip_getsockopt(sk, level, optname, optval, optlen); 1467db8dac20SDavid S. Miller } 1468db8dac20SDavid S. Miller 1469db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1470db8dac20SDavid S. Miller int compat_udp_getsockopt(struct sock *sk, int level, int optname, 1471db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 1472db8dac20SDavid S. Miller { 1473db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1474db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1475db8dac20SDavid S. Miller return compat_ip_getsockopt(sk, level, optname, optval, optlen); 1476db8dac20SDavid S. Miller } 1477db8dac20SDavid S. Miller #endif 14781da177e4SLinus Torvalds /** 14791da177e4SLinus Torvalds * udp_poll - wait for a UDP event. 14801da177e4SLinus Torvalds * @file - file struct 14811da177e4SLinus Torvalds * @sock - socket 14821da177e4SLinus Torvalds * @wait - poll table 14831da177e4SLinus Torvalds * 14841da177e4SLinus Torvalds * This is same as datagram poll, except for the special case of 14851da177e4SLinus Torvalds * blocking sockets. If application is using a blocking fd 14861da177e4SLinus Torvalds * and a packet with checksum error is in the queue; 14871da177e4SLinus Torvalds * then it could get return from select indicating data available 14881da177e4SLinus Torvalds * but then block when reading it. Add special case code 14891da177e4SLinus Torvalds * to work around these arguably broken applications. 14901da177e4SLinus Torvalds */ 14911da177e4SLinus Torvalds unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait) 14921da177e4SLinus Torvalds { 14931da177e4SLinus Torvalds unsigned int mask = datagram_poll(file, sock, wait); 14941da177e4SLinus Torvalds struct sock *sk = sock->sk; 1495ba4e58ecSGerrit Renker int is_lite = IS_UDPLITE(sk); 14961da177e4SLinus Torvalds 14971da177e4SLinus Torvalds /* Check for false positives due to checksum errors */ 14981da177e4SLinus Torvalds if ( (mask & POLLRDNORM) && 14991da177e4SLinus Torvalds !(file->f_flags & O_NONBLOCK) && 15001da177e4SLinus Torvalds !(sk->sk_shutdown & RCV_SHUTDOWN)){ 15011da177e4SLinus Torvalds struct sk_buff_head *rcvq = &sk->sk_receive_queue; 15021da177e4SLinus Torvalds struct sk_buff *skb; 15031da177e4SLinus Torvalds 1504208d8984SHerbert Xu spin_lock_bh(&rcvq->lock); 1505759e5d00SHerbert Xu while ((skb = skb_peek(rcvq)) != NULL && 1506759e5d00SHerbert Xu udp_lib_checksum_complete(skb)) { 15070283328eSPavel Emelyanov UDP_INC_STATS_BH(sock_net(sk), 15080283328eSPavel Emelyanov UDP_MIB_INERRORS, is_lite); 15091da177e4SLinus Torvalds __skb_unlink(skb, rcvq); 15101da177e4SLinus Torvalds kfree_skb(skb); 15111da177e4SLinus Torvalds } 1512208d8984SHerbert Xu spin_unlock_bh(&rcvq->lock); 15131da177e4SLinus Torvalds 15141da177e4SLinus Torvalds /* nothing to see, move along */ 15151da177e4SLinus Torvalds if (skb == NULL) 15161da177e4SLinus Torvalds mask &= ~(POLLIN | POLLRDNORM); 15171da177e4SLinus Torvalds } 15181da177e4SLinus Torvalds 15191da177e4SLinus Torvalds return mask; 15201da177e4SLinus Torvalds 15211da177e4SLinus Torvalds } 15221da177e4SLinus Torvalds 1523db8dac20SDavid S. Miller struct proto udp_prot = { 1524db8dac20SDavid S. Miller .name = "UDP", 1525db8dac20SDavid S. Miller .owner = THIS_MODULE, 1526db8dac20SDavid S. Miller .close = udp_lib_close, 1527db8dac20SDavid S. Miller .connect = ip4_datagram_connect, 1528db8dac20SDavid S. Miller .disconnect = udp_disconnect, 1529db8dac20SDavid S. Miller .ioctl = udp_ioctl, 1530db8dac20SDavid S. Miller .destroy = udp_destroy_sock, 1531db8dac20SDavid S. Miller .setsockopt = udp_setsockopt, 1532db8dac20SDavid S. Miller .getsockopt = udp_getsockopt, 1533db8dac20SDavid S. Miller .sendmsg = udp_sendmsg, 1534db8dac20SDavid S. Miller .recvmsg = udp_recvmsg, 1535db8dac20SDavid S. Miller .sendpage = udp_sendpage, 153693821778SHerbert Xu .backlog_rcv = __udp_queue_rcv_skb, 1537db8dac20SDavid S. Miller .hash = udp_lib_hash, 1538db8dac20SDavid S. Miller .unhash = udp_lib_unhash, 1539db8dac20SDavid S. Miller .get_port = udp_v4_get_port, 1540db8dac20SDavid S. Miller .memory_allocated = &udp_memory_allocated, 1541db8dac20SDavid S. Miller .sysctl_mem = sysctl_udp_mem, 1542db8dac20SDavid S. Miller .sysctl_wmem = &sysctl_udp_wmem_min, 1543db8dac20SDavid S. Miller .sysctl_rmem = &sysctl_udp_rmem_min, 1544db8dac20SDavid S. Miller .obj_size = sizeof(struct udp_sock), 1545271b72c7SEric Dumazet .slab_flags = SLAB_DESTROY_BY_RCU, 1546645ca708SEric Dumazet .h.udp_table = &udp_table, 1547db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1548db8dac20SDavid S. Miller .compat_setsockopt = compat_udp_setsockopt, 1549db8dac20SDavid S. Miller .compat_getsockopt = compat_udp_getsockopt, 1550db8dac20SDavid S. Miller #endif 1551db8dac20SDavid S. Miller }; 15521da177e4SLinus Torvalds 15531da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 15541da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 15551da177e4SLinus Torvalds 1556645ca708SEric Dumazet static struct sock *udp_get_first(struct seq_file *seq, int start) 15571da177e4SLinus Torvalds { 15581da177e4SLinus Torvalds struct sock *sk; 15591da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 15606f191efeSDenis V. Lunev struct net *net = seq_file_net(seq); 15611da177e4SLinus Torvalds 1562645ca708SEric Dumazet for (state->bucket = start; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) { 156388ab1932SEric Dumazet struct hlist_nulls_node *node; 1564645ca708SEric Dumazet struct udp_hslot *hslot = &state->udp_table->hash[state->bucket]; 1565645ca708SEric Dumazet spin_lock_bh(&hslot->lock); 156688ab1932SEric Dumazet sk_nulls_for_each(sk, node, &hslot->head) { 1567878628fbSYOSHIFUJI Hideaki if (!net_eq(sock_net(sk), net)) 1568a91275efSDaniel Lezcano continue; 15691da177e4SLinus Torvalds if (sk->sk_family == state->family) 15701da177e4SLinus Torvalds goto found; 15711da177e4SLinus Torvalds } 1572645ca708SEric Dumazet spin_unlock_bh(&hslot->lock); 15731da177e4SLinus Torvalds } 15741da177e4SLinus Torvalds sk = NULL; 15751da177e4SLinus Torvalds found: 15761da177e4SLinus Torvalds return sk; 15771da177e4SLinus Torvalds } 15781da177e4SLinus Torvalds 15791da177e4SLinus Torvalds static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk) 15801da177e4SLinus Torvalds { 15811da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 15826f191efeSDenis V. Lunev struct net *net = seq_file_net(seq); 15831da177e4SLinus Torvalds 15841da177e4SLinus Torvalds do { 158588ab1932SEric Dumazet sk = sk_nulls_next(sk); 1586878628fbSYOSHIFUJI Hideaki } while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family)); 15871da177e4SLinus Torvalds 1588645ca708SEric Dumazet if (!sk) { 1589f52b5054SEric Dumazet spin_unlock_bh(&state->udp_table->hash[state->bucket].lock); 1590645ca708SEric Dumazet return udp_get_first(seq, state->bucket + 1); 15911da177e4SLinus Torvalds } 15921da177e4SLinus Torvalds return sk; 15931da177e4SLinus Torvalds } 15941da177e4SLinus Torvalds 15951da177e4SLinus Torvalds static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos) 15961da177e4SLinus Torvalds { 1597645ca708SEric Dumazet struct sock *sk = udp_get_first(seq, 0); 15981da177e4SLinus Torvalds 15991da177e4SLinus Torvalds if (sk) 16001da177e4SLinus Torvalds while (pos && (sk = udp_get_next(seq, sk)) != NULL) 16011da177e4SLinus Torvalds --pos; 16021da177e4SLinus Torvalds return pos ? NULL : sk; 16031da177e4SLinus Torvalds } 16041da177e4SLinus Torvalds 16051da177e4SLinus Torvalds static void *udp_seq_start(struct seq_file *seq, loff_t *pos) 16061da177e4SLinus Torvalds { 1607b50660f1SYOSHIFUJI Hideaki return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN; 16081da177e4SLinus Torvalds } 16091da177e4SLinus Torvalds 16101da177e4SLinus Torvalds static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos) 16111da177e4SLinus Torvalds { 16121da177e4SLinus Torvalds struct sock *sk; 16131da177e4SLinus Torvalds 1614b50660f1SYOSHIFUJI Hideaki if (v == SEQ_START_TOKEN) 16151da177e4SLinus Torvalds sk = udp_get_idx(seq, 0); 16161da177e4SLinus Torvalds else 16171da177e4SLinus Torvalds sk = udp_get_next(seq, v); 16181da177e4SLinus Torvalds 16191da177e4SLinus Torvalds ++*pos; 16201da177e4SLinus Torvalds return sk; 16211da177e4SLinus Torvalds } 16221da177e4SLinus Torvalds 16231da177e4SLinus Torvalds static void udp_seq_stop(struct seq_file *seq, void *v) 16241da177e4SLinus Torvalds { 1625645ca708SEric Dumazet struct udp_iter_state *state = seq->private; 1626645ca708SEric Dumazet 1627645ca708SEric Dumazet if (state->bucket < UDP_HTABLE_SIZE) 1628645ca708SEric Dumazet spin_unlock_bh(&state->udp_table->hash[state->bucket].lock); 16291da177e4SLinus Torvalds } 16301da177e4SLinus Torvalds 16311da177e4SLinus Torvalds static int udp_seq_open(struct inode *inode, struct file *file) 16321da177e4SLinus Torvalds { 16331da177e4SLinus Torvalds struct udp_seq_afinfo *afinfo = PDE(inode)->data; 1634a2be75c1SDenis V. Lunev struct udp_iter_state *s; 1635a2be75c1SDenis V. Lunev int err; 16361da177e4SLinus Torvalds 1637a2be75c1SDenis V. Lunev err = seq_open_net(inode, file, &afinfo->seq_ops, 1638a2be75c1SDenis V. Lunev sizeof(struct udp_iter_state)); 1639a2be75c1SDenis V. Lunev if (err < 0) 1640a2be75c1SDenis V. Lunev return err; 1641a91275efSDaniel Lezcano 1642a2be75c1SDenis V. Lunev s = ((struct seq_file *)file->private_data)->private; 16431da177e4SLinus Torvalds s->family = afinfo->family; 1644645ca708SEric Dumazet s->udp_table = afinfo->udp_table; 1645a2be75c1SDenis V. Lunev return err; 1646a91275efSDaniel Lezcano } 1647a91275efSDaniel Lezcano 16481da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 16490c96d8c5SDaniel Lezcano int udp_proc_register(struct net *net, struct udp_seq_afinfo *afinfo) 16501da177e4SLinus Torvalds { 16511da177e4SLinus Torvalds struct proc_dir_entry *p; 16521da177e4SLinus Torvalds int rc = 0; 16531da177e4SLinus Torvalds 16543ba9441bSDenis V. Lunev afinfo->seq_fops.open = udp_seq_open; 16553ba9441bSDenis V. Lunev afinfo->seq_fops.read = seq_read; 16563ba9441bSDenis V. Lunev afinfo->seq_fops.llseek = seq_lseek; 16573ba9441bSDenis V. Lunev afinfo->seq_fops.release = seq_release_net; 16581da177e4SLinus Torvalds 1659dda61925SDenis V. Lunev afinfo->seq_ops.start = udp_seq_start; 1660dda61925SDenis V. Lunev afinfo->seq_ops.next = udp_seq_next; 1661dda61925SDenis V. Lunev afinfo->seq_ops.stop = udp_seq_stop; 1662dda61925SDenis V. Lunev 166384841c3cSDenis V. Lunev p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net, 166484841c3cSDenis V. Lunev &afinfo->seq_fops, afinfo); 166584841c3cSDenis V. Lunev if (!p) 16661da177e4SLinus Torvalds rc = -ENOMEM; 16671da177e4SLinus Torvalds return rc; 16681da177e4SLinus Torvalds } 16691da177e4SLinus Torvalds 16700c96d8c5SDaniel Lezcano void udp_proc_unregister(struct net *net, struct udp_seq_afinfo *afinfo) 16711da177e4SLinus Torvalds { 16720c96d8c5SDaniel Lezcano proc_net_remove(net, afinfo->name); 16731da177e4SLinus Torvalds } 1674db8dac20SDavid S. Miller 1675db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 16765e659e4cSPavel Emelyanov static void udp4_format_sock(struct sock *sp, struct seq_file *f, 16775e659e4cSPavel Emelyanov int bucket, int *len) 1678db8dac20SDavid S. Miller { 1679db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sp); 1680db8dac20SDavid S. Miller __be32 dest = inet->daddr; 1681db8dac20SDavid S. Miller __be32 src = inet->rcv_saddr; 1682db8dac20SDavid S. Miller __u16 destp = ntohs(inet->dport); 1683db8dac20SDavid S. Miller __u16 srcp = ntohs(inet->sport); 1684db8dac20SDavid S. Miller 16855e659e4cSPavel Emelyanov seq_printf(f, "%4d: %08X:%04X %08X:%04X" 1686cb61cb9bSEric Dumazet " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d%n", 1687db8dac20SDavid S. Miller bucket, src, srcp, dest, destp, sp->sk_state, 1688db8dac20SDavid S. Miller atomic_read(&sp->sk_wmem_alloc), 1689db8dac20SDavid S. Miller atomic_read(&sp->sk_rmem_alloc), 1690db8dac20SDavid S. Miller 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp), 1691cb61cb9bSEric Dumazet atomic_read(&sp->sk_refcnt), sp, 1692cb61cb9bSEric Dumazet atomic_read(&sp->sk_drops), len); 1693db8dac20SDavid S. Miller } 1694db8dac20SDavid S. Miller 1695db8dac20SDavid S. Miller int udp4_seq_show(struct seq_file *seq, void *v) 1696db8dac20SDavid S. Miller { 1697db8dac20SDavid S. Miller if (v == SEQ_START_TOKEN) 1698db8dac20SDavid S. Miller seq_printf(seq, "%-127s\n", 1699db8dac20SDavid S. Miller " sl local_address rem_address st tx_queue " 1700db8dac20SDavid S. Miller "rx_queue tr tm->when retrnsmt uid timeout " 1701cb61cb9bSEric Dumazet "inode ref pointer drops"); 1702db8dac20SDavid S. Miller else { 1703db8dac20SDavid S. Miller struct udp_iter_state *state = seq->private; 17045e659e4cSPavel Emelyanov int len; 1705db8dac20SDavid S. Miller 17065e659e4cSPavel Emelyanov udp4_format_sock(v, seq, state->bucket, &len); 17075e659e4cSPavel Emelyanov seq_printf(seq, "%*s\n", 127 - len ,""); 1708db8dac20SDavid S. Miller } 1709db8dac20SDavid S. Miller return 0; 1710db8dac20SDavid S. Miller } 1711db8dac20SDavid S. Miller 1712db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 1713db8dac20SDavid S. Miller static struct udp_seq_afinfo udp4_seq_afinfo = { 1714db8dac20SDavid S. Miller .name = "udp", 1715db8dac20SDavid S. Miller .family = AF_INET, 1716645ca708SEric Dumazet .udp_table = &udp_table, 17174ad96d39SDenis V. Lunev .seq_fops = { 17184ad96d39SDenis V. Lunev .owner = THIS_MODULE, 17194ad96d39SDenis V. Lunev }, 1720dda61925SDenis V. Lunev .seq_ops = { 1721dda61925SDenis V. Lunev .show = udp4_seq_show, 1722dda61925SDenis V. Lunev }, 1723db8dac20SDavid S. Miller }; 1724db8dac20SDavid S. Miller 172515439febSPavel Emelyanov static int udp4_proc_init_net(struct net *net) 172615439febSPavel Emelyanov { 172715439febSPavel Emelyanov return udp_proc_register(net, &udp4_seq_afinfo); 172815439febSPavel Emelyanov } 172915439febSPavel Emelyanov 173015439febSPavel Emelyanov static void udp4_proc_exit_net(struct net *net) 173115439febSPavel Emelyanov { 173215439febSPavel Emelyanov udp_proc_unregister(net, &udp4_seq_afinfo); 173315439febSPavel Emelyanov } 173415439febSPavel Emelyanov 173515439febSPavel Emelyanov static struct pernet_operations udp4_net_ops = { 173615439febSPavel Emelyanov .init = udp4_proc_init_net, 173715439febSPavel Emelyanov .exit = udp4_proc_exit_net, 173815439febSPavel Emelyanov }; 173915439febSPavel Emelyanov 1740db8dac20SDavid S. Miller int __init udp4_proc_init(void) 1741db8dac20SDavid S. Miller { 174215439febSPavel Emelyanov return register_pernet_subsys(&udp4_net_ops); 1743db8dac20SDavid S. Miller } 1744db8dac20SDavid S. Miller 1745db8dac20SDavid S. Miller void udp4_proc_exit(void) 1746db8dac20SDavid S. Miller { 174715439febSPavel Emelyanov unregister_pernet_subsys(&udp4_net_ops); 1748db8dac20SDavid S. Miller } 17491da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 17501da177e4SLinus Torvalds 1751645ca708SEric Dumazet void __init udp_table_init(struct udp_table *table) 1752645ca708SEric Dumazet { 1753645ca708SEric Dumazet int i; 1754645ca708SEric Dumazet 1755645ca708SEric Dumazet for (i = 0; i < UDP_HTABLE_SIZE; i++) { 175688ab1932SEric Dumazet INIT_HLIST_NULLS_HEAD(&table->hash[i].head, i); 1757645ca708SEric Dumazet spin_lock_init(&table->hash[i].lock); 1758645ca708SEric Dumazet } 1759645ca708SEric Dumazet } 1760645ca708SEric Dumazet 176195766fffSHideo Aoki void __init udp_init(void) 176295766fffSHideo Aoki { 17638203efb3SEric Dumazet unsigned long nr_pages, limit; 176495766fffSHideo Aoki 1765645ca708SEric Dumazet udp_table_init(&udp_table); 176695766fffSHideo Aoki /* Set the pressure threshold up by the same strategy of TCP. It is a 176795766fffSHideo Aoki * fraction of global memory that is up to 1/2 at 256 MB, decreasing 176895766fffSHideo Aoki * toward zero with the amount of memory, with a floor of 128 pages. 176995766fffSHideo Aoki */ 17708203efb3SEric Dumazet nr_pages = totalram_pages - totalhigh_pages; 17718203efb3SEric Dumazet limit = min(nr_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT); 17728203efb3SEric Dumazet limit = (limit * (nr_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11); 177395766fffSHideo Aoki limit = max(limit, 128UL); 177495766fffSHideo Aoki sysctl_udp_mem[0] = limit / 4 * 3; 177595766fffSHideo Aoki sysctl_udp_mem[1] = limit; 177695766fffSHideo Aoki sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2; 177795766fffSHideo Aoki 177895766fffSHideo Aoki sysctl_udp_rmem_min = SK_MEM_QUANTUM; 177995766fffSHideo Aoki sysctl_udp_wmem_min = SK_MEM_QUANTUM; 178095766fffSHideo Aoki } 178195766fffSHideo Aoki 17821da177e4SLinus Torvalds EXPORT_SYMBOL(udp_disconnect); 17831da177e4SLinus Torvalds EXPORT_SYMBOL(udp_ioctl); 1784db8dac20SDavid S. Miller EXPORT_SYMBOL(udp_prot); 1785db8dac20SDavid S. Miller EXPORT_SYMBOL(udp_sendmsg); 17864c0a6cb0SGerrit Renker EXPORT_SYMBOL(udp_lib_getsockopt); 17874c0a6cb0SGerrit Renker EXPORT_SYMBOL(udp_lib_setsockopt); 17881da177e4SLinus Torvalds EXPORT_SYMBOL(udp_poll); 17896ba5a3c5SPavel Emelyanov EXPORT_SYMBOL(udp_lib_get_port); 17901da177e4SLinus Torvalds 17911da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 17921da177e4SLinus Torvalds EXPORT_SYMBOL(udp_proc_register); 17931da177e4SLinus Torvalds EXPORT_SYMBOL(udp_proc_unregister); 17941da177e4SLinus Torvalds #endif 1795