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 13719c7578fSPavel 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 17719c7578fSPavel 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 { 21419c7578fSPavel 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)) { 23019c7578fSPavel 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); 26719c7578fSPavel 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) 529*629ca23cSPavel Emelyanov UDP_INC_STATS_USER(sock_net(sk), 530*629ca23cSPavel Emelyanov UDP_MIB_OUTDATAGRAMS, is_udplite); 531db8dac20SDavid S. Miller return err; 532db8dac20SDavid S. Miller } 533db8dac20SDavid S. Miller 534db8dac20SDavid S. Miller int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 535db8dac20SDavid S. Miller size_t len) 536db8dac20SDavid S. Miller { 537db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 538db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 539db8dac20SDavid S. Miller int ulen = len; 540db8dac20SDavid S. Miller struct ipcm_cookie ipc; 541db8dac20SDavid S. Miller struct rtable *rt = NULL; 542db8dac20SDavid S. Miller int free = 0; 543db8dac20SDavid S. Miller int connected = 0; 544db8dac20SDavid S. Miller __be32 daddr, faddr, saddr; 545db8dac20SDavid S. Miller __be16 dport; 546db8dac20SDavid S. Miller u8 tos; 547db8dac20SDavid S. Miller int err, is_udplite = IS_UDPLITE(sk); 548db8dac20SDavid S. Miller int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; 549db8dac20SDavid S. Miller int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 550db8dac20SDavid S. Miller 551db8dac20SDavid S. Miller if (len > 0xFFFF) 552db8dac20SDavid S. Miller return -EMSGSIZE; 553db8dac20SDavid S. Miller 554db8dac20SDavid S. Miller /* 555db8dac20SDavid S. Miller * Check the flags. 556db8dac20SDavid S. Miller */ 557db8dac20SDavid S. Miller 558db8dac20SDavid S. Miller if (msg->msg_flags&MSG_OOB) /* Mirror BSD error message compatibility */ 559db8dac20SDavid S. Miller return -EOPNOTSUPP; 560db8dac20SDavid S. Miller 561db8dac20SDavid S. Miller ipc.opt = NULL; 562db8dac20SDavid S. Miller 563db8dac20SDavid S. Miller if (up->pending) { 564db8dac20SDavid S. Miller /* 565db8dac20SDavid S. Miller * There are pending frames. 566db8dac20SDavid S. Miller * The socket lock must be held while it's corked. 567db8dac20SDavid S. Miller */ 568db8dac20SDavid S. Miller lock_sock(sk); 569db8dac20SDavid S. Miller if (likely(up->pending)) { 570db8dac20SDavid S. Miller if (unlikely(up->pending != AF_INET)) { 571db8dac20SDavid S. Miller release_sock(sk); 572db8dac20SDavid S. Miller return -EINVAL; 573db8dac20SDavid S. Miller } 574db8dac20SDavid S. Miller goto do_append_data; 575db8dac20SDavid S. Miller } 576db8dac20SDavid S. Miller release_sock(sk); 577db8dac20SDavid S. Miller } 578db8dac20SDavid S. Miller ulen += sizeof(struct udphdr); 579db8dac20SDavid S. Miller 580db8dac20SDavid S. Miller /* 581db8dac20SDavid S. Miller * Get and verify the address. 582db8dac20SDavid S. Miller */ 583db8dac20SDavid S. Miller if (msg->msg_name) { 584db8dac20SDavid S. Miller struct sockaddr_in * usin = (struct sockaddr_in*)msg->msg_name; 585db8dac20SDavid S. Miller if (msg->msg_namelen < sizeof(*usin)) 586db8dac20SDavid S. Miller return -EINVAL; 587db8dac20SDavid S. Miller if (usin->sin_family != AF_INET) { 588db8dac20SDavid S. Miller if (usin->sin_family != AF_UNSPEC) 589db8dac20SDavid S. Miller return -EAFNOSUPPORT; 590db8dac20SDavid S. Miller } 591db8dac20SDavid S. Miller 592db8dac20SDavid S. Miller daddr = usin->sin_addr.s_addr; 593db8dac20SDavid S. Miller dport = usin->sin_port; 594db8dac20SDavid S. Miller if (dport == 0) 595db8dac20SDavid S. Miller return -EINVAL; 596db8dac20SDavid S. Miller } else { 597db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 598db8dac20SDavid S. Miller return -EDESTADDRREQ; 599db8dac20SDavid S. Miller daddr = inet->daddr; 600db8dac20SDavid S. Miller dport = inet->dport; 601db8dac20SDavid S. Miller /* Open fast path for connected socket. 602db8dac20SDavid S. Miller Route will not be used, if at least one option is set. 603db8dac20SDavid S. Miller */ 604db8dac20SDavid S. Miller connected = 1; 605db8dac20SDavid S. Miller } 606db8dac20SDavid S. Miller ipc.addr = inet->saddr; 607db8dac20SDavid S. Miller 608db8dac20SDavid S. Miller ipc.oif = sk->sk_bound_dev_if; 609db8dac20SDavid S. Miller if (msg->msg_controllen) { 6103b1e0a65SYOSHIFUJI Hideaki err = ip_cmsg_send(sock_net(sk), msg, &ipc); 611db8dac20SDavid S. Miller if (err) 612db8dac20SDavid S. Miller return err; 613db8dac20SDavid S. Miller if (ipc.opt) 614db8dac20SDavid S. Miller free = 1; 615db8dac20SDavid S. Miller connected = 0; 616db8dac20SDavid S. Miller } 617db8dac20SDavid S. Miller if (!ipc.opt) 618db8dac20SDavid S. Miller ipc.opt = inet->opt; 619db8dac20SDavid S. Miller 620db8dac20SDavid S. Miller saddr = ipc.addr; 621db8dac20SDavid S. Miller ipc.addr = faddr = daddr; 622db8dac20SDavid S. Miller 623db8dac20SDavid S. Miller if (ipc.opt && ipc.opt->srr) { 624db8dac20SDavid S. Miller if (!daddr) 625db8dac20SDavid S. Miller return -EINVAL; 626db8dac20SDavid S. Miller faddr = ipc.opt->faddr; 627db8dac20SDavid S. Miller connected = 0; 628db8dac20SDavid S. Miller } 629db8dac20SDavid S. Miller tos = RT_TOS(inet->tos); 630db8dac20SDavid S. Miller if (sock_flag(sk, SOCK_LOCALROUTE) || 631db8dac20SDavid S. Miller (msg->msg_flags & MSG_DONTROUTE) || 632db8dac20SDavid S. Miller (ipc.opt && ipc.opt->is_strictroute)) { 633db8dac20SDavid S. Miller tos |= RTO_ONLINK; 634db8dac20SDavid S. Miller connected = 0; 635db8dac20SDavid S. Miller } 636db8dac20SDavid S. Miller 637db8dac20SDavid S. Miller if (ipv4_is_multicast(daddr)) { 638db8dac20SDavid S. Miller if (!ipc.oif) 639db8dac20SDavid S. Miller ipc.oif = inet->mc_index; 640db8dac20SDavid S. Miller if (!saddr) 641db8dac20SDavid S. Miller saddr = inet->mc_addr; 642db8dac20SDavid S. Miller connected = 0; 643db8dac20SDavid S. Miller } 644db8dac20SDavid S. Miller 645db8dac20SDavid S. Miller if (connected) 646db8dac20SDavid S. Miller rt = (struct rtable*)sk_dst_check(sk, 0); 647db8dac20SDavid S. Miller 648db8dac20SDavid S. Miller if (rt == NULL) { 649db8dac20SDavid S. Miller struct flowi fl = { .oif = ipc.oif, 650db8dac20SDavid S. Miller .nl_u = { .ip4_u = 651db8dac20SDavid S. Miller { .daddr = faddr, 652db8dac20SDavid S. Miller .saddr = saddr, 653db8dac20SDavid S. Miller .tos = tos } }, 654db8dac20SDavid S. Miller .proto = sk->sk_protocol, 655db8dac20SDavid S. Miller .uli_u = { .ports = 656db8dac20SDavid S. Miller { .sport = inet->sport, 657db8dac20SDavid S. Miller .dport = dport } } }; 658db8dac20SDavid S. Miller security_sk_classify_flow(sk, &fl); 6593b1e0a65SYOSHIFUJI Hideaki err = ip_route_output_flow(sock_net(sk), &rt, &fl, sk, 1); 660db8dac20SDavid S. Miller if (err) { 661db8dac20SDavid S. Miller if (err == -ENETUNREACH) 662db8dac20SDavid S. Miller IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES); 663db8dac20SDavid S. Miller goto out; 664db8dac20SDavid S. Miller } 665db8dac20SDavid S. Miller 666db8dac20SDavid S. Miller err = -EACCES; 667db8dac20SDavid S. Miller if ((rt->rt_flags & RTCF_BROADCAST) && 668db8dac20SDavid S. Miller !sock_flag(sk, SOCK_BROADCAST)) 669db8dac20SDavid S. Miller goto out; 670db8dac20SDavid S. Miller if (connected) 671db8dac20SDavid S. Miller sk_dst_set(sk, dst_clone(&rt->u.dst)); 672db8dac20SDavid S. Miller } 673db8dac20SDavid S. Miller 674db8dac20SDavid S. Miller if (msg->msg_flags&MSG_CONFIRM) 675db8dac20SDavid S. Miller goto do_confirm; 676db8dac20SDavid S. Miller back_from_confirm: 677db8dac20SDavid S. Miller 678db8dac20SDavid S. Miller saddr = rt->rt_src; 679db8dac20SDavid S. Miller if (!ipc.addr) 680db8dac20SDavid S. Miller daddr = ipc.addr = rt->rt_dst; 681db8dac20SDavid S. Miller 682db8dac20SDavid S. Miller lock_sock(sk); 683db8dac20SDavid S. Miller if (unlikely(up->pending)) { 684db8dac20SDavid S. Miller /* The socket is already corked while preparing it. */ 685db8dac20SDavid S. Miller /* ... which is an evident application bug. --ANK */ 686db8dac20SDavid S. Miller release_sock(sk); 687db8dac20SDavid S. Miller 688db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n"); 689db8dac20SDavid S. Miller err = -EINVAL; 690db8dac20SDavid S. Miller goto out; 691db8dac20SDavid S. Miller } 692db8dac20SDavid S. Miller /* 693db8dac20SDavid S. Miller * Now cork the socket to pend data. 694db8dac20SDavid S. Miller */ 695db8dac20SDavid S. Miller inet->cork.fl.fl4_dst = daddr; 696db8dac20SDavid S. Miller inet->cork.fl.fl_ip_dport = dport; 697db8dac20SDavid S. Miller inet->cork.fl.fl4_src = saddr; 698db8dac20SDavid S. Miller inet->cork.fl.fl_ip_sport = inet->sport; 699db8dac20SDavid S. Miller up->pending = AF_INET; 700db8dac20SDavid S. Miller 701db8dac20SDavid S. Miller do_append_data: 702db8dac20SDavid S. Miller up->len += ulen; 703db8dac20SDavid S. Miller getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 704db8dac20SDavid S. Miller err = ip_append_data(sk, getfrag, msg->msg_iov, ulen, 705db8dac20SDavid S. Miller sizeof(struct udphdr), &ipc, rt, 706db8dac20SDavid S. Miller corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); 707db8dac20SDavid S. Miller if (err) 708db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 709db8dac20SDavid S. Miller else if (!corkreq) 710db8dac20SDavid S. Miller err = udp_push_pending_frames(sk); 711db8dac20SDavid S. Miller else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 712db8dac20SDavid S. Miller up->pending = 0; 713db8dac20SDavid S. Miller release_sock(sk); 714db8dac20SDavid S. Miller 715db8dac20SDavid S. Miller out: 716db8dac20SDavid S. Miller ip_rt_put(rt); 717db8dac20SDavid S. Miller if (free) 718db8dac20SDavid S. Miller kfree(ipc.opt); 719db8dac20SDavid S. Miller if (!err) 720db8dac20SDavid S. Miller return len; 721db8dac20SDavid S. Miller /* 722db8dac20SDavid S. Miller * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 723db8dac20SDavid S. Miller * ENOBUFS might not be good (it's not tunable per se), but otherwise 724db8dac20SDavid S. Miller * we don't have a good statistic (IpOutDiscards but it can be too many 725db8dac20SDavid S. Miller * things). We could add another new stat but at least for now that 726db8dac20SDavid S. Miller * seems like overkill. 727db8dac20SDavid S. Miller */ 728db8dac20SDavid S. Miller if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 729*629ca23cSPavel Emelyanov UDP_INC_STATS_USER(sock_net(sk), 730*629ca23cSPavel Emelyanov UDP_MIB_SNDBUFERRORS, is_udplite); 731db8dac20SDavid S. Miller } 732db8dac20SDavid S. Miller return err; 733db8dac20SDavid S. Miller 734db8dac20SDavid S. Miller do_confirm: 735db8dac20SDavid S. Miller dst_confirm(&rt->u.dst); 736db8dac20SDavid S. Miller if (!(msg->msg_flags&MSG_PROBE) || len) 737db8dac20SDavid S. Miller goto back_from_confirm; 738db8dac20SDavid S. Miller err = 0; 739db8dac20SDavid S. Miller goto out; 740db8dac20SDavid S. Miller } 741db8dac20SDavid S. Miller 742db8dac20SDavid S. Miller int udp_sendpage(struct sock *sk, struct page *page, int offset, 743db8dac20SDavid S. Miller size_t size, int flags) 744db8dac20SDavid S. Miller { 745db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 746db8dac20SDavid S. Miller int ret; 747db8dac20SDavid S. Miller 748db8dac20SDavid S. Miller if (!up->pending) { 749db8dac20SDavid S. Miller struct msghdr msg = { .msg_flags = flags|MSG_MORE }; 750db8dac20SDavid S. Miller 751db8dac20SDavid S. Miller /* Call udp_sendmsg to specify destination address which 752db8dac20SDavid S. Miller * sendpage interface can't pass. 753db8dac20SDavid S. Miller * This will succeed only when the socket is connected. 754db8dac20SDavid S. Miller */ 755db8dac20SDavid S. Miller ret = udp_sendmsg(NULL, sk, &msg, 0); 756db8dac20SDavid S. Miller if (ret < 0) 757db8dac20SDavid S. Miller return ret; 758db8dac20SDavid S. Miller } 759db8dac20SDavid S. Miller 760db8dac20SDavid S. Miller lock_sock(sk); 761db8dac20SDavid S. Miller 762db8dac20SDavid S. Miller if (unlikely(!up->pending)) { 763db8dac20SDavid S. Miller release_sock(sk); 764db8dac20SDavid S. Miller 765db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n"); 766db8dac20SDavid S. Miller return -EINVAL; 767db8dac20SDavid S. Miller } 768db8dac20SDavid S. Miller 769db8dac20SDavid S. Miller ret = ip_append_page(sk, page, offset, size, flags); 770db8dac20SDavid S. Miller if (ret == -EOPNOTSUPP) { 771db8dac20SDavid S. Miller release_sock(sk); 772db8dac20SDavid S. Miller return sock_no_sendpage(sk->sk_socket, page, offset, 773db8dac20SDavid S. Miller size, flags); 774db8dac20SDavid S. Miller } 775db8dac20SDavid S. Miller if (ret < 0) { 776db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 777db8dac20SDavid S. Miller goto out; 778db8dac20SDavid S. Miller } 779db8dac20SDavid S. Miller 780db8dac20SDavid S. Miller up->len += size; 781db8dac20SDavid S. Miller if (!(up->corkflag || (flags&MSG_MORE))) 782db8dac20SDavid S. Miller ret = udp_push_pending_frames(sk); 783db8dac20SDavid S. Miller if (!ret) 784db8dac20SDavid S. Miller ret = size; 785db8dac20SDavid S. Miller out: 786db8dac20SDavid S. Miller release_sock(sk); 787db8dac20SDavid S. Miller return ret; 788db8dac20SDavid S. Miller } 789db8dac20SDavid S. Miller 7901da177e4SLinus Torvalds /* 7911da177e4SLinus Torvalds * IOCTL requests applicable to the UDP protocol 7921da177e4SLinus Torvalds */ 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds int udp_ioctl(struct sock *sk, int cmd, unsigned long arg) 7951da177e4SLinus Torvalds { 7966516c655SStephen Hemminger switch (cmd) { 7971da177e4SLinus Torvalds case SIOCOUTQ: 7981da177e4SLinus Torvalds { 7991da177e4SLinus Torvalds int amount = atomic_read(&sk->sk_wmem_alloc); 8001da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 8011da177e4SLinus Torvalds } 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds case SIOCINQ: 8041da177e4SLinus Torvalds { 8051da177e4SLinus Torvalds struct sk_buff *skb; 8061da177e4SLinus Torvalds unsigned long amount; 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds amount = 0; 809208d8984SHerbert Xu spin_lock_bh(&sk->sk_receive_queue.lock); 8101da177e4SLinus Torvalds skb = skb_peek(&sk->sk_receive_queue); 8111da177e4SLinus Torvalds if (skb != NULL) { 8121da177e4SLinus Torvalds /* 8131da177e4SLinus Torvalds * We will only return the amount 8141da177e4SLinus Torvalds * of this packet since that is all 8151da177e4SLinus Torvalds * that will be read. 8161da177e4SLinus Torvalds */ 8171da177e4SLinus Torvalds amount = skb->len - sizeof(struct udphdr); 8181da177e4SLinus Torvalds } 819208d8984SHerbert Xu spin_unlock_bh(&sk->sk_receive_queue.lock); 8201da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds 8231da177e4SLinus Torvalds default: 8241da177e4SLinus Torvalds return -ENOIOCTLCMD; 8251da177e4SLinus Torvalds } 8266516c655SStephen Hemminger 8276516c655SStephen Hemminger return 0; 8281da177e4SLinus Torvalds } 8291da177e4SLinus Torvalds 830db8dac20SDavid S. Miller /* 831db8dac20SDavid S. Miller * This should be easy, if there is something there we 832db8dac20SDavid S. Miller * return it, otherwise we block. 833db8dac20SDavid S. Miller */ 834db8dac20SDavid S. Miller 835db8dac20SDavid S. Miller int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 836db8dac20SDavid S. Miller size_t len, int noblock, int flags, int *addr_len) 837db8dac20SDavid S. Miller { 838db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 839db8dac20SDavid S. Miller struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name; 840db8dac20SDavid S. Miller struct sk_buff *skb; 841db8dac20SDavid S. Miller unsigned int ulen, copied; 842db8dac20SDavid S. Miller int peeked; 843db8dac20SDavid S. Miller int err; 844db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 845db8dac20SDavid S. Miller 846db8dac20SDavid S. Miller /* 847db8dac20SDavid S. Miller * Check any passed addresses 848db8dac20SDavid S. Miller */ 849db8dac20SDavid S. Miller if (addr_len) 850db8dac20SDavid S. Miller *addr_len=sizeof(*sin); 851db8dac20SDavid S. Miller 852db8dac20SDavid S. Miller if (flags & MSG_ERRQUEUE) 853db8dac20SDavid S. Miller return ip_recv_error(sk, msg, len); 854db8dac20SDavid S. Miller 855db8dac20SDavid S. Miller try_again: 856db8dac20SDavid S. Miller skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), 857db8dac20SDavid S. Miller &peeked, &err); 858db8dac20SDavid S. Miller if (!skb) 859db8dac20SDavid S. Miller goto out; 860db8dac20SDavid S. Miller 861db8dac20SDavid S. Miller ulen = skb->len - sizeof(struct udphdr); 862db8dac20SDavid S. Miller copied = len; 863db8dac20SDavid S. Miller if (copied > ulen) 864db8dac20SDavid S. Miller copied = ulen; 865db8dac20SDavid S. Miller else if (copied < ulen) 866db8dac20SDavid S. Miller msg->msg_flags |= MSG_TRUNC; 867db8dac20SDavid S. Miller 868db8dac20SDavid S. Miller /* 869db8dac20SDavid S. Miller * If checksum is needed at all, try to do it while copying the 870db8dac20SDavid S. Miller * data. If the data is truncated, or if we only want a partial 871db8dac20SDavid S. Miller * coverage checksum (UDP-Lite), do it before the copy. 872db8dac20SDavid S. Miller */ 873db8dac20SDavid S. Miller 874db8dac20SDavid S. Miller if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) { 875db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 876db8dac20SDavid S. Miller goto csum_copy_err; 877db8dac20SDavid S. Miller } 878db8dac20SDavid S. Miller 879db8dac20SDavid S. Miller if (skb_csum_unnecessary(skb)) 880db8dac20SDavid S. Miller err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), 881db8dac20SDavid S. Miller msg->msg_iov, copied ); 882db8dac20SDavid S. Miller else { 883db8dac20SDavid S. Miller err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov); 884db8dac20SDavid S. Miller 885db8dac20SDavid S. Miller if (err == -EINVAL) 886db8dac20SDavid S. Miller goto csum_copy_err; 887db8dac20SDavid S. Miller } 888db8dac20SDavid S. Miller 889db8dac20SDavid S. Miller if (err) 890db8dac20SDavid S. Miller goto out_free; 891db8dac20SDavid S. Miller 892db8dac20SDavid S. Miller if (!peeked) 893*629ca23cSPavel Emelyanov UDP_INC_STATS_USER(sock_net(sk), 894*629ca23cSPavel Emelyanov UDP_MIB_INDATAGRAMS, is_udplite); 895db8dac20SDavid S. Miller 896db8dac20SDavid S. Miller sock_recv_timestamp(msg, sk, skb); 897db8dac20SDavid S. Miller 898db8dac20SDavid S. Miller /* Copy the address. */ 899db8dac20SDavid S. Miller if (sin) 900db8dac20SDavid S. Miller { 901db8dac20SDavid S. Miller sin->sin_family = AF_INET; 902db8dac20SDavid S. Miller sin->sin_port = udp_hdr(skb)->source; 903db8dac20SDavid S. Miller sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 904db8dac20SDavid S. Miller memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 905db8dac20SDavid S. Miller } 906db8dac20SDavid S. Miller if (inet->cmsg_flags) 907db8dac20SDavid S. Miller ip_cmsg_recv(msg, skb); 908db8dac20SDavid S. Miller 909db8dac20SDavid S. Miller err = copied; 910db8dac20SDavid S. Miller if (flags & MSG_TRUNC) 911db8dac20SDavid S. Miller err = ulen; 912db8dac20SDavid S. Miller 913db8dac20SDavid S. Miller out_free: 914db8dac20SDavid S. Miller lock_sock(sk); 915db8dac20SDavid S. Miller skb_free_datagram(sk, skb); 916db8dac20SDavid S. Miller release_sock(sk); 917db8dac20SDavid S. Miller out: 918db8dac20SDavid S. Miller return err; 919db8dac20SDavid S. Miller 920db8dac20SDavid S. Miller csum_copy_err: 921db8dac20SDavid S. Miller lock_sock(sk); 922db8dac20SDavid S. Miller if (!skb_kill_datagram(sk, skb, flags)) 923*629ca23cSPavel Emelyanov UDP_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 924db8dac20SDavid S. Miller release_sock(sk); 925db8dac20SDavid S. Miller 926db8dac20SDavid S. Miller if (noblock) 927db8dac20SDavid S. Miller return -EAGAIN; 928db8dac20SDavid S. Miller goto try_again; 929db8dac20SDavid S. Miller } 930db8dac20SDavid S. Miller 931db8dac20SDavid S. Miller 9321da177e4SLinus Torvalds int udp_disconnect(struct sock *sk, int flags) 9331da177e4SLinus Torvalds { 9341da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 9351da177e4SLinus Torvalds /* 9361da177e4SLinus Torvalds * 1003.1g - break association. 9371da177e4SLinus Torvalds */ 9381da177e4SLinus Torvalds 9391da177e4SLinus Torvalds sk->sk_state = TCP_CLOSE; 9401da177e4SLinus Torvalds inet->daddr = 0; 9411da177e4SLinus Torvalds inet->dport = 0; 9421da177e4SLinus Torvalds sk->sk_bound_dev_if = 0; 9431da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) 9441da177e4SLinus Torvalds inet_reset_saddr(sk); 9451da177e4SLinus Torvalds 9461da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) { 9471da177e4SLinus Torvalds sk->sk_prot->unhash(sk); 9481da177e4SLinus Torvalds inet->sport = 0; 9491da177e4SLinus Torvalds } 9501da177e4SLinus Torvalds sk_dst_reset(sk); 9511da177e4SLinus Torvalds return 0; 9521da177e4SLinus Torvalds } 9531da177e4SLinus Torvalds 954db8dac20SDavid S. Miller /* returns: 955db8dac20SDavid S. Miller * -1: error 956db8dac20SDavid S. Miller * 0: success 957db8dac20SDavid S. Miller * >0: "udp encap" protocol resubmission 958db8dac20SDavid S. Miller * 959db8dac20SDavid S. Miller * Note that in the success and error cases, the skb is assumed to 960db8dac20SDavid S. Miller * have either been requeued or freed. 961db8dac20SDavid S. Miller */ 962db8dac20SDavid S. Miller int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb) 963db8dac20SDavid S. Miller { 964db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 965db8dac20SDavid S. Miller int rc; 966db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 967db8dac20SDavid S. Miller 968db8dac20SDavid S. Miller /* 969db8dac20SDavid S. Miller * Charge it to the socket, dropping if the queue is full. 970db8dac20SDavid S. Miller */ 971db8dac20SDavid S. Miller if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) 972db8dac20SDavid S. Miller goto drop; 973db8dac20SDavid S. Miller nf_reset(skb); 974db8dac20SDavid S. Miller 975db8dac20SDavid S. Miller if (up->encap_type) { 976db8dac20SDavid S. Miller /* 977db8dac20SDavid S. Miller * This is an encapsulation socket so pass the skb to 978db8dac20SDavid S. Miller * the socket's udp_encap_rcv() hook. Otherwise, just 979db8dac20SDavid S. Miller * fall through and pass this up the UDP socket. 980db8dac20SDavid S. Miller * up->encap_rcv() returns the following value: 981db8dac20SDavid S. Miller * =0 if skb was successfully passed to the encap 982db8dac20SDavid S. Miller * handler or was discarded by it. 983db8dac20SDavid S. Miller * >0 if skb should be passed on to UDP. 984db8dac20SDavid S. Miller * <0 if skb should be resubmitted as proto -N 985db8dac20SDavid S. Miller */ 986db8dac20SDavid S. Miller 987db8dac20SDavid S. Miller /* if we're overly short, let UDP handle it */ 988db8dac20SDavid S. Miller if (skb->len > sizeof(struct udphdr) && 989db8dac20SDavid S. Miller up->encap_rcv != NULL) { 990db8dac20SDavid S. Miller int ret; 991db8dac20SDavid S. Miller 992db8dac20SDavid S. Miller ret = (*up->encap_rcv)(sk, skb); 993db8dac20SDavid S. Miller if (ret <= 0) { 994db8dac20SDavid S. Miller UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS, 995db8dac20SDavid S. Miller is_udplite); 996db8dac20SDavid S. Miller return -ret; 997db8dac20SDavid S. Miller } 998db8dac20SDavid S. Miller } 999db8dac20SDavid S. Miller 1000db8dac20SDavid S. Miller /* FALLTHROUGH -- it's a UDP Packet */ 1001db8dac20SDavid S. Miller } 1002db8dac20SDavid S. Miller 1003db8dac20SDavid S. Miller /* 1004db8dac20SDavid S. Miller * UDP-Lite specific tests, ignored on UDP sockets 1005db8dac20SDavid S. Miller */ 1006db8dac20SDavid S. Miller if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 1007db8dac20SDavid S. Miller 1008db8dac20SDavid S. Miller /* 1009db8dac20SDavid S. Miller * MIB statistics other than incrementing the error count are 1010db8dac20SDavid S. Miller * disabled for the following two types of errors: these depend 1011db8dac20SDavid S. Miller * on the application settings, not on the functioning of the 1012db8dac20SDavid S. Miller * protocol stack as such. 1013db8dac20SDavid S. Miller * 1014db8dac20SDavid S. Miller * RFC 3828 here recommends (sec 3.3): "There should also be a 1015db8dac20SDavid S. Miller * way ... to ... at least let the receiving application block 1016db8dac20SDavid S. Miller * delivery of packets with coverage values less than a value 1017db8dac20SDavid S. Miller * provided by the application." 1018db8dac20SDavid S. Miller */ 1019db8dac20SDavid S. Miller if (up->pcrlen == 0) { /* full coverage was set */ 1020db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_WARNING "UDPLITE: partial coverage " 1021db8dac20SDavid S. Miller "%d while full coverage %d requested\n", 1022db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, skb->len); 1023db8dac20SDavid S. Miller goto drop; 1024db8dac20SDavid S. Miller } 1025db8dac20SDavid S. Miller /* The next case involves violating the min. coverage requested 1026db8dac20SDavid S. Miller * by the receiver. This is subtle: if receiver wants x and x is 1027db8dac20SDavid S. Miller * greater than the buffersize/MTU then receiver will complain 1028db8dac20SDavid S. Miller * that it wants x while sender emits packets of smaller size y. 1029db8dac20SDavid S. Miller * Therefore the above ...()->partial_cov statement is essential. 1030db8dac20SDavid S. Miller */ 1031db8dac20SDavid S. Miller if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 1032db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_WARNING 1033db8dac20SDavid S. Miller "UDPLITE: coverage %d too small, need min %d\n", 1034db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, up->pcrlen); 1035db8dac20SDavid S. Miller goto drop; 1036db8dac20SDavid S. Miller } 1037db8dac20SDavid S. Miller } 1038db8dac20SDavid S. Miller 1039db8dac20SDavid S. Miller if (sk->sk_filter) { 1040db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 1041db8dac20SDavid S. Miller goto drop; 1042db8dac20SDavid S. Miller } 1043db8dac20SDavid S. Miller 1044db8dac20SDavid S. Miller if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) { 1045db8dac20SDavid S. Miller /* Note that an ENOMEM error is charged twice */ 1046cb61cb9bSEric Dumazet if (rc == -ENOMEM) { 1047db8dac20SDavid S. Miller UDP_INC_STATS_BH(UDP_MIB_RCVBUFERRORS, is_udplite); 1048cb61cb9bSEric Dumazet atomic_inc(&sk->sk_drops); 1049cb61cb9bSEric Dumazet } 1050db8dac20SDavid S. Miller goto drop; 1051db8dac20SDavid S. Miller } 1052db8dac20SDavid S. Miller 1053db8dac20SDavid S. Miller return 0; 1054db8dac20SDavid S. Miller 1055db8dac20SDavid S. Miller drop: 1056db8dac20SDavid S. Miller UDP_INC_STATS_BH(UDP_MIB_INERRORS, is_udplite); 1057db8dac20SDavid S. Miller kfree_skb(skb); 1058db8dac20SDavid S. Miller return -1; 1059db8dac20SDavid S. Miller } 1060db8dac20SDavid S. Miller 1061db8dac20SDavid S. Miller /* 1062db8dac20SDavid S. Miller * Multicasts and broadcasts go to each listener. 1063db8dac20SDavid S. Miller * 1064db8dac20SDavid S. Miller * Note: called only from the BH handler context, 1065db8dac20SDavid S. Miller * so we don't need to lock the hashes. 1066db8dac20SDavid S. Miller */ 1067e3163493SPavel Emelyanov static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb, 1068db8dac20SDavid S. Miller struct udphdr *uh, 1069db8dac20SDavid S. Miller __be32 saddr, __be32 daddr, 1070db8dac20SDavid S. Miller struct hlist_head udptable[]) 1071db8dac20SDavid S. Miller { 1072db8dac20SDavid S. Miller struct sock *sk; 1073db8dac20SDavid S. Miller int dif; 1074db8dac20SDavid S. Miller 1075db8dac20SDavid S. Miller read_lock(&udp_hash_lock); 107619c7578fSPavel Emelyanov sk = sk_head(&udptable[udp_hashfn(net, ntohs(uh->dest))]); 1077db8dac20SDavid S. Miller dif = skb->dev->ifindex; 1078db8dac20SDavid S. Miller sk = udp_v4_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif); 1079db8dac20SDavid S. Miller if (sk) { 1080db8dac20SDavid S. Miller struct sock *sknext = NULL; 1081db8dac20SDavid S. Miller 1082db8dac20SDavid S. Miller do { 1083db8dac20SDavid S. Miller struct sk_buff *skb1 = skb; 1084db8dac20SDavid S. Miller 1085db8dac20SDavid S. Miller sknext = udp_v4_mcast_next(sk_next(sk), uh->dest, daddr, 1086db8dac20SDavid S. Miller uh->source, saddr, dif); 1087db8dac20SDavid S. Miller if (sknext) 1088db8dac20SDavid S. Miller skb1 = skb_clone(skb, GFP_ATOMIC); 1089db8dac20SDavid S. Miller 1090db8dac20SDavid S. Miller if (skb1) { 1091db8dac20SDavid S. Miller int ret = 0; 1092db8dac20SDavid S. Miller 1093db8dac20SDavid S. Miller bh_lock_sock_nested(sk); 1094db8dac20SDavid S. Miller if (!sock_owned_by_user(sk)) 1095db8dac20SDavid S. Miller ret = udp_queue_rcv_skb(sk, skb1); 1096db8dac20SDavid S. Miller else 1097db8dac20SDavid S. Miller sk_add_backlog(sk, skb1); 1098db8dac20SDavid S. Miller bh_unlock_sock(sk); 1099db8dac20SDavid S. Miller 1100db8dac20SDavid S. Miller if (ret > 0) 1101db8dac20SDavid S. Miller /* we should probably re-process instead 1102db8dac20SDavid S. Miller * of dropping packets here. */ 1103db8dac20SDavid S. Miller kfree_skb(skb1); 1104db8dac20SDavid S. Miller } 1105db8dac20SDavid S. Miller sk = sknext; 1106db8dac20SDavid S. Miller } while (sknext); 1107db8dac20SDavid S. Miller } else 1108db8dac20SDavid S. Miller kfree_skb(skb); 1109db8dac20SDavid S. Miller read_unlock(&udp_hash_lock); 1110db8dac20SDavid S. Miller return 0; 1111db8dac20SDavid S. Miller } 1112db8dac20SDavid S. Miller 1113db8dac20SDavid S. Miller /* Initialize UDP checksum. If exited with zero value (success), 1114db8dac20SDavid S. Miller * CHECKSUM_UNNECESSARY means, that no more checks are required. 1115db8dac20SDavid S. Miller * Otherwise, csum completion requires chacksumming packet body, 1116db8dac20SDavid S. Miller * including udp header and folding it to skb->csum. 1117db8dac20SDavid S. Miller */ 1118db8dac20SDavid S. Miller static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh, 1119db8dac20SDavid S. Miller int proto) 1120db8dac20SDavid S. Miller { 1121db8dac20SDavid S. Miller const struct iphdr *iph; 1122db8dac20SDavid S. Miller int err; 1123db8dac20SDavid S. Miller 1124db8dac20SDavid S. Miller UDP_SKB_CB(skb)->partial_cov = 0; 1125db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov = skb->len; 1126db8dac20SDavid S. Miller 1127db8dac20SDavid S. Miller if (proto == IPPROTO_UDPLITE) { 1128db8dac20SDavid S. Miller err = udplite_checksum_init(skb, uh); 1129db8dac20SDavid S. Miller if (err) 1130db8dac20SDavid S. Miller return err; 1131db8dac20SDavid S. Miller } 1132db8dac20SDavid S. Miller 1133db8dac20SDavid S. Miller iph = ip_hdr(skb); 1134db8dac20SDavid S. Miller if (uh->check == 0) { 1135db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_UNNECESSARY; 1136db8dac20SDavid S. Miller } else if (skb->ip_summed == CHECKSUM_COMPLETE) { 1137db8dac20SDavid S. Miller if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len, 1138db8dac20SDavid S. Miller proto, skb->csum)) 1139db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_UNNECESSARY; 1140db8dac20SDavid S. Miller } 1141db8dac20SDavid S. Miller if (!skb_csum_unnecessary(skb)) 1142db8dac20SDavid S. Miller skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, 1143db8dac20SDavid S. Miller skb->len, proto, 0); 1144db8dac20SDavid S. Miller /* Probably, we should checksum udp header (it should be in cache 1145db8dac20SDavid S. Miller * in any case) and data in tiny packets (< rx copybreak). 1146db8dac20SDavid S. Miller */ 1147db8dac20SDavid S. Miller 1148db8dac20SDavid S. Miller return 0; 1149db8dac20SDavid S. Miller } 1150db8dac20SDavid S. Miller 1151db8dac20SDavid S. Miller /* 1152db8dac20SDavid S. Miller * All we need to do is get the socket, and then do a checksum. 1153db8dac20SDavid S. Miller */ 1154db8dac20SDavid S. Miller 1155db8dac20SDavid S. Miller int __udp4_lib_rcv(struct sk_buff *skb, struct hlist_head udptable[], 1156db8dac20SDavid S. Miller int proto) 1157db8dac20SDavid S. Miller { 1158db8dac20SDavid S. Miller struct sock *sk; 1159db8dac20SDavid S. Miller struct udphdr *uh = udp_hdr(skb); 1160db8dac20SDavid S. Miller unsigned short ulen; 1161db8dac20SDavid S. Miller struct rtable *rt = (struct rtable*)skb->dst; 1162db8dac20SDavid S. Miller __be32 saddr = ip_hdr(skb)->saddr; 1163db8dac20SDavid S. Miller __be32 daddr = ip_hdr(skb)->daddr; 1164e3163493SPavel Emelyanov struct net *net; 1165db8dac20SDavid S. Miller 1166db8dac20SDavid S. Miller /* 1167db8dac20SDavid S. Miller * Validate the packet. 1168db8dac20SDavid S. Miller */ 1169db8dac20SDavid S. Miller if (!pskb_may_pull(skb, sizeof(struct udphdr))) 1170db8dac20SDavid S. Miller goto drop; /* No space for header. */ 1171db8dac20SDavid S. Miller 1172db8dac20SDavid S. Miller ulen = ntohs(uh->len); 1173db8dac20SDavid S. Miller if (ulen > skb->len) 1174db8dac20SDavid S. Miller goto short_packet; 1175db8dac20SDavid S. Miller 1176db8dac20SDavid S. Miller if (proto == IPPROTO_UDP) { 1177db8dac20SDavid S. Miller /* UDP validates ulen. */ 1178db8dac20SDavid S. Miller if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen)) 1179db8dac20SDavid S. Miller goto short_packet; 1180db8dac20SDavid S. Miller uh = udp_hdr(skb); 1181db8dac20SDavid S. Miller } 1182db8dac20SDavid S. Miller 1183db8dac20SDavid S. Miller if (udp4_csum_init(skb, uh, proto)) 1184db8dac20SDavid S. Miller goto csum_error; 1185db8dac20SDavid S. Miller 1186e3163493SPavel Emelyanov net = dev_net(skb->dev); 1187db8dac20SDavid S. Miller if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST)) 1188e3163493SPavel Emelyanov return __udp4_lib_mcast_deliver(net, skb, uh, 1189e3163493SPavel Emelyanov saddr, daddr, udptable); 1190db8dac20SDavid S. Miller 1191e3163493SPavel Emelyanov sk = __udp4_lib_lookup(net, saddr, uh->source, daddr, 1192db8dac20SDavid S. Miller uh->dest, inet_iif(skb), udptable); 1193db8dac20SDavid S. Miller 1194db8dac20SDavid S. Miller if (sk != NULL) { 1195db8dac20SDavid S. Miller int ret = 0; 1196db8dac20SDavid S. Miller bh_lock_sock_nested(sk); 1197db8dac20SDavid S. Miller if (!sock_owned_by_user(sk)) 1198db8dac20SDavid S. Miller ret = udp_queue_rcv_skb(sk, skb); 1199db8dac20SDavid S. Miller else 1200db8dac20SDavid S. Miller sk_add_backlog(sk, skb); 1201db8dac20SDavid S. Miller bh_unlock_sock(sk); 1202db8dac20SDavid S. Miller sock_put(sk); 1203db8dac20SDavid S. Miller 1204db8dac20SDavid S. Miller /* a return value > 0 means to resubmit the input, but 1205db8dac20SDavid S. Miller * it wants the return to be -protocol, or 0 1206db8dac20SDavid S. Miller */ 1207db8dac20SDavid S. Miller if (ret > 0) 1208db8dac20SDavid S. Miller return -ret; 1209db8dac20SDavid S. Miller return 0; 1210db8dac20SDavid S. Miller } 1211db8dac20SDavid S. Miller 1212db8dac20SDavid S. Miller if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) 1213db8dac20SDavid S. Miller goto drop; 1214db8dac20SDavid S. Miller nf_reset(skb); 1215db8dac20SDavid S. Miller 1216db8dac20SDavid S. Miller /* No socket. Drop packet silently, if checksum is wrong */ 1217db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 1218db8dac20SDavid S. Miller goto csum_error; 1219db8dac20SDavid S. Miller 1220db8dac20SDavid S. Miller UDP_INC_STATS_BH(UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); 1221db8dac20SDavid S. Miller icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); 1222db8dac20SDavid S. Miller 1223db8dac20SDavid S. Miller /* 1224db8dac20SDavid S. Miller * Hmm. We got an UDP packet to a port to which we 1225db8dac20SDavid S. Miller * don't wanna listen. Ignore it. 1226db8dac20SDavid S. Miller */ 1227db8dac20SDavid S. Miller kfree_skb(skb); 1228db8dac20SDavid S. Miller return 0; 1229db8dac20SDavid S. Miller 1230db8dac20SDavid S. Miller short_packet: 1231a7d632b6SYOSHIFUJI Hideaki LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From " NIPQUAD_FMT ":%u %d/%d to " NIPQUAD_FMT ":%u\n", 1232db8dac20SDavid S. Miller proto == IPPROTO_UDPLITE ? "-Lite" : "", 1233db8dac20SDavid S. Miller NIPQUAD(saddr), 1234db8dac20SDavid S. Miller ntohs(uh->source), 1235db8dac20SDavid S. Miller ulen, 1236db8dac20SDavid S. Miller skb->len, 1237db8dac20SDavid S. Miller NIPQUAD(daddr), 1238db8dac20SDavid S. Miller ntohs(uh->dest)); 1239db8dac20SDavid S. Miller goto drop; 1240db8dac20SDavid S. Miller 1241db8dac20SDavid S. Miller csum_error: 1242db8dac20SDavid S. Miller /* 1243db8dac20SDavid S. Miller * RFC1122: OK. Discards the bad packet silently (as far as 1244db8dac20SDavid S. Miller * the network is concerned, anyway) as per 4.1.3.4 (MUST). 1245db8dac20SDavid S. Miller */ 1246a7d632b6SYOSHIFUJI Hideaki LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From " NIPQUAD_FMT ":%u to " NIPQUAD_FMT ":%u ulen %d\n", 1247db8dac20SDavid S. Miller proto == IPPROTO_UDPLITE ? "-Lite" : "", 1248db8dac20SDavid S. Miller NIPQUAD(saddr), 1249db8dac20SDavid S. Miller ntohs(uh->source), 1250db8dac20SDavid S. Miller NIPQUAD(daddr), 1251db8dac20SDavid S. Miller ntohs(uh->dest), 1252db8dac20SDavid S. Miller ulen); 1253db8dac20SDavid S. Miller drop: 1254db8dac20SDavid S. Miller UDP_INC_STATS_BH(UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 1255db8dac20SDavid S. Miller kfree_skb(skb); 1256db8dac20SDavid S. Miller return 0; 1257db8dac20SDavid S. Miller } 1258db8dac20SDavid S. Miller 1259db8dac20SDavid S. Miller int udp_rcv(struct sk_buff *skb) 1260db8dac20SDavid S. Miller { 1261db8dac20SDavid S. Miller return __udp4_lib_rcv(skb, udp_hash, IPPROTO_UDP); 1262db8dac20SDavid S. Miller } 1263db8dac20SDavid S. Miller 12647d06b2e0SBrian Haley void udp_destroy_sock(struct sock *sk) 1265db8dac20SDavid S. Miller { 1266db8dac20SDavid S. Miller lock_sock(sk); 1267db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 1268db8dac20SDavid S. Miller release_sock(sk); 1269db8dac20SDavid S. Miller } 1270db8dac20SDavid S. Miller 12711da177e4SLinus Torvalds /* 12721da177e4SLinus Torvalds * Socket option code for UDP 12731da177e4SLinus Torvalds */ 12744c0a6cb0SGerrit Renker int udp_lib_setsockopt(struct sock *sk, int level, int optname, 12754c0a6cb0SGerrit Renker char __user *optval, int optlen, 12764c0a6cb0SGerrit Renker int (*push_pending_frames)(struct sock *)) 12771da177e4SLinus Torvalds { 12781da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 12791da177e4SLinus Torvalds int val; 12801da177e4SLinus Torvalds int err = 0; 1281b2bf1e26SWang Chen int is_udplite = IS_UDPLITE(sk); 12821da177e4SLinus Torvalds 12831da177e4SLinus Torvalds if (optlen<sizeof(int)) 12841da177e4SLinus Torvalds return -EINVAL; 12851da177e4SLinus Torvalds 12861da177e4SLinus Torvalds if (get_user(val, (int __user *)optval)) 12871da177e4SLinus Torvalds return -EFAULT; 12881da177e4SLinus Torvalds 12891da177e4SLinus Torvalds switch (optname) { 12901da177e4SLinus Torvalds case UDP_CORK: 12911da177e4SLinus Torvalds if (val != 0) { 12921da177e4SLinus Torvalds up->corkflag = 1; 12931da177e4SLinus Torvalds } else { 12941da177e4SLinus Torvalds up->corkflag = 0; 12951da177e4SLinus Torvalds lock_sock(sk); 12964c0a6cb0SGerrit Renker (*push_pending_frames)(sk); 12971da177e4SLinus Torvalds release_sock(sk); 12981da177e4SLinus Torvalds } 12991da177e4SLinus Torvalds break; 13001da177e4SLinus Torvalds 13011da177e4SLinus Torvalds case UDP_ENCAP: 13021da177e4SLinus Torvalds switch (val) { 13031da177e4SLinus Torvalds case 0: 13041da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP: 13051da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP_NON_IKE: 1306067b207bSJames Chapman up->encap_rcv = xfrm4_udp_encap_rcv; 1307067b207bSJames Chapman /* FALLTHROUGH */ 1308342f0234SJames Chapman case UDP_ENCAP_L2TPINUDP: 13091da177e4SLinus Torvalds up->encap_type = val; 13101da177e4SLinus Torvalds break; 13111da177e4SLinus Torvalds default: 13121da177e4SLinus Torvalds err = -ENOPROTOOPT; 13131da177e4SLinus Torvalds break; 13141da177e4SLinus Torvalds } 13151da177e4SLinus Torvalds break; 13161da177e4SLinus Torvalds 1317ba4e58ecSGerrit Renker /* 1318ba4e58ecSGerrit Renker * UDP-Lite's partial checksum coverage (RFC 3828). 1319ba4e58ecSGerrit Renker */ 1320ba4e58ecSGerrit Renker /* The sender sets actual checksum coverage length via this option. 1321ba4e58ecSGerrit Renker * The case coverage > packet length is handled by send module. */ 1322ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 1323b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 1324ba4e58ecSGerrit Renker return -ENOPROTOOPT; 1325ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Illegal coverage: use default (8) */ 1326ba4e58ecSGerrit Renker val = 8; 1327ba4e58ecSGerrit Renker up->pcslen = val; 1328ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_SEND_CC; 1329ba4e58ecSGerrit Renker break; 1330ba4e58ecSGerrit Renker 1331ba4e58ecSGerrit Renker /* The receiver specifies a minimum checksum coverage value. To make 1332ba4e58ecSGerrit Renker * sense, this should be set to at least 8 (as done below). If zero is 1333ba4e58ecSGerrit Renker * used, this again means full checksum coverage. */ 1334ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 1335b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 1336ba4e58ecSGerrit Renker return -ENOPROTOOPT; 1337ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Avoid silly minimal values. */ 1338ba4e58ecSGerrit Renker val = 8; 1339ba4e58ecSGerrit Renker up->pcrlen = val; 1340ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_RECV_CC; 1341ba4e58ecSGerrit Renker break; 1342ba4e58ecSGerrit Renker 13431da177e4SLinus Torvalds default: 13441da177e4SLinus Torvalds err = -ENOPROTOOPT; 13451da177e4SLinus Torvalds break; 13466516c655SStephen Hemminger } 13471da177e4SLinus Torvalds 13481da177e4SLinus Torvalds return err; 13491da177e4SLinus Torvalds } 13501da177e4SLinus Torvalds 1351db8dac20SDavid S. Miller int udp_setsockopt(struct sock *sk, int level, int optname, 1352db8dac20SDavid S. Miller char __user *optval, int optlen) 1353db8dac20SDavid S. Miller { 1354db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1355db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1356db8dac20SDavid S. Miller udp_push_pending_frames); 1357db8dac20SDavid S. Miller return ip_setsockopt(sk, level, optname, optval, optlen); 1358db8dac20SDavid S. Miller } 1359db8dac20SDavid S. Miller 1360db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1361db8dac20SDavid S. Miller int compat_udp_setsockopt(struct sock *sk, int level, int optname, 1362db8dac20SDavid S. Miller char __user *optval, int optlen) 1363db8dac20SDavid S. Miller { 1364db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1365db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1366db8dac20SDavid S. Miller udp_push_pending_frames); 1367db8dac20SDavid S. Miller return compat_ip_setsockopt(sk, level, optname, optval, optlen); 1368db8dac20SDavid S. Miller } 1369db8dac20SDavid S. Miller #endif 1370db8dac20SDavid S. Miller 13714c0a6cb0SGerrit Renker int udp_lib_getsockopt(struct sock *sk, int level, int optname, 13721da177e4SLinus Torvalds char __user *optval, int __user *optlen) 13731da177e4SLinus Torvalds { 13741da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 13751da177e4SLinus Torvalds int val, len; 13761da177e4SLinus Torvalds 13771da177e4SLinus Torvalds if (get_user(len,optlen)) 13781da177e4SLinus Torvalds return -EFAULT; 13791da177e4SLinus Torvalds 13801da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(int)); 13811da177e4SLinus Torvalds 13821da177e4SLinus Torvalds if (len < 0) 13831da177e4SLinus Torvalds return -EINVAL; 13841da177e4SLinus Torvalds 13851da177e4SLinus Torvalds switch (optname) { 13861da177e4SLinus Torvalds case UDP_CORK: 13871da177e4SLinus Torvalds val = up->corkflag; 13881da177e4SLinus Torvalds break; 13891da177e4SLinus Torvalds 13901da177e4SLinus Torvalds case UDP_ENCAP: 13911da177e4SLinus Torvalds val = up->encap_type; 13921da177e4SLinus Torvalds break; 13931da177e4SLinus Torvalds 1394ba4e58ecSGerrit Renker /* The following two cannot be changed on UDP sockets, the return is 1395ba4e58ecSGerrit Renker * always 0 (which corresponds to the full checksum coverage of UDP). */ 1396ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 1397ba4e58ecSGerrit Renker val = up->pcslen; 1398ba4e58ecSGerrit Renker break; 1399ba4e58ecSGerrit Renker 1400ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 1401ba4e58ecSGerrit Renker val = up->pcrlen; 1402ba4e58ecSGerrit Renker break; 1403ba4e58ecSGerrit Renker 14041da177e4SLinus Torvalds default: 14051da177e4SLinus Torvalds return -ENOPROTOOPT; 14066516c655SStephen Hemminger } 14071da177e4SLinus Torvalds 14081da177e4SLinus Torvalds if (put_user(len, optlen)) 14091da177e4SLinus Torvalds return -EFAULT; 14101da177e4SLinus Torvalds if (copy_to_user(optval, &val,len)) 14111da177e4SLinus Torvalds return -EFAULT; 14121da177e4SLinus Torvalds return 0; 14131da177e4SLinus Torvalds } 14141da177e4SLinus Torvalds 1415db8dac20SDavid S. Miller int udp_getsockopt(struct sock *sk, int level, int optname, 1416db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 1417db8dac20SDavid S. Miller { 1418db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1419db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1420db8dac20SDavid S. Miller return ip_getsockopt(sk, level, optname, optval, optlen); 1421db8dac20SDavid S. Miller } 1422db8dac20SDavid S. Miller 1423db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1424db8dac20SDavid S. Miller int compat_udp_getsockopt(struct sock *sk, int level, int optname, 1425db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 1426db8dac20SDavid S. Miller { 1427db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1428db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1429db8dac20SDavid S. Miller return compat_ip_getsockopt(sk, level, optname, optval, optlen); 1430db8dac20SDavid S. Miller } 1431db8dac20SDavid S. Miller #endif 14321da177e4SLinus Torvalds /** 14331da177e4SLinus Torvalds * udp_poll - wait for a UDP event. 14341da177e4SLinus Torvalds * @file - file struct 14351da177e4SLinus Torvalds * @sock - socket 14361da177e4SLinus Torvalds * @wait - poll table 14371da177e4SLinus Torvalds * 14381da177e4SLinus Torvalds * This is same as datagram poll, except for the special case of 14391da177e4SLinus Torvalds * blocking sockets. If application is using a blocking fd 14401da177e4SLinus Torvalds * and a packet with checksum error is in the queue; 14411da177e4SLinus Torvalds * then it could get return from select indicating data available 14421da177e4SLinus Torvalds * but then block when reading it. Add special case code 14431da177e4SLinus Torvalds * to work around these arguably broken applications. 14441da177e4SLinus Torvalds */ 14451da177e4SLinus Torvalds unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait) 14461da177e4SLinus Torvalds { 14471da177e4SLinus Torvalds unsigned int mask = datagram_poll(file, sock, wait); 14481da177e4SLinus Torvalds struct sock *sk = sock->sk; 1449ba4e58ecSGerrit Renker int is_lite = IS_UDPLITE(sk); 14501da177e4SLinus Torvalds 14511da177e4SLinus Torvalds /* Check for false positives due to checksum errors */ 14521da177e4SLinus Torvalds if ( (mask & POLLRDNORM) && 14531da177e4SLinus Torvalds !(file->f_flags & O_NONBLOCK) && 14541da177e4SLinus Torvalds !(sk->sk_shutdown & RCV_SHUTDOWN)){ 14551da177e4SLinus Torvalds struct sk_buff_head *rcvq = &sk->sk_receive_queue; 14561da177e4SLinus Torvalds struct sk_buff *skb; 14571da177e4SLinus Torvalds 1458208d8984SHerbert Xu spin_lock_bh(&rcvq->lock); 1459759e5d00SHerbert Xu while ((skb = skb_peek(rcvq)) != NULL && 1460759e5d00SHerbert Xu udp_lib_checksum_complete(skb)) { 1461ba4e58ecSGerrit Renker UDP_INC_STATS_BH(UDP_MIB_INERRORS, is_lite); 14621da177e4SLinus Torvalds __skb_unlink(skb, rcvq); 14631da177e4SLinus Torvalds kfree_skb(skb); 14641da177e4SLinus Torvalds } 1465208d8984SHerbert Xu spin_unlock_bh(&rcvq->lock); 14661da177e4SLinus Torvalds 14671da177e4SLinus Torvalds /* nothing to see, move along */ 14681da177e4SLinus Torvalds if (skb == NULL) 14691da177e4SLinus Torvalds mask &= ~(POLLIN | POLLRDNORM); 14701da177e4SLinus Torvalds } 14711da177e4SLinus Torvalds 14721da177e4SLinus Torvalds return mask; 14731da177e4SLinus Torvalds 14741da177e4SLinus Torvalds } 14751da177e4SLinus Torvalds 1476db8dac20SDavid S. Miller struct proto udp_prot = { 1477db8dac20SDavid S. Miller .name = "UDP", 1478db8dac20SDavid S. Miller .owner = THIS_MODULE, 1479db8dac20SDavid S. Miller .close = udp_lib_close, 1480db8dac20SDavid S. Miller .connect = ip4_datagram_connect, 1481db8dac20SDavid S. Miller .disconnect = udp_disconnect, 1482db8dac20SDavid S. Miller .ioctl = udp_ioctl, 1483db8dac20SDavid S. Miller .destroy = udp_destroy_sock, 1484db8dac20SDavid S. Miller .setsockopt = udp_setsockopt, 1485db8dac20SDavid S. Miller .getsockopt = udp_getsockopt, 1486db8dac20SDavid S. Miller .sendmsg = udp_sendmsg, 1487db8dac20SDavid S. Miller .recvmsg = udp_recvmsg, 1488db8dac20SDavid S. Miller .sendpage = udp_sendpage, 1489db8dac20SDavid S. Miller .backlog_rcv = udp_queue_rcv_skb, 1490db8dac20SDavid S. Miller .hash = udp_lib_hash, 1491db8dac20SDavid S. Miller .unhash = udp_lib_unhash, 1492db8dac20SDavid S. Miller .get_port = udp_v4_get_port, 1493db8dac20SDavid S. Miller .memory_allocated = &udp_memory_allocated, 1494db8dac20SDavid S. Miller .sysctl_mem = sysctl_udp_mem, 1495db8dac20SDavid S. Miller .sysctl_wmem = &sysctl_udp_wmem_min, 1496db8dac20SDavid S. Miller .sysctl_rmem = &sysctl_udp_rmem_min, 1497db8dac20SDavid S. Miller .obj_size = sizeof(struct udp_sock), 14986ba5a3c5SPavel Emelyanov .h.udp_hash = udp_hash, 1499db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1500db8dac20SDavid S. Miller .compat_setsockopt = compat_udp_setsockopt, 1501db8dac20SDavid S. Miller .compat_getsockopt = compat_udp_getsockopt, 1502db8dac20SDavid S. Miller #endif 1503db8dac20SDavid S. Miller }; 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; 15126f191efeSDenis V. Lunev struct net *net = seq_file_net(seq); 15131da177e4SLinus Torvalds 15141da177e4SLinus Torvalds for (state->bucket = 0; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) { 15151da177e4SLinus Torvalds struct hlist_node *node; 1516ba4e58ecSGerrit Renker sk_for_each(sk, node, state->hashtable + state->bucket) { 1517878628fbSYOSHIFUJI Hideaki if (!net_eq(sock_net(sk), net)) 1518a91275efSDaniel Lezcano continue; 15191da177e4SLinus Torvalds if (sk->sk_family == state->family) 15201da177e4SLinus Torvalds goto found; 15211da177e4SLinus Torvalds } 15221da177e4SLinus Torvalds } 15231da177e4SLinus Torvalds sk = NULL; 15241da177e4SLinus Torvalds found: 15251da177e4SLinus Torvalds return sk; 15261da177e4SLinus Torvalds } 15271da177e4SLinus Torvalds 15281da177e4SLinus Torvalds static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk) 15291da177e4SLinus Torvalds { 15301da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 15316f191efeSDenis V. Lunev struct net *net = seq_file_net(seq); 15321da177e4SLinus Torvalds 15331da177e4SLinus Torvalds do { 15341da177e4SLinus Torvalds sk = sk_next(sk); 15351da177e4SLinus Torvalds try_again: 15361da177e4SLinus Torvalds ; 1537878628fbSYOSHIFUJI Hideaki } while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family)); 15381da177e4SLinus Torvalds 15391da177e4SLinus Torvalds if (!sk && ++state->bucket < UDP_HTABLE_SIZE) { 1540ba4e58ecSGerrit Renker sk = sk_head(state->hashtable + state->bucket); 15411da177e4SLinus Torvalds goto try_again; 15421da177e4SLinus Torvalds } 15431da177e4SLinus Torvalds return sk; 15441da177e4SLinus Torvalds } 15451da177e4SLinus Torvalds 15461da177e4SLinus Torvalds static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos) 15471da177e4SLinus Torvalds { 15481da177e4SLinus Torvalds struct sock *sk = udp_get_first(seq); 15491da177e4SLinus Torvalds 15501da177e4SLinus Torvalds if (sk) 15511da177e4SLinus Torvalds while (pos && (sk = udp_get_next(seq, sk)) != NULL) 15521da177e4SLinus Torvalds --pos; 15531da177e4SLinus Torvalds return pos ? NULL : sk; 15541da177e4SLinus Torvalds } 15551da177e4SLinus Torvalds 15561da177e4SLinus Torvalds static void *udp_seq_start(struct seq_file *seq, loff_t *pos) 15579a429c49SEric Dumazet __acquires(udp_hash_lock) 15581da177e4SLinus Torvalds { 15591da177e4SLinus Torvalds read_lock(&udp_hash_lock); 1560b50660f1SYOSHIFUJI Hideaki return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN; 15611da177e4SLinus Torvalds } 15621da177e4SLinus Torvalds 15631da177e4SLinus Torvalds static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos) 15641da177e4SLinus Torvalds { 15651da177e4SLinus Torvalds struct sock *sk; 15661da177e4SLinus Torvalds 1567b50660f1SYOSHIFUJI Hideaki if (v == SEQ_START_TOKEN) 15681da177e4SLinus Torvalds sk = udp_get_idx(seq, 0); 15691da177e4SLinus Torvalds else 15701da177e4SLinus Torvalds sk = udp_get_next(seq, v); 15711da177e4SLinus Torvalds 15721da177e4SLinus Torvalds ++*pos; 15731da177e4SLinus Torvalds return sk; 15741da177e4SLinus Torvalds } 15751da177e4SLinus Torvalds 15761da177e4SLinus Torvalds static void udp_seq_stop(struct seq_file *seq, void *v) 15779a429c49SEric Dumazet __releases(udp_hash_lock) 15781da177e4SLinus Torvalds { 15791da177e4SLinus Torvalds read_unlock(&udp_hash_lock); 15801da177e4SLinus Torvalds } 15811da177e4SLinus Torvalds 15821da177e4SLinus Torvalds static int udp_seq_open(struct inode *inode, struct file *file) 15831da177e4SLinus Torvalds { 15841da177e4SLinus Torvalds struct udp_seq_afinfo *afinfo = PDE(inode)->data; 1585a2be75c1SDenis V. Lunev struct udp_iter_state *s; 1586a2be75c1SDenis V. Lunev int err; 15871da177e4SLinus Torvalds 1588a2be75c1SDenis V. Lunev err = seq_open_net(inode, file, &afinfo->seq_ops, 1589a2be75c1SDenis V. Lunev sizeof(struct udp_iter_state)); 1590a2be75c1SDenis V. Lunev if (err < 0) 1591a2be75c1SDenis V. Lunev return err; 1592a91275efSDaniel Lezcano 1593a2be75c1SDenis V. Lunev s = ((struct seq_file *)file->private_data)->private; 15941da177e4SLinus Torvalds s->family = afinfo->family; 1595ba4e58ecSGerrit Renker s->hashtable = afinfo->hashtable; 1596a2be75c1SDenis V. Lunev return err; 1597a91275efSDaniel Lezcano } 1598a91275efSDaniel Lezcano 15991da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 16000c96d8c5SDaniel Lezcano int udp_proc_register(struct net *net, struct udp_seq_afinfo *afinfo) 16011da177e4SLinus Torvalds { 16021da177e4SLinus Torvalds struct proc_dir_entry *p; 16031da177e4SLinus Torvalds int rc = 0; 16041da177e4SLinus Torvalds 16053ba9441bSDenis V. Lunev afinfo->seq_fops.open = udp_seq_open; 16063ba9441bSDenis V. Lunev afinfo->seq_fops.read = seq_read; 16073ba9441bSDenis V. Lunev afinfo->seq_fops.llseek = seq_lseek; 16083ba9441bSDenis V. Lunev afinfo->seq_fops.release = seq_release_net; 16091da177e4SLinus Torvalds 1610dda61925SDenis V. Lunev afinfo->seq_ops.start = udp_seq_start; 1611dda61925SDenis V. Lunev afinfo->seq_ops.next = udp_seq_next; 1612dda61925SDenis V. Lunev afinfo->seq_ops.stop = udp_seq_stop; 1613dda61925SDenis V. Lunev 161484841c3cSDenis V. Lunev p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net, 161584841c3cSDenis V. Lunev &afinfo->seq_fops, afinfo); 161684841c3cSDenis V. Lunev if (!p) 16171da177e4SLinus Torvalds rc = -ENOMEM; 16181da177e4SLinus Torvalds return rc; 16191da177e4SLinus Torvalds } 16201da177e4SLinus Torvalds 16210c96d8c5SDaniel Lezcano void udp_proc_unregister(struct net *net, struct udp_seq_afinfo *afinfo) 16221da177e4SLinus Torvalds { 16230c96d8c5SDaniel Lezcano proc_net_remove(net, afinfo->name); 16241da177e4SLinus Torvalds } 1625db8dac20SDavid S. Miller 1626db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 16275e659e4cSPavel Emelyanov static void udp4_format_sock(struct sock *sp, struct seq_file *f, 16285e659e4cSPavel Emelyanov int bucket, int *len) 1629db8dac20SDavid S. Miller { 1630db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sp); 1631db8dac20SDavid S. Miller __be32 dest = inet->daddr; 1632db8dac20SDavid S. Miller __be32 src = inet->rcv_saddr; 1633db8dac20SDavid S. Miller __u16 destp = ntohs(inet->dport); 1634db8dac20SDavid S. Miller __u16 srcp = ntohs(inet->sport); 1635db8dac20SDavid S. Miller 16365e659e4cSPavel Emelyanov seq_printf(f, "%4d: %08X:%04X %08X:%04X" 1637cb61cb9bSEric Dumazet " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d%n", 1638db8dac20SDavid S. Miller bucket, src, srcp, dest, destp, sp->sk_state, 1639db8dac20SDavid S. Miller atomic_read(&sp->sk_wmem_alloc), 1640db8dac20SDavid S. Miller atomic_read(&sp->sk_rmem_alloc), 1641db8dac20SDavid S. Miller 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp), 1642cb61cb9bSEric Dumazet atomic_read(&sp->sk_refcnt), sp, 1643cb61cb9bSEric Dumazet atomic_read(&sp->sk_drops), len); 1644db8dac20SDavid S. Miller } 1645db8dac20SDavid S. Miller 1646db8dac20SDavid S. Miller int udp4_seq_show(struct seq_file *seq, void *v) 1647db8dac20SDavid S. Miller { 1648db8dac20SDavid S. Miller if (v == SEQ_START_TOKEN) 1649db8dac20SDavid S. Miller seq_printf(seq, "%-127s\n", 1650db8dac20SDavid S. Miller " sl local_address rem_address st tx_queue " 1651db8dac20SDavid S. Miller "rx_queue tr tm->when retrnsmt uid timeout " 1652cb61cb9bSEric Dumazet "inode ref pointer drops"); 1653db8dac20SDavid S. Miller else { 1654db8dac20SDavid S. Miller struct udp_iter_state *state = seq->private; 16555e659e4cSPavel Emelyanov int len; 1656db8dac20SDavid S. Miller 16575e659e4cSPavel Emelyanov udp4_format_sock(v, seq, state->bucket, &len); 16585e659e4cSPavel Emelyanov seq_printf(seq, "%*s\n", 127 - len ,""); 1659db8dac20SDavid S. Miller } 1660db8dac20SDavid S. Miller return 0; 1661db8dac20SDavid S. Miller } 1662db8dac20SDavid S. Miller 1663db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 1664db8dac20SDavid S. Miller static struct udp_seq_afinfo udp4_seq_afinfo = { 1665db8dac20SDavid S. Miller .name = "udp", 1666db8dac20SDavid S. Miller .family = AF_INET, 1667db8dac20SDavid S. Miller .hashtable = udp_hash, 16684ad96d39SDenis V. Lunev .seq_fops = { 16694ad96d39SDenis V. Lunev .owner = THIS_MODULE, 16704ad96d39SDenis V. Lunev }, 1671dda61925SDenis V. Lunev .seq_ops = { 1672dda61925SDenis V. Lunev .show = udp4_seq_show, 1673dda61925SDenis V. Lunev }, 1674db8dac20SDavid S. Miller }; 1675db8dac20SDavid S. Miller 167615439febSPavel Emelyanov static int udp4_proc_init_net(struct net *net) 167715439febSPavel Emelyanov { 167815439febSPavel Emelyanov return udp_proc_register(net, &udp4_seq_afinfo); 167915439febSPavel Emelyanov } 168015439febSPavel Emelyanov 168115439febSPavel Emelyanov static void udp4_proc_exit_net(struct net *net) 168215439febSPavel Emelyanov { 168315439febSPavel Emelyanov udp_proc_unregister(net, &udp4_seq_afinfo); 168415439febSPavel Emelyanov } 168515439febSPavel Emelyanov 168615439febSPavel Emelyanov static struct pernet_operations udp4_net_ops = { 168715439febSPavel Emelyanov .init = udp4_proc_init_net, 168815439febSPavel Emelyanov .exit = udp4_proc_exit_net, 168915439febSPavel Emelyanov }; 169015439febSPavel Emelyanov 1691db8dac20SDavid S. Miller int __init udp4_proc_init(void) 1692db8dac20SDavid S. Miller { 169315439febSPavel Emelyanov return register_pernet_subsys(&udp4_net_ops); 1694db8dac20SDavid S. Miller } 1695db8dac20SDavid S. Miller 1696db8dac20SDavid S. Miller void udp4_proc_exit(void) 1697db8dac20SDavid S. Miller { 169815439febSPavel Emelyanov unregister_pernet_subsys(&udp4_net_ops); 1699db8dac20SDavid S. Miller } 17001da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 17011da177e4SLinus Torvalds 170295766fffSHideo Aoki void __init udp_init(void) 170395766fffSHideo Aoki { 170495766fffSHideo Aoki unsigned long limit; 170595766fffSHideo Aoki 170695766fffSHideo Aoki /* Set the pressure threshold up by the same strategy of TCP. It is a 170795766fffSHideo Aoki * fraction of global memory that is up to 1/2 at 256 MB, decreasing 170895766fffSHideo Aoki * toward zero with the amount of memory, with a floor of 128 pages. 170995766fffSHideo Aoki */ 171095766fffSHideo Aoki limit = min(nr_all_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT); 171195766fffSHideo Aoki limit = (limit * (nr_all_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11); 171295766fffSHideo Aoki limit = max(limit, 128UL); 171395766fffSHideo Aoki sysctl_udp_mem[0] = limit / 4 * 3; 171495766fffSHideo Aoki sysctl_udp_mem[1] = limit; 171595766fffSHideo Aoki sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2; 171695766fffSHideo Aoki 171795766fffSHideo Aoki sysctl_udp_rmem_min = SK_MEM_QUANTUM; 171895766fffSHideo Aoki sysctl_udp_wmem_min = SK_MEM_QUANTUM; 171995766fffSHideo Aoki } 172095766fffSHideo Aoki 17211da177e4SLinus Torvalds EXPORT_SYMBOL(udp_disconnect); 17221da177e4SLinus Torvalds EXPORT_SYMBOL(udp_hash); 17231da177e4SLinus Torvalds EXPORT_SYMBOL(udp_hash_lock); 17241da177e4SLinus Torvalds EXPORT_SYMBOL(udp_ioctl); 1725db8dac20SDavid S. Miller EXPORT_SYMBOL(udp_prot); 1726db8dac20SDavid S. Miller EXPORT_SYMBOL(udp_sendmsg); 17274c0a6cb0SGerrit Renker EXPORT_SYMBOL(udp_lib_getsockopt); 17284c0a6cb0SGerrit Renker EXPORT_SYMBOL(udp_lib_setsockopt); 17291da177e4SLinus Torvalds EXPORT_SYMBOL(udp_poll); 17306ba5a3c5SPavel Emelyanov EXPORT_SYMBOL(udp_lib_get_port); 17311da177e4SLinus Torvalds 17321da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 17331da177e4SLinus Torvalds EXPORT_SYMBOL(udp_proc_register); 17341da177e4SLinus Torvalds EXPORT_SYMBOL(udp_proc_unregister); 17351da177e4SLinus Torvalds #endif 1736