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 * 81da177e4SLinus Torvalds * Version: $Id: udp.c,v 1.102 2002/02/01 22:01:04 davem Exp $ 91da177e4SLinus Torvalds * 1002c30a84SJesper Juhl * Authors: Ross Biro 111da177e4SLinus Torvalds * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 121da177e4SLinus Torvalds * Arnt Gulbrandsen, <agulbra@nvg.unit.no> 131da177e4SLinus Torvalds * Alan Cox, <Alan.Cox@linux.org> 141da177e4SLinus Torvalds * Hirokazu Takahashi, <taka@valinux.co.jp> 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * Fixes: 171da177e4SLinus Torvalds * Alan Cox : verify_area() calls 181da177e4SLinus Torvalds * Alan Cox : stopped close while in use off icmp 191da177e4SLinus Torvalds * messages. Not a fix but a botch that 201da177e4SLinus Torvalds * for udp at least is 'valid'. 211da177e4SLinus Torvalds * Alan Cox : Fixed icmp handling properly 221da177e4SLinus Torvalds * Alan Cox : Correct error for oversized datagrams 231da177e4SLinus Torvalds * Alan Cox : Tidied select() semantics. 241da177e4SLinus Torvalds * Alan Cox : udp_err() fixed properly, also now 251da177e4SLinus Torvalds * select and read wake correctly on errors 261da177e4SLinus Torvalds * Alan Cox : udp_send verify_area moved to avoid mem leak 271da177e4SLinus Torvalds * Alan Cox : UDP can count its memory 281da177e4SLinus Torvalds * Alan Cox : send to an unknown connection causes 291da177e4SLinus Torvalds * an ECONNREFUSED off the icmp, but 301da177e4SLinus Torvalds * does NOT close. 311da177e4SLinus Torvalds * Alan Cox : Switched to new sk_buff handlers. No more backlog! 321da177e4SLinus Torvalds * Alan Cox : Using generic datagram code. Even smaller and the PEEK 331da177e4SLinus Torvalds * bug no longer crashes it. 341da177e4SLinus Torvalds * Fred Van Kempen : Net2e support for sk->broadcast. 351da177e4SLinus Torvalds * Alan Cox : Uses skb_free_datagram 361da177e4SLinus Torvalds * Alan Cox : Added get/set sockopt support. 371da177e4SLinus Torvalds * Alan Cox : Broadcasting without option set returns EACCES. 381da177e4SLinus Torvalds * Alan Cox : No wakeup calls. Instead we now use the callbacks. 391da177e4SLinus Torvalds * Alan Cox : Use ip_tos and ip_ttl 401da177e4SLinus Torvalds * Alan Cox : SNMP Mibs 411da177e4SLinus Torvalds * Alan Cox : MSG_DONTROUTE, and 0.0.0.0 support. 421da177e4SLinus Torvalds * Matt Dillon : UDP length checks. 431da177e4SLinus Torvalds * Alan Cox : Smarter af_inet used properly. 441da177e4SLinus Torvalds * Alan Cox : Use new kernel side addressing. 451da177e4SLinus Torvalds * Alan Cox : Incorrect return on truncated datagram receive. 461da177e4SLinus Torvalds * Arnt Gulbrandsen : New udp_send and stuff 471da177e4SLinus Torvalds * Alan Cox : Cache last socket 481da177e4SLinus Torvalds * Alan Cox : Route cache 491da177e4SLinus Torvalds * Jon Peatfield : Minor efficiency fix to sendto(). 501da177e4SLinus Torvalds * Mike Shaver : RFC1122 checks. 511da177e4SLinus Torvalds * Alan Cox : Nonblocking error fix. 521da177e4SLinus Torvalds * Willy Konynenberg : Transparent proxying support. 531da177e4SLinus Torvalds * Mike McLagan : Routing by source 541da177e4SLinus Torvalds * David S. Miller : New socket lookup architecture. 551da177e4SLinus Torvalds * Last socket cache retained as it 561da177e4SLinus Torvalds * does have a high hit rate. 571da177e4SLinus Torvalds * Olaf Kirch : Don't linearise iovec on sendmsg. 581da177e4SLinus Torvalds * Andi Kleen : Some cleanups, cache destination entry 591da177e4SLinus Torvalds * for connect. 601da177e4SLinus Torvalds * Vitaly E. Lavrov : Transparent proxy revived after year coma. 611da177e4SLinus Torvalds * Melvin Smith : Check msg_name not msg_namelen in sendto(), 621da177e4SLinus Torvalds * return ENOTCONN for unconnected sockets (POSIX) 631da177e4SLinus Torvalds * Janos Farkas : don't deliver multi/broadcasts to a different 641da177e4SLinus Torvalds * bound-to-device socket 651da177e4SLinus Torvalds * Hirokazu Takahashi : HW checksumming for outgoing UDP 661da177e4SLinus Torvalds * datagrams. 671da177e4SLinus Torvalds * Hirokazu Takahashi : sendfile() on UDP works now. 681da177e4SLinus Torvalds * Arnaldo C. Melo : convert /proc/net/udp to seq_file 691da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which 701da177e4SLinus Torvalds * Alexey Kuznetsov: allow both IPv4 and IPv6 sockets to bind 711da177e4SLinus Torvalds * a single port at the same time. 721da177e4SLinus Torvalds * Derek Atkins <derek@ihtfp.com>: Add Encapulation Support 73342f0234SJames Chapman * James Chapman : Add L2TP encapsulation type. 741da177e4SLinus Torvalds * 751da177e4SLinus Torvalds * 761da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 771da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 781da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 791da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 801da177e4SLinus Torvalds */ 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds #include <asm/system.h> 831da177e4SLinus Torvalds #include <asm/uaccess.h> 841da177e4SLinus Torvalds #include <asm/ioctls.h> 85*95766fffSHideo Aoki #include <linux/bootmem.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 1091da177e4SLinus Torvalds /* 1101da177e4SLinus Torvalds * Snmp MIB for the UDP layer 1111da177e4SLinus Torvalds */ 1121da177e4SLinus Torvalds 113ba89966cSEric Dumazet DEFINE_SNMP_STAT(struct udp_mib, udp_statistics) __read_mostly; 1141781f7f5SHerbert Xu EXPORT_SYMBOL(udp_statistics); 1151da177e4SLinus Torvalds 1169055e051SHerbert Xu DEFINE_SNMP_STAT(struct udp_mib, udp_stats_in6) __read_mostly; 1179055e051SHerbert Xu EXPORT_SYMBOL(udp_stats_in6); 1189055e051SHerbert Xu 1191da177e4SLinus Torvalds struct hlist_head udp_hash[UDP_HTABLE_SIZE]; 1201da177e4SLinus Torvalds DEFINE_RWLOCK(udp_hash_lock); 1211da177e4SLinus Torvalds 122*95766fffSHideo Aoki int sysctl_udp_mem[3] __read_mostly; 123*95766fffSHideo Aoki int sysctl_udp_rmem_min __read_mostly; 124*95766fffSHideo Aoki int sysctl_udp_wmem_min __read_mostly; 125*95766fffSHideo Aoki 126*95766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_mem); 127*95766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_rmem_min); 128*95766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_wmem_min); 129*95766fffSHideo Aoki 130*95766fffSHideo Aoki atomic_t udp_memory_allocated; 131*95766fffSHideo Aoki EXPORT_SYMBOL(udp_memory_allocated); 132*95766fffSHideo Aoki 13332c1da70SStephen Hemminger static inline int __udp_lib_lport_inuse(__u16 num, 13432c1da70SStephen Hemminger const struct hlist_head udptable[]) 13525030a7fSGerrit Renker { 13625030a7fSGerrit Renker struct sock *sk; 13725030a7fSGerrit Renker struct hlist_node *node; 13825030a7fSGerrit Renker 139df2bc459SDavid S. Miller sk_for_each(sk, node, &udptable[num & (UDP_HTABLE_SIZE - 1)]) 140df2bc459SDavid S. Miller if (sk->sk_hash == num) 141fc038410SDavid S. Miller return 1; 14225030a7fSGerrit Renker return 0; 14325030a7fSGerrit Renker } 14425030a7fSGerrit Renker 14525030a7fSGerrit Renker /** 146ba4e58ecSGerrit Renker * __udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6 14725030a7fSGerrit Renker * 14825030a7fSGerrit Renker * @sk: socket struct in question 14925030a7fSGerrit Renker * @snum: port number to look up 150ba4e58ecSGerrit Renker * @udptable: hash list table, must be of UDP_HTABLE_SIZE 151df2bc459SDavid S. Miller * @saddr_comp: AF-dependent comparison of bound local IP addresses 15225030a7fSGerrit Renker */ 153ba4e58ecSGerrit Renker int __udp_lib_get_port(struct sock *sk, unsigned short snum, 15432c1da70SStephen Hemminger struct hlist_head udptable[], 155df2bc459SDavid S. Miller int (*saddr_comp)(const struct sock *sk1, 156df2bc459SDavid S. Miller const struct sock *sk2 ) ) 1571da177e4SLinus Torvalds { 1581da177e4SLinus Torvalds struct hlist_node *node; 15925030a7fSGerrit Renker struct hlist_head *head; 1601da177e4SLinus Torvalds struct sock *sk2; 16125030a7fSGerrit Renker int error = 1; 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds write_lock_bh(&udp_hash_lock); 1641da177e4SLinus Torvalds 16532c1da70SStephen Hemminger if (!snum) { 166a25de534SAnton Arapov int i, low, high, remaining; 16732c1da70SStephen Hemminger unsigned rover, best, best_size_so_far; 1681da177e4SLinus Torvalds 169227b60f5SStephen Hemminger inet_get_local_port_range(&low, &high); 170a25de534SAnton Arapov remaining = (high - low) + 1; 171227b60f5SStephen Hemminger 17232c1da70SStephen Hemminger best_size_so_far = UINT_MAX; 173a25de534SAnton Arapov best = rover = net_random() % remaining + low; 17432c1da70SStephen Hemminger 17532c1da70SStephen Hemminger /* 1st pass: look for empty (or shortest) hash chain */ 17632c1da70SStephen Hemminger for (i = 0; i < UDP_HTABLE_SIZE; i++) { 17732c1da70SStephen Hemminger int size = 0; 17832c1da70SStephen Hemminger 17932c1da70SStephen Hemminger head = &udptable[rover & (UDP_HTABLE_SIZE - 1)]; 18032c1da70SStephen Hemminger if (hlist_empty(head)) 1811da177e4SLinus Torvalds goto gotit; 18232c1da70SStephen Hemminger 1835c668704SDavid S. Miller sk_for_each(sk2, node, head) { 1845c668704SDavid S. Miller if (++size >= best_size_so_far) 1855c668704SDavid S. Miller goto next; 1865c668704SDavid S. Miller } 1871da177e4SLinus Torvalds best_size_so_far = size; 18832c1da70SStephen Hemminger best = rover; 1895c668704SDavid S. Miller next: 19032c1da70SStephen Hemminger /* fold back if end of range */ 19132c1da70SStephen Hemminger if (++rover > high) 19232c1da70SStephen Hemminger rover = low + ((rover - low) 19332c1da70SStephen Hemminger & (UDP_HTABLE_SIZE - 1)); 19432c1da70SStephen Hemminger 19532c1da70SStephen Hemminger 1961da177e4SLinus Torvalds } 19732c1da70SStephen Hemminger 19832c1da70SStephen Hemminger /* 2nd pass: find hole in shortest hash chain */ 19932c1da70SStephen Hemminger rover = best; 20032c1da70SStephen Hemminger for (i = 0; i < (1 << 16) / UDP_HTABLE_SIZE; i++) { 20132c1da70SStephen Hemminger if (! __udp_lib_lport_inuse(rover, udptable)) 20232c1da70SStephen Hemminger goto gotit; 20332c1da70SStephen Hemminger rover += UDP_HTABLE_SIZE; 20432c1da70SStephen Hemminger if (rover > high) 20532c1da70SStephen Hemminger rover = low + ((rover - low) 20632c1da70SStephen Hemminger & (UDP_HTABLE_SIZE - 1)); 2071da177e4SLinus Torvalds } 20832c1da70SStephen Hemminger 20932c1da70SStephen Hemminger 21032c1da70SStephen Hemminger /* All ports in use! */ 2111da177e4SLinus Torvalds goto fail; 21232c1da70SStephen Hemminger 2131da177e4SLinus Torvalds gotit: 21432c1da70SStephen Hemminger snum = rover; 2151da177e4SLinus Torvalds } else { 216df2bc459SDavid S. Miller head = &udptable[snum & (UDP_HTABLE_SIZE - 1)]; 2171da177e4SLinus Torvalds 21825030a7fSGerrit Renker sk_for_each(sk2, node, head) 219df2bc459SDavid S. Miller if (sk2->sk_hash == snum && 2201da177e4SLinus Torvalds sk2 != sk && 22125030a7fSGerrit Renker (!sk2->sk_reuse || !sk->sk_reuse) && 222df2bc459SDavid S. Miller (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if 223df2bc459SDavid S. Miller || sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 224df2bc459SDavid S. Miller (*saddr_comp)(sk, sk2) ) 2251da177e4SLinus Torvalds goto fail; 2261da177e4SLinus Torvalds } 22732c1da70SStephen Hemminger 22825030a7fSGerrit Renker inet_sk(sk)->num = snum; 229df2bc459SDavid S. Miller sk->sk_hash = snum; 2301da177e4SLinus Torvalds if (sk_unhashed(sk)) { 231df2bc459SDavid S. Miller head = &udptable[snum & (UDP_HTABLE_SIZE - 1)]; 23225030a7fSGerrit Renker sk_add_node(sk, head); 2331da177e4SLinus Torvalds sock_prot_inc_use(sk->sk_prot); 2341da177e4SLinus Torvalds } 23525030a7fSGerrit Renker error = 0; 2361da177e4SLinus Torvalds fail: 2371da177e4SLinus Torvalds write_unlock_bh(&udp_hash_lock); 23825030a7fSGerrit Renker return error; 2391da177e4SLinus Torvalds } 2401da177e4SLinus Torvalds 2413fbe070aSStephen Hemminger int udp_get_port(struct sock *sk, unsigned short snum, 242df2bc459SDavid S. Miller int (*scmp)(const struct sock *, const struct sock *)) 243ba4e58ecSGerrit Renker { 24432c1da70SStephen Hemminger return __udp_lib_get_port(sk, snum, udp_hash, scmp); 245ba4e58ecSGerrit Renker } 246ba4e58ecSGerrit Renker 247df2bc459SDavid S. Miller int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2) 24825030a7fSGerrit Renker { 24925030a7fSGerrit Renker struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2); 25025030a7fSGerrit Renker 25125030a7fSGerrit Renker return ( !ipv6_only_sock(sk2) && 25225030a7fSGerrit Renker (!inet1->rcv_saddr || !inet2->rcv_saddr || 25325030a7fSGerrit Renker inet1->rcv_saddr == inet2->rcv_saddr )); 25425030a7fSGerrit Renker } 25525030a7fSGerrit Renker 25625030a7fSGerrit Renker static inline int udp_v4_get_port(struct sock *sk, unsigned short snum) 25725030a7fSGerrit Renker { 258df2bc459SDavid S. Miller return udp_get_port(sk, snum, ipv4_rcv_saddr_equal); 25925030a7fSGerrit Renker } 26025030a7fSGerrit Renker 2611da177e4SLinus Torvalds /* UDP is nearly always wildcards out the wazoo, it makes no sense to try 2621da177e4SLinus Torvalds * harder than this. -DaveM 2631da177e4SLinus Torvalds */ 264ba4e58ecSGerrit Renker static struct sock *__udp4_lib_lookup(__be32 saddr, __be16 sport, 265ba4e58ecSGerrit Renker __be32 daddr, __be16 dport, 266ba4e58ecSGerrit Renker int dif, struct hlist_head udptable[]) 2671da177e4SLinus Torvalds { 2681da177e4SLinus Torvalds struct sock *sk, *result = NULL; 2691da177e4SLinus Torvalds struct hlist_node *node; 270df2bc459SDavid S. Miller unsigned short hnum = ntohs(dport); 271df2bc459SDavid S. Miller int badness = -1; 2721da177e4SLinus Torvalds 273ba4e58ecSGerrit Renker read_lock(&udp_hash_lock); 274df2bc459SDavid S. Miller sk_for_each(sk, node, &udptable[hnum & (UDP_HTABLE_SIZE - 1)]) { 2751da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 2761da177e4SLinus Torvalds 277df2bc459SDavid S. Miller if (sk->sk_hash == hnum && !ipv6_only_sock(sk)) { 278df2bc459SDavid S. Miller int score = (sk->sk_family == PF_INET ? 1 : 0); 2791da177e4SLinus Torvalds if (inet->rcv_saddr) { 2801da177e4SLinus Torvalds if (inet->rcv_saddr != daddr) 2811da177e4SLinus Torvalds continue; 2821da177e4SLinus Torvalds score+=2; 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds if (inet->daddr) { 2851da177e4SLinus Torvalds if (inet->daddr != saddr) 2861da177e4SLinus Torvalds continue; 2871da177e4SLinus Torvalds score+=2; 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds if (inet->dport) { 2901da177e4SLinus Torvalds if (inet->dport != sport) 2911da177e4SLinus Torvalds continue; 2921da177e4SLinus Torvalds score+=2; 2931da177e4SLinus Torvalds } 2941da177e4SLinus Torvalds if (sk->sk_bound_dev_if) { 2951da177e4SLinus Torvalds if (sk->sk_bound_dev_if != dif) 2961da177e4SLinus Torvalds continue; 2971da177e4SLinus Torvalds score+=2; 2981da177e4SLinus Torvalds } 2991da177e4SLinus Torvalds if (score == 9) { 3001da177e4SLinus Torvalds result = sk; 301df2bc459SDavid S. Miller break; 302df2bc459SDavid S. Miller } else if (score > badness) { 3031da177e4SLinus Torvalds result = sk; 304df2bc459SDavid S. Miller badness = score; 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds } 308ba4e58ecSGerrit Renker if (result) 309ba4e58ecSGerrit Renker sock_hold(result); 3101da177e4SLinus Torvalds read_unlock(&udp_hash_lock); 311ba4e58ecSGerrit Renker return result; 3121da177e4SLinus Torvalds } 3131da177e4SLinus Torvalds 314df2bc459SDavid S. Miller static inline struct sock *udp_v4_mcast_next(struct sock *sk, 315df2bc459SDavid S. Miller __be16 loc_port, __be32 loc_addr, 316734ab87fSAl Viro __be16 rmt_port, __be32 rmt_addr, 3171da177e4SLinus Torvalds int dif) 3181da177e4SLinus Torvalds { 3191da177e4SLinus Torvalds struct hlist_node *node; 3201da177e4SLinus Torvalds struct sock *s = sk; 321df2bc459SDavid S. Miller unsigned short hnum = ntohs(loc_port); 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds sk_for_each_from(s, node) { 3241da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(s); 3251da177e4SLinus Torvalds 32695f30b33SEric Dumazet if (s->sk_hash != hnum || 3271da177e4SLinus Torvalds (inet->daddr && inet->daddr != rmt_addr) || 3281da177e4SLinus Torvalds (inet->dport != rmt_port && inet->dport) || 3291da177e4SLinus Torvalds (inet->rcv_saddr && inet->rcv_saddr != loc_addr) || 3301da177e4SLinus Torvalds ipv6_only_sock(s) || 3311da177e4SLinus Torvalds (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)) 3321da177e4SLinus Torvalds continue; 3331da177e4SLinus Torvalds if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif)) 3341da177e4SLinus Torvalds continue; 3351da177e4SLinus Torvalds goto found; 3361da177e4SLinus Torvalds } 3371da177e4SLinus Torvalds s = NULL; 3381da177e4SLinus Torvalds found: 3391da177e4SLinus Torvalds return s; 3401da177e4SLinus Torvalds } 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds /* 3431da177e4SLinus Torvalds * This routine is called by the ICMP module when it gets some 3441da177e4SLinus Torvalds * sort of error condition. If err < 0 then the socket should 3451da177e4SLinus Torvalds * be closed and the error returned to the user. If err > 0 3461da177e4SLinus Torvalds * it's just the icmp type << 8 | icmp code. 3471da177e4SLinus Torvalds * Header points to the ip header of the error packet. We move 3481da177e4SLinus Torvalds * on past this. Then (as it used to claim before adjustment) 3491da177e4SLinus Torvalds * header points to the first 8 bytes of the udp header. We need 3501da177e4SLinus Torvalds * to find the appropriate port. 3511da177e4SLinus Torvalds */ 3521da177e4SLinus Torvalds 353ba4e58ecSGerrit Renker void __udp4_lib_err(struct sk_buff *skb, u32 info, struct hlist_head udptable[]) 3541da177e4SLinus Torvalds { 3551da177e4SLinus Torvalds struct inet_sock *inet; 3561da177e4SLinus Torvalds struct iphdr *iph = (struct iphdr*)skb->data; 3571da177e4SLinus Torvalds struct udphdr *uh = (struct udphdr*)(skb->data+(iph->ihl<<2)); 35888c7664fSArnaldo Carvalho de Melo const int type = icmp_hdr(skb)->type; 35988c7664fSArnaldo Carvalho de Melo const int code = icmp_hdr(skb)->code; 3601da177e4SLinus Torvalds struct sock *sk; 3611da177e4SLinus Torvalds int harderr; 3621da177e4SLinus Torvalds int err; 3631da177e4SLinus Torvalds 364ba4e58ecSGerrit Renker sk = __udp4_lib_lookup(iph->daddr, uh->dest, iph->saddr, uh->source, 365ba4e58ecSGerrit Renker skb->dev->ifindex, udptable ); 3661da177e4SLinus Torvalds if (sk == NULL) { 3671da177e4SLinus Torvalds ICMP_INC_STATS_BH(ICMP_MIB_INERRORS); 3681da177e4SLinus Torvalds return; /* No socket for error */ 3691da177e4SLinus Torvalds } 3701da177e4SLinus Torvalds 3711da177e4SLinus Torvalds err = 0; 3721da177e4SLinus Torvalds harderr = 0; 3731da177e4SLinus Torvalds inet = inet_sk(sk); 3741da177e4SLinus Torvalds 3751da177e4SLinus Torvalds switch (type) { 3761da177e4SLinus Torvalds default: 3771da177e4SLinus Torvalds case ICMP_TIME_EXCEEDED: 3781da177e4SLinus Torvalds err = EHOSTUNREACH; 3791da177e4SLinus Torvalds break; 3801da177e4SLinus Torvalds case ICMP_SOURCE_QUENCH: 3811da177e4SLinus Torvalds goto out; 3821da177e4SLinus Torvalds case ICMP_PARAMETERPROB: 3831da177e4SLinus Torvalds err = EPROTO; 3841da177e4SLinus Torvalds harderr = 1; 3851da177e4SLinus Torvalds break; 3861da177e4SLinus Torvalds case ICMP_DEST_UNREACH: 3871da177e4SLinus Torvalds if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */ 3881da177e4SLinus Torvalds if (inet->pmtudisc != IP_PMTUDISC_DONT) { 3891da177e4SLinus Torvalds err = EMSGSIZE; 3901da177e4SLinus Torvalds harderr = 1; 3911da177e4SLinus Torvalds break; 3921da177e4SLinus Torvalds } 3931da177e4SLinus Torvalds goto out; 3941da177e4SLinus Torvalds } 3951da177e4SLinus Torvalds err = EHOSTUNREACH; 3961da177e4SLinus Torvalds if (code <= NR_ICMP_UNREACH) { 3971da177e4SLinus Torvalds harderr = icmp_err_convert[code].fatal; 3981da177e4SLinus Torvalds err = icmp_err_convert[code].errno; 3991da177e4SLinus Torvalds } 4001da177e4SLinus Torvalds break; 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds /* 4041da177e4SLinus Torvalds * RFC1122: OK. Passes ICMP errors back to application, as per 4051da177e4SLinus Torvalds * 4.1.3.3. 4061da177e4SLinus Torvalds */ 4071da177e4SLinus Torvalds if (!inet->recverr) { 4081da177e4SLinus Torvalds if (!harderr || sk->sk_state != TCP_ESTABLISHED) 4091da177e4SLinus Torvalds goto out; 4101da177e4SLinus Torvalds } else { 4111da177e4SLinus Torvalds ip_icmp_error(sk, skb, err, uh->dest, info, (u8*)(uh+1)); 4121da177e4SLinus Torvalds } 4131da177e4SLinus Torvalds sk->sk_err = err; 4141da177e4SLinus Torvalds sk->sk_error_report(sk); 4151da177e4SLinus Torvalds out: 4161da177e4SLinus Torvalds sock_put(sk); 4171da177e4SLinus Torvalds } 4181da177e4SLinus Torvalds 4193fbe070aSStephen Hemminger void udp_err(struct sk_buff *skb, u32 info) 420ba4e58ecSGerrit Renker { 421ba4e58ecSGerrit Renker return __udp4_lib_err(skb, info, udp_hash); 422ba4e58ecSGerrit Renker } 423ba4e58ecSGerrit Renker 4241da177e4SLinus Torvalds /* 4251da177e4SLinus Torvalds * Throw away all pending data and cancel the corking. Socket is locked. 4261da177e4SLinus Torvalds */ 4271da177e4SLinus Torvalds static void udp_flush_pending_frames(struct sock *sk) 4281da177e4SLinus Torvalds { 4291da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 4301da177e4SLinus Torvalds 4311da177e4SLinus Torvalds if (up->pending) { 4321da177e4SLinus Torvalds up->len = 0; 4331da177e4SLinus Torvalds up->pending = 0; 4341da177e4SLinus Torvalds ip_flush_pending_frames(sk); 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds 438ba4e58ecSGerrit Renker /** 439ba4e58ecSGerrit Renker * udp4_hwcsum_outgoing - handle outgoing HW checksumming 440ba4e58ecSGerrit Renker * @sk: socket we are sending on 441ba4e58ecSGerrit Renker * @skb: sk_buff containing the filled-in UDP header 442ba4e58ecSGerrit Renker * (checksum field must be zeroed out) 443ba4e58ecSGerrit Renker */ 444ba4e58ecSGerrit Renker static void udp4_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb, 445ba4e58ecSGerrit Renker __be32 src, __be32 dst, int len ) 446ba4e58ecSGerrit Renker { 4476b11687eSAl Viro unsigned int offset; 4484bedb452SArnaldo Carvalho de Melo struct udphdr *uh = udp_hdr(skb); 4496b11687eSAl Viro __wsum csum = 0; 450ba4e58ecSGerrit Renker 451ba4e58ecSGerrit Renker if (skb_queue_len(&sk->sk_write_queue) == 1) { 452ba4e58ecSGerrit Renker /* 453ba4e58ecSGerrit Renker * Only one fragment on the socket. 454ba4e58ecSGerrit Renker */ 455663ead3bSHerbert Xu skb->csum_start = skb_transport_header(skb) - skb->head; 456ff1dcadbSAl Viro skb->csum_offset = offsetof(struct udphdr, check); 457ba4e58ecSGerrit Renker uh->check = ~csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, 0); 458ba4e58ecSGerrit Renker } else { 459ba4e58ecSGerrit Renker /* 460ba4e58ecSGerrit Renker * HW-checksum won't work as there are two or more 461ba4e58ecSGerrit Renker * fragments on the socket so that all csums of sk_buffs 462ba4e58ecSGerrit Renker * should be together 463ba4e58ecSGerrit Renker */ 464ea2ae17dSArnaldo Carvalho de Melo offset = skb_transport_offset(skb); 465ba4e58ecSGerrit Renker skb->csum = skb_checksum(skb, offset, skb->len - offset, 0); 466ba4e58ecSGerrit Renker 467ba4e58ecSGerrit Renker skb->ip_summed = CHECKSUM_NONE; 468ba4e58ecSGerrit Renker 469ba4e58ecSGerrit Renker skb_queue_walk(&sk->sk_write_queue, skb) { 470ba4e58ecSGerrit Renker csum = csum_add(csum, skb->csum); 471ba4e58ecSGerrit Renker } 472ba4e58ecSGerrit Renker 473ba4e58ecSGerrit Renker uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum); 474ba4e58ecSGerrit Renker if (uh->check == 0) 475f6ab0288SAl Viro uh->check = CSUM_MANGLED_0; 476ba4e58ecSGerrit Renker } 477ba4e58ecSGerrit Renker } 478ba4e58ecSGerrit Renker 4791da177e4SLinus Torvalds /* 4801da177e4SLinus Torvalds * Push out all pending data as one UDP datagram. Socket is locked. 4811da177e4SLinus Torvalds */ 4824c0a6cb0SGerrit Renker static int udp_push_pending_frames(struct sock *sk) 4831da177e4SLinus Torvalds { 4844c0a6cb0SGerrit Renker struct udp_sock *up = udp_sk(sk); 4851da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 4861da177e4SLinus Torvalds struct flowi *fl = &inet->cork.fl; 4871da177e4SLinus Torvalds struct sk_buff *skb; 4881da177e4SLinus Torvalds struct udphdr *uh; 4891da177e4SLinus Torvalds int err = 0; 490b2bf1e26SWang Chen int is_udplite = IS_UDPLITE(sk); 4918e5200f5SAl Viro __wsum csum = 0; 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds /* Grab the skbuff where UDP header space exists. */ 4941da177e4SLinus Torvalds if ((skb = skb_peek(&sk->sk_write_queue)) == NULL) 4951da177e4SLinus Torvalds goto out; 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds /* 4981da177e4SLinus Torvalds * Create a UDP header 4991da177e4SLinus Torvalds */ 5004bedb452SArnaldo Carvalho de Melo uh = udp_hdr(skb); 5011da177e4SLinus Torvalds uh->source = fl->fl_ip_sport; 5021da177e4SLinus Torvalds uh->dest = fl->fl_ip_dport; 5031da177e4SLinus Torvalds uh->len = htons(up->len); 5041da177e4SLinus Torvalds uh->check = 0; 5051da177e4SLinus Torvalds 506b2bf1e26SWang Chen if (is_udplite) /* UDP-Lite */ 507ba4e58ecSGerrit Renker csum = udplite_csum_outgoing(sk, skb); 508ba4e58ecSGerrit Renker 509ba4e58ecSGerrit Renker else if (sk->sk_no_check == UDP_CSUM_NOXMIT) { /* UDP csum disabled */ 510ba4e58ecSGerrit Renker 5111da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 5121da177e4SLinus Torvalds goto send; 5131da177e4SLinus Torvalds 514ba4e58ecSGerrit Renker } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 515ba4e58ecSGerrit Renker 516ba4e58ecSGerrit Renker udp4_hwcsum_outgoing(sk, skb, fl->fl4_src,fl->fl4_dst, up->len); 517ba4e58ecSGerrit Renker goto send; 518ba4e58ecSGerrit Renker 519ba4e58ecSGerrit Renker } else /* `normal' UDP */ 520ba4e58ecSGerrit Renker csum = udp_csum_outgoing(sk, skb); 521ba4e58ecSGerrit Renker 522ba4e58ecSGerrit Renker /* add protocol-dependent pseudo-header */ 523ba4e58ecSGerrit Renker uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst, up->len, 524ba4e58ecSGerrit Renker sk->sk_protocol, csum ); 5251da177e4SLinus Torvalds if (uh->check == 0) 526f6ab0288SAl Viro uh->check = CSUM_MANGLED_0; 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds send: 5291da177e4SLinus Torvalds err = ip_push_pending_frames(sk); 5301da177e4SLinus Torvalds out: 5311da177e4SLinus Torvalds up->len = 0; 5321da177e4SLinus Torvalds up->pending = 0; 5332a0c6c98SYOSHIFUJI Hideaki if (!err) 534b2bf1e26SWang Chen UDP_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS, is_udplite); 5351da177e4SLinus Torvalds return err; 5361da177e4SLinus Torvalds } 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 5391da177e4SLinus Torvalds size_t len) 5401da177e4SLinus Torvalds { 5411da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 5421da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 5431da177e4SLinus Torvalds int ulen = len; 5441da177e4SLinus Torvalds struct ipcm_cookie ipc; 5451da177e4SLinus Torvalds struct rtable *rt = NULL; 5461da177e4SLinus Torvalds int free = 0; 5471da177e4SLinus Torvalds int connected = 0; 5483ca3c68eSAl Viro __be32 daddr, faddr, saddr; 549734ab87fSAl Viro __be16 dport; 5501da177e4SLinus Torvalds u8 tos; 551b2bf1e26SWang Chen int err, is_udplite = IS_UDPLITE(sk); 5521da177e4SLinus Torvalds int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; 553ba4e58ecSGerrit Renker int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds if (len > 0xFFFF) 5561da177e4SLinus Torvalds return -EMSGSIZE; 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds /* 5591da177e4SLinus Torvalds * Check the flags. 5601da177e4SLinus Torvalds */ 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds if (msg->msg_flags&MSG_OOB) /* Mirror BSD error message compatibility */ 5631da177e4SLinus Torvalds return -EOPNOTSUPP; 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds ipc.opt = NULL; 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds if (up->pending) { 5681da177e4SLinus Torvalds /* 5691da177e4SLinus Torvalds * There are pending frames. 5701da177e4SLinus Torvalds * The socket lock must be held while it's corked. 5711da177e4SLinus Torvalds */ 5721da177e4SLinus Torvalds lock_sock(sk); 5731da177e4SLinus Torvalds if (likely(up->pending)) { 5741da177e4SLinus Torvalds if (unlikely(up->pending != AF_INET)) { 5751da177e4SLinus Torvalds release_sock(sk); 5761da177e4SLinus Torvalds return -EINVAL; 5771da177e4SLinus Torvalds } 5781da177e4SLinus Torvalds goto do_append_data; 5791da177e4SLinus Torvalds } 5801da177e4SLinus Torvalds release_sock(sk); 5811da177e4SLinus Torvalds } 5821da177e4SLinus Torvalds ulen += sizeof(struct udphdr); 5831da177e4SLinus Torvalds 5841da177e4SLinus Torvalds /* 5851da177e4SLinus Torvalds * Get and verify the address. 5861da177e4SLinus Torvalds */ 5871da177e4SLinus Torvalds if (msg->msg_name) { 5881da177e4SLinus Torvalds struct sockaddr_in * usin = (struct sockaddr_in*)msg->msg_name; 5891da177e4SLinus Torvalds if (msg->msg_namelen < sizeof(*usin)) 5901da177e4SLinus Torvalds return -EINVAL; 5911da177e4SLinus Torvalds if (usin->sin_family != AF_INET) { 5921da177e4SLinus Torvalds if (usin->sin_family != AF_UNSPEC) 5931da177e4SLinus Torvalds return -EAFNOSUPPORT; 5941da177e4SLinus Torvalds } 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds daddr = usin->sin_addr.s_addr; 5971da177e4SLinus Torvalds dport = usin->sin_port; 5981da177e4SLinus Torvalds if (dport == 0) 5991da177e4SLinus Torvalds return -EINVAL; 6001da177e4SLinus Torvalds } else { 6011da177e4SLinus Torvalds if (sk->sk_state != TCP_ESTABLISHED) 6021da177e4SLinus Torvalds return -EDESTADDRREQ; 6031da177e4SLinus Torvalds daddr = inet->daddr; 6041da177e4SLinus Torvalds dport = inet->dport; 6051da177e4SLinus Torvalds /* Open fast path for connected socket. 6061da177e4SLinus Torvalds Route will not be used, if at least one option is set. 6071da177e4SLinus Torvalds */ 6081da177e4SLinus Torvalds connected = 1; 6091da177e4SLinus Torvalds } 6101da177e4SLinus Torvalds ipc.addr = inet->saddr; 6111da177e4SLinus Torvalds 6121da177e4SLinus Torvalds ipc.oif = sk->sk_bound_dev_if; 6131da177e4SLinus Torvalds if (msg->msg_controllen) { 6141da177e4SLinus Torvalds err = ip_cmsg_send(msg, &ipc); 6151da177e4SLinus Torvalds if (err) 6161da177e4SLinus Torvalds return err; 6171da177e4SLinus Torvalds if (ipc.opt) 6181da177e4SLinus Torvalds free = 1; 6191da177e4SLinus Torvalds connected = 0; 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds if (!ipc.opt) 6221da177e4SLinus Torvalds ipc.opt = inet->opt; 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds saddr = ipc.addr; 6251da177e4SLinus Torvalds ipc.addr = faddr = daddr; 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds if (ipc.opt && ipc.opt->srr) { 6281da177e4SLinus Torvalds if (!daddr) 6291da177e4SLinus Torvalds return -EINVAL; 6301da177e4SLinus Torvalds faddr = ipc.opt->faddr; 6311da177e4SLinus Torvalds connected = 0; 6321da177e4SLinus Torvalds } 6331da177e4SLinus Torvalds tos = RT_TOS(inet->tos); 6341da177e4SLinus Torvalds if (sock_flag(sk, SOCK_LOCALROUTE) || 6351da177e4SLinus Torvalds (msg->msg_flags & MSG_DONTROUTE) || 6361da177e4SLinus Torvalds (ipc.opt && ipc.opt->is_strictroute)) { 6371da177e4SLinus Torvalds tos |= RTO_ONLINK; 6381da177e4SLinus Torvalds connected = 0; 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds 641f97c1e0cSJoe Perches if (ipv4_is_multicast(daddr)) { 6421da177e4SLinus Torvalds if (!ipc.oif) 6431da177e4SLinus Torvalds ipc.oif = inet->mc_index; 6441da177e4SLinus Torvalds if (!saddr) 6451da177e4SLinus Torvalds saddr = inet->mc_addr; 6461da177e4SLinus Torvalds connected = 0; 6471da177e4SLinus Torvalds } 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds if (connected) 6501da177e4SLinus Torvalds rt = (struct rtable*)sk_dst_check(sk, 0); 6511da177e4SLinus Torvalds 6521da177e4SLinus Torvalds if (rt == NULL) { 6531da177e4SLinus Torvalds struct flowi fl = { .oif = ipc.oif, 6541da177e4SLinus Torvalds .nl_u = { .ip4_u = 6551da177e4SLinus Torvalds { .daddr = faddr, 6561da177e4SLinus Torvalds .saddr = saddr, 6571da177e4SLinus Torvalds .tos = tos } }, 658ba4e58ecSGerrit Renker .proto = sk->sk_protocol, 6591da177e4SLinus Torvalds .uli_u = { .ports = 6601da177e4SLinus Torvalds { .sport = inet->sport, 6611da177e4SLinus Torvalds .dport = dport } } }; 662beb8d13bSVenkat Yekkirala security_sk_classify_flow(sk, &fl); 6638eb9086fSDavid S. Miller err = ip_route_output_flow(&rt, &fl, sk, 1); 664584bdf8cSWei Dong if (err) { 665584bdf8cSWei Dong if (err == -ENETUNREACH) 666584bdf8cSWei Dong IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES); 6671da177e4SLinus Torvalds goto out; 668584bdf8cSWei Dong } 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds err = -EACCES; 6711da177e4SLinus Torvalds if ((rt->rt_flags & RTCF_BROADCAST) && 6721da177e4SLinus Torvalds !sock_flag(sk, SOCK_BROADCAST)) 6731da177e4SLinus Torvalds goto out; 6741da177e4SLinus Torvalds if (connected) 6751da177e4SLinus Torvalds sk_dst_set(sk, dst_clone(&rt->u.dst)); 6761da177e4SLinus Torvalds } 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds if (msg->msg_flags&MSG_CONFIRM) 6791da177e4SLinus Torvalds goto do_confirm; 6801da177e4SLinus Torvalds back_from_confirm: 6811da177e4SLinus Torvalds 6821da177e4SLinus Torvalds saddr = rt->rt_src; 6831da177e4SLinus Torvalds if (!ipc.addr) 6841da177e4SLinus Torvalds daddr = ipc.addr = rt->rt_dst; 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds lock_sock(sk); 6871da177e4SLinus Torvalds if (unlikely(up->pending)) { 6881da177e4SLinus Torvalds /* The socket is already corked while preparing it. */ 6891da177e4SLinus Torvalds /* ... which is an evident application bug. --ANK */ 6901da177e4SLinus Torvalds release_sock(sk); 6911da177e4SLinus Torvalds 69264ce2073SPatrick McHardy LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n"); 6931da177e4SLinus Torvalds err = -EINVAL; 6941da177e4SLinus Torvalds goto out; 6951da177e4SLinus Torvalds } 6961da177e4SLinus Torvalds /* 6971da177e4SLinus Torvalds * Now cork the socket to pend data. 6981da177e4SLinus Torvalds */ 6991da177e4SLinus Torvalds inet->cork.fl.fl4_dst = daddr; 7001da177e4SLinus Torvalds inet->cork.fl.fl_ip_dport = dport; 7011da177e4SLinus Torvalds inet->cork.fl.fl4_src = saddr; 7021da177e4SLinus Torvalds inet->cork.fl.fl_ip_sport = inet->sport; 7031da177e4SLinus Torvalds up->pending = AF_INET; 7041da177e4SLinus Torvalds 7051da177e4SLinus Torvalds do_append_data: 7061da177e4SLinus Torvalds up->len += ulen; 707ba4e58ecSGerrit Renker getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 708ba4e58ecSGerrit Renker err = ip_append_data(sk, getfrag, msg->msg_iov, ulen, 7091da177e4SLinus Torvalds sizeof(struct udphdr), &ipc, rt, 7101da177e4SLinus Torvalds corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); 7111da177e4SLinus Torvalds if (err) 7121da177e4SLinus Torvalds udp_flush_pending_frames(sk); 7131da177e4SLinus Torvalds else if (!corkreq) 7144c0a6cb0SGerrit Renker err = udp_push_pending_frames(sk); 7151e0c14f4SHerbert Xu else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 7161e0c14f4SHerbert Xu up->pending = 0; 7171da177e4SLinus Torvalds release_sock(sk); 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds out: 7201da177e4SLinus Torvalds ip_rt_put(rt); 7211da177e4SLinus Torvalds if (free) 7221da177e4SLinus Torvalds kfree(ipc.opt); 7232a0c6c98SYOSHIFUJI Hideaki if (!err) 7241da177e4SLinus Torvalds return len; 72581aa646cSMartin Bligh /* 72681aa646cSMartin Bligh * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 72781aa646cSMartin Bligh * ENOBUFS might not be good (it's not tunable per se), but otherwise 72881aa646cSMartin Bligh * we don't have a good statistic (IpOutDiscards but it can be too many 72981aa646cSMartin Bligh * things). We could add another new stat but at least for now that 73081aa646cSMartin Bligh * seems like overkill. 73181aa646cSMartin Bligh */ 73281aa646cSMartin Bligh if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 733ba4e58ecSGerrit Renker UDP_INC_STATS_USER(UDP_MIB_SNDBUFERRORS, is_udplite); 73481aa646cSMartin Bligh } 7351da177e4SLinus Torvalds return err; 7361da177e4SLinus Torvalds 7371da177e4SLinus Torvalds do_confirm: 7381da177e4SLinus Torvalds dst_confirm(&rt->u.dst); 7391da177e4SLinus Torvalds if (!(msg->msg_flags&MSG_PROBE) || len) 7401da177e4SLinus Torvalds goto back_from_confirm; 7411da177e4SLinus Torvalds err = 0; 7421da177e4SLinus Torvalds goto out; 7431da177e4SLinus Torvalds } 7441da177e4SLinus Torvalds 745ba4e58ecSGerrit Renker int udp_sendpage(struct sock *sk, struct page *page, int offset, 7461da177e4SLinus Torvalds size_t size, int flags) 7471da177e4SLinus Torvalds { 7481da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 7491da177e4SLinus Torvalds int ret; 7501da177e4SLinus Torvalds 7511da177e4SLinus Torvalds if (!up->pending) { 7521da177e4SLinus Torvalds struct msghdr msg = { .msg_flags = flags|MSG_MORE }; 7531da177e4SLinus Torvalds 7541da177e4SLinus Torvalds /* Call udp_sendmsg to specify destination address which 7551da177e4SLinus Torvalds * sendpage interface can't pass. 7561da177e4SLinus Torvalds * This will succeed only when the socket is connected. 7571da177e4SLinus Torvalds */ 7581da177e4SLinus Torvalds ret = udp_sendmsg(NULL, sk, &msg, 0); 7591da177e4SLinus Torvalds if (ret < 0) 7601da177e4SLinus Torvalds return ret; 7611da177e4SLinus Torvalds } 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds lock_sock(sk); 7641da177e4SLinus Torvalds 7651da177e4SLinus Torvalds if (unlikely(!up->pending)) { 7661da177e4SLinus Torvalds release_sock(sk); 7671da177e4SLinus Torvalds 76864ce2073SPatrick McHardy LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n"); 7691da177e4SLinus Torvalds return -EINVAL; 7701da177e4SLinus Torvalds } 7711da177e4SLinus Torvalds 7721da177e4SLinus Torvalds ret = ip_append_page(sk, page, offset, size, flags); 7731da177e4SLinus Torvalds if (ret == -EOPNOTSUPP) { 7741da177e4SLinus Torvalds release_sock(sk); 7751da177e4SLinus Torvalds return sock_no_sendpage(sk->sk_socket, page, offset, 7761da177e4SLinus Torvalds size, flags); 7771da177e4SLinus Torvalds } 7781da177e4SLinus Torvalds if (ret < 0) { 7791da177e4SLinus Torvalds udp_flush_pending_frames(sk); 7801da177e4SLinus Torvalds goto out; 7811da177e4SLinus Torvalds } 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds up->len += size; 7841da177e4SLinus Torvalds if (!(up->corkflag || (flags&MSG_MORE))) 7854c0a6cb0SGerrit Renker ret = udp_push_pending_frames(sk); 7861da177e4SLinus Torvalds if (!ret) 7871da177e4SLinus Torvalds ret = size; 7881da177e4SLinus Torvalds out: 7891da177e4SLinus Torvalds release_sock(sk); 7901da177e4SLinus Torvalds return ret; 7911da177e4SLinus Torvalds } 7921da177e4SLinus Torvalds 7931da177e4SLinus Torvalds /* 7941da177e4SLinus Torvalds * IOCTL requests applicable to the UDP protocol 7951da177e4SLinus Torvalds */ 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds int udp_ioctl(struct sock *sk, int cmd, unsigned long arg) 7981da177e4SLinus Torvalds { 7996516c655SStephen Hemminger switch (cmd) { 8001da177e4SLinus Torvalds case SIOCOUTQ: 8011da177e4SLinus Torvalds { 8021da177e4SLinus Torvalds int amount = atomic_read(&sk->sk_wmem_alloc); 8031da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 8041da177e4SLinus Torvalds } 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds case SIOCINQ: 8071da177e4SLinus Torvalds { 8081da177e4SLinus Torvalds struct sk_buff *skb; 8091da177e4SLinus Torvalds unsigned long amount; 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds amount = 0; 812208d8984SHerbert Xu spin_lock_bh(&sk->sk_receive_queue.lock); 8131da177e4SLinus Torvalds skb = skb_peek(&sk->sk_receive_queue); 8141da177e4SLinus Torvalds if (skb != NULL) { 8151da177e4SLinus Torvalds /* 8161da177e4SLinus Torvalds * We will only return the amount 8171da177e4SLinus Torvalds * of this packet since that is all 8181da177e4SLinus Torvalds * that will be read. 8191da177e4SLinus Torvalds */ 8201da177e4SLinus Torvalds amount = skb->len - sizeof(struct udphdr); 8211da177e4SLinus Torvalds } 822208d8984SHerbert Xu spin_unlock_bh(&sk->sk_receive_queue.lock); 8231da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 8241da177e4SLinus Torvalds } 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds default: 8271da177e4SLinus Torvalds return -ENOIOCTLCMD; 8281da177e4SLinus Torvalds } 8296516c655SStephen Hemminger 8306516c655SStephen Hemminger return 0; 8311da177e4SLinus Torvalds } 8321da177e4SLinus Torvalds 8331da177e4SLinus Torvalds /* 8341da177e4SLinus Torvalds * This should be easy, if there is something there we 8351da177e4SLinus Torvalds * return it, otherwise we block. 8361da177e4SLinus Torvalds */ 8371da177e4SLinus Torvalds 838ba4e58ecSGerrit Renker int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 8391da177e4SLinus Torvalds size_t len, int noblock, int flags, int *addr_len) 8401da177e4SLinus Torvalds { 8411da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 8421da177e4SLinus Torvalds struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name; 8431da177e4SLinus Torvalds struct sk_buff *skb; 844759e5d00SHerbert Xu unsigned int ulen, copied; 845a59322beSHerbert Xu int peeked; 846759e5d00SHerbert Xu int err; 847759e5d00SHerbert Xu int is_udplite = IS_UDPLITE(sk); 8481da177e4SLinus Torvalds 8491da177e4SLinus Torvalds /* 8501da177e4SLinus Torvalds * Check any passed addresses 8511da177e4SLinus Torvalds */ 8521da177e4SLinus Torvalds if (addr_len) 8531da177e4SLinus Torvalds *addr_len=sizeof(*sin); 8541da177e4SLinus Torvalds 8551da177e4SLinus Torvalds if (flags & MSG_ERRQUEUE) 8561da177e4SLinus Torvalds return ip_recv_error(sk, msg, len); 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds try_again: 859a59322beSHerbert Xu skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), 860a59322beSHerbert Xu &peeked, &err); 8611da177e4SLinus Torvalds if (!skb) 8621da177e4SLinus Torvalds goto out; 8631da177e4SLinus Torvalds 864759e5d00SHerbert Xu ulen = skb->len - sizeof(struct udphdr); 8651da177e4SLinus Torvalds copied = len; 866759e5d00SHerbert Xu if (copied > ulen) 867759e5d00SHerbert Xu copied = ulen; 868759e5d00SHerbert Xu else if (copied < ulen) 8691da177e4SLinus Torvalds msg->msg_flags |= MSG_TRUNC; 8701da177e4SLinus Torvalds 871ba4e58ecSGerrit Renker /* 872759e5d00SHerbert Xu * If checksum is needed at all, try to do it while copying the 873759e5d00SHerbert Xu * data. If the data is truncated, or if we only want a partial 874759e5d00SHerbert Xu * coverage checksum (UDP-Lite), do it before the copy. 875ba4e58ecSGerrit Renker */ 876ba4e58ecSGerrit Renker 877759e5d00SHerbert Xu if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) { 878759e5d00SHerbert Xu if (udp_lib_checksum_complete(skb)) 8791da177e4SLinus Torvalds goto csum_copy_err; 880ba4e58ecSGerrit Renker } 881ba4e58ecSGerrit Renker 88260476372SHerbert Xu if (skb_csum_unnecessary(skb)) 883ba4e58ecSGerrit Renker err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), 884ba4e58ecSGerrit Renker msg->msg_iov, copied ); 885ba4e58ecSGerrit Renker else { 8861da177e4SLinus Torvalds err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov); 8871da177e4SLinus Torvalds 8881da177e4SLinus Torvalds if (err == -EINVAL) 8891da177e4SLinus Torvalds goto csum_copy_err; 8901da177e4SLinus Torvalds } 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds if (err) 8931da177e4SLinus Torvalds goto out_free; 8941da177e4SLinus Torvalds 895a59322beSHerbert Xu if (!peeked) 896cb75994eSWang Chen UDP_INC_STATS_USER(UDP_MIB_INDATAGRAMS, is_udplite); 897cb75994eSWang Chen 8981da177e4SLinus Torvalds sock_recv_timestamp(msg, sk, skb); 8991da177e4SLinus Torvalds 9001da177e4SLinus Torvalds /* Copy the address. */ 9011da177e4SLinus Torvalds if (sin) 9021da177e4SLinus Torvalds { 9031da177e4SLinus Torvalds sin->sin_family = AF_INET; 9044bedb452SArnaldo Carvalho de Melo sin->sin_port = udp_hdr(skb)->source; 905eddc9ec5SArnaldo Carvalho de Melo sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 9061da177e4SLinus Torvalds memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 9071da177e4SLinus Torvalds } 9081da177e4SLinus Torvalds if (inet->cmsg_flags) 9091da177e4SLinus Torvalds ip_cmsg_recv(msg, skb); 9101da177e4SLinus Torvalds 9111da177e4SLinus Torvalds err = copied; 9121da177e4SLinus Torvalds if (flags & MSG_TRUNC) 913759e5d00SHerbert Xu err = ulen; 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds out_free: 916*95766fffSHideo Aoki lock_sock(sk); 9171da177e4SLinus Torvalds skb_free_datagram(sk, skb); 918*95766fffSHideo Aoki release_sock(sk); 9191da177e4SLinus Torvalds out: 9201da177e4SLinus Torvalds return err; 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds csum_copy_err: 923*95766fffSHideo Aoki lock_sock(sk); 92427ab2568SHerbert Xu if (!skb_kill_datagram(sk, skb, flags)) 925bbca1768SWang Chen UDP_INC_STATS_USER(UDP_MIB_INERRORS, is_udplite); 926*95766fffSHideo Aoki release_sock(sk); 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds if (noblock) 9291da177e4SLinus Torvalds return -EAGAIN; 9301da177e4SLinus Torvalds goto try_again; 9311da177e4SLinus Torvalds } 9321da177e4SLinus Torvalds 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds int udp_disconnect(struct sock *sk, int flags) 9351da177e4SLinus Torvalds { 9361da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 9371da177e4SLinus Torvalds /* 9381da177e4SLinus Torvalds * 1003.1g - break association. 9391da177e4SLinus Torvalds */ 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds sk->sk_state = TCP_CLOSE; 9421da177e4SLinus Torvalds inet->daddr = 0; 9431da177e4SLinus Torvalds inet->dport = 0; 9441da177e4SLinus Torvalds sk->sk_bound_dev_if = 0; 9451da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) 9461da177e4SLinus Torvalds inet_reset_saddr(sk); 9471da177e4SLinus Torvalds 9481da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) { 9491da177e4SLinus Torvalds sk->sk_prot->unhash(sk); 9501da177e4SLinus Torvalds inet->sport = 0; 9511da177e4SLinus Torvalds } 9521da177e4SLinus Torvalds sk_dst_reset(sk); 9531da177e4SLinus Torvalds return 0; 9541da177e4SLinus Torvalds } 9551da177e4SLinus Torvalds 9561da177e4SLinus Torvalds /* returns: 9571da177e4SLinus Torvalds * -1: error 9581da177e4SLinus Torvalds * 0: success 9591da177e4SLinus Torvalds * >0: "udp encap" protocol resubmission 9601da177e4SLinus Torvalds * 9611da177e4SLinus Torvalds * Note that in the success and error cases, the skb is assumed to 9621da177e4SLinus Torvalds * have either been requeued or freed. 9631da177e4SLinus Torvalds */ 964ba4e58ecSGerrit Renker int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb) 9651da177e4SLinus Torvalds { 9661da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 96781aa646cSMartin Bligh int rc; 968b2bf1e26SWang Chen int is_udplite = IS_UDPLITE(sk); 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds /* 9711da177e4SLinus Torvalds * Charge it to the socket, dropping if the queue is full. 9721da177e4SLinus Torvalds */ 973ba4e58ecSGerrit Renker if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) 974ba4e58ecSGerrit Renker goto drop; 975b59c2701SPatrick McHardy nf_reset(skb); 9761da177e4SLinus Torvalds 9771da177e4SLinus Torvalds if (up->encap_type) { 9781da177e4SLinus Torvalds /* 979067b207bSJames Chapman * This is an encapsulation socket so pass the skb to 980067b207bSJames Chapman * the socket's udp_encap_rcv() hook. Otherwise, just 981067b207bSJames Chapman * fall through and pass this up the UDP socket. 982067b207bSJames Chapman * up->encap_rcv() returns the following value: 983067b207bSJames Chapman * =0 if skb was successfully passed to the encap 984067b207bSJames Chapman * handler or was discarded by it. 985067b207bSJames Chapman * >0 if skb should be passed on to UDP. 986067b207bSJames Chapman * <0 if skb should be resubmitted as proto -N 9871da177e4SLinus Torvalds */ 988067b207bSJames Chapman 989067b207bSJames Chapman /* if we're overly short, let UDP handle it */ 9903be550f3SPatrick McHardy if (skb->len > sizeof(struct udphdr) && 9913be550f3SPatrick McHardy up->encap_rcv != NULL) { 9921da177e4SLinus Torvalds int ret; 9931da177e4SLinus Torvalds 994342f0234SJames Chapman ret = (*up->encap_rcv)(sk, skb); 9951781f7f5SHerbert Xu if (ret <= 0) { 9961781f7f5SHerbert Xu UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS, 9971781f7f5SHerbert Xu is_udplite); 998067b207bSJames Chapman return -ret; 999342f0234SJames Chapman } 10001781f7f5SHerbert Xu } 1001342f0234SJames Chapman 10021da177e4SLinus Torvalds /* FALLTHROUGH -- it's a UDP Packet */ 10031da177e4SLinus Torvalds } 10041da177e4SLinus Torvalds 1005ba4e58ecSGerrit Renker /* 1006ba4e58ecSGerrit Renker * UDP-Lite specific tests, ignored on UDP sockets 1007ba4e58ecSGerrit Renker */ 1008b2bf1e26SWang Chen if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 1009ba4e58ecSGerrit Renker 1010ba4e58ecSGerrit Renker /* 1011ba4e58ecSGerrit Renker * MIB statistics other than incrementing the error count are 1012ba4e58ecSGerrit Renker * disabled for the following two types of errors: these depend 1013ba4e58ecSGerrit Renker * on the application settings, not on the functioning of the 1014ba4e58ecSGerrit Renker * protocol stack as such. 1015ba4e58ecSGerrit Renker * 1016ba4e58ecSGerrit Renker * RFC 3828 here recommends (sec 3.3): "There should also be a 1017ba4e58ecSGerrit Renker * way ... to ... at least let the receiving application block 1018ba4e58ecSGerrit Renker * delivery of packets with coverage values less than a value 1019ba4e58ecSGerrit Renker * provided by the application." 1020ba4e58ecSGerrit Renker */ 1021ba4e58ecSGerrit Renker if (up->pcrlen == 0) { /* full coverage was set */ 1022ba4e58ecSGerrit Renker LIMIT_NETDEBUG(KERN_WARNING "UDPLITE: partial coverage " 1023ba4e58ecSGerrit Renker "%d while full coverage %d requested\n", 1024ba4e58ecSGerrit Renker UDP_SKB_CB(skb)->cscov, skb->len); 1025ba4e58ecSGerrit Renker goto drop; 10261da177e4SLinus Torvalds } 1027ba4e58ecSGerrit Renker /* The next case involves violating the min. coverage requested 1028ba4e58ecSGerrit Renker * by the receiver. This is subtle: if receiver wants x and x is 1029ba4e58ecSGerrit Renker * greater than the buffersize/MTU then receiver will complain 1030ba4e58ecSGerrit Renker * that it wants x while sender emits packets of smaller size y. 1031ba4e58ecSGerrit Renker * Therefore the above ...()->partial_cov statement is essential. 1032ba4e58ecSGerrit Renker */ 1033ba4e58ecSGerrit Renker if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 1034ba4e58ecSGerrit Renker LIMIT_NETDEBUG(KERN_WARNING 1035ba4e58ecSGerrit Renker "UDPLITE: coverage %d too small, need min %d\n", 1036ba4e58ecSGerrit Renker UDP_SKB_CB(skb)->cscov, up->pcrlen); 1037ba4e58ecSGerrit Renker goto drop; 1038ba4e58ecSGerrit Renker } 1039ba4e58ecSGerrit Renker } 1040ba4e58ecSGerrit Renker 1041759e5d00SHerbert Xu if (sk->sk_filter) { 1042759e5d00SHerbert Xu if (udp_lib_checksum_complete(skb)) 1043ba4e58ecSGerrit Renker goto drop; 10441da177e4SLinus Torvalds } 10451da177e4SLinus Torvalds 104681aa646cSMartin Bligh if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) { 104781aa646cSMartin Bligh /* Note that an ENOMEM error is charged twice */ 104881aa646cSMartin Bligh if (rc == -ENOMEM) 1049b2bf1e26SWang Chen UDP_INC_STATS_BH(UDP_MIB_RCVBUFERRORS, is_udplite); 1050ba4e58ecSGerrit Renker goto drop; 1051ba4e58ecSGerrit Renker } 1052ba4e58ecSGerrit Renker 1053ba4e58ecSGerrit Renker return 0; 1054ba4e58ecSGerrit Renker 1055ba4e58ecSGerrit Renker drop: 1056b2bf1e26SWang Chen UDP_INC_STATS_BH(UDP_MIB_INERRORS, is_udplite); 10571da177e4SLinus Torvalds kfree_skb(skb); 10581da177e4SLinus Torvalds return -1; 10591da177e4SLinus Torvalds } 10601da177e4SLinus Torvalds 10611da177e4SLinus Torvalds /* 10621da177e4SLinus Torvalds * Multicasts and broadcasts go to each listener. 10631da177e4SLinus Torvalds * 10641da177e4SLinus Torvalds * Note: called only from the BH handler context, 10651da177e4SLinus Torvalds * so we don't need to lock the hashes. 10661da177e4SLinus Torvalds */ 1067ba4e58ecSGerrit Renker static int __udp4_lib_mcast_deliver(struct sk_buff *skb, 1068ba4e58ecSGerrit Renker struct udphdr *uh, 1069ba4e58ecSGerrit Renker __be32 saddr, __be32 daddr, 1070ba4e58ecSGerrit Renker struct hlist_head udptable[]) 10711da177e4SLinus Torvalds { 1072df2bc459SDavid S. Miller struct sock *sk; 10731da177e4SLinus Torvalds int dif; 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds read_lock(&udp_hash_lock); 1076df2bc459SDavid S. Miller sk = sk_head(&udptable[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]); 1077df2bc459SDavid S. Miller dif = skb->dev->ifindex; 1078df2bc459SDavid S. Miller sk = udp_v4_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif); 10796aaf47faSEric Dumazet if (sk) { 1080df2bc459SDavid S. Miller struct sock *sknext = NULL; 1081df2bc459SDavid S. Miller 10821da177e4SLinus Torvalds do { 10831da177e4SLinus Torvalds struct sk_buff *skb1 = skb; 1084df2bc459SDavid S. Miller 1085df2bc459SDavid S. Miller sknext = udp_v4_mcast_next(sk_next(sk), uh->dest, daddr, 1086df2bc459SDavid S. Miller uh->source, saddr, dif); 10871da177e4SLinus Torvalds if (sknext) 10881da177e4SLinus Torvalds skb1 = skb_clone(skb, GFP_ATOMIC); 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds if (skb1) { 1091*95766fffSHideo Aoki int ret = 0; 1092*95766fffSHideo Aoki 1093*95766fffSHideo Aoki bh_lock_sock_nested(sk); 1094*95766fffSHideo Aoki if (!sock_owned_by_user(sk)) 1095*95766fffSHideo Aoki ret = udp_queue_rcv_skb(sk, skb1); 1096*95766fffSHideo Aoki else 1097*95766fffSHideo Aoki sk_add_backlog(sk, skb1); 1098*95766fffSHideo Aoki bh_unlock_sock(sk); 1099*95766fffSHideo Aoki 11001da177e4SLinus Torvalds if (ret > 0) 1101df2bc459SDavid S. Miller /* we should probably re-process instead 1102df2bc459SDavid S. Miller * of dropping packets here. */ 11031da177e4SLinus Torvalds kfree_skb(skb1); 11041da177e4SLinus Torvalds } 11051da177e4SLinus Torvalds sk = sknext; 11061da177e4SLinus Torvalds } while (sknext); 11071da177e4SLinus Torvalds } else 11081da177e4SLinus Torvalds kfree_skb(skb); 11091da177e4SLinus Torvalds read_unlock(&udp_hash_lock); 11101da177e4SLinus Torvalds return 0; 11111da177e4SLinus Torvalds } 11121da177e4SLinus Torvalds 11131da177e4SLinus Torvalds /* Initialize UDP checksum. If exited with zero value (success), 11141da177e4SLinus Torvalds * CHECKSUM_UNNECESSARY means, that no more checks are required. 11151da177e4SLinus Torvalds * Otherwise, csum completion requires chacksumming packet body, 11161da177e4SLinus Torvalds * including udp header and folding it to skb->csum. 11171da177e4SLinus Torvalds */ 1118759e5d00SHerbert Xu static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh, 1119759e5d00SHerbert Xu int proto) 11201da177e4SLinus Torvalds { 1121eddc9ec5SArnaldo Carvalho de Melo const struct iphdr *iph; 1122759e5d00SHerbert Xu int err; 1123759e5d00SHerbert Xu 1124759e5d00SHerbert Xu UDP_SKB_CB(skb)->partial_cov = 0; 1125759e5d00SHerbert Xu UDP_SKB_CB(skb)->cscov = skb->len; 1126759e5d00SHerbert Xu 1127759e5d00SHerbert Xu if (proto == IPPROTO_UDPLITE) { 1128759e5d00SHerbert Xu err = udplite_checksum_init(skb, uh); 1129759e5d00SHerbert Xu if (err) 1130759e5d00SHerbert Xu return err; 1131759e5d00SHerbert Xu } 1132759e5d00SHerbert Xu 1133eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 11341da177e4SLinus Torvalds if (uh->check == 0) { 11351da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_UNNECESSARY; 113684fa7933SPatrick McHardy } else if (skb->ip_summed == CHECKSUM_COMPLETE) { 1137eddc9ec5SArnaldo Carvalho de Melo if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len, 1138eddc9ec5SArnaldo Carvalho de Melo proto, skb->csum)) 1139fb286bb2SHerbert Xu skb->ip_summed = CHECKSUM_UNNECESSARY; 11401da177e4SLinus Torvalds } 114160476372SHerbert Xu if (!skb_csum_unnecessary(skb)) 1142eddc9ec5SArnaldo Carvalho de Melo skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, 1143759e5d00SHerbert Xu skb->len, proto, 0); 11441da177e4SLinus Torvalds /* Probably, we should checksum udp header (it should be in cache 11451da177e4SLinus Torvalds * in any case) and data in tiny packets (< rx copybreak). 11461da177e4SLinus Torvalds */ 1147ba4e58ecSGerrit Renker 1148759e5d00SHerbert Xu return 0; 11491da177e4SLinus Torvalds } 11501da177e4SLinus Torvalds 11511da177e4SLinus Torvalds /* 11521da177e4SLinus Torvalds * All we need to do is get the socket, and then do a checksum. 11531da177e4SLinus Torvalds */ 11541da177e4SLinus Torvalds 1155ba4e58ecSGerrit Renker int __udp4_lib_rcv(struct sk_buff *skb, struct hlist_head udptable[], 1156759e5d00SHerbert Xu int proto) 11571da177e4SLinus Torvalds { 11581da177e4SLinus Torvalds struct sock *sk; 11594bedb452SArnaldo Carvalho de Melo struct udphdr *uh = udp_hdr(skb); 11601da177e4SLinus Torvalds unsigned short ulen; 11611da177e4SLinus Torvalds struct rtable *rt = (struct rtable*)skb->dst; 1162eddc9ec5SArnaldo Carvalho de Melo __be32 saddr = ip_hdr(skb)->saddr; 1163eddc9ec5SArnaldo Carvalho de Melo __be32 daddr = ip_hdr(skb)->daddr; 11641da177e4SLinus Torvalds 11651da177e4SLinus Torvalds /* 1166ba4e58ecSGerrit Renker * Validate the packet. 11671da177e4SLinus Torvalds */ 11681da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct udphdr))) 1169ba4e58ecSGerrit Renker goto drop; /* No space for header. */ 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds ulen = ntohs(uh->len); 1172ba4e58ecSGerrit Renker if (ulen > skb->len) 11731da177e4SLinus Torvalds goto short_packet; 11741da177e4SLinus Torvalds 1175759e5d00SHerbert Xu if (proto == IPPROTO_UDP) { 1176759e5d00SHerbert Xu /* UDP validates ulen. */ 1177ba4e58ecSGerrit Renker if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen)) 11781da177e4SLinus Torvalds goto short_packet; 11794bedb452SArnaldo Carvalho de Melo uh = udp_hdr(skb); 1180ba4e58ecSGerrit Renker } 11811da177e4SLinus Torvalds 1182759e5d00SHerbert Xu if (udp4_csum_init(skb, uh, proto)) 1183759e5d00SHerbert Xu goto csum_error; 1184759e5d00SHerbert Xu 11851da177e4SLinus Torvalds if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST)) 1186ba4e58ecSGerrit Renker return __udp4_lib_mcast_deliver(skb, uh, saddr, daddr, udptable); 11871da177e4SLinus Torvalds 1188ba4e58ecSGerrit Renker sk = __udp4_lib_lookup(saddr, uh->source, daddr, uh->dest, 1189fee9dee7SVlad Yasevich inet_iif(skb), udptable); 11901da177e4SLinus Torvalds 11911da177e4SLinus Torvalds if (sk != NULL) { 1192*95766fffSHideo Aoki int ret = 0; 1193*95766fffSHideo Aoki bh_lock_sock_nested(sk); 1194*95766fffSHideo Aoki if (!sock_owned_by_user(sk)) 1195*95766fffSHideo Aoki ret = udp_queue_rcv_skb(sk, skb); 1196*95766fffSHideo Aoki else 1197*95766fffSHideo Aoki sk_add_backlog(sk, skb); 1198*95766fffSHideo Aoki bh_unlock_sock(sk); 11991da177e4SLinus Torvalds sock_put(sk); 12001da177e4SLinus Torvalds 12011da177e4SLinus Torvalds /* a return value > 0 means to resubmit the input, but 1202ba4e58ecSGerrit Renker * it wants the return to be -protocol, or 0 12031da177e4SLinus Torvalds */ 12041da177e4SLinus Torvalds if (ret > 0) 12051da177e4SLinus Torvalds return -ret; 12061da177e4SLinus Torvalds return 0; 12071da177e4SLinus Torvalds } 12081da177e4SLinus Torvalds 12091da177e4SLinus Torvalds if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) 12101da177e4SLinus Torvalds goto drop; 1211b59c2701SPatrick McHardy nf_reset(skb); 12121da177e4SLinus Torvalds 12131da177e4SLinus Torvalds /* No socket. Drop packet silently, if checksum is wrong */ 1214ba4e58ecSGerrit Renker if (udp_lib_checksum_complete(skb)) 12151da177e4SLinus Torvalds goto csum_error; 12161da177e4SLinus Torvalds 1217759e5d00SHerbert Xu UDP_INC_STATS_BH(UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); 12181da177e4SLinus Torvalds icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds /* 12211da177e4SLinus Torvalds * Hmm. We got an UDP packet to a port to which we 12221da177e4SLinus Torvalds * don't wanna listen. Ignore it. 12231da177e4SLinus Torvalds */ 12241da177e4SLinus Torvalds kfree_skb(skb); 12256516c655SStephen Hemminger return 0; 12261da177e4SLinus Torvalds 12271da177e4SLinus Torvalds short_packet: 1228ba4e58ecSGerrit Renker LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From %u.%u.%u.%u:%u %d/%d to %u.%u.%u.%u:%u\n", 1229759e5d00SHerbert Xu proto == IPPROTO_UDPLITE ? "-Lite" : "", 12301da177e4SLinus Torvalds NIPQUAD(saddr), 12311da177e4SLinus Torvalds ntohs(uh->source), 12321da177e4SLinus Torvalds ulen, 1233ba4e58ecSGerrit Renker skb->len, 12341da177e4SLinus Torvalds NIPQUAD(daddr), 123564ce2073SPatrick McHardy ntohs(uh->dest)); 1236ba4e58ecSGerrit Renker goto drop; 12371da177e4SLinus Torvalds 12381da177e4SLinus Torvalds csum_error: 12391da177e4SLinus Torvalds /* 12401da177e4SLinus Torvalds * RFC1122: OK. Discards the bad packet silently (as far as 12411da177e4SLinus Torvalds * the network is concerned, anyway) as per 4.1.3.4 (MUST). 12421da177e4SLinus Torvalds */ 1243ba4e58ecSGerrit Renker LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From %d.%d.%d.%d:%d to %d.%d.%d.%d:%d ulen %d\n", 1244759e5d00SHerbert Xu proto == IPPROTO_UDPLITE ? "-Lite" : "", 12451da177e4SLinus Torvalds NIPQUAD(saddr), 12461da177e4SLinus Torvalds ntohs(uh->source), 12471da177e4SLinus Torvalds NIPQUAD(daddr), 12481da177e4SLinus Torvalds ntohs(uh->dest), 124964ce2073SPatrick McHardy ulen); 12501da177e4SLinus Torvalds drop: 1251759e5d00SHerbert Xu UDP_INC_STATS_BH(UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 12521da177e4SLinus Torvalds kfree_skb(skb); 12536516c655SStephen Hemminger return 0; 12541da177e4SLinus Torvalds } 12551da177e4SLinus Torvalds 12563fbe070aSStephen Hemminger int udp_rcv(struct sk_buff *skb) 1257ba4e58ecSGerrit Renker { 1258759e5d00SHerbert Xu return __udp4_lib_rcv(skb, udp_hash, IPPROTO_UDP); 1259ba4e58ecSGerrit Renker } 1260ba4e58ecSGerrit Renker 1261ba4e58ecSGerrit Renker int udp_destroy_sock(struct sock *sk) 12621da177e4SLinus Torvalds { 12631da177e4SLinus Torvalds lock_sock(sk); 12641da177e4SLinus Torvalds udp_flush_pending_frames(sk); 12651da177e4SLinus Torvalds release_sock(sk); 12661da177e4SLinus Torvalds return 0; 12671da177e4SLinus Torvalds } 12681da177e4SLinus Torvalds 12691da177e4SLinus Torvalds /* 12701da177e4SLinus Torvalds * Socket option code for UDP 12711da177e4SLinus Torvalds */ 12724c0a6cb0SGerrit Renker int udp_lib_setsockopt(struct sock *sk, int level, int optname, 12734c0a6cb0SGerrit Renker char __user *optval, int optlen, 12744c0a6cb0SGerrit Renker int (*push_pending_frames)(struct sock *)) 12751da177e4SLinus Torvalds { 12761da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 12771da177e4SLinus Torvalds int val; 12781da177e4SLinus Torvalds int err = 0; 1279b2bf1e26SWang Chen int is_udplite = IS_UDPLITE(sk); 12801da177e4SLinus Torvalds 12811da177e4SLinus Torvalds if (optlen<sizeof(int)) 12821da177e4SLinus Torvalds return -EINVAL; 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds if (get_user(val, (int __user *)optval)) 12851da177e4SLinus Torvalds return -EFAULT; 12861da177e4SLinus Torvalds 12871da177e4SLinus Torvalds switch (optname) { 12881da177e4SLinus Torvalds case UDP_CORK: 12891da177e4SLinus Torvalds if (val != 0) { 12901da177e4SLinus Torvalds up->corkflag = 1; 12911da177e4SLinus Torvalds } else { 12921da177e4SLinus Torvalds up->corkflag = 0; 12931da177e4SLinus Torvalds lock_sock(sk); 12944c0a6cb0SGerrit Renker (*push_pending_frames)(sk); 12951da177e4SLinus Torvalds release_sock(sk); 12961da177e4SLinus Torvalds } 12971da177e4SLinus Torvalds break; 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds case UDP_ENCAP: 13001da177e4SLinus Torvalds switch (val) { 13011da177e4SLinus Torvalds case 0: 13021da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP: 13031da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP_NON_IKE: 1304067b207bSJames Chapman up->encap_rcv = xfrm4_udp_encap_rcv; 1305067b207bSJames Chapman /* FALLTHROUGH */ 1306342f0234SJames Chapman case UDP_ENCAP_L2TPINUDP: 13071da177e4SLinus Torvalds up->encap_type = val; 13081da177e4SLinus Torvalds break; 13091da177e4SLinus Torvalds default: 13101da177e4SLinus Torvalds err = -ENOPROTOOPT; 13111da177e4SLinus Torvalds break; 13121da177e4SLinus Torvalds } 13131da177e4SLinus Torvalds break; 13141da177e4SLinus Torvalds 1315ba4e58ecSGerrit Renker /* 1316ba4e58ecSGerrit Renker * UDP-Lite's partial checksum coverage (RFC 3828). 1317ba4e58ecSGerrit Renker */ 1318ba4e58ecSGerrit Renker /* The sender sets actual checksum coverage length via this option. 1319ba4e58ecSGerrit Renker * The case coverage > packet length is handled by send module. */ 1320ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 1321b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 1322ba4e58ecSGerrit Renker return -ENOPROTOOPT; 1323ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Illegal coverage: use default (8) */ 1324ba4e58ecSGerrit Renker val = 8; 1325ba4e58ecSGerrit Renker up->pcslen = val; 1326ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_SEND_CC; 1327ba4e58ecSGerrit Renker break; 1328ba4e58ecSGerrit Renker 1329ba4e58ecSGerrit Renker /* The receiver specifies a minimum checksum coverage value. To make 1330ba4e58ecSGerrit Renker * sense, this should be set to at least 8 (as done below). If zero is 1331ba4e58ecSGerrit Renker * used, this again means full checksum coverage. */ 1332ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 1333b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 1334ba4e58ecSGerrit Renker return -ENOPROTOOPT; 1335ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Avoid silly minimal values. */ 1336ba4e58ecSGerrit Renker val = 8; 1337ba4e58ecSGerrit Renker up->pcrlen = val; 1338ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_RECV_CC; 1339ba4e58ecSGerrit Renker break; 1340ba4e58ecSGerrit Renker 13411da177e4SLinus Torvalds default: 13421da177e4SLinus Torvalds err = -ENOPROTOOPT; 13431da177e4SLinus Torvalds break; 13446516c655SStephen Hemminger } 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds return err; 13471da177e4SLinus Torvalds } 13481da177e4SLinus Torvalds 1349ba4e58ecSGerrit Renker int udp_setsockopt(struct sock *sk, int level, int optname, 13503fdadf7dSDmitry Mishin char __user *optval, int optlen) 13513fdadf7dSDmitry Mishin { 1352ba4e58ecSGerrit Renker if (level == SOL_UDP || level == SOL_UDPLITE) 13534c0a6cb0SGerrit Renker return udp_lib_setsockopt(sk, level, optname, optval, optlen, 13544c0a6cb0SGerrit Renker udp_push_pending_frames); 1355ba4e58ecSGerrit Renker return ip_setsockopt(sk, level, optname, optval, optlen); 13563fdadf7dSDmitry Mishin } 13573fdadf7dSDmitry Mishin 13583fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 1359ba4e58ecSGerrit Renker int compat_udp_setsockopt(struct sock *sk, int level, int optname, 13603fdadf7dSDmitry Mishin char __user *optval, int optlen) 13613fdadf7dSDmitry Mishin { 1362ba4e58ecSGerrit Renker if (level == SOL_UDP || level == SOL_UDPLITE) 13634c0a6cb0SGerrit Renker return udp_lib_setsockopt(sk, level, optname, optval, optlen, 13644c0a6cb0SGerrit Renker udp_push_pending_frames); 1365ba4e58ecSGerrit Renker return compat_ip_setsockopt(sk, level, optname, optval, optlen); 13663fdadf7dSDmitry Mishin } 13673fdadf7dSDmitry Mishin #endif 13683fdadf7dSDmitry Mishin 13694c0a6cb0SGerrit Renker int udp_lib_getsockopt(struct sock *sk, int level, int optname, 13701da177e4SLinus Torvalds char __user *optval, int __user *optlen) 13711da177e4SLinus Torvalds { 13721da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 13731da177e4SLinus Torvalds int val, len; 13741da177e4SLinus Torvalds 13751da177e4SLinus Torvalds if (get_user(len,optlen)) 13761da177e4SLinus Torvalds return -EFAULT; 13771da177e4SLinus Torvalds 13781da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(int)); 13791da177e4SLinus Torvalds 13801da177e4SLinus Torvalds if (len < 0) 13811da177e4SLinus Torvalds return -EINVAL; 13821da177e4SLinus Torvalds 13831da177e4SLinus Torvalds switch (optname) { 13841da177e4SLinus Torvalds case UDP_CORK: 13851da177e4SLinus Torvalds val = up->corkflag; 13861da177e4SLinus Torvalds break; 13871da177e4SLinus Torvalds 13881da177e4SLinus Torvalds case UDP_ENCAP: 13891da177e4SLinus Torvalds val = up->encap_type; 13901da177e4SLinus Torvalds break; 13911da177e4SLinus Torvalds 1392ba4e58ecSGerrit Renker /* The following two cannot be changed on UDP sockets, the return is 1393ba4e58ecSGerrit Renker * always 0 (which corresponds to the full checksum coverage of UDP). */ 1394ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 1395ba4e58ecSGerrit Renker val = up->pcslen; 1396ba4e58ecSGerrit Renker break; 1397ba4e58ecSGerrit Renker 1398ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 1399ba4e58ecSGerrit Renker val = up->pcrlen; 1400ba4e58ecSGerrit Renker break; 1401ba4e58ecSGerrit Renker 14021da177e4SLinus Torvalds default: 14031da177e4SLinus Torvalds return -ENOPROTOOPT; 14046516c655SStephen Hemminger } 14051da177e4SLinus Torvalds 14061da177e4SLinus Torvalds if (put_user(len, optlen)) 14071da177e4SLinus Torvalds return -EFAULT; 14081da177e4SLinus Torvalds if (copy_to_user(optval, &val,len)) 14091da177e4SLinus Torvalds return -EFAULT; 14101da177e4SLinus Torvalds return 0; 14111da177e4SLinus Torvalds } 14121da177e4SLinus Torvalds 1413ba4e58ecSGerrit Renker int udp_getsockopt(struct sock *sk, int level, int optname, 14143fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 14153fdadf7dSDmitry Mishin { 1416ba4e58ecSGerrit Renker if (level == SOL_UDP || level == SOL_UDPLITE) 14174c0a6cb0SGerrit Renker return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1418ba4e58ecSGerrit Renker return ip_getsockopt(sk, level, optname, optval, optlen); 14193fdadf7dSDmitry Mishin } 14203fdadf7dSDmitry Mishin 14213fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 1422ba4e58ecSGerrit Renker int compat_udp_getsockopt(struct sock *sk, int level, int optname, 14233fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 14243fdadf7dSDmitry Mishin { 1425ba4e58ecSGerrit Renker if (level == SOL_UDP || level == SOL_UDPLITE) 14264c0a6cb0SGerrit Renker return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1427ba4e58ecSGerrit Renker return compat_ip_getsockopt(sk, level, optname, optval, optlen); 14283fdadf7dSDmitry Mishin } 14293fdadf7dSDmitry Mishin #endif 14301da177e4SLinus Torvalds /** 14311da177e4SLinus Torvalds * udp_poll - wait for a UDP event. 14321da177e4SLinus Torvalds * @file - file struct 14331da177e4SLinus Torvalds * @sock - socket 14341da177e4SLinus Torvalds * @wait - poll table 14351da177e4SLinus Torvalds * 14361da177e4SLinus Torvalds * This is same as datagram poll, except for the special case of 14371da177e4SLinus Torvalds * blocking sockets. If application is using a blocking fd 14381da177e4SLinus Torvalds * and a packet with checksum error is in the queue; 14391da177e4SLinus Torvalds * then it could get return from select indicating data available 14401da177e4SLinus Torvalds * but then block when reading it. Add special case code 14411da177e4SLinus Torvalds * to work around these arguably broken applications. 14421da177e4SLinus Torvalds */ 14431da177e4SLinus Torvalds unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait) 14441da177e4SLinus Torvalds { 14451da177e4SLinus Torvalds unsigned int mask = datagram_poll(file, sock, wait); 14461da177e4SLinus Torvalds struct sock *sk = sock->sk; 1447ba4e58ecSGerrit Renker int is_lite = IS_UDPLITE(sk); 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds /* Check for false positives due to checksum errors */ 14501da177e4SLinus Torvalds if ( (mask & POLLRDNORM) && 14511da177e4SLinus Torvalds !(file->f_flags & O_NONBLOCK) && 14521da177e4SLinus Torvalds !(sk->sk_shutdown & RCV_SHUTDOWN)){ 14531da177e4SLinus Torvalds struct sk_buff_head *rcvq = &sk->sk_receive_queue; 14541da177e4SLinus Torvalds struct sk_buff *skb; 14551da177e4SLinus Torvalds 1456208d8984SHerbert Xu spin_lock_bh(&rcvq->lock); 1457759e5d00SHerbert Xu while ((skb = skb_peek(rcvq)) != NULL && 1458759e5d00SHerbert Xu udp_lib_checksum_complete(skb)) { 1459ba4e58ecSGerrit Renker UDP_INC_STATS_BH(UDP_MIB_INERRORS, is_lite); 14601da177e4SLinus Torvalds __skb_unlink(skb, rcvq); 14611da177e4SLinus Torvalds kfree_skb(skb); 14621da177e4SLinus Torvalds } 1463208d8984SHerbert Xu spin_unlock_bh(&rcvq->lock); 14641da177e4SLinus Torvalds 14651da177e4SLinus Torvalds /* nothing to see, move along */ 14661da177e4SLinus Torvalds if (skb == NULL) 14671da177e4SLinus Torvalds mask &= ~(POLLIN | POLLRDNORM); 14681da177e4SLinus Torvalds } 14691da177e4SLinus Torvalds 14701da177e4SLinus Torvalds return mask; 14711da177e4SLinus Torvalds 14721da177e4SLinus Torvalds } 14731da177e4SLinus Torvalds 147447a31a6fSEric Dumazet DEFINE_PROTO_INUSE(udp) 147547a31a6fSEric Dumazet 14761da177e4SLinus Torvalds struct proto udp_prot = { 14771da177e4SLinus Torvalds .name = "UDP", 14781da177e4SLinus Torvalds .owner = THIS_MODULE, 1479ba4e58ecSGerrit Renker .close = udp_lib_close, 14801da177e4SLinus Torvalds .connect = ip4_datagram_connect, 14811da177e4SLinus Torvalds .disconnect = udp_disconnect, 14821da177e4SLinus Torvalds .ioctl = udp_ioctl, 14831da177e4SLinus Torvalds .destroy = udp_destroy_sock, 14841da177e4SLinus Torvalds .setsockopt = udp_setsockopt, 14851da177e4SLinus Torvalds .getsockopt = udp_getsockopt, 14861da177e4SLinus Torvalds .sendmsg = udp_sendmsg, 14871da177e4SLinus Torvalds .recvmsg = udp_recvmsg, 14881da177e4SLinus Torvalds .sendpage = udp_sendpage, 14891da177e4SLinus Torvalds .backlog_rcv = udp_queue_rcv_skb, 1490ba4e58ecSGerrit Renker .hash = udp_lib_hash, 1491ba4e58ecSGerrit Renker .unhash = udp_lib_unhash, 14921da177e4SLinus Torvalds .get_port = udp_v4_get_port, 1493*95766fffSHideo Aoki .memory_allocated = &udp_memory_allocated, 1494*95766fffSHideo Aoki .sysctl_mem = sysctl_udp_mem, 1495*95766fffSHideo Aoki .sysctl_wmem = &sysctl_udp_wmem_min, 1496*95766fffSHideo Aoki .sysctl_rmem = &sysctl_udp_rmem_min, 14971da177e4SLinus Torvalds .obj_size = sizeof(struct udp_sock), 1498543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT 1499543d9cfeSArnaldo Carvalho de Melo .compat_setsockopt = compat_udp_setsockopt, 1500543d9cfeSArnaldo Carvalho de Melo .compat_getsockopt = compat_udp_getsockopt, 1501543d9cfeSArnaldo Carvalho de Melo #endif 150247a31a6fSEric Dumazet REF_PROTO_INUSE(udp) 15031da177e4SLinus Torvalds }; 15041da177e4SLinus Torvalds 15051da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 15061da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 15071da177e4SLinus Torvalds 15081da177e4SLinus Torvalds static struct sock *udp_get_first(struct seq_file *seq) 15091da177e4SLinus Torvalds { 15101da177e4SLinus Torvalds struct sock *sk; 15111da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 15121da177e4SLinus Torvalds 15131da177e4SLinus Torvalds for (state->bucket = 0; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) { 15141da177e4SLinus Torvalds struct hlist_node *node; 1515ba4e58ecSGerrit Renker sk_for_each(sk, node, state->hashtable + state->bucket) { 15161da177e4SLinus Torvalds if (sk->sk_family == state->family) 15171da177e4SLinus Torvalds goto found; 15181da177e4SLinus Torvalds } 15191da177e4SLinus Torvalds } 15201da177e4SLinus Torvalds sk = NULL; 15211da177e4SLinus Torvalds found: 15221da177e4SLinus Torvalds return sk; 15231da177e4SLinus Torvalds } 15241da177e4SLinus Torvalds 15251da177e4SLinus Torvalds static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk) 15261da177e4SLinus Torvalds { 15271da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 15281da177e4SLinus Torvalds 15291da177e4SLinus Torvalds do { 15301da177e4SLinus Torvalds sk = sk_next(sk); 15311da177e4SLinus Torvalds try_again: 15321da177e4SLinus Torvalds ; 15331da177e4SLinus Torvalds } while (sk && sk->sk_family != state->family); 15341da177e4SLinus Torvalds 15351da177e4SLinus Torvalds if (!sk && ++state->bucket < UDP_HTABLE_SIZE) { 1536ba4e58ecSGerrit Renker sk = sk_head(state->hashtable + state->bucket); 15371da177e4SLinus Torvalds goto try_again; 15381da177e4SLinus Torvalds } 15391da177e4SLinus Torvalds return sk; 15401da177e4SLinus Torvalds } 15411da177e4SLinus Torvalds 15421da177e4SLinus Torvalds static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos) 15431da177e4SLinus Torvalds { 15441da177e4SLinus Torvalds struct sock *sk = udp_get_first(seq); 15451da177e4SLinus Torvalds 15461da177e4SLinus Torvalds if (sk) 15471da177e4SLinus Torvalds while (pos && (sk = udp_get_next(seq, sk)) != NULL) 15481da177e4SLinus Torvalds --pos; 15491da177e4SLinus Torvalds return pos ? NULL : sk; 15501da177e4SLinus Torvalds } 15511da177e4SLinus Torvalds 15521da177e4SLinus Torvalds static void *udp_seq_start(struct seq_file *seq, loff_t *pos) 15531da177e4SLinus Torvalds { 15541da177e4SLinus Torvalds read_lock(&udp_hash_lock); 15551da177e4SLinus Torvalds return *pos ? udp_get_idx(seq, *pos-1) : (void *)1; 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 15621da177e4SLinus Torvalds if (v == (void *)1) 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) 15721da177e4SLinus Torvalds { 15731da177e4SLinus Torvalds read_unlock(&udp_hash_lock); 15741da177e4SLinus Torvalds } 15751da177e4SLinus Torvalds 15761da177e4SLinus Torvalds static int udp_seq_open(struct inode *inode, struct file *file) 15771da177e4SLinus Torvalds { 15781da177e4SLinus Torvalds struct udp_seq_afinfo *afinfo = PDE(inode)->data; 15791da177e4SLinus Torvalds struct seq_file *seq; 15801da177e4SLinus Torvalds int rc = -ENOMEM; 15810da974f4SPanagiotis Issaris struct udp_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL); 15821da177e4SLinus Torvalds 15831da177e4SLinus Torvalds if (!s) 15841da177e4SLinus Torvalds goto out; 15851da177e4SLinus Torvalds s->family = afinfo->family; 1586ba4e58ecSGerrit Renker s->hashtable = afinfo->hashtable; 15871da177e4SLinus Torvalds s->seq_ops.start = udp_seq_start; 15881da177e4SLinus Torvalds s->seq_ops.next = udp_seq_next; 15891da177e4SLinus Torvalds s->seq_ops.show = afinfo->seq_show; 15901da177e4SLinus Torvalds s->seq_ops.stop = udp_seq_stop; 15911da177e4SLinus Torvalds 15921da177e4SLinus Torvalds rc = seq_open(file, &s->seq_ops); 15931da177e4SLinus Torvalds if (rc) 15941da177e4SLinus Torvalds goto out_kfree; 15951da177e4SLinus Torvalds 15961da177e4SLinus Torvalds seq = file->private_data; 15971da177e4SLinus Torvalds seq->private = s; 15981da177e4SLinus Torvalds out: 15991da177e4SLinus Torvalds return rc; 16001da177e4SLinus Torvalds out_kfree: 16011da177e4SLinus Torvalds kfree(s); 16021da177e4SLinus Torvalds goto out; 16031da177e4SLinus Torvalds } 16041da177e4SLinus Torvalds 16051da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 16061da177e4SLinus Torvalds int udp_proc_register(struct udp_seq_afinfo *afinfo) 16071da177e4SLinus Torvalds { 16081da177e4SLinus Torvalds struct proc_dir_entry *p; 16091da177e4SLinus Torvalds int rc = 0; 16101da177e4SLinus Torvalds 16111da177e4SLinus Torvalds if (!afinfo) 16121da177e4SLinus Torvalds return -EINVAL; 16131da177e4SLinus Torvalds afinfo->seq_fops->owner = afinfo->owner; 16141da177e4SLinus Torvalds afinfo->seq_fops->open = udp_seq_open; 16151da177e4SLinus Torvalds afinfo->seq_fops->read = seq_read; 16161da177e4SLinus Torvalds afinfo->seq_fops->llseek = seq_lseek; 16171da177e4SLinus Torvalds afinfo->seq_fops->release = seq_release_private; 16181da177e4SLinus Torvalds 1619457c4cbcSEric W. Biederman p = proc_net_fops_create(&init_net, afinfo->name, S_IRUGO, afinfo->seq_fops); 16201da177e4SLinus Torvalds if (p) 16211da177e4SLinus Torvalds p->data = afinfo; 16221da177e4SLinus Torvalds else 16231da177e4SLinus Torvalds rc = -ENOMEM; 16241da177e4SLinus Torvalds return rc; 16251da177e4SLinus Torvalds } 16261da177e4SLinus Torvalds 16271da177e4SLinus Torvalds void udp_proc_unregister(struct udp_seq_afinfo *afinfo) 16281da177e4SLinus Torvalds { 16291da177e4SLinus Torvalds if (!afinfo) 16301da177e4SLinus Torvalds return; 1631457c4cbcSEric W. Biederman proc_net_remove(&init_net, afinfo->name); 16321da177e4SLinus Torvalds memset(afinfo->seq_fops, 0, sizeof(*afinfo->seq_fops)); 16331da177e4SLinus Torvalds } 16341da177e4SLinus Torvalds 16351da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 16361da177e4SLinus Torvalds static void udp4_format_sock(struct sock *sp, char *tmpbuf, int bucket) 16371da177e4SLinus Torvalds { 16381da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sp); 1639734ab87fSAl Viro __be32 dest = inet->daddr; 1640734ab87fSAl Viro __be32 src = inet->rcv_saddr; 16411da177e4SLinus Torvalds __u16 destp = ntohs(inet->dport); 16421da177e4SLinus Torvalds __u16 srcp = ntohs(inet->sport); 16431da177e4SLinus Torvalds 16441da177e4SLinus Torvalds sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X" 16451da177e4SLinus Torvalds " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p", 16461da177e4SLinus Torvalds bucket, src, srcp, dest, destp, sp->sk_state, 16471da177e4SLinus Torvalds atomic_read(&sp->sk_wmem_alloc), 16481da177e4SLinus Torvalds atomic_read(&sp->sk_rmem_alloc), 16491da177e4SLinus Torvalds 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp), 16501da177e4SLinus Torvalds atomic_read(&sp->sk_refcnt), sp); 16511da177e4SLinus Torvalds } 16521da177e4SLinus Torvalds 1653ba4e58ecSGerrit Renker int udp4_seq_show(struct seq_file *seq, void *v) 16541da177e4SLinus Torvalds { 16551da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 16561da177e4SLinus Torvalds seq_printf(seq, "%-127s\n", 16571da177e4SLinus Torvalds " sl local_address rem_address st tx_queue " 16581da177e4SLinus Torvalds "rx_queue tr tm->when retrnsmt uid timeout " 16591da177e4SLinus Torvalds "inode"); 16601da177e4SLinus Torvalds else { 16611da177e4SLinus Torvalds char tmpbuf[129]; 16621da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 16631da177e4SLinus Torvalds 16641da177e4SLinus Torvalds udp4_format_sock(v, tmpbuf, state->bucket); 16651da177e4SLinus Torvalds seq_printf(seq, "%-127s\n", tmpbuf); 16661da177e4SLinus Torvalds } 16671da177e4SLinus Torvalds return 0; 16681da177e4SLinus Torvalds } 16691da177e4SLinus Torvalds 16701da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 16711da177e4SLinus Torvalds static struct file_operations udp4_seq_fops; 16721da177e4SLinus Torvalds static struct udp_seq_afinfo udp4_seq_afinfo = { 16731da177e4SLinus Torvalds .owner = THIS_MODULE, 16741da177e4SLinus Torvalds .name = "udp", 16751da177e4SLinus Torvalds .family = AF_INET, 1676ba4e58ecSGerrit Renker .hashtable = udp_hash, 16771da177e4SLinus Torvalds .seq_show = udp4_seq_show, 16781da177e4SLinus Torvalds .seq_fops = &udp4_seq_fops, 16791da177e4SLinus Torvalds }; 16801da177e4SLinus Torvalds 16811da177e4SLinus Torvalds int __init udp4_proc_init(void) 16821da177e4SLinus Torvalds { 16831da177e4SLinus Torvalds return udp_proc_register(&udp4_seq_afinfo); 16841da177e4SLinus Torvalds } 16851da177e4SLinus Torvalds 16861da177e4SLinus Torvalds void udp4_proc_exit(void) 16871da177e4SLinus Torvalds { 16881da177e4SLinus Torvalds udp_proc_unregister(&udp4_seq_afinfo); 16891da177e4SLinus Torvalds } 16901da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 16911da177e4SLinus Torvalds 1692*95766fffSHideo Aoki void __init udp_init(void) 1693*95766fffSHideo Aoki { 1694*95766fffSHideo Aoki unsigned long limit; 1695*95766fffSHideo Aoki 1696*95766fffSHideo Aoki /* Set the pressure threshold up by the same strategy of TCP. It is a 1697*95766fffSHideo Aoki * fraction of global memory that is up to 1/2 at 256 MB, decreasing 1698*95766fffSHideo Aoki * toward zero with the amount of memory, with a floor of 128 pages. 1699*95766fffSHideo Aoki */ 1700*95766fffSHideo Aoki limit = min(nr_all_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT); 1701*95766fffSHideo Aoki limit = (limit * (nr_all_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11); 1702*95766fffSHideo Aoki limit = max(limit, 128UL); 1703*95766fffSHideo Aoki sysctl_udp_mem[0] = limit / 4 * 3; 1704*95766fffSHideo Aoki sysctl_udp_mem[1] = limit; 1705*95766fffSHideo Aoki sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2; 1706*95766fffSHideo Aoki 1707*95766fffSHideo Aoki sysctl_udp_rmem_min = SK_MEM_QUANTUM; 1708*95766fffSHideo Aoki sysctl_udp_wmem_min = SK_MEM_QUANTUM; 1709*95766fffSHideo Aoki } 1710*95766fffSHideo Aoki 17111da177e4SLinus Torvalds EXPORT_SYMBOL(udp_disconnect); 17121da177e4SLinus Torvalds EXPORT_SYMBOL(udp_hash); 17131da177e4SLinus Torvalds EXPORT_SYMBOL(udp_hash_lock); 17141da177e4SLinus Torvalds EXPORT_SYMBOL(udp_ioctl); 171525030a7fSGerrit Renker EXPORT_SYMBOL(udp_get_port); 17161da177e4SLinus Torvalds EXPORT_SYMBOL(udp_prot); 17171da177e4SLinus Torvalds EXPORT_SYMBOL(udp_sendmsg); 17184c0a6cb0SGerrit Renker EXPORT_SYMBOL(udp_lib_getsockopt); 17194c0a6cb0SGerrit Renker EXPORT_SYMBOL(udp_lib_setsockopt); 17201da177e4SLinus Torvalds EXPORT_SYMBOL(udp_poll); 17211da177e4SLinus Torvalds 17221da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 17231da177e4SLinus Torvalds EXPORT_SYMBOL(udp_proc_register); 17241da177e4SLinus Torvalds EXPORT_SYMBOL(udp_proc_unregister); 17251da177e4SLinus Torvalds #endif 1726