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> 111da177e4SLinus Torvalds * Alan Cox, <Alan.Cox@linux.org> 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> 841da177e4SLinus Torvalds #include <linux/types.h> 851da177e4SLinus Torvalds #include <linux/fcntl.h> 861da177e4SLinus Torvalds #include <linux/module.h> 871da177e4SLinus Torvalds #include <linux/socket.h> 881da177e4SLinus Torvalds #include <linux/sockios.h> 8914c85021SArnaldo Carvalho de Melo #include <linux/igmp.h> 901da177e4SLinus Torvalds #include <linux/in.h> 911da177e4SLinus Torvalds #include <linux/errno.h> 921da177e4SLinus Torvalds #include <linux/timer.h> 931da177e4SLinus Torvalds #include <linux/mm.h> 941da177e4SLinus Torvalds #include <linux/inet.h> 951da177e4SLinus Torvalds #include <linux/netdevice.h> 96c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h> 971da177e4SLinus Torvalds #include <linux/skbuff.h> 981da177e4SLinus Torvalds #include <linux/proc_fs.h> 991da177e4SLinus Torvalds #include <linux/seq_file.h> 100457c4cbcSEric W. Biederman #include <net/net_namespace.h> 1011da177e4SLinus Torvalds #include <net/icmp.h> 1021da177e4SLinus Torvalds #include <net/route.h> 1031da177e4SLinus Torvalds #include <net/checksum.h> 1041da177e4SLinus Torvalds #include <net/xfrm.h> 105ba4e58ecSGerrit Renker #include "udp_impl.h" 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds /* 1081da177e4SLinus Torvalds * Snmp MIB for the UDP layer 1091da177e4SLinus Torvalds */ 1101da177e4SLinus Torvalds 111ba89966cSEric Dumazet DEFINE_SNMP_STAT(struct udp_mib, udp_statistics) __read_mostly; 1121781f7f5SHerbert Xu EXPORT_SYMBOL(udp_statistics); 1131da177e4SLinus Torvalds 1149055e051SHerbert Xu DEFINE_SNMP_STAT(struct udp_mib, udp_stats_in6) __read_mostly; 1159055e051SHerbert Xu EXPORT_SYMBOL(udp_stats_in6); 1169055e051SHerbert Xu 1171da177e4SLinus Torvalds struct hlist_head udp_hash[UDP_HTABLE_SIZE]; 1181da177e4SLinus Torvalds DEFINE_RWLOCK(udp_hash_lock); 1191da177e4SLinus Torvalds 12095766fffSHideo Aoki int sysctl_udp_mem[3] __read_mostly; 12195766fffSHideo Aoki int sysctl_udp_rmem_min __read_mostly; 12295766fffSHideo Aoki int sysctl_udp_wmem_min __read_mostly; 12395766fffSHideo Aoki 12495766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_mem); 12595766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_rmem_min); 12695766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_wmem_min); 12795766fffSHideo Aoki 12895766fffSHideo Aoki atomic_t udp_memory_allocated; 12995766fffSHideo Aoki EXPORT_SYMBOL(udp_memory_allocated); 13095766fffSHideo Aoki 131fa4d3c62SPavel Emelyanov static inline int __udp_lib_lport_inuse(struct net *net, __u16 num, 13232c1da70SStephen Hemminger const struct hlist_head udptable[]) 13325030a7fSGerrit Renker { 13425030a7fSGerrit Renker struct sock *sk; 13525030a7fSGerrit Renker struct hlist_node *node; 13625030a7fSGerrit Renker 137*19c7578fSPavel Emelyanov sk_for_each(sk, node, &udptable[udp_hashfn(net, num)]) 138878628fbSYOSHIFUJI Hideaki if (net_eq(sock_net(sk), net) && sk->sk_hash == num) 139fc038410SDavid S. Miller return 1; 14025030a7fSGerrit Renker return 0; 14125030a7fSGerrit Renker } 14225030a7fSGerrit Renker 14325030a7fSGerrit Renker /** 1446ba5a3c5SPavel Emelyanov * udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6 14525030a7fSGerrit Renker * 14625030a7fSGerrit Renker * @sk: socket struct in question 14725030a7fSGerrit Renker * @snum: port number to look up 148df2bc459SDavid S. Miller * @saddr_comp: AF-dependent comparison of bound local IP addresses 14925030a7fSGerrit Renker */ 1506ba5a3c5SPavel Emelyanov int udp_lib_get_port(struct sock *sk, unsigned short snum, 151df2bc459SDavid S. Miller int (*saddr_comp)(const struct sock *sk1, 152df2bc459SDavid S. Miller const struct sock *sk2 ) ) 1531da177e4SLinus Torvalds { 1546ba5a3c5SPavel Emelyanov struct hlist_head *udptable = sk->sk_prot->h.udp_hash; 1551da177e4SLinus Torvalds struct hlist_node *node; 15625030a7fSGerrit Renker struct hlist_head *head; 1571da177e4SLinus Torvalds struct sock *sk2; 15825030a7fSGerrit Renker int error = 1; 1593b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds write_lock_bh(&udp_hash_lock); 1621da177e4SLinus Torvalds 16332c1da70SStephen Hemminger if (!snum) { 164a25de534SAnton Arapov int i, low, high, remaining; 16532c1da70SStephen Hemminger unsigned rover, best, best_size_so_far; 1661da177e4SLinus Torvalds 167227b60f5SStephen Hemminger inet_get_local_port_range(&low, &high); 168a25de534SAnton Arapov remaining = (high - low) + 1; 169227b60f5SStephen Hemminger 17032c1da70SStephen Hemminger best_size_so_far = UINT_MAX; 171a25de534SAnton Arapov best = rover = net_random() % remaining + low; 17232c1da70SStephen Hemminger 17332c1da70SStephen Hemminger /* 1st pass: look for empty (or shortest) hash chain */ 17432c1da70SStephen Hemminger for (i = 0; i < UDP_HTABLE_SIZE; i++) { 17532c1da70SStephen Hemminger int size = 0; 17632c1da70SStephen Hemminger 177*19c7578fSPavel Emelyanov head = &udptable[udp_hashfn(net, rover)]; 17832c1da70SStephen Hemminger if (hlist_empty(head)) 1791da177e4SLinus Torvalds goto gotit; 18032c1da70SStephen Hemminger 1815c668704SDavid S. Miller sk_for_each(sk2, node, head) { 1825c668704SDavid S. Miller if (++size >= best_size_so_far) 1835c668704SDavid S. Miller goto next; 1845c668704SDavid S. Miller } 1851da177e4SLinus Torvalds best_size_so_far = size; 18632c1da70SStephen Hemminger best = rover; 1875c668704SDavid S. Miller next: 18832c1da70SStephen Hemminger /* fold back if end of range */ 18932c1da70SStephen Hemminger if (++rover > high) 19032c1da70SStephen Hemminger rover = low + ((rover - low) 19132c1da70SStephen Hemminger & (UDP_HTABLE_SIZE - 1)); 19232c1da70SStephen Hemminger 19332c1da70SStephen Hemminger 1941da177e4SLinus Torvalds } 19532c1da70SStephen Hemminger 19632c1da70SStephen Hemminger /* 2nd pass: find hole in shortest hash chain */ 19732c1da70SStephen Hemminger rover = best; 19832c1da70SStephen Hemminger for (i = 0; i < (1 << 16) / UDP_HTABLE_SIZE; i++) { 199fa4d3c62SPavel Emelyanov if (! __udp_lib_lport_inuse(net, rover, udptable)) 20032c1da70SStephen Hemminger goto gotit; 20132c1da70SStephen Hemminger rover += UDP_HTABLE_SIZE; 20232c1da70SStephen Hemminger if (rover > high) 20332c1da70SStephen Hemminger rover = low + ((rover - low) 20432c1da70SStephen Hemminger & (UDP_HTABLE_SIZE - 1)); 2051da177e4SLinus Torvalds } 20632c1da70SStephen Hemminger 20732c1da70SStephen Hemminger 20832c1da70SStephen Hemminger /* All ports in use! */ 2091da177e4SLinus Torvalds goto fail; 21032c1da70SStephen Hemminger 2111da177e4SLinus Torvalds gotit: 21232c1da70SStephen Hemminger snum = rover; 2131da177e4SLinus Torvalds } else { 214*19c7578fSPavel Emelyanov head = &udptable[udp_hashfn(net, snum)]; 2151da177e4SLinus Torvalds 21625030a7fSGerrit Renker sk_for_each(sk2, node, head) 217df2bc459SDavid S. Miller if (sk2->sk_hash == snum && 2181da177e4SLinus Torvalds sk2 != sk && 219878628fbSYOSHIFUJI Hideaki net_eq(sock_net(sk2), net) && 22025030a7fSGerrit Renker (!sk2->sk_reuse || !sk->sk_reuse) && 221df2bc459SDavid S. Miller (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if 222df2bc459SDavid S. Miller || sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 223df2bc459SDavid S. Miller (*saddr_comp)(sk, sk2) ) 2241da177e4SLinus Torvalds goto fail; 2251da177e4SLinus Torvalds } 22632c1da70SStephen Hemminger 22725030a7fSGerrit Renker inet_sk(sk)->num = snum; 228df2bc459SDavid S. Miller sk->sk_hash = snum; 2291da177e4SLinus Torvalds if (sk_unhashed(sk)) { 230*19c7578fSPavel Emelyanov head = &udptable[udp_hashfn(net, snum)]; 23125030a7fSGerrit Renker sk_add_node(sk, head); 232c29a0bc4SPavel Emelyanov sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 2331da177e4SLinus Torvalds } 23425030a7fSGerrit Renker error = 0; 2351da177e4SLinus Torvalds fail: 2361da177e4SLinus Torvalds write_unlock_bh(&udp_hash_lock); 23725030a7fSGerrit Renker return error; 2381da177e4SLinus Torvalds } 2391da177e4SLinus Torvalds 2406ba5a3c5SPavel Emelyanov static int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2) 241db8dac20SDavid S. Miller { 242db8dac20SDavid S. Miller struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2); 243db8dac20SDavid S. Miller 244db8dac20SDavid S. Miller return ( !ipv6_only_sock(sk2) && 245db8dac20SDavid S. Miller (!inet1->rcv_saddr || !inet2->rcv_saddr || 246db8dac20SDavid S. Miller inet1->rcv_saddr == inet2->rcv_saddr )); 247db8dac20SDavid S. Miller } 248db8dac20SDavid S. Miller 2496ba5a3c5SPavel Emelyanov int udp_v4_get_port(struct sock *sk, unsigned short snum) 250db8dac20SDavid S. Miller { 2516ba5a3c5SPavel Emelyanov return udp_lib_get_port(sk, snum, ipv4_rcv_saddr_equal); 252db8dac20SDavid S. Miller } 253db8dac20SDavid S. Miller 254db8dac20SDavid S. Miller /* UDP is nearly always wildcards out the wazoo, it makes no sense to try 255db8dac20SDavid S. Miller * harder than this. -DaveM 256db8dac20SDavid S. Miller */ 257db8dac20SDavid S. Miller static struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, 258db8dac20SDavid S. Miller __be16 sport, __be32 daddr, __be16 dport, 259db8dac20SDavid S. Miller int dif, struct hlist_head udptable[]) 260db8dac20SDavid S. Miller { 261db8dac20SDavid S. Miller struct sock *sk, *result = NULL; 262db8dac20SDavid S. Miller struct hlist_node *node; 263db8dac20SDavid S. Miller unsigned short hnum = ntohs(dport); 264db8dac20SDavid S. Miller int badness = -1; 265db8dac20SDavid S. Miller 266db8dac20SDavid S. Miller read_lock(&udp_hash_lock); 267*19c7578fSPavel Emelyanov sk_for_each(sk, node, &udptable[udp_hashfn(net, hnum)]) { 268db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 269db8dac20SDavid S. Miller 270878628fbSYOSHIFUJI Hideaki if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum && 271db8dac20SDavid S. Miller !ipv6_only_sock(sk)) { 272db8dac20SDavid S. Miller int score = (sk->sk_family == PF_INET ? 1 : 0); 273db8dac20SDavid S. Miller if (inet->rcv_saddr) { 274db8dac20SDavid S. Miller if (inet->rcv_saddr != daddr) 275db8dac20SDavid S. Miller continue; 276db8dac20SDavid S. Miller score+=2; 277db8dac20SDavid S. Miller } 278db8dac20SDavid S. Miller if (inet->daddr) { 279db8dac20SDavid S. Miller if (inet->daddr != saddr) 280db8dac20SDavid S. Miller continue; 281db8dac20SDavid S. Miller score+=2; 282db8dac20SDavid S. Miller } 283db8dac20SDavid S. Miller if (inet->dport) { 284db8dac20SDavid S. Miller if (inet->dport != sport) 285db8dac20SDavid S. Miller continue; 286db8dac20SDavid S. Miller score+=2; 287db8dac20SDavid S. Miller } 288db8dac20SDavid S. Miller if (sk->sk_bound_dev_if) { 289db8dac20SDavid S. Miller if (sk->sk_bound_dev_if != dif) 290db8dac20SDavid S. Miller continue; 291db8dac20SDavid S. Miller score+=2; 292db8dac20SDavid S. Miller } 293db8dac20SDavid S. Miller if (score == 9) { 294db8dac20SDavid S. Miller result = sk; 295db8dac20SDavid S. Miller break; 296db8dac20SDavid S. Miller } else if (score > badness) { 297db8dac20SDavid S. Miller result = sk; 298db8dac20SDavid S. Miller badness = score; 299db8dac20SDavid S. Miller } 300db8dac20SDavid S. Miller } 301db8dac20SDavid S. Miller } 302db8dac20SDavid S. Miller if (result) 303db8dac20SDavid S. Miller sock_hold(result); 304db8dac20SDavid S. Miller read_unlock(&udp_hash_lock); 305db8dac20SDavid S. Miller return result; 306db8dac20SDavid S. Miller } 307db8dac20SDavid S. Miller 308db8dac20SDavid S. Miller static inline struct sock *udp_v4_mcast_next(struct sock *sk, 309db8dac20SDavid S. Miller __be16 loc_port, __be32 loc_addr, 310db8dac20SDavid S. Miller __be16 rmt_port, __be32 rmt_addr, 311db8dac20SDavid S. Miller int dif) 312db8dac20SDavid S. Miller { 313db8dac20SDavid S. Miller struct hlist_node *node; 314db8dac20SDavid S. Miller struct sock *s = sk; 315db8dac20SDavid S. Miller unsigned short hnum = ntohs(loc_port); 316db8dac20SDavid S. Miller 317db8dac20SDavid S. Miller sk_for_each_from(s, node) { 318db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(s); 319db8dac20SDavid S. Miller 320db8dac20SDavid S. Miller if (s->sk_hash != hnum || 321db8dac20SDavid S. Miller (inet->daddr && inet->daddr != rmt_addr) || 322db8dac20SDavid S. Miller (inet->dport != rmt_port && inet->dport) || 323db8dac20SDavid S. Miller (inet->rcv_saddr && inet->rcv_saddr != loc_addr) || 324db8dac20SDavid S. Miller ipv6_only_sock(s) || 325db8dac20SDavid S. Miller (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)) 326db8dac20SDavid S. Miller continue; 327db8dac20SDavid S. Miller if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif)) 328db8dac20SDavid S. Miller continue; 329db8dac20SDavid S. Miller goto found; 330db8dac20SDavid S. Miller } 331db8dac20SDavid S. Miller s = NULL; 332db8dac20SDavid S. Miller found: 333db8dac20SDavid S. Miller return s; 334db8dac20SDavid S. Miller } 335db8dac20SDavid S. Miller 336db8dac20SDavid S. Miller /* 337db8dac20SDavid S. Miller * This routine is called by the ICMP module when it gets some 338db8dac20SDavid S. Miller * sort of error condition. If err < 0 then the socket should 339db8dac20SDavid S. Miller * be closed and the error returned to the user. If err > 0 340db8dac20SDavid S. Miller * it's just the icmp type << 8 | icmp code. 341db8dac20SDavid S. Miller * Header points to the ip header of the error packet. We move 342db8dac20SDavid S. Miller * on past this. Then (as it used to claim before adjustment) 343db8dac20SDavid S. Miller * header points to the first 8 bytes of the udp header. We need 344db8dac20SDavid S. Miller * to find the appropriate port. 345db8dac20SDavid S. Miller */ 346db8dac20SDavid S. Miller 347db8dac20SDavid S. Miller void __udp4_lib_err(struct sk_buff *skb, u32 info, struct hlist_head udptable[]) 348db8dac20SDavid S. Miller { 349db8dac20SDavid S. Miller struct inet_sock *inet; 350db8dac20SDavid S. Miller struct iphdr *iph = (struct iphdr*)skb->data; 351db8dac20SDavid S. Miller struct udphdr *uh = (struct udphdr*)(skb->data+(iph->ihl<<2)); 352db8dac20SDavid S. Miller const int type = icmp_hdr(skb)->type; 353db8dac20SDavid S. Miller const int code = icmp_hdr(skb)->code; 354db8dac20SDavid S. Miller struct sock *sk; 355db8dac20SDavid S. Miller int harderr; 356db8dac20SDavid S. Miller int err; 357db8dac20SDavid S. Miller 358c346dca1SYOSHIFUJI Hideaki sk = __udp4_lib_lookup(dev_net(skb->dev), iph->daddr, uh->dest, 359db8dac20SDavid S. Miller iph->saddr, uh->source, skb->dev->ifindex, udptable); 360db8dac20SDavid S. Miller if (sk == NULL) { 361db8dac20SDavid S. Miller ICMP_INC_STATS_BH(ICMP_MIB_INERRORS); 362db8dac20SDavid S. Miller return; /* No socket for error */ 363db8dac20SDavid S. Miller } 364db8dac20SDavid S. Miller 365db8dac20SDavid S. Miller err = 0; 366db8dac20SDavid S. Miller harderr = 0; 367db8dac20SDavid S. Miller inet = inet_sk(sk); 368db8dac20SDavid S. Miller 369db8dac20SDavid S. Miller switch (type) { 370db8dac20SDavid S. Miller default: 371db8dac20SDavid S. Miller case ICMP_TIME_EXCEEDED: 372db8dac20SDavid S. Miller err = EHOSTUNREACH; 373db8dac20SDavid S. Miller break; 374db8dac20SDavid S. Miller case ICMP_SOURCE_QUENCH: 375db8dac20SDavid S. Miller goto out; 376db8dac20SDavid S. Miller case ICMP_PARAMETERPROB: 377db8dac20SDavid S. Miller err = EPROTO; 378db8dac20SDavid S. Miller harderr = 1; 379db8dac20SDavid S. Miller break; 380db8dac20SDavid S. Miller case ICMP_DEST_UNREACH: 381db8dac20SDavid S. Miller if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */ 382db8dac20SDavid S. Miller if (inet->pmtudisc != IP_PMTUDISC_DONT) { 383db8dac20SDavid S. Miller err = EMSGSIZE; 384db8dac20SDavid S. Miller harderr = 1; 385db8dac20SDavid S. Miller break; 386db8dac20SDavid S. Miller } 387db8dac20SDavid S. Miller goto out; 388db8dac20SDavid S. Miller } 389db8dac20SDavid S. Miller err = EHOSTUNREACH; 390db8dac20SDavid S. Miller if (code <= NR_ICMP_UNREACH) { 391db8dac20SDavid S. Miller harderr = icmp_err_convert[code].fatal; 392db8dac20SDavid S. Miller err = icmp_err_convert[code].errno; 393db8dac20SDavid S. Miller } 394db8dac20SDavid S. Miller break; 395db8dac20SDavid S. Miller } 396db8dac20SDavid S. Miller 397db8dac20SDavid S. Miller /* 398db8dac20SDavid S. Miller * RFC1122: OK. Passes ICMP errors back to application, as per 399db8dac20SDavid S. Miller * 4.1.3.3. 400db8dac20SDavid S. Miller */ 401db8dac20SDavid S. Miller if (!inet->recverr) { 402db8dac20SDavid S. Miller if (!harderr || sk->sk_state != TCP_ESTABLISHED) 403db8dac20SDavid S. Miller goto out; 404db8dac20SDavid S. Miller } else { 405db8dac20SDavid S. Miller ip_icmp_error(sk, skb, err, uh->dest, info, (u8*)(uh+1)); 406db8dac20SDavid S. Miller } 407db8dac20SDavid S. Miller sk->sk_err = err; 408db8dac20SDavid S. Miller sk->sk_error_report(sk); 409db8dac20SDavid S. Miller out: 410db8dac20SDavid S. Miller sock_put(sk); 411db8dac20SDavid S. Miller } 412db8dac20SDavid S. Miller 413db8dac20SDavid S. Miller void udp_err(struct sk_buff *skb, u32 info) 414db8dac20SDavid S. Miller { 415db8dac20SDavid S. Miller __udp4_lib_err(skb, info, udp_hash); 416db8dac20SDavid S. Miller } 417db8dac20SDavid S. Miller 418db8dac20SDavid S. Miller /* 419db8dac20SDavid S. Miller * Throw away all pending data and cancel the corking. Socket is locked. 420db8dac20SDavid S. Miller */ 42136d926b9SDenis V. Lunev void udp_flush_pending_frames(struct sock *sk) 422db8dac20SDavid S. Miller { 423db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 424db8dac20SDavid S. Miller 425db8dac20SDavid S. Miller if (up->pending) { 426db8dac20SDavid S. Miller up->len = 0; 427db8dac20SDavid S. Miller up->pending = 0; 428db8dac20SDavid S. Miller ip_flush_pending_frames(sk); 429db8dac20SDavid S. Miller } 430db8dac20SDavid S. Miller } 43136d926b9SDenis V. Lunev EXPORT_SYMBOL(udp_flush_pending_frames); 432db8dac20SDavid S. Miller 433db8dac20SDavid S. Miller /** 434db8dac20SDavid S. Miller * udp4_hwcsum_outgoing - handle outgoing HW checksumming 435db8dac20SDavid S. Miller * @sk: socket we are sending on 436db8dac20SDavid S. Miller * @skb: sk_buff containing the filled-in UDP header 437db8dac20SDavid S. Miller * (checksum field must be zeroed out) 438db8dac20SDavid S. Miller */ 439db8dac20SDavid S. Miller static void udp4_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb, 440db8dac20SDavid S. Miller __be32 src, __be32 dst, int len ) 441db8dac20SDavid S. Miller { 442db8dac20SDavid S. Miller unsigned int offset; 443db8dac20SDavid S. Miller struct udphdr *uh = udp_hdr(skb); 444db8dac20SDavid S. Miller __wsum csum = 0; 445db8dac20SDavid S. Miller 446db8dac20SDavid S. Miller if (skb_queue_len(&sk->sk_write_queue) == 1) { 447db8dac20SDavid S. Miller /* 448db8dac20SDavid S. Miller * Only one fragment on the socket. 449db8dac20SDavid S. Miller */ 450db8dac20SDavid S. Miller skb->csum_start = skb_transport_header(skb) - skb->head; 451db8dac20SDavid S. Miller skb->csum_offset = offsetof(struct udphdr, check); 452db8dac20SDavid S. Miller uh->check = ~csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, 0); 453db8dac20SDavid S. Miller } else { 454db8dac20SDavid S. Miller /* 455db8dac20SDavid S. Miller * HW-checksum won't work as there are two or more 456db8dac20SDavid S. Miller * fragments on the socket so that all csums of sk_buffs 457db8dac20SDavid S. Miller * should be together 458db8dac20SDavid S. Miller */ 459db8dac20SDavid S. Miller offset = skb_transport_offset(skb); 460db8dac20SDavid S. Miller skb->csum = skb_checksum(skb, offset, skb->len - offset, 0); 461db8dac20SDavid S. Miller 462db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_NONE; 463db8dac20SDavid S. Miller 464db8dac20SDavid S. Miller skb_queue_walk(&sk->sk_write_queue, skb) { 465db8dac20SDavid S. Miller csum = csum_add(csum, skb->csum); 466db8dac20SDavid S. Miller } 467db8dac20SDavid S. Miller 468db8dac20SDavid S. Miller uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum); 469db8dac20SDavid S. Miller if (uh->check == 0) 470db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 471db8dac20SDavid S. Miller } 472db8dac20SDavid S. Miller } 473db8dac20SDavid S. Miller 474db8dac20SDavid S. Miller /* 475db8dac20SDavid S. Miller * Push out all pending data as one UDP datagram. Socket is locked. 476db8dac20SDavid S. Miller */ 477db8dac20SDavid S. Miller static int udp_push_pending_frames(struct sock *sk) 478db8dac20SDavid S. Miller { 479db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 480db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 481db8dac20SDavid S. Miller struct flowi *fl = &inet->cork.fl; 482db8dac20SDavid S. Miller struct sk_buff *skb; 483db8dac20SDavid S. Miller struct udphdr *uh; 484db8dac20SDavid S. Miller int err = 0; 485db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 486db8dac20SDavid S. Miller __wsum csum = 0; 487db8dac20SDavid S. Miller 488db8dac20SDavid S. Miller /* Grab the skbuff where UDP header space exists. */ 489db8dac20SDavid S. Miller if ((skb = skb_peek(&sk->sk_write_queue)) == NULL) 490db8dac20SDavid S. Miller goto out; 491db8dac20SDavid S. Miller 492db8dac20SDavid S. Miller /* 493db8dac20SDavid S. Miller * Create a UDP header 494db8dac20SDavid S. Miller */ 495db8dac20SDavid S. Miller uh = udp_hdr(skb); 496db8dac20SDavid S. Miller uh->source = fl->fl_ip_sport; 497db8dac20SDavid S. Miller uh->dest = fl->fl_ip_dport; 498db8dac20SDavid S. Miller uh->len = htons(up->len); 499db8dac20SDavid S. Miller uh->check = 0; 500db8dac20SDavid S. Miller 501db8dac20SDavid S. Miller if (is_udplite) /* UDP-Lite */ 502db8dac20SDavid S. Miller csum = udplite_csum_outgoing(sk, skb); 503db8dac20SDavid S. Miller 504db8dac20SDavid S. Miller else if (sk->sk_no_check == UDP_CSUM_NOXMIT) { /* UDP csum disabled */ 505db8dac20SDavid S. Miller 506db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_NONE; 507db8dac20SDavid S. Miller goto send; 508db8dac20SDavid S. Miller 509db8dac20SDavid S. Miller } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 510db8dac20SDavid S. Miller 511db8dac20SDavid S. Miller udp4_hwcsum_outgoing(sk, skb, fl->fl4_src,fl->fl4_dst, up->len); 512db8dac20SDavid S. Miller goto send; 513db8dac20SDavid S. Miller 514db8dac20SDavid S. Miller } else /* `normal' UDP */ 515db8dac20SDavid S. Miller csum = udp_csum_outgoing(sk, skb); 516db8dac20SDavid S. Miller 517db8dac20SDavid S. Miller /* add protocol-dependent pseudo-header */ 518db8dac20SDavid S. Miller uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst, up->len, 519db8dac20SDavid S. Miller sk->sk_protocol, csum ); 520db8dac20SDavid S. Miller if (uh->check == 0) 521db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 522db8dac20SDavid S. Miller 523db8dac20SDavid S. Miller send: 524db8dac20SDavid S. Miller err = ip_push_pending_frames(sk); 525db8dac20SDavid S. Miller out: 526db8dac20SDavid S. Miller up->len = 0; 527db8dac20SDavid S. Miller up->pending = 0; 528db8dac20SDavid S. Miller if (!err) 529db8dac20SDavid S. Miller UDP_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS, is_udplite); 530db8dac20SDavid S. Miller return err; 531db8dac20SDavid S. Miller } 532db8dac20SDavid S. Miller 533db8dac20SDavid S. Miller int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 534db8dac20SDavid S. Miller size_t len) 535db8dac20SDavid S. Miller { 536db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 537db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 538db8dac20SDavid S. Miller int ulen = len; 539db8dac20SDavid S. Miller struct ipcm_cookie ipc; 540db8dac20SDavid S. Miller struct rtable *rt = NULL; 541db8dac20SDavid S. Miller int free = 0; 542db8dac20SDavid S. Miller int connected = 0; 543db8dac20SDavid S. Miller __be32 daddr, faddr, saddr; 544db8dac20SDavid S. Miller __be16 dport; 545db8dac20SDavid S. Miller u8 tos; 546db8dac20SDavid S. Miller int err, is_udplite = IS_UDPLITE(sk); 547db8dac20SDavid S. Miller int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; 548db8dac20SDavid S. Miller int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 549db8dac20SDavid S. Miller 550db8dac20SDavid S. Miller if (len > 0xFFFF) 551db8dac20SDavid S. Miller return -EMSGSIZE; 552db8dac20SDavid S. Miller 553db8dac20SDavid S. Miller /* 554db8dac20SDavid S. Miller * Check the flags. 555db8dac20SDavid S. Miller */ 556db8dac20SDavid S. Miller 557db8dac20SDavid S. Miller if (msg->msg_flags&MSG_OOB) /* Mirror BSD error message compatibility */ 558db8dac20SDavid S. Miller return -EOPNOTSUPP; 559db8dac20SDavid S. Miller 560db8dac20SDavid S. Miller ipc.opt = NULL; 561db8dac20SDavid S. Miller 562db8dac20SDavid S. Miller if (up->pending) { 563db8dac20SDavid S. Miller /* 564db8dac20SDavid S. Miller * There are pending frames. 565db8dac20SDavid S. Miller * The socket lock must be held while it's corked. 566db8dac20SDavid S. Miller */ 567db8dac20SDavid S. Miller lock_sock(sk); 568db8dac20SDavid S. Miller if (likely(up->pending)) { 569db8dac20SDavid S. Miller if (unlikely(up->pending != AF_INET)) { 570db8dac20SDavid S. Miller release_sock(sk); 571db8dac20SDavid S. Miller return -EINVAL; 572db8dac20SDavid S. Miller } 573db8dac20SDavid S. Miller goto do_append_data; 574db8dac20SDavid S. Miller } 575db8dac20SDavid S. Miller release_sock(sk); 576db8dac20SDavid S. Miller } 577db8dac20SDavid S. Miller ulen += sizeof(struct udphdr); 578db8dac20SDavid S. Miller 579db8dac20SDavid S. Miller /* 580db8dac20SDavid S. Miller * Get and verify the address. 581db8dac20SDavid S. Miller */ 582db8dac20SDavid S. Miller if (msg->msg_name) { 583db8dac20SDavid S. Miller struct sockaddr_in * usin = (struct sockaddr_in*)msg->msg_name; 584db8dac20SDavid S. Miller if (msg->msg_namelen < sizeof(*usin)) 585db8dac20SDavid S. Miller return -EINVAL; 586db8dac20SDavid S. Miller if (usin->sin_family != AF_INET) { 587db8dac20SDavid S. Miller if (usin->sin_family != AF_UNSPEC) 588db8dac20SDavid S. Miller return -EAFNOSUPPORT; 589db8dac20SDavid S. Miller } 590db8dac20SDavid S. Miller 591db8dac20SDavid S. Miller daddr = usin->sin_addr.s_addr; 592db8dac20SDavid S. Miller dport = usin->sin_port; 593db8dac20SDavid S. Miller if (dport == 0) 594db8dac20SDavid S. Miller return -EINVAL; 595db8dac20SDavid S. Miller } else { 596db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 597db8dac20SDavid S. Miller return -EDESTADDRREQ; 598db8dac20SDavid S. Miller daddr = inet->daddr; 599db8dac20SDavid S. Miller dport = inet->dport; 600db8dac20SDavid S. Miller /* Open fast path for connected socket. 601db8dac20SDavid S. Miller Route will not be used, if at least one option is set. 602db8dac20SDavid S. Miller */ 603db8dac20SDavid S. Miller connected = 1; 604db8dac20SDavid S. Miller } 605db8dac20SDavid S. Miller ipc.addr = inet->saddr; 606db8dac20SDavid S. Miller 607db8dac20SDavid S. Miller ipc.oif = sk->sk_bound_dev_if; 608db8dac20SDavid S. Miller if (msg->msg_controllen) { 6093b1e0a65SYOSHIFUJI Hideaki err = ip_cmsg_send(sock_net(sk), msg, &ipc); 610db8dac20SDavid S. Miller if (err) 611db8dac20SDavid S. Miller return err; 612db8dac20SDavid S. Miller if (ipc.opt) 613db8dac20SDavid S. Miller free = 1; 614db8dac20SDavid S. Miller connected = 0; 615db8dac20SDavid S. Miller } 616db8dac20SDavid S. Miller if (!ipc.opt) 617db8dac20SDavid S. Miller ipc.opt = inet->opt; 618db8dac20SDavid S. Miller 619db8dac20SDavid S. Miller saddr = ipc.addr; 620db8dac20SDavid S. Miller ipc.addr = faddr = daddr; 621db8dac20SDavid S. Miller 622db8dac20SDavid S. Miller if (ipc.opt && ipc.opt->srr) { 623db8dac20SDavid S. Miller if (!daddr) 624db8dac20SDavid S. Miller return -EINVAL; 625db8dac20SDavid S. Miller faddr = ipc.opt->faddr; 626db8dac20SDavid S. Miller connected = 0; 627db8dac20SDavid S. Miller } 628db8dac20SDavid S. Miller tos = RT_TOS(inet->tos); 629db8dac20SDavid S. Miller if (sock_flag(sk, SOCK_LOCALROUTE) || 630db8dac20SDavid S. Miller (msg->msg_flags & MSG_DONTROUTE) || 631db8dac20SDavid S. Miller (ipc.opt && ipc.opt->is_strictroute)) { 632db8dac20SDavid S. Miller tos |= RTO_ONLINK; 633db8dac20SDavid S. Miller connected = 0; 634db8dac20SDavid S. Miller } 635db8dac20SDavid S. Miller 636db8dac20SDavid S. Miller if (ipv4_is_multicast(daddr)) { 637db8dac20SDavid S. Miller if (!ipc.oif) 638db8dac20SDavid S. Miller ipc.oif = inet->mc_index; 639db8dac20SDavid S. Miller if (!saddr) 640db8dac20SDavid S. Miller saddr = inet->mc_addr; 641db8dac20SDavid S. Miller connected = 0; 642db8dac20SDavid S. Miller } 643db8dac20SDavid S. Miller 644db8dac20SDavid S. Miller if (connected) 645db8dac20SDavid S. Miller rt = (struct rtable*)sk_dst_check(sk, 0); 646db8dac20SDavid S. Miller 647db8dac20SDavid S. Miller if (rt == NULL) { 648db8dac20SDavid S. Miller struct flowi fl = { .oif = ipc.oif, 649db8dac20SDavid S. Miller .nl_u = { .ip4_u = 650db8dac20SDavid S. Miller { .daddr = faddr, 651db8dac20SDavid S. Miller .saddr = saddr, 652db8dac20SDavid S. Miller .tos = tos } }, 653db8dac20SDavid S. Miller .proto = sk->sk_protocol, 654db8dac20SDavid S. Miller .uli_u = { .ports = 655db8dac20SDavid S. Miller { .sport = inet->sport, 656db8dac20SDavid S. Miller .dport = dport } } }; 657db8dac20SDavid S. Miller security_sk_classify_flow(sk, &fl); 6583b1e0a65SYOSHIFUJI Hideaki err = ip_route_output_flow(sock_net(sk), &rt, &fl, sk, 1); 659db8dac20SDavid S. Miller if (err) { 660db8dac20SDavid S. Miller if (err == -ENETUNREACH) 661db8dac20SDavid S. Miller IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES); 662db8dac20SDavid S. Miller goto out; 663db8dac20SDavid S. Miller } 664db8dac20SDavid S. Miller 665db8dac20SDavid S. Miller err = -EACCES; 666db8dac20SDavid S. Miller if ((rt->rt_flags & RTCF_BROADCAST) && 667db8dac20SDavid S. Miller !sock_flag(sk, SOCK_BROADCAST)) 668db8dac20SDavid S. Miller goto out; 669db8dac20SDavid S. Miller if (connected) 670db8dac20SDavid S. Miller sk_dst_set(sk, dst_clone(&rt->u.dst)); 671db8dac20SDavid S. Miller } 672db8dac20SDavid S. Miller 673db8dac20SDavid S. Miller if (msg->msg_flags&MSG_CONFIRM) 674db8dac20SDavid S. Miller goto do_confirm; 675db8dac20SDavid S. Miller back_from_confirm: 676db8dac20SDavid S. Miller 677db8dac20SDavid S. Miller saddr = rt->rt_src; 678db8dac20SDavid S. Miller if (!ipc.addr) 679db8dac20SDavid S. Miller daddr = ipc.addr = rt->rt_dst; 680db8dac20SDavid S. Miller 681db8dac20SDavid S. Miller lock_sock(sk); 682db8dac20SDavid S. Miller if (unlikely(up->pending)) { 683db8dac20SDavid S. Miller /* The socket is already corked while preparing it. */ 684db8dac20SDavid S. Miller /* ... which is an evident application bug. --ANK */ 685db8dac20SDavid S. Miller release_sock(sk); 686db8dac20SDavid S. Miller 687db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n"); 688db8dac20SDavid S. Miller err = -EINVAL; 689db8dac20SDavid S. Miller goto out; 690db8dac20SDavid S. Miller } 691db8dac20SDavid S. Miller /* 692db8dac20SDavid S. Miller * Now cork the socket to pend data. 693db8dac20SDavid S. Miller */ 694db8dac20SDavid S. Miller inet->cork.fl.fl4_dst = daddr; 695db8dac20SDavid S. Miller inet->cork.fl.fl_ip_dport = dport; 696db8dac20SDavid S. Miller inet->cork.fl.fl4_src = saddr; 697db8dac20SDavid S. Miller inet->cork.fl.fl_ip_sport = inet->sport; 698db8dac20SDavid S. Miller up->pending = AF_INET; 699db8dac20SDavid S. Miller 700db8dac20SDavid S. Miller do_append_data: 701db8dac20SDavid S. Miller up->len += ulen; 702db8dac20SDavid S. Miller getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 703db8dac20SDavid S. Miller err = ip_append_data(sk, getfrag, msg->msg_iov, ulen, 704db8dac20SDavid S. Miller sizeof(struct udphdr), &ipc, rt, 705db8dac20SDavid S. Miller corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); 706db8dac20SDavid S. Miller if (err) 707db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 708db8dac20SDavid S. Miller else if (!corkreq) 709db8dac20SDavid S. Miller err = udp_push_pending_frames(sk); 710db8dac20SDavid S. Miller else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 711db8dac20SDavid S. Miller up->pending = 0; 712db8dac20SDavid S. Miller release_sock(sk); 713db8dac20SDavid S. Miller 714db8dac20SDavid S. Miller out: 715db8dac20SDavid S. Miller ip_rt_put(rt); 716db8dac20SDavid S. Miller if (free) 717db8dac20SDavid S. Miller kfree(ipc.opt); 718db8dac20SDavid S. Miller if (!err) 719db8dac20SDavid S. Miller return len; 720db8dac20SDavid S. Miller /* 721db8dac20SDavid S. Miller * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 722db8dac20SDavid S. Miller * ENOBUFS might not be good (it's not tunable per se), but otherwise 723db8dac20SDavid S. Miller * we don't have a good statistic (IpOutDiscards but it can be too many 724db8dac20SDavid S. Miller * things). We could add another new stat but at least for now that 725db8dac20SDavid S. Miller * seems like overkill. 726db8dac20SDavid S. Miller */ 727db8dac20SDavid S. Miller if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 728db8dac20SDavid S. Miller UDP_INC_STATS_USER(UDP_MIB_SNDBUFERRORS, is_udplite); 729db8dac20SDavid S. Miller } 730db8dac20SDavid S. Miller return err; 731db8dac20SDavid S. Miller 732db8dac20SDavid S. Miller do_confirm: 733db8dac20SDavid S. Miller dst_confirm(&rt->u.dst); 734db8dac20SDavid S. Miller if (!(msg->msg_flags&MSG_PROBE) || len) 735db8dac20SDavid S. Miller goto back_from_confirm; 736db8dac20SDavid S. Miller err = 0; 737db8dac20SDavid S. Miller goto out; 738db8dac20SDavid S. Miller } 739db8dac20SDavid S. Miller 740db8dac20SDavid S. Miller int udp_sendpage(struct sock *sk, struct page *page, int offset, 741db8dac20SDavid S. Miller size_t size, int flags) 742db8dac20SDavid S. Miller { 743db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 744db8dac20SDavid S. Miller int ret; 745db8dac20SDavid S. Miller 746db8dac20SDavid S. Miller if (!up->pending) { 747db8dac20SDavid S. Miller struct msghdr msg = { .msg_flags = flags|MSG_MORE }; 748db8dac20SDavid S. Miller 749db8dac20SDavid S. Miller /* Call udp_sendmsg to specify destination address which 750db8dac20SDavid S. Miller * sendpage interface can't pass. 751db8dac20SDavid S. Miller * This will succeed only when the socket is connected. 752db8dac20SDavid S. Miller */ 753db8dac20SDavid S. Miller ret = udp_sendmsg(NULL, sk, &msg, 0); 754db8dac20SDavid S. Miller if (ret < 0) 755db8dac20SDavid S. Miller return ret; 756db8dac20SDavid S. Miller } 757db8dac20SDavid S. Miller 758db8dac20SDavid S. Miller lock_sock(sk); 759db8dac20SDavid S. Miller 760db8dac20SDavid S. Miller if (unlikely(!up->pending)) { 761db8dac20SDavid S. Miller release_sock(sk); 762db8dac20SDavid S. Miller 763db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n"); 764db8dac20SDavid S. Miller return -EINVAL; 765db8dac20SDavid S. Miller } 766db8dac20SDavid S. Miller 767db8dac20SDavid S. Miller ret = ip_append_page(sk, page, offset, size, flags); 768db8dac20SDavid S. Miller if (ret == -EOPNOTSUPP) { 769db8dac20SDavid S. Miller release_sock(sk); 770db8dac20SDavid S. Miller return sock_no_sendpage(sk->sk_socket, page, offset, 771db8dac20SDavid S. Miller size, flags); 772db8dac20SDavid S. Miller } 773db8dac20SDavid S. Miller if (ret < 0) { 774db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 775db8dac20SDavid S. Miller goto out; 776db8dac20SDavid S. Miller } 777db8dac20SDavid S. Miller 778db8dac20SDavid S. Miller up->len += size; 779db8dac20SDavid S. Miller if (!(up->corkflag || (flags&MSG_MORE))) 780db8dac20SDavid S. Miller ret = udp_push_pending_frames(sk); 781db8dac20SDavid S. Miller if (!ret) 782db8dac20SDavid S. Miller ret = size; 783db8dac20SDavid S. Miller out: 784db8dac20SDavid S. Miller release_sock(sk); 785db8dac20SDavid S. Miller return ret; 786db8dac20SDavid S. Miller } 787db8dac20SDavid S. Miller 7881da177e4SLinus Torvalds /* 7891da177e4SLinus Torvalds * IOCTL requests applicable to the UDP protocol 7901da177e4SLinus Torvalds */ 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds int udp_ioctl(struct sock *sk, int cmd, unsigned long arg) 7931da177e4SLinus Torvalds { 7946516c655SStephen Hemminger switch (cmd) { 7951da177e4SLinus Torvalds case SIOCOUTQ: 7961da177e4SLinus Torvalds { 7971da177e4SLinus Torvalds int amount = atomic_read(&sk->sk_wmem_alloc); 7981da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 7991da177e4SLinus Torvalds } 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds case SIOCINQ: 8021da177e4SLinus Torvalds { 8031da177e4SLinus Torvalds struct sk_buff *skb; 8041da177e4SLinus Torvalds unsigned long amount; 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds amount = 0; 807208d8984SHerbert Xu spin_lock_bh(&sk->sk_receive_queue.lock); 8081da177e4SLinus Torvalds skb = skb_peek(&sk->sk_receive_queue); 8091da177e4SLinus Torvalds if (skb != NULL) { 8101da177e4SLinus Torvalds /* 8111da177e4SLinus Torvalds * We will only return the amount 8121da177e4SLinus Torvalds * of this packet since that is all 8131da177e4SLinus Torvalds * that will be read. 8141da177e4SLinus Torvalds */ 8151da177e4SLinus Torvalds amount = skb->len - sizeof(struct udphdr); 8161da177e4SLinus Torvalds } 817208d8984SHerbert Xu spin_unlock_bh(&sk->sk_receive_queue.lock); 8181da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 8191da177e4SLinus Torvalds } 8201da177e4SLinus Torvalds 8211da177e4SLinus Torvalds default: 8221da177e4SLinus Torvalds return -ENOIOCTLCMD; 8231da177e4SLinus Torvalds } 8246516c655SStephen Hemminger 8256516c655SStephen Hemminger return 0; 8261da177e4SLinus Torvalds } 8271da177e4SLinus Torvalds 828db8dac20SDavid S. Miller /* 829db8dac20SDavid S. Miller * This should be easy, if there is something there we 830db8dac20SDavid S. Miller * return it, otherwise we block. 831db8dac20SDavid S. Miller */ 832db8dac20SDavid S. Miller 833db8dac20SDavid S. Miller int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 834db8dac20SDavid S. Miller size_t len, int noblock, int flags, int *addr_len) 835db8dac20SDavid S. Miller { 836db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 837db8dac20SDavid S. Miller struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name; 838db8dac20SDavid S. Miller struct sk_buff *skb; 839db8dac20SDavid S. Miller unsigned int ulen, copied; 840db8dac20SDavid S. Miller int peeked; 841db8dac20SDavid S. Miller int err; 842db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 843db8dac20SDavid S. Miller 844db8dac20SDavid S. Miller /* 845db8dac20SDavid S. Miller * Check any passed addresses 846db8dac20SDavid S. Miller */ 847db8dac20SDavid S. Miller if (addr_len) 848db8dac20SDavid S. Miller *addr_len=sizeof(*sin); 849db8dac20SDavid S. Miller 850db8dac20SDavid S. Miller if (flags & MSG_ERRQUEUE) 851db8dac20SDavid S. Miller return ip_recv_error(sk, msg, len); 852db8dac20SDavid S. Miller 853db8dac20SDavid S. Miller try_again: 854db8dac20SDavid S. Miller skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), 855db8dac20SDavid S. Miller &peeked, &err); 856db8dac20SDavid S. Miller if (!skb) 857db8dac20SDavid S. Miller goto out; 858db8dac20SDavid S. Miller 859db8dac20SDavid S. Miller ulen = skb->len - sizeof(struct udphdr); 860db8dac20SDavid S. Miller copied = len; 861db8dac20SDavid S. Miller if (copied > ulen) 862db8dac20SDavid S. Miller copied = ulen; 863db8dac20SDavid S. Miller else if (copied < ulen) 864db8dac20SDavid S. Miller msg->msg_flags |= MSG_TRUNC; 865db8dac20SDavid S. Miller 866db8dac20SDavid S. Miller /* 867db8dac20SDavid S. Miller * If checksum is needed at all, try to do it while copying the 868db8dac20SDavid S. Miller * data. If the data is truncated, or if we only want a partial 869db8dac20SDavid S. Miller * coverage checksum (UDP-Lite), do it before the copy. 870db8dac20SDavid S. Miller */ 871db8dac20SDavid S. Miller 872db8dac20SDavid S. Miller if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) { 873db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 874db8dac20SDavid S. Miller goto csum_copy_err; 875db8dac20SDavid S. Miller } 876db8dac20SDavid S. Miller 877db8dac20SDavid S. Miller if (skb_csum_unnecessary(skb)) 878db8dac20SDavid S. Miller err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), 879db8dac20SDavid S. Miller msg->msg_iov, copied ); 880db8dac20SDavid S. Miller else { 881db8dac20SDavid S. Miller err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov); 882db8dac20SDavid S. Miller 883db8dac20SDavid S. Miller if (err == -EINVAL) 884db8dac20SDavid S. Miller goto csum_copy_err; 885db8dac20SDavid S. Miller } 886db8dac20SDavid S. Miller 887db8dac20SDavid S. Miller if (err) 888db8dac20SDavid S. Miller goto out_free; 889db8dac20SDavid S. Miller 890db8dac20SDavid S. Miller if (!peeked) 891db8dac20SDavid S. Miller UDP_INC_STATS_USER(UDP_MIB_INDATAGRAMS, is_udplite); 892db8dac20SDavid S. Miller 893db8dac20SDavid S. Miller sock_recv_timestamp(msg, sk, skb); 894db8dac20SDavid S. Miller 895db8dac20SDavid S. Miller /* Copy the address. */ 896db8dac20SDavid S. Miller if (sin) 897db8dac20SDavid S. Miller { 898db8dac20SDavid S. Miller sin->sin_family = AF_INET; 899db8dac20SDavid S. Miller sin->sin_port = udp_hdr(skb)->source; 900db8dac20SDavid S. Miller sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 901db8dac20SDavid S. Miller memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 902db8dac20SDavid S. Miller } 903db8dac20SDavid S. Miller if (inet->cmsg_flags) 904db8dac20SDavid S. Miller ip_cmsg_recv(msg, skb); 905db8dac20SDavid S. Miller 906db8dac20SDavid S. Miller err = copied; 907db8dac20SDavid S. Miller if (flags & MSG_TRUNC) 908db8dac20SDavid S. Miller err = ulen; 909db8dac20SDavid S. Miller 910db8dac20SDavid S. Miller out_free: 911db8dac20SDavid S. Miller lock_sock(sk); 912db8dac20SDavid S. Miller skb_free_datagram(sk, skb); 913db8dac20SDavid S. Miller release_sock(sk); 914db8dac20SDavid S. Miller out: 915db8dac20SDavid S. Miller return err; 916db8dac20SDavid S. Miller 917db8dac20SDavid S. Miller csum_copy_err: 918db8dac20SDavid S. Miller lock_sock(sk); 919db8dac20SDavid S. Miller if (!skb_kill_datagram(sk, skb, flags)) 920db8dac20SDavid S. Miller UDP_INC_STATS_USER(UDP_MIB_INERRORS, is_udplite); 921db8dac20SDavid S. Miller release_sock(sk); 922db8dac20SDavid S. Miller 923db8dac20SDavid S. Miller if (noblock) 924db8dac20SDavid S. Miller return -EAGAIN; 925db8dac20SDavid S. Miller goto try_again; 926db8dac20SDavid S. Miller } 927db8dac20SDavid S. Miller 928db8dac20SDavid S. Miller 9291da177e4SLinus Torvalds int udp_disconnect(struct sock *sk, int flags) 9301da177e4SLinus Torvalds { 9311da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 9321da177e4SLinus Torvalds /* 9331da177e4SLinus Torvalds * 1003.1g - break association. 9341da177e4SLinus Torvalds */ 9351da177e4SLinus Torvalds 9361da177e4SLinus Torvalds sk->sk_state = TCP_CLOSE; 9371da177e4SLinus Torvalds inet->daddr = 0; 9381da177e4SLinus Torvalds inet->dport = 0; 9391da177e4SLinus Torvalds sk->sk_bound_dev_if = 0; 9401da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) 9411da177e4SLinus Torvalds inet_reset_saddr(sk); 9421da177e4SLinus Torvalds 9431da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) { 9441da177e4SLinus Torvalds sk->sk_prot->unhash(sk); 9451da177e4SLinus Torvalds inet->sport = 0; 9461da177e4SLinus Torvalds } 9471da177e4SLinus Torvalds sk_dst_reset(sk); 9481da177e4SLinus Torvalds return 0; 9491da177e4SLinus Torvalds } 9501da177e4SLinus Torvalds 951db8dac20SDavid S. Miller /* returns: 952db8dac20SDavid S. Miller * -1: error 953db8dac20SDavid S. Miller * 0: success 954db8dac20SDavid S. Miller * >0: "udp encap" protocol resubmission 955db8dac20SDavid S. Miller * 956db8dac20SDavid S. Miller * Note that in the success and error cases, the skb is assumed to 957db8dac20SDavid S. Miller * have either been requeued or freed. 958db8dac20SDavid S. Miller */ 959db8dac20SDavid S. Miller int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb) 960db8dac20SDavid S. Miller { 961db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 962db8dac20SDavid S. Miller int rc; 963db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 964db8dac20SDavid S. Miller 965db8dac20SDavid S. Miller /* 966db8dac20SDavid S. Miller * Charge it to the socket, dropping if the queue is full. 967db8dac20SDavid S. Miller */ 968db8dac20SDavid S. Miller if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) 969db8dac20SDavid S. Miller goto drop; 970db8dac20SDavid S. Miller nf_reset(skb); 971db8dac20SDavid S. Miller 972db8dac20SDavid S. Miller if (up->encap_type) { 973db8dac20SDavid S. Miller /* 974db8dac20SDavid S. Miller * This is an encapsulation socket so pass the skb to 975db8dac20SDavid S. Miller * the socket's udp_encap_rcv() hook. Otherwise, just 976db8dac20SDavid S. Miller * fall through and pass this up the UDP socket. 977db8dac20SDavid S. Miller * up->encap_rcv() returns the following value: 978db8dac20SDavid S. Miller * =0 if skb was successfully passed to the encap 979db8dac20SDavid S. Miller * handler or was discarded by it. 980db8dac20SDavid S. Miller * >0 if skb should be passed on to UDP. 981db8dac20SDavid S. Miller * <0 if skb should be resubmitted as proto -N 982db8dac20SDavid S. Miller */ 983db8dac20SDavid S. Miller 984db8dac20SDavid S. Miller /* if we're overly short, let UDP handle it */ 985db8dac20SDavid S. Miller if (skb->len > sizeof(struct udphdr) && 986db8dac20SDavid S. Miller up->encap_rcv != NULL) { 987db8dac20SDavid S. Miller int ret; 988db8dac20SDavid S. Miller 989db8dac20SDavid S. Miller ret = (*up->encap_rcv)(sk, skb); 990db8dac20SDavid S. Miller if (ret <= 0) { 991db8dac20SDavid S. Miller UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS, 992db8dac20SDavid S. Miller is_udplite); 993db8dac20SDavid S. Miller return -ret; 994db8dac20SDavid S. Miller } 995db8dac20SDavid S. Miller } 996db8dac20SDavid S. Miller 997db8dac20SDavid S. Miller /* FALLTHROUGH -- it's a UDP Packet */ 998db8dac20SDavid S. Miller } 999db8dac20SDavid S. Miller 1000db8dac20SDavid S. Miller /* 1001db8dac20SDavid S. Miller * UDP-Lite specific tests, ignored on UDP sockets 1002db8dac20SDavid S. Miller */ 1003db8dac20SDavid S. Miller if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 1004db8dac20SDavid S. Miller 1005db8dac20SDavid S. Miller /* 1006db8dac20SDavid S. Miller * MIB statistics other than incrementing the error count are 1007db8dac20SDavid S. Miller * disabled for the following two types of errors: these depend 1008db8dac20SDavid S. Miller * on the application settings, not on the functioning of the 1009db8dac20SDavid S. Miller * protocol stack as such. 1010db8dac20SDavid S. Miller * 1011db8dac20SDavid S. Miller * RFC 3828 here recommends (sec 3.3): "There should also be a 1012db8dac20SDavid S. Miller * way ... to ... at least let the receiving application block 1013db8dac20SDavid S. Miller * delivery of packets with coverage values less than a value 1014db8dac20SDavid S. Miller * provided by the application." 1015db8dac20SDavid S. Miller */ 1016db8dac20SDavid S. Miller if (up->pcrlen == 0) { /* full coverage was set */ 1017db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_WARNING "UDPLITE: partial coverage " 1018db8dac20SDavid S. Miller "%d while full coverage %d requested\n", 1019db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, skb->len); 1020db8dac20SDavid S. Miller goto drop; 1021db8dac20SDavid S. Miller } 1022db8dac20SDavid S. Miller /* The next case involves violating the min. coverage requested 1023db8dac20SDavid S. Miller * by the receiver. This is subtle: if receiver wants x and x is 1024db8dac20SDavid S. Miller * greater than the buffersize/MTU then receiver will complain 1025db8dac20SDavid S. Miller * that it wants x while sender emits packets of smaller size y. 1026db8dac20SDavid S. Miller * Therefore the above ...()->partial_cov statement is essential. 1027db8dac20SDavid S. Miller */ 1028db8dac20SDavid S. Miller if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 1029db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_WARNING 1030db8dac20SDavid S. Miller "UDPLITE: coverage %d too small, need min %d\n", 1031db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, up->pcrlen); 1032db8dac20SDavid S. Miller goto drop; 1033db8dac20SDavid S. Miller } 1034db8dac20SDavid S. Miller } 1035db8dac20SDavid S. Miller 1036db8dac20SDavid S. Miller if (sk->sk_filter) { 1037db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 1038db8dac20SDavid S. Miller goto drop; 1039db8dac20SDavid S. Miller } 1040db8dac20SDavid S. Miller 1041db8dac20SDavid S. Miller if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) { 1042db8dac20SDavid S. Miller /* Note that an ENOMEM error is charged twice */ 1043db8dac20SDavid S. Miller if (rc == -ENOMEM) 1044db8dac20SDavid S. Miller UDP_INC_STATS_BH(UDP_MIB_RCVBUFERRORS, is_udplite); 1045db8dac20SDavid S. Miller goto drop; 1046db8dac20SDavid S. Miller } 1047db8dac20SDavid S. Miller 1048db8dac20SDavid S. Miller return 0; 1049db8dac20SDavid S. Miller 1050db8dac20SDavid S. Miller drop: 1051db8dac20SDavid S. Miller UDP_INC_STATS_BH(UDP_MIB_INERRORS, is_udplite); 1052db8dac20SDavid S. Miller kfree_skb(skb); 1053db8dac20SDavid S. Miller return -1; 1054db8dac20SDavid S. Miller } 1055db8dac20SDavid S. Miller 1056db8dac20SDavid S. Miller /* 1057db8dac20SDavid S. Miller * Multicasts and broadcasts go to each listener. 1058db8dac20SDavid S. Miller * 1059db8dac20SDavid S. Miller * Note: called only from the BH handler context, 1060db8dac20SDavid S. Miller * so we don't need to lock the hashes. 1061db8dac20SDavid S. Miller */ 1062e3163493SPavel Emelyanov static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb, 1063db8dac20SDavid S. Miller struct udphdr *uh, 1064db8dac20SDavid S. Miller __be32 saddr, __be32 daddr, 1065db8dac20SDavid S. Miller struct hlist_head udptable[]) 1066db8dac20SDavid S. Miller { 1067db8dac20SDavid S. Miller struct sock *sk; 1068db8dac20SDavid S. Miller int dif; 1069db8dac20SDavid S. Miller 1070db8dac20SDavid S. Miller read_lock(&udp_hash_lock); 1071*19c7578fSPavel Emelyanov sk = sk_head(&udptable[udp_hashfn(net, ntohs(uh->dest))]); 1072db8dac20SDavid S. Miller dif = skb->dev->ifindex; 1073db8dac20SDavid S. Miller sk = udp_v4_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif); 1074db8dac20SDavid S. Miller if (sk) { 1075db8dac20SDavid S. Miller struct sock *sknext = NULL; 1076db8dac20SDavid S. Miller 1077db8dac20SDavid S. Miller do { 1078db8dac20SDavid S. Miller struct sk_buff *skb1 = skb; 1079db8dac20SDavid S. Miller 1080db8dac20SDavid S. Miller sknext = udp_v4_mcast_next(sk_next(sk), uh->dest, daddr, 1081db8dac20SDavid S. Miller uh->source, saddr, dif); 1082db8dac20SDavid S. Miller if (sknext) 1083db8dac20SDavid S. Miller skb1 = skb_clone(skb, GFP_ATOMIC); 1084db8dac20SDavid S. Miller 1085db8dac20SDavid S. Miller if (skb1) { 1086db8dac20SDavid S. Miller int ret = 0; 1087db8dac20SDavid S. Miller 1088db8dac20SDavid S. Miller bh_lock_sock_nested(sk); 1089db8dac20SDavid S. Miller if (!sock_owned_by_user(sk)) 1090db8dac20SDavid S. Miller ret = udp_queue_rcv_skb(sk, skb1); 1091db8dac20SDavid S. Miller else 1092db8dac20SDavid S. Miller sk_add_backlog(sk, skb1); 1093db8dac20SDavid S. Miller bh_unlock_sock(sk); 1094db8dac20SDavid S. Miller 1095db8dac20SDavid S. Miller if (ret > 0) 1096db8dac20SDavid S. Miller /* we should probably re-process instead 1097db8dac20SDavid S. Miller * of dropping packets here. */ 1098db8dac20SDavid S. Miller kfree_skb(skb1); 1099db8dac20SDavid S. Miller } 1100db8dac20SDavid S. Miller sk = sknext; 1101db8dac20SDavid S. Miller } while (sknext); 1102db8dac20SDavid S. Miller } else 1103db8dac20SDavid S. Miller kfree_skb(skb); 1104db8dac20SDavid S. Miller read_unlock(&udp_hash_lock); 1105db8dac20SDavid S. Miller return 0; 1106db8dac20SDavid S. Miller } 1107db8dac20SDavid S. Miller 1108db8dac20SDavid S. Miller /* Initialize UDP checksum. If exited with zero value (success), 1109db8dac20SDavid S. Miller * CHECKSUM_UNNECESSARY means, that no more checks are required. 1110db8dac20SDavid S. Miller * Otherwise, csum completion requires chacksumming packet body, 1111db8dac20SDavid S. Miller * including udp header and folding it to skb->csum. 1112db8dac20SDavid S. Miller */ 1113db8dac20SDavid S. Miller static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh, 1114db8dac20SDavid S. Miller int proto) 1115db8dac20SDavid S. Miller { 1116db8dac20SDavid S. Miller const struct iphdr *iph; 1117db8dac20SDavid S. Miller int err; 1118db8dac20SDavid S. Miller 1119db8dac20SDavid S. Miller UDP_SKB_CB(skb)->partial_cov = 0; 1120db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov = skb->len; 1121db8dac20SDavid S. Miller 1122db8dac20SDavid S. Miller if (proto == IPPROTO_UDPLITE) { 1123db8dac20SDavid S. Miller err = udplite_checksum_init(skb, uh); 1124db8dac20SDavid S. Miller if (err) 1125db8dac20SDavid S. Miller return err; 1126db8dac20SDavid S. Miller } 1127db8dac20SDavid S. Miller 1128db8dac20SDavid S. Miller iph = ip_hdr(skb); 1129db8dac20SDavid S. Miller if (uh->check == 0) { 1130db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_UNNECESSARY; 1131db8dac20SDavid S. Miller } else if (skb->ip_summed == CHECKSUM_COMPLETE) { 1132db8dac20SDavid S. Miller if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len, 1133db8dac20SDavid S. Miller proto, skb->csum)) 1134db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_UNNECESSARY; 1135db8dac20SDavid S. Miller } 1136db8dac20SDavid S. Miller if (!skb_csum_unnecessary(skb)) 1137db8dac20SDavid S. Miller skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, 1138db8dac20SDavid S. Miller skb->len, proto, 0); 1139db8dac20SDavid S. Miller /* Probably, we should checksum udp header (it should be in cache 1140db8dac20SDavid S. Miller * in any case) and data in tiny packets (< rx copybreak). 1141db8dac20SDavid S. Miller */ 1142db8dac20SDavid S. Miller 1143db8dac20SDavid S. Miller return 0; 1144db8dac20SDavid S. Miller } 1145db8dac20SDavid S. Miller 1146db8dac20SDavid S. Miller /* 1147db8dac20SDavid S. Miller * All we need to do is get the socket, and then do a checksum. 1148db8dac20SDavid S. Miller */ 1149db8dac20SDavid S. Miller 1150db8dac20SDavid S. Miller int __udp4_lib_rcv(struct sk_buff *skb, struct hlist_head udptable[], 1151db8dac20SDavid S. Miller int proto) 1152db8dac20SDavid S. Miller { 1153db8dac20SDavid S. Miller struct sock *sk; 1154db8dac20SDavid S. Miller struct udphdr *uh = udp_hdr(skb); 1155db8dac20SDavid S. Miller unsigned short ulen; 1156db8dac20SDavid S. Miller struct rtable *rt = (struct rtable*)skb->dst; 1157db8dac20SDavid S. Miller __be32 saddr = ip_hdr(skb)->saddr; 1158db8dac20SDavid S. Miller __be32 daddr = ip_hdr(skb)->daddr; 1159e3163493SPavel Emelyanov struct net *net; 1160db8dac20SDavid S. Miller 1161db8dac20SDavid S. Miller /* 1162db8dac20SDavid S. Miller * Validate the packet. 1163db8dac20SDavid S. Miller */ 1164db8dac20SDavid S. Miller if (!pskb_may_pull(skb, sizeof(struct udphdr))) 1165db8dac20SDavid S. Miller goto drop; /* No space for header. */ 1166db8dac20SDavid S. Miller 1167db8dac20SDavid S. Miller ulen = ntohs(uh->len); 1168db8dac20SDavid S. Miller if (ulen > skb->len) 1169db8dac20SDavid S. Miller goto short_packet; 1170db8dac20SDavid S. Miller 1171db8dac20SDavid S. Miller if (proto == IPPROTO_UDP) { 1172db8dac20SDavid S. Miller /* UDP validates ulen. */ 1173db8dac20SDavid S. Miller if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen)) 1174db8dac20SDavid S. Miller goto short_packet; 1175db8dac20SDavid S. Miller uh = udp_hdr(skb); 1176db8dac20SDavid S. Miller } 1177db8dac20SDavid S. Miller 1178db8dac20SDavid S. Miller if (udp4_csum_init(skb, uh, proto)) 1179db8dac20SDavid S. Miller goto csum_error; 1180db8dac20SDavid S. Miller 1181e3163493SPavel Emelyanov net = dev_net(skb->dev); 1182db8dac20SDavid S. Miller if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST)) 1183e3163493SPavel Emelyanov return __udp4_lib_mcast_deliver(net, skb, uh, 1184e3163493SPavel Emelyanov saddr, daddr, udptable); 1185db8dac20SDavid S. Miller 1186e3163493SPavel Emelyanov sk = __udp4_lib_lookup(net, saddr, uh->source, daddr, 1187db8dac20SDavid S. Miller uh->dest, inet_iif(skb), udptable); 1188db8dac20SDavid S. Miller 1189db8dac20SDavid S. Miller if (sk != NULL) { 1190db8dac20SDavid S. Miller int ret = 0; 1191db8dac20SDavid S. Miller bh_lock_sock_nested(sk); 1192db8dac20SDavid S. Miller if (!sock_owned_by_user(sk)) 1193db8dac20SDavid S. Miller ret = udp_queue_rcv_skb(sk, skb); 1194db8dac20SDavid S. Miller else 1195db8dac20SDavid S. Miller sk_add_backlog(sk, skb); 1196db8dac20SDavid S. Miller bh_unlock_sock(sk); 1197db8dac20SDavid S. Miller sock_put(sk); 1198db8dac20SDavid S. Miller 1199db8dac20SDavid S. Miller /* a return value > 0 means to resubmit the input, but 1200db8dac20SDavid S. Miller * it wants the return to be -protocol, or 0 1201db8dac20SDavid S. Miller */ 1202db8dac20SDavid S. Miller if (ret > 0) 1203db8dac20SDavid S. Miller return -ret; 1204db8dac20SDavid S. Miller return 0; 1205db8dac20SDavid S. Miller } 1206db8dac20SDavid S. Miller 1207db8dac20SDavid S. Miller if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) 1208db8dac20SDavid S. Miller goto drop; 1209db8dac20SDavid S. Miller nf_reset(skb); 1210db8dac20SDavid S. Miller 1211db8dac20SDavid S. Miller /* No socket. Drop packet silently, if checksum is wrong */ 1212db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 1213db8dac20SDavid S. Miller goto csum_error; 1214db8dac20SDavid S. Miller 1215db8dac20SDavid S. Miller UDP_INC_STATS_BH(UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); 1216db8dac20SDavid S. Miller icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); 1217db8dac20SDavid S. Miller 1218db8dac20SDavid S. Miller /* 1219db8dac20SDavid S. Miller * Hmm. We got an UDP packet to a port to which we 1220db8dac20SDavid S. Miller * don't wanna listen. Ignore it. 1221db8dac20SDavid S. Miller */ 1222db8dac20SDavid S. Miller kfree_skb(skb); 1223db8dac20SDavid S. Miller return 0; 1224db8dac20SDavid S. Miller 1225db8dac20SDavid S. Miller short_packet: 1226a7d632b6SYOSHIFUJI Hideaki LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From " NIPQUAD_FMT ":%u %d/%d to " NIPQUAD_FMT ":%u\n", 1227db8dac20SDavid S. Miller proto == IPPROTO_UDPLITE ? "-Lite" : "", 1228db8dac20SDavid S. Miller NIPQUAD(saddr), 1229db8dac20SDavid S. Miller ntohs(uh->source), 1230db8dac20SDavid S. Miller ulen, 1231db8dac20SDavid S. Miller skb->len, 1232db8dac20SDavid S. Miller NIPQUAD(daddr), 1233db8dac20SDavid S. Miller ntohs(uh->dest)); 1234db8dac20SDavid S. Miller goto drop; 1235db8dac20SDavid S. Miller 1236db8dac20SDavid S. Miller csum_error: 1237db8dac20SDavid S. Miller /* 1238db8dac20SDavid S. Miller * RFC1122: OK. Discards the bad packet silently (as far as 1239db8dac20SDavid S. Miller * the network is concerned, anyway) as per 4.1.3.4 (MUST). 1240db8dac20SDavid S. Miller */ 1241a7d632b6SYOSHIFUJI Hideaki LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From " NIPQUAD_FMT ":%u to " NIPQUAD_FMT ":%u ulen %d\n", 1242db8dac20SDavid S. Miller proto == IPPROTO_UDPLITE ? "-Lite" : "", 1243db8dac20SDavid S. Miller NIPQUAD(saddr), 1244db8dac20SDavid S. Miller ntohs(uh->source), 1245db8dac20SDavid S. Miller NIPQUAD(daddr), 1246db8dac20SDavid S. Miller ntohs(uh->dest), 1247db8dac20SDavid S. Miller ulen); 1248db8dac20SDavid S. Miller drop: 1249db8dac20SDavid S. Miller UDP_INC_STATS_BH(UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 1250db8dac20SDavid S. Miller kfree_skb(skb); 1251db8dac20SDavid S. Miller return 0; 1252db8dac20SDavid S. Miller } 1253db8dac20SDavid S. Miller 1254db8dac20SDavid S. Miller int udp_rcv(struct sk_buff *skb) 1255db8dac20SDavid S. Miller { 1256db8dac20SDavid S. Miller return __udp4_lib_rcv(skb, udp_hash, IPPROTO_UDP); 1257db8dac20SDavid S. Miller } 1258db8dac20SDavid S. Miller 12597d06b2e0SBrian Haley void udp_destroy_sock(struct sock *sk) 1260db8dac20SDavid S. Miller { 1261db8dac20SDavid S. Miller lock_sock(sk); 1262db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 1263db8dac20SDavid S. Miller release_sock(sk); 1264db8dac20SDavid S. Miller } 1265db8dac20SDavid S. Miller 12661da177e4SLinus Torvalds /* 12671da177e4SLinus Torvalds * Socket option code for UDP 12681da177e4SLinus Torvalds */ 12694c0a6cb0SGerrit Renker int udp_lib_setsockopt(struct sock *sk, int level, int optname, 12704c0a6cb0SGerrit Renker char __user *optval, int optlen, 12714c0a6cb0SGerrit Renker int (*push_pending_frames)(struct sock *)) 12721da177e4SLinus Torvalds { 12731da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 12741da177e4SLinus Torvalds int val; 12751da177e4SLinus Torvalds int err = 0; 1276b2bf1e26SWang Chen int is_udplite = IS_UDPLITE(sk); 12771da177e4SLinus Torvalds 12781da177e4SLinus Torvalds if (optlen<sizeof(int)) 12791da177e4SLinus Torvalds return -EINVAL; 12801da177e4SLinus Torvalds 12811da177e4SLinus Torvalds if (get_user(val, (int __user *)optval)) 12821da177e4SLinus Torvalds return -EFAULT; 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds switch (optname) { 12851da177e4SLinus Torvalds case UDP_CORK: 12861da177e4SLinus Torvalds if (val != 0) { 12871da177e4SLinus Torvalds up->corkflag = 1; 12881da177e4SLinus Torvalds } else { 12891da177e4SLinus Torvalds up->corkflag = 0; 12901da177e4SLinus Torvalds lock_sock(sk); 12914c0a6cb0SGerrit Renker (*push_pending_frames)(sk); 12921da177e4SLinus Torvalds release_sock(sk); 12931da177e4SLinus Torvalds } 12941da177e4SLinus Torvalds break; 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds case UDP_ENCAP: 12971da177e4SLinus Torvalds switch (val) { 12981da177e4SLinus Torvalds case 0: 12991da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP: 13001da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP_NON_IKE: 1301067b207bSJames Chapman up->encap_rcv = xfrm4_udp_encap_rcv; 1302067b207bSJames Chapman /* FALLTHROUGH */ 1303342f0234SJames Chapman case UDP_ENCAP_L2TPINUDP: 13041da177e4SLinus Torvalds up->encap_type = val; 13051da177e4SLinus Torvalds break; 13061da177e4SLinus Torvalds default: 13071da177e4SLinus Torvalds err = -ENOPROTOOPT; 13081da177e4SLinus Torvalds break; 13091da177e4SLinus Torvalds } 13101da177e4SLinus Torvalds break; 13111da177e4SLinus Torvalds 1312ba4e58ecSGerrit Renker /* 1313ba4e58ecSGerrit Renker * UDP-Lite's partial checksum coverage (RFC 3828). 1314ba4e58ecSGerrit Renker */ 1315ba4e58ecSGerrit Renker /* The sender sets actual checksum coverage length via this option. 1316ba4e58ecSGerrit Renker * The case coverage > packet length is handled by send module. */ 1317ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 1318b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 1319ba4e58ecSGerrit Renker return -ENOPROTOOPT; 1320ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Illegal coverage: use default (8) */ 1321ba4e58ecSGerrit Renker val = 8; 1322ba4e58ecSGerrit Renker up->pcslen = val; 1323ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_SEND_CC; 1324ba4e58ecSGerrit Renker break; 1325ba4e58ecSGerrit Renker 1326ba4e58ecSGerrit Renker /* The receiver specifies a minimum checksum coverage value. To make 1327ba4e58ecSGerrit Renker * sense, this should be set to at least 8 (as done below). If zero is 1328ba4e58ecSGerrit Renker * used, this again means full checksum coverage. */ 1329ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 1330b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 1331ba4e58ecSGerrit Renker return -ENOPROTOOPT; 1332ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Avoid silly minimal values. */ 1333ba4e58ecSGerrit Renker val = 8; 1334ba4e58ecSGerrit Renker up->pcrlen = val; 1335ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_RECV_CC; 1336ba4e58ecSGerrit Renker break; 1337ba4e58ecSGerrit Renker 13381da177e4SLinus Torvalds default: 13391da177e4SLinus Torvalds err = -ENOPROTOOPT; 13401da177e4SLinus Torvalds break; 13416516c655SStephen Hemminger } 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds return err; 13441da177e4SLinus Torvalds } 13451da177e4SLinus Torvalds 1346db8dac20SDavid S. Miller int udp_setsockopt(struct sock *sk, int level, int optname, 1347db8dac20SDavid S. Miller char __user *optval, int optlen) 1348db8dac20SDavid S. Miller { 1349db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1350db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1351db8dac20SDavid S. Miller udp_push_pending_frames); 1352db8dac20SDavid S. Miller return ip_setsockopt(sk, level, optname, optval, optlen); 1353db8dac20SDavid S. Miller } 1354db8dac20SDavid S. Miller 1355db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1356db8dac20SDavid S. Miller int compat_udp_setsockopt(struct sock *sk, int level, int optname, 1357db8dac20SDavid S. Miller char __user *optval, int optlen) 1358db8dac20SDavid S. Miller { 1359db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1360db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1361db8dac20SDavid S. Miller udp_push_pending_frames); 1362db8dac20SDavid S. Miller return compat_ip_setsockopt(sk, level, optname, optval, optlen); 1363db8dac20SDavid S. Miller } 1364db8dac20SDavid S. Miller #endif 1365db8dac20SDavid S. Miller 13664c0a6cb0SGerrit Renker int udp_lib_getsockopt(struct sock *sk, int level, int optname, 13671da177e4SLinus Torvalds char __user *optval, int __user *optlen) 13681da177e4SLinus Torvalds { 13691da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 13701da177e4SLinus Torvalds int val, len; 13711da177e4SLinus Torvalds 13721da177e4SLinus Torvalds if (get_user(len,optlen)) 13731da177e4SLinus Torvalds return -EFAULT; 13741da177e4SLinus Torvalds 13751da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(int)); 13761da177e4SLinus Torvalds 13771da177e4SLinus Torvalds if (len < 0) 13781da177e4SLinus Torvalds return -EINVAL; 13791da177e4SLinus Torvalds 13801da177e4SLinus Torvalds switch (optname) { 13811da177e4SLinus Torvalds case UDP_CORK: 13821da177e4SLinus Torvalds val = up->corkflag; 13831da177e4SLinus Torvalds break; 13841da177e4SLinus Torvalds 13851da177e4SLinus Torvalds case UDP_ENCAP: 13861da177e4SLinus Torvalds val = up->encap_type; 13871da177e4SLinus Torvalds break; 13881da177e4SLinus Torvalds 1389ba4e58ecSGerrit Renker /* The following two cannot be changed on UDP sockets, the return is 1390ba4e58ecSGerrit Renker * always 0 (which corresponds to the full checksum coverage of UDP). */ 1391ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 1392ba4e58ecSGerrit Renker val = up->pcslen; 1393ba4e58ecSGerrit Renker break; 1394ba4e58ecSGerrit Renker 1395ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 1396ba4e58ecSGerrit Renker val = up->pcrlen; 1397ba4e58ecSGerrit Renker break; 1398ba4e58ecSGerrit Renker 13991da177e4SLinus Torvalds default: 14001da177e4SLinus Torvalds return -ENOPROTOOPT; 14016516c655SStephen Hemminger } 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds if (put_user(len, optlen)) 14041da177e4SLinus Torvalds return -EFAULT; 14051da177e4SLinus Torvalds if (copy_to_user(optval, &val,len)) 14061da177e4SLinus Torvalds return -EFAULT; 14071da177e4SLinus Torvalds return 0; 14081da177e4SLinus Torvalds } 14091da177e4SLinus Torvalds 1410db8dac20SDavid S. Miller int udp_getsockopt(struct sock *sk, int level, int optname, 1411db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 1412db8dac20SDavid S. Miller { 1413db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1414db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1415db8dac20SDavid S. Miller return ip_getsockopt(sk, level, optname, optval, optlen); 1416db8dac20SDavid S. Miller } 1417db8dac20SDavid S. Miller 1418db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1419db8dac20SDavid S. Miller int compat_udp_getsockopt(struct sock *sk, int level, int optname, 1420db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 1421db8dac20SDavid S. Miller { 1422db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1423db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1424db8dac20SDavid S. Miller return compat_ip_getsockopt(sk, level, optname, optval, optlen); 1425db8dac20SDavid S. Miller } 1426db8dac20SDavid S. Miller #endif 14271da177e4SLinus Torvalds /** 14281da177e4SLinus Torvalds * udp_poll - wait for a UDP event. 14291da177e4SLinus Torvalds * @file - file struct 14301da177e4SLinus Torvalds * @sock - socket 14311da177e4SLinus Torvalds * @wait - poll table 14321da177e4SLinus Torvalds * 14331da177e4SLinus Torvalds * This is same as datagram poll, except for the special case of 14341da177e4SLinus Torvalds * blocking sockets. If application is using a blocking fd 14351da177e4SLinus Torvalds * and a packet with checksum error is in the queue; 14361da177e4SLinus Torvalds * then it could get return from select indicating data available 14371da177e4SLinus Torvalds * but then block when reading it. Add special case code 14381da177e4SLinus Torvalds * to work around these arguably broken applications. 14391da177e4SLinus Torvalds */ 14401da177e4SLinus Torvalds unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait) 14411da177e4SLinus Torvalds { 14421da177e4SLinus Torvalds unsigned int mask = datagram_poll(file, sock, wait); 14431da177e4SLinus Torvalds struct sock *sk = sock->sk; 1444ba4e58ecSGerrit Renker int is_lite = IS_UDPLITE(sk); 14451da177e4SLinus Torvalds 14461da177e4SLinus Torvalds /* Check for false positives due to checksum errors */ 14471da177e4SLinus Torvalds if ( (mask & POLLRDNORM) && 14481da177e4SLinus Torvalds !(file->f_flags & O_NONBLOCK) && 14491da177e4SLinus Torvalds !(sk->sk_shutdown & RCV_SHUTDOWN)){ 14501da177e4SLinus Torvalds struct sk_buff_head *rcvq = &sk->sk_receive_queue; 14511da177e4SLinus Torvalds struct sk_buff *skb; 14521da177e4SLinus Torvalds 1453208d8984SHerbert Xu spin_lock_bh(&rcvq->lock); 1454759e5d00SHerbert Xu while ((skb = skb_peek(rcvq)) != NULL && 1455759e5d00SHerbert Xu udp_lib_checksum_complete(skb)) { 1456ba4e58ecSGerrit Renker UDP_INC_STATS_BH(UDP_MIB_INERRORS, is_lite); 14571da177e4SLinus Torvalds __skb_unlink(skb, rcvq); 14581da177e4SLinus Torvalds kfree_skb(skb); 14591da177e4SLinus Torvalds } 1460208d8984SHerbert Xu spin_unlock_bh(&rcvq->lock); 14611da177e4SLinus Torvalds 14621da177e4SLinus Torvalds /* nothing to see, move along */ 14631da177e4SLinus Torvalds if (skb == NULL) 14641da177e4SLinus Torvalds mask &= ~(POLLIN | POLLRDNORM); 14651da177e4SLinus Torvalds } 14661da177e4SLinus Torvalds 14671da177e4SLinus Torvalds return mask; 14681da177e4SLinus Torvalds 14691da177e4SLinus Torvalds } 14701da177e4SLinus Torvalds 1471db8dac20SDavid S. Miller struct proto udp_prot = { 1472db8dac20SDavid S. Miller .name = "UDP", 1473db8dac20SDavid S. Miller .owner = THIS_MODULE, 1474db8dac20SDavid S. Miller .close = udp_lib_close, 1475db8dac20SDavid S. Miller .connect = ip4_datagram_connect, 1476db8dac20SDavid S. Miller .disconnect = udp_disconnect, 1477db8dac20SDavid S. Miller .ioctl = udp_ioctl, 1478db8dac20SDavid S. Miller .destroy = udp_destroy_sock, 1479db8dac20SDavid S. Miller .setsockopt = udp_setsockopt, 1480db8dac20SDavid S. Miller .getsockopt = udp_getsockopt, 1481db8dac20SDavid S. Miller .sendmsg = udp_sendmsg, 1482db8dac20SDavid S. Miller .recvmsg = udp_recvmsg, 1483db8dac20SDavid S. Miller .sendpage = udp_sendpage, 1484db8dac20SDavid S. Miller .backlog_rcv = udp_queue_rcv_skb, 1485db8dac20SDavid S. Miller .hash = udp_lib_hash, 1486db8dac20SDavid S. Miller .unhash = udp_lib_unhash, 1487db8dac20SDavid S. Miller .get_port = udp_v4_get_port, 1488db8dac20SDavid S. Miller .memory_allocated = &udp_memory_allocated, 1489db8dac20SDavid S. Miller .sysctl_mem = sysctl_udp_mem, 1490db8dac20SDavid S. Miller .sysctl_wmem = &sysctl_udp_wmem_min, 1491db8dac20SDavid S. Miller .sysctl_rmem = &sysctl_udp_rmem_min, 1492db8dac20SDavid S. Miller .obj_size = sizeof(struct udp_sock), 14936ba5a3c5SPavel Emelyanov .h.udp_hash = udp_hash, 1494db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1495db8dac20SDavid S. Miller .compat_setsockopt = compat_udp_setsockopt, 1496db8dac20SDavid S. Miller .compat_getsockopt = compat_udp_getsockopt, 1497db8dac20SDavid S. Miller #endif 1498db8dac20SDavid S. Miller }; 14991da177e4SLinus Torvalds 15001da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 15011da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 15021da177e4SLinus Torvalds 15031da177e4SLinus Torvalds static struct sock *udp_get_first(struct seq_file *seq) 15041da177e4SLinus Torvalds { 15051da177e4SLinus Torvalds struct sock *sk; 15061da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 15076f191efeSDenis V. Lunev struct net *net = seq_file_net(seq); 15081da177e4SLinus Torvalds 15091da177e4SLinus Torvalds for (state->bucket = 0; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) { 15101da177e4SLinus Torvalds struct hlist_node *node; 1511ba4e58ecSGerrit Renker sk_for_each(sk, node, state->hashtable + state->bucket) { 1512878628fbSYOSHIFUJI Hideaki if (!net_eq(sock_net(sk), net)) 1513a91275efSDaniel Lezcano continue; 15141da177e4SLinus Torvalds if (sk->sk_family == state->family) 15151da177e4SLinus Torvalds goto found; 15161da177e4SLinus Torvalds } 15171da177e4SLinus Torvalds } 15181da177e4SLinus Torvalds sk = NULL; 15191da177e4SLinus Torvalds found: 15201da177e4SLinus Torvalds return sk; 15211da177e4SLinus Torvalds } 15221da177e4SLinus Torvalds 15231da177e4SLinus Torvalds static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk) 15241da177e4SLinus Torvalds { 15251da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 15266f191efeSDenis V. Lunev struct net *net = seq_file_net(seq); 15271da177e4SLinus Torvalds 15281da177e4SLinus Torvalds do { 15291da177e4SLinus Torvalds sk = sk_next(sk); 15301da177e4SLinus Torvalds try_again: 15311da177e4SLinus Torvalds ; 1532878628fbSYOSHIFUJI Hideaki } while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family)); 15331da177e4SLinus Torvalds 15341da177e4SLinus Torvalds if (!sk && ++state->bucket < UDP_HTABLE_SIZE) { 1535ba4e58ecSGerrit Renker sk = sk_head(state->hashtable + state->bucket); 15361da177e4SLinus Torvalds goto try_again; 15371da177e4SLinus Torvalds } 15381da177e4SLinus Torvalds return sk; 15391da177e4SLinus Torvalds } 15401da177e4SLinus Torvalds 15411da177e4SLinus Torvalds static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos) 15421da177e4SLinus Torvalds { 15431da177e4SLinus Torvalds struct sock *sk = udp_get_first(seq); 15441da177e4SLinus Torvalds 15451da177e4SLinus Torvalds if (sk) 15461da177e4SLinus Torvalds while (pos && (sk = udp_get_next(seq, sk)) != NULL) 15471da177e4SLinus Torvalds --pos; 15481da177e4SLinus Torvalds return pos ? NULL : sk; 15491da177e4SLinus Torvalds } 15501da177e4SLinus Torvalds 15511da177e4SLinus Torvalds static void *udp_seq_start(struct seq_file *seq, loff_t *pos) 15529a429c49SEric Dumazet __acquires(udp_hash_lock) 15531da177e4SLinus Torvalds { 15541da177e4SLinus Torvalds read_lock(&udp_hash_lock); 1555b50660f1SYOSHIFUJI Hideaki return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN; 15561da177e4SLinus Torvalds } 15571da177e4SLinus Torvalds 15581da177e4SLinus Torvalds static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos) 15591da177e4SLinus Torvalds { 15601da177e4SLinus Torvalds struct sock *sk; 15611da177e4SLinus Torvalds 1562b50660f1SYOSHIFUJI Hideaki if (v == SEQ_START_TOKEN) 15631da177e4SLinus Torvalds sk = udp_get_idx(seq, 0); 15641da177e4SLinus Torvalds else 15651da177e4SLinus Torvalds sk = udp_get_next(seq, v); 15661da177e4SLinus Torvalds 15671da177e4SLinus Torvalds ++*pos; 15681da177e4SLinus Torvalds return sk; 15691da177e4SLinus Torvalds } 15701da177e4SLinus Torvalds 15711da177e4SLinus Torvalds static void udp_seq_stop(struct seq_file *seq, void *v) 15729a429c49SEric Dumazet __releases(udp_hash_lock) 15731da177e4SLinus Torvalds { 15741da177e4SLinus Torvalds read_unlock(&udp_hash_lock); 15751da177e4SLinus Torvalds } 15761da177e4SLinus Torvalds 15771da177e4SLinus Torvalds static int udp_seq_open(struct inode *inode, struct file *file) 15781da177e4SLinus Torvalds { 15791da177e4SLinus Torvalds struct udp_seq_afinfo *afinfo = PDE(inode)->data; 1580a2be75c1SDenis V. Lunev struct udp_iter_state *s; 1581a2be75c1SDenis V. Lunev int err; 15821da177e4SLinus Torvalds 1583a2be75c1SDenis V. Lunev err = seq_open_net(inode, file, &afinfo->seq_ops, 1584a2be75c1SDenis V. Lunev sizeof(struct udp_iter_state)); 1585a2be75c1SDenis V. Lunev if (err < 0) 1586a2be75c1SDenis V. Lunev return err; 1587a91275efSDaniel Lezcano 1588a2be75c1SDenis V. Lunev s = ((struct seq_file *)file->private_data)->private; 15891da177e4SLinus Torvalds s->family = afinfo->family; 1590ba4e58ecSGerrit Renker s->hashtable = afinfo->hashtable; 1591a2be75c1SDenis V. Lunev return err; 1592a91275efSDaniel Lezcano } 1593a91275efSDaniel Lezcano 15941da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 15950c96d8c5SDaniel Lezcano int udp_proc_register(struct net *net, struct udp_seq_afinfo *afinfo) 15961da177e4SLinus Torvalds { 15971da177e4SLinus Torvalds struct proc_dir_entry *p; 15981da177e4SLinus Torvalds int rc = 0; 15991da177e4SLinus Torvalds 16003ba9441bSDenis V. Lunev afinfo->seq_fops.open = udp_seq_open; 16013ba9441bSDenis V. Lunev afinfo->seq_fops.read = seq_read; 16023ba9441bSDenis V. Lunev afinfo->seq_fops.llseek = seq_lseek; 16033ba9441bSDenis V. Lunev afinfo->seq_fops.release = seq_release_net; 16041da177e4SLinus Torvalds 1605dda61925SDenis V. Lunev afinfo->seq_ops.start = udp_seq_start; 1606dda61925SDenis V. Lunev afinfo->seq_ops.next = udp_seq_next; 1607dda61925SDenis V. Lunev afinfo->seq_ops.stop = udp_seq_stop; 1608dda61925SDenis V. Lunev 160984841c3cSDenis V. Lunev p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net, 161084841c3cSDenis V. Lunev &afinfo->seq_fops, afinfo); 161184841c3cSDenis V. Lunev if (!p) 16121da177e4SLinus Torvalds rc = -ENOMEM; 16131da177e4SLinus Torvalds return rc; 16141da177e4SLinus Torvalds } 16151da177e4SLinus Torvalds 16160c96d8c5SDaniel Lezcano void udp_proc_unregister(struct net *net, struct udp_seq_afinfo *afinfo) 16171da177e4SLinus Torvalds { 16180c96d8c5SDaniel Lezcano proc_net_remove(net, afinfo->name); 16191da177e4SLinus Torvalds } 1620db8dac20SDavid S. Miller 1621db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 16225e659e4cSPavel Emelyanov static void udp4_format_sock(struct sock *sp, struct seq_file *f, 16235e659e4cSPavel Emelyanov int bucket, int *len) 1624db8dac20SDavid S. Miller { 1625db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sp); 1626db8dac20SDavid S. Miller __be32 dest = inet->daddr; 1627db8dac20SDavid S. Miller __be32 src = inet->rcv_saddr; 1628db8dac20SDavid S. Miller __u16 destp = ntohs(inet->dport); 1629db8dac20SDavid S. Miller __u16 srcp = ntohs(inet->sport); 1630db8dac20SDavid S. Miller 16315e659e4cSPavel Emelyanov seq_printf(f, "%4d: %08X:%04X %08X:%04X" 16325e659e4cSPavel Emelyanov " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p%n", 1633db8dac20SDavid S. Miller bucket, src, srcp, dest, destp, sp->sk_state, 1634db8dac20SDavid S. Miller atomic_read(&sp->sk_wmem_alloc), 1635db8dac20SDavid S. Miller atomic_read(&sp->sk_rmem_alloc), 1636db8dac20SDavid S. Miller 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp), 16375e659e4cSPavel Emelyanov atomic_read(&sp->sk_refcnt), sp, len); 1638db8dac20SDavid S. Miller } 1639db8dac20SDavid S. Miller 1640db8dac20SDavid S. Miller int udp4_seq_show(struct seq_file *seq, void *v) 1641db8dac20SDavid S. Miller { 1642db8dac20SDavid S. Miller if (v == SEQ_START_TOKEN) 1643db8dac20SDavid S. Miller seq_printf(seq, "%-127s\n", 1644db8dac20SDavid S. Miller " sl local_address rem_address st tx_queue " 1645db8dac20SDavid S. Miller "rx_queue tr tm->when retrnsmt uid timeout " 1646db8dac20SDavid S. Miller "inode"); 1647db8dac20SDavid S. Miller else { 1648db8dac20SDavid S. Miller struct udp_iter_state *state = seq->private; 16495e659e4cSPavel Emelyanov int len; 1650db8dac20SDavid S. Miller 16515e659e4cSPavel Emelyanov udp4_format_sock(v, seq, state->bucket, &len); 16525e659e4cSPavel Emelyanov seq_printf(seq, "%*s\n", 127 - len ,""); 1653db8dac20SDavid S. Miller } 1654db8dac20SDavid S. Miller return 0; 1655db8dac20SDavid S. Miller } 1656db8dac20SDavid S. Miller 1657db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 1658db8dac20SDavid S. Miller static struct udp_seq_afinfo udp4_seq_afinfo = { 1659db8dac20SDavid S. Miller .name = "udp", 1660db8dac20SDavid S. Miller .family = AF_INET, 1661db8dac20SDavid S. Miller .hashtable = udp_hash, 16624ad96d39SDenis V. Lunev .seq_fops = { 16634ad96d39SDenis V. Lunev .owner = THIS_MODULE, 16644ad96d39SDenis V. Lunev }, 1665dda61925SDenis V. Lunev .seq_ops = { 1666dda61925SDenis V. Lunev .show = udp4_seq_show, 1667dda61925SDenis V. Lunev }, 1668db8dac20SDavid S. Miller }; 1669db8dac20SDavid S. Miller 167015439febSPavel Emelyanov static int udp4_proc_init_net(struct net *net) 167115439febSPavel Emelyanov { 167215439febSPavel Emelyanov return udp_proc_register(net, &udp4_seq_afinfo); 167315439febSPavel Emelyanov } 167415439febSPavel Emelyanov 167515439febSPavel Emelyanov static void udp4_proc_exit_net(struct net *net) 167615439febSPavel Emelyanov { 167715439febSPavel Emelyanov udp_proc_unregister(net, &udp4_seq_afinfo); 167815439febSPavel Emelyanov } 167915439febSPavel Emelyanov 168015439febSPavel Emelyanov static struct pernet_operations udp4_net_ops = { 168115439febSPavel Emelyanov .init = udp4_proc_init_net, 168215439febSPavel Emelyanov .exit = udp4_proc_exit_net, 168315439febSPavel Emelyanov }; 168415439febSPavel Emelyanov 1685db8dac20SDavid S. Miller int __init udp4_proc_init(void) 1686db8dac20SDavid S. Miller { 168715439febSPavel Emelyanov return register_pernet_subsys(&udp4_net_ops); 1688db8dac20SDavid S. Miller } 1689db8dac20SDavid S. Miller 1690db8dac20SDavid S. Miller void udp4_proc_exit(void) 1691db8dac20SDavid S. Miller { 169215439febSPavel Emelyanov unregister_pernet_subsys(&udp4_net_ops); 1693db8dac20SDavid S. Miller } 16941da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 16951da177e4SLinus Torvalds 169695766fffSHideo Aoki void __init udp_init(void) 169795766fffSHideo Aoki { 169895766fffSHideo Aoki unsigned long limit; 169995766fffSHideo Aoki 170095766fffSHideo Aoki /* Set the pressure threshold up by the same strategy of TCP. It is a 170195766fffSHideo Aoki * fraction of global memory that is up to 1/2 at 256 MB, decreasing 170295766fffSHideo Aoki * toward zero with the amount of memory, with a floor of 128 pages. 170395766fffSHideo Aoki */ 170495766fffSHideo Aoki limit = min(nr_all_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT); 170595766fffSHideo Aoki limit = (limit * (nr_all_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11); 170695766fffSHideo Aoki limit = max(limit, 128UL); 170795766fffSHideo Aoki sysctl_udp_mem[0] = limit / 4 * 3; 170895766fffSHideo Aoki sysctl_udp_mem[1] = limit; 170995766fffSHideo Aoki sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2; 171095766fffSHideo Aoki 171195766fffSHideo Aoki sysctl_udp_rmem_min = SK_MEM_QUANTUM; 171295766fffSHideo Aoki sysctl_udp_wmem_min = SK_MEM_QUANTUM; 171395766fffSHideo Aoki } 171495766fffSHideo Aoki 17151da177e4SLinus Torvalds EXPORT_SYMBOL(udp_disconnect); 17161da177e4SLinus Torvalds EXPORT_SYMBOL(udp_hash); 17171da177e4SLinus Torvalds EXPORT_SYMBOL(udp_hash_lock); 17181da177e4SLinus Torvalds EXPORT_SYMBOL(udp_ioctl); 1719db8dac20SDavid S. Miller EXPORT_SYMBOL(udp_prot); 1720db8dac20SDavid S. Miller EXPORT_SYMBOL(udp_sendmsg); 17214c0a6cb0SGerrit Renker EXPORT_SYMBOL(udp_lib_getsockopt); 17224c0a6cb0SGerrit Renker EXPORT_SYMBOL(udp_lib_setsockopt); 17231da177e4SLinus Torvalds EXPORT_SYMBOL(udp_poll); 17246ba5a3c5SPavel Emelyanov EXPORT_SYMBOL(udp_lib_get_port); 17251da177e4SLinus Torvalds 17261da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 17271da177e4SLinus Torvalds EXPORT_SYMBOL(udp_proc_register); 17281da177e4SLinus Torvalds EXPORT_SYMBOL(udp_proc_unregister); 17291da177e4SLinus Torvalds #endif 1730