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> 8595766fffSHideo 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 12295766fffSHideo Aoki int sysctl_udp_mem[3] __read_mostly; 12395766fffSHideo Aoki int sysctl_udp_rmem_min __read_mostly; 12495766fffSHideo Aoki int sysctl_udp_wmem_min __read_mostly; 12595766fffSHideo Aoki 12695766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_mem); 12795766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_rmem_min); 12895766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_wmem_min); 12995766fffSHideo Aoki 13095766fffSHideo Aoki atomic_t udp_memory_allocated; 13195766fffSHideo Aoki EXPORT_SYMBOL(udp_memory_allocated); 13295766fffSHideo Aoki 133fa4d3c62SPavel Emelyanov static inline int __udp_lib_lport_inuse(struct net *net, __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)]) 140fa4d3c62SPavel Emelyanov if (sk->sk_net == net && 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; 162fa4d3c62SPavel Emelyanov struct net *net = sk->sk_net; 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds write_lock_bh(&udp_hash_lock); 1651da177e4SLinus Torvalds 16632c1da70SStephen Hemminger if (!snum) { 167a25de534SAnton Arapov int i, low, high, remaining; 16832c1da70SStephen Hemminger unsigned rover, best, best_size_so_far; 1691da177e4SLinus Torvalds 170227b60f5SStephen Hemminger inet_get_local_port_range(&low, &high); 171a25de534SAnton Arapov remaining = (high - low) + 1; 172227b60f5SStephen Hemminger 17332c1da70SStephen Hemminger best_size_so_far = UINT_MAX; 174a25de534SAnton Arapov best = rover = net_random() % remaining + low; 17532c1da70SStephen Hemminger 17632c1da70SStephen Hemminger /* 1st pass: look for empty (or shortest) hash chain */ 17732c1da70SStephen Hemminger for (i = 0; i < UDP_HTABLE_SIZE; i++) { 17832c1da70SStephen Hemminger int size = 0; 17932c1da70SStephen Hemminger 18032c1da70SStephen Hemminger head = &udptable[rover & (UDP_HTABLE_SIZE - 1)]; 18132c1da70SStephen Hemminger if (hlist_empty(head)) 1821da177e4SLinus Torvalds goto gotit; 18332c1da70SStephen Hemminger 1845c668704SDavid S. Miller sk_for_each(sk2, node, head) { 1855c668704SDavid S. Miller if (++size >= best_size_so_far) 1865c668704SDavid S. Miller goto next; 1875c668704SDavid S. Miller } 1881da177e4SLinus Torvalds best_size_so_far = size; 18932c1da70SStephen Hemminger best = rover; 1905c668704SDavid S. Miller next: 19132c1da70SStephen Hemminger /* fold back if end of range */ 19232c1da70SStephen Hemminger if (++rover > high) 19332c1da70SStephen Hemminger rover = low + ((rover - low) 19432c1da70SStephen Hemminger & (UDP_HTABLE_SIZE - 1)); 19532c1da70SStephen Hemminger 19632c1da70SStephen Hemminger 1971da177e4SLinus Torvalds } 19832c1da70SStephen Hemminger 19932c1da70SStephen Hemminger /* 2nd pass: find hole in shortest hash chain */ 20032c1da70SStephen Hemminger rover = best; 20132c1da70SStephen Hemminger for (i = 0; i < (1 << 16) / UDP_HTABLE_SIZE; i++) { 202fa4d3c62SPavel Emelyanov if (! __udp_lib_lport_inuse(net, rover, udptable)) 20332c1da70SStephen Hemminger goto gotit; 20432c1da70SStephen Hemminger rover += UDP_HTABLE_SIZE; 20532c1da70SStephen Hemminger if (rover > high) 20632c1da70SStephen Hemminger rover = low + ((rover - low) 20732c1da70SStephen Hemminger & (UDP_HTABLE_SIZE - 1)); 2081da177e4SLinus Torvalds } 20932c1da70SStephen Hemminger 21032c1da70SStephen Hemminger 21132c1da70SStephen Hemminger /* All ports in use! */ 2121da177e4SLinus Torvalds goto fail; 21332c1da70SStephen Hemminger 2141da177e4SLinus Torvalds gotit: 21532c1da70SStephen Hemminger snum = rover; 2161da177e4SLinus Torvalds } else { 217df2bc459SDavid S. Miller head = &udptable[snum & (UDP_HTABLE_SIZE - 1)]; 2181da177e4SLinus Torvalds 21925030a7fSGerrit Renker sk_for_each(sk2, node, head) 220df2bc459SDavid S. Miller if (sk2->sk_hash == snum && 2211da177e4SLinus Torvalds sk2 != sk && 222fa4d3c62SPavel Emelyanov sk2->sk_net == net && 22325030a7fSGerrit Renker (!sk2->sk_reuse || !sk->sk_reuse) && 224df2bc459SDavid S. Miller (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if 225df2bc459SDavid S. Miller || sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 226df2bc459SDavid S. Miller (*saddr_comp)(sk, sk2) ) 2271da177e4SLinus Torvalds goto fail; 2281da177e4SLinus Torvalds } 22932c1da70SStephen Hemminger 23025030a7fSGerrit Renker inet_sk(sk)->num = snum; 231df2bc459SDavid S. Miller sk->sk_hash = snum; 2321da177e4SLinus Torvalds if (sk_unhashed(sk)) { 233df2bc459SDavid S. Miller head = &udptable[snum & (UDP_HTABLE_SIZE - 1)]; 23425030a7fSGerrit Renker sk_add_node(sk, head); 23565f76517SEric Dumazet sock_prot_inuse_add(sk->sk_prot, 1); 2361da177e4SLinus Torvalds } 23725030a7fSGerrit Renker error = 0; 2381da177e4SLinus Torvalds fail: 2391da177e4SLinus Torvalds write_unlock_bh(&udp_hash_lock); 24025030a7fSGerrit Renker return error; 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds 2433fbe070aSStephen Hemminger int udp_get_port(struct sock *sk, unsigned short snum, 244df2bc459SDavid S. Miller int (*scmp)(const struct sock *, const struct sock *)) 245ba4e58ecSGerrit Renker { 24632c1da70SStephen Hemminger return __udp_lib_get_port(sk, snum, udp_hash, scmp); 247ba4e58ecSGerrit Renker } 248ba4e58ecSGerrit Renker 249*db8dac20SDavid S. Miller int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2) 250*db8dac20SDavid S. Miller { 251*db8dac20SDavid S. Miller struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2); 252*db8dac20SDavid S. Miller 253*db8dac20SDavid S. Miller return ( !ipv6_only_sock(sk2) && 254*db8dac20SDavid S. Miller (!inet1->rcv_saddr || !inet2->rcv_saddr || 255*db8dac20SDavid S. Miller inet1->rcv_saddr == inet2->rcv_saddr )); 256*db8dac20SDavid S. Miller } 257*db8dac20SDavid S. Miller 258*db8dac20SDavid S. Miller static inline int udp_v4_get_port(struct sock *sk, unsigned short snum) 259*db8dac20SDavid S. Miller { 260*db8dac20SDavid S. Miller return udp_get_port(sk, snum, ipv4_rcv_saddr_equal); 261*db8dac20SDavid S. Miller } 262*db8dac20SDavid S. Miller 263*db8dac20SDavid S. Miller /* UDP is nearly always wildcards out the wazoo, it makes no sense to try 264*db8dac20SDavid S. Miller * harder than this. -DaveM 265*db8dac20SDavid S. Miller */ 266*db8dac20SDavid S. Miller static struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, 267*db8dac20SDavid S. Miller __be16 sport, __be32 daddr, __be16 dport, 268*db8dac20SDavid S. Miller int dif, struct hlist_head udptable[]) 269*db8dac20SDavid S. Miller { 270*db8dac20SDavid S. Miller struct sock *sk, *result = NULL; 271*db8dac20SDavid S. Miller struct hlist_node *node; 272*db8dac20SDavid S. Miller unsigned short hnum = ntohs(dport); 273*db8dac20SDavid S. Miller int badness = -1; 274*db8dac20SDavid S. Miller 275*db8dac20SDavid S. Miller read_lock(&udp_hash_lock); 276*db8dac20SDavid S. Miller sk_for_each(sk, node, &udptable[hnum & (UDP_HTABLE_SIZE - 1)]) { 277*db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 278*db8dac20SDavid S. Miller 279*db8dac20SDavid S. Miller if (sk->sk_net == net && sk->sk_hash == hnum && 280*db8dac20SDavid S. Miller !ipv6_only_sock(sk)) { 281*db8dac20SDavid S. Miller int score = (sk->sk_family == PF_INET ? 1 : 0); 282*db8dac20SDavid S. Miller if (inet->rcv_saddr) { 283*db8dac20SDavid S. Miller if (inet->rcv_saddr != daddr) 284*db8dac20SDavid S. Miller continue; 285*db8dac20SDavid S. Miller score+=2; 286*db8dac20SDavid S. Miller } 287*db8dac20SDavid S. Miller if (inet->daddr) { 288*db8dac20SDavid S. Miller if (inet->daddr != saddr) 289*db8dac20SDavid S. Miller continue; 290*db8dac20SDavid S. Miller score+=2; 291*db8dac20SDavid S. Miller } 292*db8dac20SDavid S. Miller if (inet->dport) { 293*db8dac20SDavid S. Miller if (inet->dport != sport) 294*db8dac20SDavid S. Miller continue; 295*db8dac20SDavid S. Miller score+=2; 296*db8dac20SDavid S. Miller } 297*db8dac20SDavid S. Miller if (sk->sk_bound_dev_if) { 298*db8dac20SDavid S. Miller if (sk->sk_bound_dev_if != dif) 299*db8dac20SDavid S. Miller continue; 300*db8dac20SDavid S. Miller score+=2; 301*db8dac20SDavid S. Miller } 302*db8dac20SDavid S. Miller if (score == 9) { 303*db8dac20SDavid S. Miller result = sk; 304*db8dac20SDavid S. Miller break; 305*db8dac20SDavid S. Miller } else if (score > badness) { 306*db8dac20SDavid S. Miller result = sk; 307*db8dac20SDavid S. Miller badness = score; 308*db8dac20SDavid S. Miller } 309*db8dac20SDavid S. Miller } 310*db8dac20SDavid S. Miller } 311*db8dac20SDavid S. Miller if (result) 312*db8dac20SDavid S. Miller sock_hold(result); 313*db8dac20SDavid S. Miller read_unlock(&udp_hash_lock); 314*db8dac20SDavid S. Miller return result; 315*db8dac20SDavid S. Miller } 316*db8dac20SDavid S. Miller 317*db8dac20SDavid S. Miller static inline struct sock *udp_v4_mcast_next(struct sock *sk, 318*db8dac20SDavid S. Miller __be16 loc_port, __be32 loc_addr, 319*db8dac20SDavid S. Miller __be16 rmt_port, __be32 rmt_addr, 320*db8dac20SDavid S. Miller int dif) 321*db8dac20SDavid S. Miller { 322*db8dac20SDavid S. Miller struct hlist_node *node; 323*db8dac20SDavid S. Miller struct sock *s = sk; 324*db8dac20SDavid S. Miller unsigned short hnum = ntohs(loc_port); 325*db8dac20SDavid S. Miller 326*db8dac20SDavid S. Miller sk_for_each_from(s, node) { 327*db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(s); 328*db8dac20SDavid S. Miller 329*db8dac20SDavid S. Miller if (s->sk_hash != hnum || 330*db8dac20SDavid S. Miller (inet->daddr && inet->daddr != rmt_addr) || 331*db8dac20SDavid S. Miller (inet->dport != rmt_port && inet->dport) || 332*db8dac20SDavid S. Miller (inet->rcv_saddr && inet->rcv_saddr != loc_addr) || 333*db8dac20SDavid S. Miller ipv6_only_sock(s) || 334*db8dac20SDavid S. Miller (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)) 335*db8dac20SDavid S. Miller continue; 336*db8dac20SDavid S. Miller if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif)) 337*db8dac20SDavid S. Miller continue; 338*db8dac20SDavid S. Miller goto found; 339*db8dac20SDavid S. Miller } 340*db8dac20SDavid S. Miller s = NULL; 341*db8dac20SDavid S. Miller found: 342*db8dac20SDavid S. Miller return s; 343*db8dac20SDavid S. Miller } 344*db8dac20SDavid S. Miller 345*db8dac20SDavid S. Miller /* 346*db8dac20SDavid S. Miller * This routine is called by the ICMP module when it gets some 347*db8dac20SDavid S. Miller * sort of error condition. If err < 0 then the socket should 348*db8dac20SDavid S. Miller * be closed and the error returned to the user. If err > 0 349*db8dac20SDavid S. Miller * it's just the icmp type << 8 | icmp code. 350*db8dac20SDavid S. Miller * Header points to the ip header of the error packet. We move 351*db8dac20SDavid S. Miller * on past this. Then (as it used to claim before adjustment) 352*db8dac20SDavid S. Miller * header points to the first 8 bytes of the udp header. We need 353*db8dac20SDavid S. Miller * to find the appropriate port. 354*db8dac20SDavid S. Miller */ 355*db8dac20SDavid S. Miller 356*db8dac20SDavid S. Miller void __udp4_lib_err(struct sk_buff *skb, u32 info, struct hlist_head udptable[]) 357*db8dac20SDavid S. Miller { 358*db8dac20SDavid S. Miller struct inet_sock *inet; 359*db8dac20SDavid S. Miller struct iphdr *iph = (struct iphdr*)skb->data; 360*db8dac20SDavid S. Miller struct udphdr *uh = (struct udphdr*)(skb->data+(iph->ihl<<2)); 361*db8dac20SDavid S. Miller const int type = icmp_hdr(skb)->type; 362*db8dac20SDavid S. Miller const int code = icmp_hdr(skb)->code; 363*db8dac20SDavid S. Miller struct sock *sk; 364*db8dac20SDavid S. Miller int harderr; 365*db8dac20SDavid S. Miller int err; 366*db8dac20SDavid S. Miller 367*db8dac20SDavid S. Miller sk = __udp4_lib_lookup(skb->dev->nd_net, iph->daddr, uh->dest, 368*db8dac20SDavid S. Miller iph->saddr, uh->source, skb->dev->ifindex, udptable); 369*db8dac20SDavid S. Miller if (sk == NULL) { 370*db8dac20SDavid S. Miller ICMP_INC_STATS_BH(ICMP_MIB_INERRORS); 371*db8dac20SDavid S. Miller return; /* No socket for error */ 372*db8dac20SDavid S. Miller } 373*db8dac20SDavid S. Miller 374*db8dac20SDavid S. Miller err = 0; 375*db8dac20SDavid S. Miller harderr = 0; 376*db8dac20SDavid S. Miller inet = inet_sk(sk); 377*db8dac20SDavid S. Miller 378*db8dac20SDavid S. Miller switch (type) { 379*db8dac20SDavid S. Miller default: 380*db8dac20SDavid S. Miller case ICMP_TIME_EXCEEDED: 381*db8dac20SDavid S. Miller err = EHOSTUNREACH; 382*db8dac20SDavid S. Miller break; 383*db8dac20SDavid S. Miller case ICMP_SOURCE_QUENCH: 384*db8dac20SDavid S. Miller goto out; 385*db8dac20SDavid S. Miller case ICMP_PARAMETERPROB: 386*db8dac20SDavid S. Miller err = EPROTO; 387*db8dac20SDavid S. Miller harderr = 1; 388*db8dac20SDavid S. Miller break; 389*db8dac20SDavid S. Miller case ICMP_DEST_UNREACH: 390*db8dac20SDavid S. Miller if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */ 391*db8dac20SDavid S. Miller if (inet->pmtudisc != IP_PMTUDISC_DONT) { 392*db8dac20SDavid S. Miller err = EMSGSIZE; 393*db8dac20SDavid S. Miller harderr = 1; 394*db8dac20SDavid S. Miller break; 395*db8dac20SDavid S. Miller } 396*db8dac20SDavid S. Miller goto out; 397*db8dac20SDavid S. Miller } 398*db8dac20SDavid S. Miller err = EHOSTUNREACH; 399*db8dac20SDavid S. Miller if (code <= NR_ICMP_UNREACH) { 400*db8dac20SDavid S. Miller harderr = icmp_err_convert[code].fatal; 401*db8dac20SDavid S. Miller err = icmp_err_convert[code].errno; 402*db8dac20SDavid S. Miller } 403*db8dac20SDavid S. Miller break; 404*db8dac20SDavid S. Miller } 405*db8dac20SDavid S. Miller 406*db8dac20SDavid S. Miller /* 407*db8dac20SDavid S. Miller * RFC1122: OK. Passes ICMP errors back to application, as per 408*db8dac20SDavid S. Miller * 4.1.3.3. 409*db8dac20SDavid S. Miller */ 410*db8dac20SDavid S. Miller if (!inet->recverr) { 411*db8dac20SDavid S. Miller if (!harderr || sk->sk_state != TCP_ESTABLISHED) 412*db8dac20SDavid S. Miller goto out; 413*db8dac20SDavid S. Miller } else { 414*db8dac20SDavid S. Miller ip_icmp_error(sk, skb, err, uh->dest, info, (u8*)(uh+1)); 415*db8dac20SDavid S. Miller } 416*db8dac20SDavid S. Miller sk->sk_err = err; 417*db8dac20SDavid S. Miller sk->sk_error_report(sk); 418*db8dac20SDavid S. Miller out: 419*db8dac20SDavid S. Miller sock_put(sk); 420*db8dac20SDavid S. Miller } 421*db8dac20SDavid S. Miller 422*db8dac20SDavid S. Miller void udp_err(struct sk_buff *skb, u32 info) 423*db8dac20SDavid S. Miller { 424*db8dac20SDavid S. Miller __udp4_lib_err(skb, info, udp_hash); 425*db8dac20SDavid S. Miller } 426*db8dac20SDavid S. Miller 427*db8dac20SDavid S. Miller /* 428*db8dac20SDavid S. Miller * Throw away all pending data and cancel the corking. Socket is locked. 429*db8dac20SDavid S. Miller */ 430*db8dac20SDavid S. Miller static void udp_flush_pending_frames(struct sock *sk) 431*db8dac20SDavid S. Miller { 432*db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 433*db8dac20SDavid S. Miller 434*db8dac20SDavid S. Miller if (up->pending) { 435*db8dac20SDavid S. Miller up->len = 0; 436*db8dac20SDavid S. Miller up->pending = 0; 437*db8dac20SDavid S. Miller ip_flush_pending_frames(sk); 438*db8dac20SDavid S. Miller } 439*db8dac20SDavid S. Miller } 440*db8dac20SDavid S. Miller 441*db8dac20SDavid S. Miller /** 442*db8dac20SDavid S. Miller * udp4_hwcsum_outgoing - handle outgoing HW checksumming 443*db8dac20SDavid S. Miller * @sk: socket we are sending on 444*db8dac20SDavid S. Miller * @skb: sk_buff containing the filled-in UDP header 445*db8dac20SDavid S. Miller * (checksum field must be zeroed out) 446*db8dac20SDavid S. Miller */ 447*db8dac20SDavid S. Miller static void udp4_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb, 448*db8dac20SDavid S. Miller __be32 src, __be32 dst, int len ) 449*db8dac20SDavid S. Miller { 450*db8dac20SDavid S. Miller unsigned int offset; 451*db8dac20SDavid S. Miller struct udphdr *uh = udp_hdr(skb); 452*db8dac20SDavid S. Miller __wsum csum = 0; 453*db8dac20SDavid S. Miller 454*db8dac20SDavid S. Miller if (skb_queue_len(&sk->sk_write_queue) == 1) { 455*db8dac20SDavid S. Miller /* 456*db8dac20SDavid S. Miller * Only one fragment on the socket. 457*db8dac20SDavid S. Miller */ 458*db8dac20SDavid S. Miller skb->csum_start = skb_transport_header(skb) - skb->head; 459*db8dac20SDavid S. Miller skb->csum_offset = offsetof(struct udphdr, check); 460*db8dac20SDavid S. Miller uh->check = ~csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, 0); 461*db8dac20SDavid S. Miller } else { 462*db8dac20SDavid S. Miller /* 463*db8dac20SDavid S. Miller * HW-checksum won't work as there are two or more 464*db8dac20SDavid S. Miller * fragments on the socket so that all csums of sk_buffs 465*db8dac20SDavid S. Miller * should be together 466*db8dac20SDavid S. Miller */ 467*db8dac20SDavid S. Miller offset = skb_transport_offset(skb); 468*db8dac20SDavid S. Miller skb->csum = skb_checksum(skb, offset, skb->len - offset, 0); 469*db8dac20SDavid S. Miller 470*db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_NONE; 471*db8dac20SDavid S. Miller 472*db8dac20SDavid S. Miller skb_queue_walk(&sk->sk_write_queue, skb) { 473*db8dac20SDavid S. Miller csum = csum_add(csum, skb->csum); 474*db8dac20SDavid S. Miller } 475*db8dac20SDavid S. Miller 476*db8dac20SDavid S. Miller uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum); 477*db8dac20SDavid S. Miller if (uh->check == 0) 478*db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 479*db8dac20SDavid S. Miller } 480*db8dac20SDavid S. Miller } 481*db8dac20SDavid S. Miller 482*db8dac20SDavid S. Miller /* 483*db8dac20SDavid S. Miller * Push out all pending data as one UDP datagram. Socket is locked. 484*db8dac20SDavid S. Miller */ 485*db8dac20SDavid S. Miller static int udp_push_pending_frames(struct sock *sk) 486*db8dac20SDavid S. Miller { 487*db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 488*db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 489*db8dac20SDavid S. Miller struct flowi *fl = &inet->cork.fl; 490*db8dac20SDavid S. Miller struct sk_buff *skb; 491*db8dac20SDavid S. Miller struct udphdr *uh; 492*db8dac20SDavid S. Miller int err = 0; 493*db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 494*db8dac20SDavid S. Miller __wsum csum = 0; 495*db8dac20SDavid S. Miller 496*db8dac20SDavid S. Miller /* Grab the skbuff where UDP header space exists. */ 497*db8dac20SDavid S. Miller if ((skb = skb_peek(&sk->sk_write_queue)) == NULL) 498*db8dac20SDavid S. Miller goto out; 499*db8dac20SDavid S. Miller 500*db8dac20SDavid S. Miller /* 501*db8dac20SDavid S. Miller * Create a UDP header 502*db8dac20SDavid S. Miller */ 503*db8dac20SDavid S. Miller uh = udp_hdr(skb); 504*db8dac20SDavid S. Miller uh->source = fl->fl_ip_sport; 505*db8dac20SDavid S. Miller uh->dest = fl->fl_ip_dport; 506*db8dac20SDavid S. Miller uh->len = htons(up->len); 507*db8dac20SDavid S. Miller uh->check = 0; 508*db8dac20SDavid S. Miller 509*db8dac20SDavid S. Miller if (is_udplite) /* UDP-Lite */ 510*db8dac20SDavid S. Miller csum = udplite_csum_outgoing(sk, skb); 511*db8dac20SDavid S. Miller 512*db8dac20SDavid S. Miller else if (sk->sk_no_check == UDP_CSUM_NOXMIT) { /* UDP csum disabled */ 513*db8dac20SDavid S. Miller 514*db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_NONE; 515*db8dac20SDavid S. Miller goto send; 516*db8dac20SDavid S. Miller 517*db8dac20SDavid S. Miller } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 518*db8dac20SDavid S. Miller 519*db8dac20SDavid S. Miller udp4_hwcsum_outgoing(sk, skb, fl->fl4_src,fl->fl4_dst, up->len); 520*db8dac20SDavid S. Miller goto send; 521*db8dac20SDavid S. Miller 522*db8dac20SDavid S. Miller } else /* `normal' UDP */ 523*db8dac20SDavid S. Miller csum = udp_csum_outgoing(sk, skb); 524*db8dac20SDavid S. Miller 525*db8dac20SDavid S. Miller /* add protocol-dependent pseudo-header */ 526*db8dac20SDavid S. Miller uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst, up->len, 527*db8dac20SDavid S. Miller sk->sk_protocol, csum ); 528*db8dac20SDavid S. Miller if (uh->check == 0) 529*db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 530*db8dac20SDavid S. Miller 531*db8dac20SDavid S. Miller send: 532*db8dac20SDavid S. Miller err = ip_push_pending_frames(sk); 533*db8dac20SDavid S. Miller out: 534*db8dac20SDavid S. Miller up->len = 0; 535*db8dac20SDavid S. Miller up->pending = 0; 536*db8dac20SDavid S. Miller if (!err) 537*db8dac20SDavid S. Miller UDP_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS, is_udplite); 538*db8dac20SDavid S. Miller return err; 539*db8dac20SDavid S. Miller } 540*db8dac20SDavid S. Miller 541*db8dac20SDavid S. Miller int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 542*db8dac20SDavid S. Miller size_t len) 543*db8dac20SDavid S. Miller { 544*db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 545*db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 546*db8dac20SDavid S. Miller int ulen = len; 547*db8dac20SDavid S. Miller struct ipcm_cookie ipc; 548*db8dac20SDavid S. Miller struct rtable *rt = NULL; 549*db8dac20SDavid S. Miller int free = 0; 550*db8dac20SDavid S. Miller int connected = 0; 551*db8dac20SDavid S. Miller __be32 daddr, faddr, saddr; 552*db8dac20SDavid S. Miller __be16 dport; 553*db8dac20SDavid S. Miller u8 tos; 554*db8dac20SDavid S. Miller int err, is_udplite = IS_UDPLITE(sk); 555*db8dac20SDavid S. Miller int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; 556*db8dac20SDavid S. Miller int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 557*db8dac20SDavid S. Miller 558*db8dac20SDavid S. Miller if (len > 0xFFFF) 559*db8dac20SDavid S. Miller return -EMSGSIZE; 560*db8dac20SDavid S. Miller 561*db8dac20SDavid S. Miller /* 562*db8dac20SDavid S. Miller * Check the flags. 563*db8dac20SDavid S. Miller */ 564*db8dac20SDavid S. Miller 565*db8dac20SDavid S. Miller if (msg->msg_flags&MSG_OOB) /* Mirror BSD error message compatibility */ 566*db8dac20SDavid S. Miller return -EOPNOTSUPP; 567*db8dac20SDavid S. Miller 568*db8dac20SDavid S. Miller ipc.opt = NULL; 569*db8dac20SDavid S. Miller 570*db8dac20SDavid S. Miller if (up->pending) { 571*db8dac20SDavid S. Miller /* 572*db8dac20SDavid S. Miller * There are pending frames. 573*db8dac20SDavid S. Miller * The socket lock must be held while it's corked. 574*db8dac20SDavid S. Miller */ 575*db8dac20SDavid S. Miller lock_sock(sk); 576*db8dac20SDavid S. Miller if (likely(up->pending)) { 577*db8dac20SDavid S. Miller if (unlikely(up->pending != AF_INET)) { 578*db8dac20SDavid S. Miller release_sock(sk); 579*db8dac20SDavid S. Miller return -EINVAL; 580*db8dac20SDavid S. Miller } 581*db8dac20SDavid S. Miller goto do_append_data; 582*db8dac20SDavid S. Miller } 583*db8dac20SDavid S. Miller release_sock(sk); 584*db8dac20SDavid S. Miller } 585*db8dac20SDavid S. Miller ulen += sizeof(struct udphdr); 586*db8dac20SDavid S. Miller 587*db8dac20SDavid S. Miller /* 588*db8dac20SDavid S. Miller * Get and verify the address. 589*db8dac20SDavid S. Miller */ 590*db8dac20SDavid S. Miller if (msg->msg_name) { 591*db8dac20SDavid S. Miller struct sockaddr_in * usin = (struct sockaddr_in*)msg->msg_name; 592*db8dac20SDavid S. Miller if (msg->msg_namelen < sizeof(*usin)) 593*db8dac20SDavid S. Miller return -EINVAL; 594*db8dac20SDavid S. Miller if (usin->sin_family != AF_INET) { 595*db8dac20SDavid S. Miller if (usin->sin_family != AF_UNSPEC) 596*db8dac20SDavid S. Miller return -EAFNOSUPPORT; 597*db8dac20SDavid S. Miller } 598*db8dac20SDavid S. Miller 599*db8dac20SDavid S. Miller daddr = usin->sin_addr.s_addr; 600*db8dac20SDavid S. Miller dport = usin->sin_port; 601*db8dac20SDavid S. Miller if (dport == 0) 602*db8dac20SDavid S. Miller return -EINVAL; 603*db8dac20SDavid S. Miller } else { 604*db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 605*db8dac20SDavid S. Miller return -EDESTADDRREQ; 606*db8dac20SDavid S. Miller daddr = inet->daddr; 607*db8dac20SDavid S. Miller dport = inet->dport; 608*db8dac20SDavid S. Miller /* Open fast path for connected socket. 609*db8dac20SDavid S. Miller Route will not be used, if at least one option is set. 610*db8dac20SDavid S. Miller */ 611*db8dac20SDavid S. Miller connected = 1; 612*db8dac20SDavid S. Miller } 613*db8dac20SDavid S. Miller ipc.addr = inet->saddr; 614*db8dac20SDavid S. Miller 615*db8dac20SDavid S. Miller ipc.oif = sk->sk_bound_dev_if; 616*db8dac20SDavid S. Miller if (msg->msg_controllen) { 617*db8dac20SDavid S. Miller err = ip_cmsg_send(msg, &ipc); 618*db8dac20SDavid S. Miller if (err) 619*db8dac20SDavid S. Miller return err; 620*db8dac20SDavid S. Miller if (ipc.opt) 621*db8dac20SDavid S. Miller free = 1; 622*db8dac20SDavid S. Miller connected = 0; 623*db8dac20SDavid S. Miller } 624*db8dac20SDavid S. Miller if (!ipc.opt) 625*db8dac20SDavid S. Miller ipc.opt = inet->opt; 626*db8dac20SDavid S. Miller 627*db8dac20SDavid S. Miller saddr = ipc.addr; 628*db8dac20SDavid S. Miller ipc.addr = faddr = daddr; 629*db8dac20SDavid S. Miller 630*db8dac20SDavid S. Miller if (ipc.opt && ipc.opt->srr) { 631*db8dac20SDavid S. Miller if (!daddr) 632*db8dac20SDavid S. Miller return -EINVAL; 633*db8dac20SDavid S. Miller faddr = ipc.opt->faddr; 634*db8dac20SDavid S. Miller connected = 0; 635*db8dac20SDavid S. Miller } 636*db8dac20SDavid S. Miller tos = RT_TOS(inet->tos); 637*db8dac20SDavid S. Miller if (sock_flag(sk, SOCK_LOCALROUTE) || 638*db8dac20SDavid S. Miller (msg->msg_flags & MSG_DONTROUTE) || 639*db8dac20SDavid S. Miller (ipc.opt && ipc.opt->is_strictroute)) { 640*db8dac20SDavid S. Miller tos |= RTO_ONLINK; 641*db8dac20SDavid S. Miller connected = 0; 642*db8dac20SDavid S. Miller } 643*db8dac20SDavid S. Miller 644*db8dac20SDavid S. Miller if (ipv4_is_multicast(daddr)) { 645*db8dac20SDavid S. Miller if (!ipc.oif) 646*db8dac20SDavid S. Miller ipc.oif = inet->mc_index; 647*db8dac20SDavid S. Miller if (!saddr) 648*db8dac20SDavid S. Miller saddr = inet->mc_addr; 649*db8dac20SDavid S. Miller connected = 0; 650*db8dac20SDavid S. Miller } 651*db8dac20SDavid S. Miller 652*db8dac20SDavid S. Miller if (connected) 653*db8dac20SDavid S. Miller rt = (struct rtable*)sk_dst_check(sk, 0); 654*db8dac20SDavid S. Miller 655*db8dac20SDavid S. Miller if (rt == NULL) { 656*db8dac20SDavid S. Miller struct flowi fl = { .oif = ipc.oif, 657*db8dac20SDavid S. Miller .nl_u = { .ip4_u = 658*db8dac20SDavid S. Miller { .daddr = faddr, 659*db8dac20SDavid S. Miller .saddr = saddr, 660*db8dac20SDavid S. Miller .tos = tos } }, 661*db8dac20SDavid S. Miller .proto = sk->sk_protocol, 662*db8dac20SDavid S. Miller .uli_u = { .ports = 663*db8dac20SDavid S. Miller { .sport = inet->sport, 664*db8dac20SDavid S. Miller .dport = dport } } }; 665*db8dac20SDavid S. Miller security_sk_classify_flow(sk, &fl); 666*db8dac20SDavid S. Miller err = ip_route_output_flow(&init_net, &rt, &fl, sk, 1); 667*db8dac20SDavid S. Miller if (err) { 668*db8dac20SDavid S. Miller if (err == -ENETUNREACH) 669*db8dac20SDavid S. Miller IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES); 670*db8dac20SDavid S. Miller goto out; 671*db8dac20SDavid S. Miller } 672*db8dac20SDavid S. Miller 673*db8dac20SDavid S. Miller err = -EACCES; 674*db8dac20SDavid S. Miller if ((rt->rt_flags & RTCF_BROADCAST) && 675*db8dac20SDavid S. Miller !sock_flag(sk, SOCK_BROADCAST)) 676*db8dac20SDavid S. Miller goto out; 677*db8dac20SDavid S. Miller if (connected) 678*db8dac20SDavid S. Miller sk_dst_set(sk, dst_clone(&rt->u.dst)); 679*db8dac20SDavid S. Miller } 680*db8dac20SDavid S. Miller 681*db8dac20SDavid S. Miller if (msg->msg_flags&MSG_CONFIRM) 682*db8dac20SDavid S. Miller goto do_confirm; 683*db8dac20SDavid S. Miller back_from_confirm: 684*db8dac20SDavid S. Miller 685*db8dac20SDavid S. Miller saddr = rt->rt_src; 686*db8dac20SDavid S. Miller if (!ipc.addr) 687*db8dac20SDavid S. Miller daddr = ipc.addr = rt->rt_dst; 688*db8dac20SDavid S. Miller 689*db8dac20SDavid S. Miller lock_sock(sk); 690*db8dac20SDavid S. Miller if (unlikely(up->pending)) { 691*db8dac20SDavid S. Miller /* The socket is already corked while preparing it. */ 692*db8dac20SDavid S. Miller /* ... which is an evident application bug. --ANK */ 693*db8dac20SDavid S. Miller release_sock(sk); 694*db8dac20SDavid S. Miller 695*db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n"); 696*db8dac20SDavid S. Miller err = -EINVAL; 697*db8dac20SDavid S. Miller goto out; 698*db8dac20SDavid S. Miller } 699*db8dac20SDavid S. Miller /* 700*db8dac20SDavid S. Miller * Now cork the socket to pend data. 701*db8dac20SDavid S. Miller */ 702*db8dac20SDavid S. Miller inet->cork.fl.fl4_dst = daddr; 703*db8dac20SDavid S. Miller inet->cork.fl.fl_ip_dport = dport; 704*db8dac20SDavid S. Miller inet->cork.fl.fl4_src = saddr; 705*db8dac20SDavid S. Miller inet->cork.fl.fl_ip_sport = inet->sport; 706*db8dac20SDavid S. Miller up->pending = AF_INET; 707*db8dac20SDavid S. Miller 708*db8dac20SDavid S. Miller do_append_data: 709*db8dac20SDavid S. Miller up->len += ulen; 710*db8dac20SDavid S. Miller getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 711*db8dac20SDavid S. Miller err = ip_append_data(sk, getfrag, msg->msg_iov, ulen, 712*db8dac20SDavid S. Miller sizeof(struct udphdr), &ipc, rt, 713*db8dac20SDavid S. Miller corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); 714*db8dac20SDavid S. Miller if (err) 715*db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 716*db8dac20SDavid S. Miller else if (!corkreq) 717*db8dac20SDavid S. Miller err = udp_push_pending_frames(sk); 718*db8dac20SDavid S. Miller else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 719*db8dac20SDavid S. Miller up->pending = 0; 720*db8dac20SDavid S. Miller release_sock(sk); 721*db8dac20SDavid S. Miller 722*db8dac20SDavid S. Miller out: 723*db8dac20SDavid S. Miller ip_rt_put(rt); 724*db8dac20SDavid S. Miller if (free) 725*db8dac20SDavid S. Miller kfree(ipc.opt); 726*db8dac20SDavid S. Miller if (!err) 727*db8dac20SDavid S. Miller return len; 728*db8dac20SDavid S. Miller /* 729*db8dac20SDavid S. Miller * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 730*db8dac20SDavid S. Miller * ENOBUFS might not be good (it's not tunable per se), but otherwise 731*db8dac20SDavid S. Miller * we don't have a good statistic (IpOutDiscards but it can be too many 732*db8dac20SDavid S. Miller * things). We could add another new stat but at least for now that 733*db8dac20SDavid S. Miller * seems like overkill. 734*db8dac20SDavid S. Miller */ 735*db8dac20SDavid S. Miller if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 736*db8dac20SDavid S. Miller UDP_INC_STATS_USER(UDP_MIB_SNDBUFERRORS, is_udplite); 737*db8dac20SDavid S. Miller } 738*db8dac20SDavid S. Miller return err; 739*db8dac20SDavid S. Miller 740*db8dac20SDavid S. Miller do_confirm: 741*db8dac20SDavid S. Miller dst_confirm(&rt->u.dst); 742*db8dac20SDavid S. Miller if (!(msg->msg_flags&MSG_PROBE) || len) 743*db8dac20SDavid S. Miller goto back_from_confirm; 744*db8dac20SDavid S. Miller err = 0; 745*db8dac20SDavid S. Miller goto out; 746*db8dac20SDavid S. Miller } 747*db8dac20SDavid S. Miller 748*db8dac20SDavid S. Miller int udp_sendpage(struct sock *sk, struct page *page, int offset, 749*db8dac20SDavid S. Miller size_t size, int flags) 750*db8dac20SDavid S. Miller { 751*db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 752*db8dac20SDavid S. Miller int ret; 753*db8dac20SDavid S. Miller 754*db8dac20SDavid S. Miller if (!up->pending) { 755*db8dac20SDavid S. Miller struct msghdr msg = { .msg_flags = flags|MSG_MORE }; 756*db8dac20SDavid S. Miller 757*db8dac20SDavid S. Miller /* Call udp_sendmsg to specify destination address which 758*db8dac20SDavid S. Miller * sendpage interface can't pass. 759*db8dac20SDavid S. Miller * This will succeed only when the socket is connected. 760*db8dac20SDavid S. Miller */ 761*db8dac20SDavid S. Miller ret = udp_sendmsg(NULL, sk, &msg, 0); 762*db8dac20SDavid S. Miller if (ret < 0) 763*db8dac20SDavid S. Miller return ret; 764*db8dac20SDavid S. Miller } 765*db8dac20SDavid S. Miller 766*db8dac20SDavid S. Miller lock_sock(sk); 767*db8dac20SDavid S. Miller 768*db8dac20SDavid S. Miller if (unlikely(!up->pending)) { 769*db8dac20SDavid S. Miller release_sock(sk); 770*db8dac20SDavid S. Miller 771*db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n"); 772*db8dac20SDavid S. Miller return -EINVAL; 773*db8dac20SDavid S. Miller } 774*db8dac20SDavid S. Miller 775*db8dac20SDavid S. Miller ret = ip_append_page(sk, page, offset, size, flags); 776*db8dac20SDavid S. Miller if (ret == -EOPNOTSUPP) { 777*db8dac20SDavid S. Miller release_sock(sk); 778*db8dac20SDavid S. Miller return sock_no_sendpage(sk->sk_socket, page, offset, 779*db8dac20SDavid S. Miller size, flags); 780*db8dac20SDavid S. Miller } 781*db8dac20SDavid S. Miller if (ret < 0) { 782*db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 783*db8dac20SDavid S. Miller goto out; 784*db8dac20SDavid S. Miller } 785*db8dac20SDavid S. Miller 786*db8dac20SDavid S. Miller up->len += size; 787*db8dac20SDavid S. Miller if (!(up->corkflag || (flags&MSG_MORE))) 788*db8dac20SDavid S. Miller ret = udp_push_pending_frames(sk); 789*db8dac20SDavid S. Miller if (!ret) 790*db8dac20SDavid S. Miller ret = size; 791*db8dac20SDavid S. Miller out: 792*db8dac20SDavid S. Miller release_sock(sk); 793*db8dac20SDavid S. Miller return ret; 794*db8dac20SDavid S. Miller } 795*db8dac20SDavid S. Miller 7961da177e4SLinus Torvalds /* 7971da177e4SLinus Torvalds * IOCTL requests applicable to the UDP protocol 7981da177e4SLinus Torvalds */ 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds int udp_ioctl(struct sock *sk, int cmd, unsigned long arg) 8011da177e4SLinus Torvalds { 8026516c655SStephen Hemminger switch (cmd) { 8031da177e4SLinus Torvalds case SIOCOUTQ: 8041da177e4SLinus Torvalds { 8051da177e4SLinus Torvalds int amount = atomic_read(&sk->sk_wmem_alloc); 8061da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 8071da177e4SLinus Torvalds } 8081da177e4SLinus Torvalds 8091da177e4SLinus Torvalds case SIOCINQ: 8101da177e4SLinus Torvalds { 8111da177e4SLinus Torvalds struct sk_buff *skb; 8121da177e4SLinus Torvalds unsigned long amount; 8131da177e4SLinus Torvalds 8141da177e4SLinus Torvalds amount = 0; 815208d8984SHerbert Xu spin_lock_bh(&sk->sk_receive_queue.lock); 8161da177e4SLinus Torvalds skb = skb_peek(&sk->sk_receive_queue); 8171da177e4SLinus Torvalds if (skb != NULL) { 8181da177e4SLinus Torvalds /* 8191da177e4SLinus Torvalds * We will only return the amount 8201da177e4SLinus Torvalds * of this packet since that is all 8211da177e4SLinus Torvalds * that will be read. 8221da177e4SLinus Torvalds */ 8231da177e4SLinus Torvalds amount = skb->len - sizeof(struct udphdr); 8241da177e4SLinus Torvalds } 825208d8984SHerbert Xu spin_unlock_bh(&sk->sk_receive_queue.lock); 8261da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 8271da177e4SLinus Torvalds } 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds default: 8301da177e4SLinus Torvalds return -ENOIOCTLCMD; 8311da177e4SLinus Torvalds } 8326516c655SStephen Hemminger 8336516c655SStephen Hemminger return 0; 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds 836*db8dac20SDavid S. Miller /* 837*db8dac20SDavid S. Miller * This should be easy, if there is something there we 838*db8dac20SDavid S. Miller * return it, otherwise we block. 839*db8dac20SDavid S. Miller */ 840*db8dac20SDavid S. Miller 841*db8dac20SDavid S. Miller int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 842*db8dac20SDavid S. Miller size_t len, int noblock, int flags, int *addr_len) 843*db8dac20SDavid S. Miller { 844*db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 845*db8dac20SDavid S. Miller struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name; 846*db8dac20SDavid S. Miller struct sk_buff *skb; 847*db8dac20SDavid S. Miller unsigned int ulen, copied; 848*db8dac20SDavid S. Miller int peeked; 849*db8dac20SDavid S. Miller int err; 850*db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 851*db8dac20SDavid S. Miller 852*db8dac20SDavid S. Miller /* 853*db8dac20SDavid S. Miller * Check any passed addresses 854*db8dac20SDavid S. Miller */ 855*db8dac20SDavid S. Miller if (addr_len) 856*db8dac20SDavid S. Miller *addr_len=sizeof(*sin); 857*db8dac20SDavid S. Miller 858*db8dac20SDavid S. Miller if (flags & MSG_ERRQUEUE) 859*db8dac20SDavid S. Miller return ip_recv_error(sk, msg, len); 860*db8dac20SDavid S. Miller 861*db8dac20SDavid S. Miller try_again: 862*db8dac20SDavid S. Miller skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), 863*db8dac20SDavid S. Miller &peeked, &err); 864*db8dac20SDavid S. Miller if (!skb) 865*db8dac20SDavid S. Miller goto out; 866*db8dac20SDavid S. Miller 867*db8dac20SDavid S. Miller ulen = skb->len - sizeof(struct udphdr); 868*db8dac20SDavid S. Miller copied = len; 869*db8dac20SDavid S. Miller if (copied > ulen) 870*db8dac20SDavid S. Miller copied = ulen; 871*db8dac20SDavid S. Miller else if (copied < ulen) 872*db8dac20SDavid S. Miller msg->msg_flags |= MSG_TRUNC; 873*db8dac20SDavid S. Miller 874*db8dac20SDavid S. Miller /* 875*db8dac20SDavid S. Miller * If checksum is needed at all, try to do it while copying the 876*db8dac20SDavid S. Miller * data. If the data is truncated, or if we only want a partial 877*db8dac20SDavid S. Miller * coverage checksum (UDP-Lite), do it before the copy. 878*db8dac20SDavid S. Miller */ 879*db8dac20SDavid S. Miller 880*db8dac20SDavid S. Miller if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) { 881*db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 882*db8dac20SDavid S. Miller goto csum_copy_err; 883*db8dac20SDavid S. Miller } 884*db8dac20SDavid S. Miller 885*db8dac20SDavid S. Miller if (skb_csum_unnecessary(skb)) 886*db8dac20SDavid S. Miller err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), 887*db8dac20SDavid S. Miller msg->msg_iov, copied ); 888*db8dac20SDavid S. Miller else { 889*db8dac20SDavid S. Miller err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov); 890*db8dac20SDavid S. Miller 891*db8dac20SDavid S. Miller if (err == -EINVAL) 892*db8dac20SDavid S. Miller goto csum_copy_err; 893*db8dac20SDavid S. Miller } 894*db8dac20SDavid S. Miller 895*db8dac20SDavid S. Miller if (err) 896*db8dac20SDavid S. Miller goto out_free; 897*db8dac20SDavid S. Miller 898*db8dac20SDavid S. Miller if (!peeked) 899*db8dac20SDavid S. Miller UDP_INC_STATS_USER(UDP_MIB_INDATAGRAMS, is_udplite); 900*db8dac20SDavid S. Miller 901*db8dac20SDavid S. Miller sock_recv_timestamp(msg, sk, skb); 902*db8dac20SDavid S. Miller 903*db8dac20SDavid S. Miller /* Copy the address. */ 904*db8dac20SDavid S. Miller if (sin) 905*db8dac20SDavid S. Miller { 906*db8dac20SDavid S. Miller sin->sin_family = AF_INET; 907*db8dac20SDavid S. Miller sin->sin_port = udp_hdr(skb)->source; 908*db8dac20SDavid S. Miller sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 909*db8dac20SDavid S. Miller memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 910*db8dac20SDavid S. Miller } 911*db8dac20SDavid S. Miller if (inet->cmsg_flags) 912*db8dac20SDavid S. Miller ip_cmsg_recv(msg, skb); 913*db8dac20SDavid S. Miller 914*db8dac20SDavid S. Miller err = copied; 915*db8dac20SDavid S. Miller if (flags & MSG_TRUNC) 916*db8dac20SDavid S. Miller err = ulen; 917*db8dac20SDavid S. Miller 918*db8dac20SDavid S. Miller out_free: 919*db8dac20SDavid S. Miller lock_sock(sk); 920*db8dac20SDavid S. Miller skb_free_datagram(sk, skb); 921*db8dac20SDavid S. Miller release_sock(sk); 922*db8dac20SDavid S. Miller out: 923*db8dac20SDavid S. Miller return err; 924*db8dac20SDavid S. Miller 925*db8dac20SDavid S. Miller csum_copy_err: 926*db8dac20SDavid S. Miller lock_sock(sk); 927*db8dac20SDavid S. Miller if (!skb_kill_datagram(sk, skb, flags)) 928*db8dac20SDavid S. Miller UDP_INC_STATS_USER(UDP_MIB_INERRORS, is_udplite); 929*db8dac20SDavid S. Miller release_sock(sk); 930*db8dac20SDavid S. Miller 931*db8dac20SDavid S. Miller if (noblock) 932*db8dac20SDavid S. Miller return -EAGAIN; 933*db8dac20SDavid S. Miller goto try_again; 934*db8dac20SDavid S. Miller } 935*db8dac20SDavid S. Miller 936*db8dac20SDavid S. Miller 9371da177e4SLinus Torvalds int udp_disconnect(struct sock *sk, int flags) 9381da177e4SLinus Torvalds { 9391da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 9401da177e4SLinus Torvalds /* 9411da177e4SLinus Torvalds * 1003.1g - break association. 9421da177e4SLinus Torvalds */ 9431da177e4SLinus Torvalds 9441da177e4SLinus Torvalds sk->sk_state = TCP_CLOSE; 9451da177e4SLinus Torvalds inet->daddr = 0; 9461da177e4SLinus Torvalds inet->dport = 0; 9471da177e4SLinus Torvalds sk->sk_bound_dev_if = 0; 9481da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) 9491da177e4SLinus Torvalds inet_reset_saddr(sk); 9501da177e4SLinus Torvalds 9511da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) { 9521da177e4SLinus Torvalds sk->sk_prot->unhash(sk); 9531da177e4SLinus Torvalds inet->sport = 0; 9541da177e4SLinus Torvalds } 9551da177e4SLinus Torvalds sk_dst_reset(sk); 9561da177e4SLinus Torvalds return 0; 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds 959*db8dac20SDavid S. Miller /* returns: 960*db8dac20SDavid S. Miller * -1: error 961*db8dac20SDavid S. Miller * 0: success 962*db8dac20SDavid S. Miller * >0: "udp encap" protocol resubmission 963*db8dac20SDavid S. Miller * 964*db8dac20SDavid S. Miller * Note that in the success and error cases, the skb is assumed to 965*db8dac20SDavid S. Miller * have either been requeued or freed. 966*db8dac20SDavid S. Miller */ 967*db8dac20SDavid S. Miller int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb) 968*db8dac20SDavid S. Miller { 969*db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 970*db8dac20SDavid S. Miller int rc; 971*db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 972*db8dac20SDavid S. Miller 973*db8dac20SDavid S. Miller /* 974*db8dac20SDavid S. Miller * Charge it to the socket, dropping if the queue is full. 975*db8dac20SDavid S. Miller */ 976*db8dac20SDavid S. Miller if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) 977*db8dac20SDavid S. Miller goto drop; 978*db8dac20SDavid S. Miller nf_reset(skb); 979*db8dac20SDavid S. Miller 980*db8dac20SDavid S. Miller if (up->encap_type) { 981*db8dac20SDavid S. Miller /* 982*db8dac20SDavid S. Miller * This is an encapsulation socket so pass the skb to 983*db8dac20SDavid S. Miller * the socket's udp_encap_rcv() hook. Otherwise, just 984*db8dac20SDavid S. Miller * fall through and pass this up the UDP socket. 985*db8dac20SDavid S. Miller * up->encap_rcv() returns the following value: 986*db8dac20SDavid S. Miller * =0 if skb was successfully passed to the encap 987*db8dac20SDavid S. Miller * handler or was discarded by it. 988*db8dac20SDavid S. Miller * >0 if skb should be passed on to UDP. 989*db8dac20SDavid S. Miller * <0 if skb should be resubmitted as proto -N 990*db8dac20SDavid S. Miller */ 991*db8dac20SDavid S. Miller 992*db8dac20SDavid S. Miller /* if we're overly short, let UDP handle it */ 993*db8dac20SDavid S. Miller if (skb->len > sizeof(struct udphdr) && 994*db8dac20SDavid S. Miller up->encap_rcv != NULL) { 995*db8dac20SDavid S. Miller int ret; 996*db8dac20SDavid S. Miller 997*db8dac20SDavid S. Miller ret = (*up->encap_rcv)(sk, skb); 998*db8dac20SDavid S. Miller if (ret <= 0) { 999*db8dac20SDavid S. Miller UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS, 1000*db8dac20SDavid S. Miller is_udplite); 1001*db8dac20SDavid S. Miller return -ret; 1002*db8dac20SDavid S. Miller } 1003*db8dac20SDavid S. Miller } 1004*db8dac20SDavid S. Miller 1005*db8dac20SDavid S. Miller /* FALLTHROUGH -- it's a UDP Packet */ 1006*db8dac20SDavid S. Miller } 1007*db8dac20SDavid S. Miller 1008*db8dac20SDavid S. Miller /* 1009*db8dac20SDavid S. Miller * UDP-Lite specific tests, ignored on UDP sockets 1010*db8dac20SDavid S. Miller */ 1011*db8dac20SDavid S. Miller if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 1012*db8dac20SDavid S. Miller 1013*db8dac20SDavid S. Miller /* 1014*db8dac20SDavid S. Miller * MIB statistics other than incrementing the error count are 1015*db8dac20SDavid S. Miller * disabled for the following two types of errors: these depend 1016*db8dac20SDavid S. Miller * on the application settings, not on the functioning of the 1017*db8dac20SDavid S. Miller * protocol stack as such. 1018*db8dac20SDavid S. Miller * 1019*db8dac20SDavid S. Miller * RFC 3828 here recommends (sec 3.3): "There should also be a 1020*db8dac20SDavid S. Miller * way ... to ... at least let the receiving application block 1021*db8dac20SDavid S. Miller * delivery of packets with coverage values less than a value 1022*db8dac20SDavid S. Miller * provided by the application." 1023*db8dac20SDavid S. Miller */ 1024*db8dac20SDavid S. Miller if (up->pcrlen == 0) { /* full coverage was set */ 1025*db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_WARNING "UDPLITE: partial coverage " 1026*db8dac20SDavid S. Miller "%d while full coverage %d requested\n", 1027*db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, skb->len); 1028*db8dac20SDavid S. Miller goto drop; 1029*db8dac20SDavid S. Miller } 1030*db8dac20SDavid S. Miller /* The next case involves violating the min. coverage requested 1031*db8dac20SDavid S. Miller * by the receiver. This is subtle: if receiver wants x and x is 1032*db8dac20SDavid S. Miller * greater than the buffersize/MTU then receiver will complain 1033*db8dac20SDavid S. Miller * that it wants x while sender emits packets of smaller size y. 1034*db8dac20SDavid S. Miller * Therefore the above ...()->partial_cov statement is essential. 1035*db8dac20SDavid S. Miller */ 1036*db8dac20SDavid S. Miller if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 1037*db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_WARNING 1038*db8dac20SDavid S. Miller "UDPLITE: coverage %d too small, need min %d\n", 1039*db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, up->pcrlen); 1040*db8dac20SDavid S. Miller goto drop; 1041*db8dac20SDavid S. Miller } 1042*db8dac20SDavid S. Miller } 1043*db8dac20SDavid S. Miller 1044*db8dac20SDavid S. Miller if (sk->sk_filter) { 1045*db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 1046*db8dac20SDavid S. Miller goto drop; 1047*db8dac20SDavid S. Miller } 1048*db8dac20SDavid S. Miller 1049*db8dac20SDavid S. Miller if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) { 1050*db8dac20SDavid S. Miller /* Note that an ENOMEM error is charged twice */ 1051*db8dac20SDavid S. Miller if (rc == -ENOMEM) 1052*db8dac20SDavid S. Miller UDP_INC_STATS_BH(UDP_MIB_RCVBUFERRORS, is_udplite); 1053*db8dac20SDavid S. Miller goto drop; 1054*db8dac20SDavid S. Miller } 1055*db8dac20SDavid S. Miller 1056*db8dac20SDavid S. Miller return 0; 1057*db8dac20SDavid S. Miller 1058*db8dac20SDavid S. Miller drop: 1059*db8dac20SDavid S. Miller UDP_INC_STATS_BH(UDP_MIB_INERRORS, is_udplite); 1060*db8dac20SDavid S. Miller kfree_skb(skb); 1061*db8dac20SDavid S. Miller return -1; 1062*db8dac20SDavid S. Miller } 1063*db8dac20SDavid S. Miller 1064*db8dac20SDavid S. Miller /* 1065*db8dac20SDavid S. Miller * Multicasts and broadcasts go to each listener. 1066*db8dac20SDavid S. Miller * 1067*db8dac20SDavid S. Miller * Note: called only from the BH handler context, 1068*db8dac20SDavid S. Miller * so we don't need to lock the hashes. 1069*db8dac20SDavid S. Miller */ 1070*db8dac20SDavid S. Miller static int __udp4_lib_mcast_deliver(struct sk_buff *skb, 1071*db8dac20SDavid S. Miller struct udphdr *uh, 1072*db8dac20SDavid S. Miller __be32 saddr, __be32 daddr, 1073*db8dac20SDavid S. Miller struct hlist_head udptable[]) 1074*db8dac20SDavid S. Miller { 1075*db8dac20SDavid S. Miller struct sock *sk; 1076*db8dac20SDavid S. Miller int dif; 1077*db8dac20SDavid S. Miller 1078*db8dac20SDavid S. Miller read_lock(&udp_hash_lock); 1079*db8dac20SDavid S. Miller sk = sk_head(&udptable[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]); 1080*db8dac20SDavid S. Miller dif = skb->dev->ifindex; 1081*db8dac20SDavid S. Miller sk = udp_v4_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif); 1082*db8dac20SDavid S. Miller if (sk) { 1083*db8dac20SDavid S. Miller struct sock *sknext = NULL; 1084*db8dac20SDavid S. Miller 1085*db8dac20SDavid S. Miller do { 1086*db8dac20SDavid S. Miller struct sk_buff *skb1 = skb; 1087*db8dac20SDavid S. Miller 1088*db8dac20SDavid S. Miller sknext = udp_v4_mcast_next(sk_next(sk), uh->dest, daddr, 1089*db8dac20SDavid S. Miller uh->source, saddr, dif); 1090*db8dac20SDavid S. Miller if (sknext) 1091*db8dac20SDavid S. Miller skb1 = skb_clone(skb, GFP_ATOMIC); 1092*db8dac20SDavid S. Miller 1093*db8dac20SDavid S. Miller if (skb1) { 1094*db8dac20SDavid S. Miller int ret = 0; 1095*db8dac20SDavid S. Miller 1096*db8dac20SDavid S. Miller bh_lock_sock_nested(sk); 1097*db8dac20SDavid S. Miller if (!sock_owned_by_user(sk)) 1098*db8dac20SDavid S. Miller ret = udp_queue_rcv_skb(sk, skb1); 1099*db8dac20SDavid S. Miller else 1100*db8dac20SDavid S. Miller sk_add_backlog(sk, skb1); 1101*db8dac20SDavid S. Miller bh_unlock_sock(sk); 1102*db8dac20SDavid S. Miller 1103*db8dac20SDavid S. Miller if (ret > 0) 1104*db8dac20SDavid S. Miller /* we should probably re-process instead 1105*db8dac20SDavid S. Miller * of dropping packets here. */ 1106*db8dac20SDavid S. Miller kfree_skb(skb1); 1107*db8dac20SDavid S. Miller } 1108*db8dac20SDavid S. Miller sk = sknext; 1109*db8dac20SDavid S. Miller } while (sknext); 1110*db8dac20SDavid S. Miller } else 1111*db8dac20SDavid S. Miller kfree_skb(skb); 1112*db8dac20SDavid S. Miller read_unlock(&udp_hash_lock); 1113*db8dac20SDavid S. Miller return 0; 1114*db8dac20SDavid S. Miller } 1115*db8dac20SDavid S. Miller 1116*db8dac20SDavid S. Miller /* Initialize UDP checksum. If exited with zero value (success), 1117*db8dac20SDavid S. Miller * CHECKSUM_UNNECESSARY means, that no more checks are required. 1118*db8dac20SDavid S. Miller * Otherwise, csum completion requires chacksumming packet body, 1119*db8dac20SDavid S. Miller * including udp header and folding it to skb->csum. 1120*db8dac20SDavid S. Miller */ 1121*db8dac20SDavid S. Miller static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh, 1122*db8dac20SDavid S. Miller int proto) 1123*db8dac20SDavid S. Miller { 1124*db8dac20SDavid S. Miller const struct iphdr *iph; 1125*db8dac20SDavid S. Miller int err; 1126*db8dac20SDavid S. Miller 1127*db8dac20SDavid S. Miller UDP_SKB_CB(skb)->partial_cov = 0; 1128*db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov = skb->len; 1129*db8dac20SDavid S. Miller 1130*db8dac20SDavid S. Miller if (proto == IPPROTO_UDPLITE) { 1131*db8dac20SDavid S. Miller err = udplite_checksum_init(skb, uh); 1132*db8dac20SDavid S. Miller if (err) 1133*db8dac20SDavid S. Miller return err; 1134*db8dac20SDavid S. Miller } 1135*db8dac20SDavid S. Miller 1136*db8dac20SDavid S. Miller iph = ip_hdr(skb); 1137*db8dac20SDavid S. Miller if (uh->check == 0) { 1138*db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_UNNECESSARY; 1139*db8dac20SDavid S. Miller } else if (skb->ip_summed == CHECKSUM_COMPLETE) { 1140*db8dac20SDavid S. Miller if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len, 1141*db8dac20SDavid S. Miller proto, skb->csum)) 1142*db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_UNNECESSARY; 1143*db8dac20SDavid S. Miller } 1144*db8dac20SDavid S. Miller if (!skb_csum_unnecessary(skb)) 1145*db8dac20SDavid S. Miller skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, 1146*db8dac20SDavid S. Miller skb->len, proto, 0); 1147*db8dac20SDavid S. Miller /* Probably, we should checksum udp header (it should be in cache 1148*db8dac20SDavid S. Miller * in any case) and data in tiny packets (< rx copybreak). 1149*db8dac20SDavid S. Miller */ 1150*db8dac20SDavid S. Miller 1151*db8dac20SDavid S. Miller return 0; 1152*db8dac20SDavid S. Miller } 1153*db8dac20SDavid S. Miller 1154*db8dac20SDavid S. Miller /* 1155*db8dac20SDavid S. Miller * All we need to do is get the socket, and then do a checksum. 1156*db8dac20SDavid S. Miller */ 1157*db8dac20SDavid S. Miller 1158*db8dac20SDavid S. Miller int __udp4_lib_rcv(struct sk_buff *skb, struct hlist_head udptable[], 1159*db8dac20SDavid S. Miller int proto) 1160*db8dac20SDavid S. Miller { 1161*db8dac20SDavid S. Miller struct sock *sk; 1162*db8dac20SDavid S. Miller struct udphdr *uh = udp_hdr(skb); 1163*db8dac20SDavid S. Miller unsigned short ulen; 1164*db8dac20SDavid S. Miller struct rtable *rt = (struct rtable*)skb->dst; 1165*db8dac20SDavid S. Miller __be32 saddr = ip_hdr(skb)->saddr; 1166*db8dac20SDavid S. Miller __be32 daddr = ip_hdr(skb)->daddr; 1167*db8dac20SDavid S. Miller 1168*db8dac20SDavid S. Miller /* 1169*db8dac20SDavid S. Miller * Validate the packet. 1170*db8dac20SDavid S. Miller */ 1171*db8dac20SDavid S. Miller if (!pskb_may_pull(skb, sizeof(struct udphdr))) 1172*db8dac20SDavid S. Miller goto drop; /* No space for header. */ 1173*db8dac20SDavid S. Miller 1174*db8dac20SDavid S. Miller ulen = ntohs(uh->len); 1175*db8dac20SDavid S. Miller if (ulen > skb->len) 1176*db8dac20SDavid S. Miller goto short_packet; 1177*db8dac20SDavid S. Miller 1178*db8dac20SDavid S. Miller if (proto == IPPROTO_UDP) { 1179*db8dac20SDavid S. Miller /* UDP validates ulen. */ 1180*db8dac20SDavid S. Miller if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen)) 1181*db8dac20SDavid S. Miller goto short_packet; 1182*db8dac20SDavid S. Miller uh = udp_hdr(skb); 1183*db8dac20SDavid S. Miller } 1184*db8dac20SDavid S. Miller 1185*db8dac20SDavid S. Miller if (udp4_csum_init(skb, uh, proto)) 1186*db8dac20SDavid S. Miller goto csum_error; 1187*db8dac20SDavid S. Miller 1188*db8dac20SDavid S. Miller if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST)) 1189*db8dac20SDavid S. Miller return __udp4_lib_mcast_deliver(skb, uh, saddr, daddr, udptable); 1190*db8dac20SDavid S. Miller 1191*db8dac20SDavid S. Miller sk = __udp4_lib_lookup(skb->dev->nd_net, saddr, uh->source, daddr, 1192*db8dac20SDavid S. Miller uh->dest, inet_iif(skb), udptable); 1193*db8dac20SDavid S. Miller 1194*db8dac20SDavid S. Miller if (sk != NULL) { 1195*db8dac20SDavid S. Miller int ret = 0; 1196*db8dac20SDavid S. Miller bh_lock_sock_nested(sk); 1197*db8dac20SDavid S. Miller if (!sock_owned_by_user(sk)) 1198*db8dac20SDavid S. Miller ret = udp_queue_rcv_skb(sk, skb); 1199*db8dac20SDavid S. Miller else 1200*db8dac20SDavid S. Miller sk_add_backlog(sk, skb); 1201*db8dac20SDavid S. Miller bh_unlock_sock(sk); 1202*db8dac20SDavid S. Miller sock_put(sk); 1203*db8dac20SDavid S. Miller 1204*db8dac20SDavid S. Miller /* a return value > 0 means to resubmit the input, but 1205*db8dac20SDavid S. Miller * it wants the return to be -protocol, or 0 1206*db8dac20SDavid S. Miller */ 1207*db8dac20SDavid S. Miller if (ret > 0) 1208*db8dac20SDavid S. Miller return -ret; 1209*db8dac20SDavid S. Miller return 0; 1210*db8dac20SDavid S. Miller } 1211*db8dac20SDavid S. Miller 1212*db8dac20SDavid S. Miller if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) 1213*db8dac20SDavid S. Miller goto drop; 1214*db8dac20SDavid S. Miller nf_reset(skb); 1215*db8dac20SDavid S. Miller 1216*db8dac20SDavid S. Miller /* No socket. Drop packet silently, if checksum is wrong */ 1217*db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 1218*db8dac20SDavid S. Miller goto csum_error; 1219*db8dac20SDavid S. Miller 1220*db8dac20SDavid S. Miller UDP_INC_STATS_BH(UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); 1221*db8dac20SDavid S. Miller icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); 1222*db8dac20SDavid S. Miller 1223*db8dac20SDavid S. Miller /* 1224*db8dac20SDavid S. Miller * Hmm. We got an UDP packet to a port to which we 1225*db8dac20SDavid S. Miller * don't wanna listen. Ignore it. 1226*db8dac20SDavid S. Miller */ 1227*db8dac20SDavid S. Miller kfree_skb(skb); 1228*db8dac20SDavid S. Miller return 0; 1229*db8dac20SDavid S. Miller 1230*db8dac20SDavid S. Miller short_packet: 1231*db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From %u.%u.%u.%u:%u %d/%d to %u.%u.%u.%u:%u\n", 1232*db8dac20SDavid S. Miller proto == IPPROTO_UDPLITE ? "-Lite" : "", 1233*db8dac20SDavid S. Miller NIPQUAD(saddr), 1234*db8dac20SDavid S. Miller ntohs(uh->source), 1235*db8dac20SDavid S. Miller ulen, 1236*db8dac20SDavid S. Miller skb->len, 1237*db8dac20SDavid S. Miller NIPQUAD(daddr), 1238*db8dac20SDavid S. Miller ntohs(uh->dest)); 1239*db8dac20SDavid S. Miller goto drop; 1240*db8dac20SDavid S. Miller 1241*db8dac20SDavid S. Miller csum_error: 1242*db8dac20SDavid S. Miller /* 1243*db8dac20SDavid S. Miller * RFC1122: OK. Discards the bad packet silently (as far as 1244*db8dac20SDavid S. Miller * the network is concerned, anyway) as per 4.1.3.4 (MUST). 1245*db8dac20SDavid S. Miller */ 1246*db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From %d.%d.%d.%d:%d to %d.%d.%d.%d:%d ulen %d\n", 1247*db8dac20SDavid S. Miller proto == IPPROTO_UDPLITE ? "-Lite" : "", 1248*db8dac20SDavid S. Miller NIPQUAD(saddr), 1249*db8dac20SDavid S. Miller ntohs(uh->source), 1250*db8dac20SDavid S. Miller NIPQUAD(daddr), 1251*db8dac20SDavid S. Miller ntohs(uh->dest), 1252*db8dac20SDavid S. Miller ulen); 1253*db8dac20SDavid S. Miller drop: 1254*db8dac20SDavid S. Miller UDP_INC_STATS_BH(UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 1255*db8dac20SDavid S. Miller kfree_skb(skb); 1256*db8dac20SDavid S. Miller return 0; 1257*db8dac20SDavid S. Miller } 1258*db8dac20SDavid S. Miller 1259*db8dac20SDavid S. Miller int udp_rcv(struct sk_buff *skb) 1260*db8dac20SDavid S. Miller { 1261*db8dac20SDavid S. Miller return __udp4_lib_rcv(skb, udp_hash, IPPROTO_UDP); 1262*db8dac20SDavid S. Miller } 1263*db8dac20SDavid S. Miller 1264*db8dac20SDavid S. Miller int udp_destroy_sock(struct sock *sk) 1265*db8dac20SDavid S. Miller { 1266*db8dac20SDavid S. Miller lock_sock(sk); 1267*db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 1268*db8dac20SDavid S. Miller release_sock(sk); 1269*db8dac20SDavid S. Miller return 0; 1270*db8dac20SDavid S. Miller } 1271*db8dac20SDavid S. Miller 12721da177e4SLinus Torvalds /* 12731da177e4SLinus Torvalds * Socket option code for UDP 12741da177e4SLinus Torvalds */ 12754c0a6cb0SGerrit Renker int udp_lib_setsockopt(struct sock *sk, int level, int optname, 12764c0a6cb0SGerrit Renker char __user *optval, int optlen, 12774c0a6cb0SGerrit Renker int (*push_pending_frames)(struct sock *)) 12781da177e4SLinus Torvalds { 12791da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 12801da177e4SLinus Torvalds int val; 12811da177e4SLinus Torvalds int err = 0; 1282b2bf1e26SWang Chen int is_udplite = IS_UDPLITE(sk); 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds if (optlen<sizeof(int)) 12851da177e4SLinus Torvalds return -EINVAL; 12861da177e4SLinus Torvalds 12871da177e4SLinus Torvalds if (get_user(val, (int __user *)optval)) 12881da177e4SLinus Torvalds return -EFAULT; 12891da177e4SLinus Torvalds 12901da177e4SLinus Torvalds switch (optname) { 12911da177e4SLinus Torvalds case UDP_CORK: 12921da177e4SLinus Torvalds if (val != 0) { 12931da177e4SLinus Torvalds up->corkflag = 1; 12941da177e4SLinus Torvalds } else { 12951da177e4SLinus Torvalds up->corkflag = 0; 12961da177e4SLinus Torvalds lock_sock(sk); 12974c0a6cb0SGerrit Renker (*push_pending_frames)(sk); 12981da177e4SLinus Torvalds release_sock(sk); 12991da177e4SLinus Torvalds } 13001da177e4SLinus Torvalds break; 13011da177e4SLinus Torvalds 13021da177e4SLinus Torvalds case UDP_ENCAP: 13031da177e4SLinus Torvalds switch (val) { 13041da177e4SLinus Torvalds case 0: 13051da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP: 13061da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP_NON_IKE: 1307067b207bSJames Chapman up->encap_rcv = xfrm4_udp_encap_rcv; 1308067b207bSJames Chapman /* FALLTHROUGH */ 1309342f0234SJames Chapman case UDP_ENCAP_L2TPINUDP: 13101da177e4SLinus Torvalds up->encap_type = val; 13111da177e4SLinus Torvalds break; 13121da177e4SLinus Torvalds default: 13131da177e4SLinus Torvalds err = -ENOPROTOOPT; 13141da177e4SLinus Torvalds break; 13151da177e4SLinus Torvalds } 13161da177e4SLinus Torvalds break; 13171da177e4SLinus Torvalds 1318ba4e58ecSGerrit Renker /* 1319ba4e58ecSGerrit Renker * UDP-Lite's partial checksum coverage (RFC 3828). 1320ba4e58ecSGerrit Renker */ 1321ba4e58ecSGerrit Renker /* The sender sets actual checksum coverage length via this option. 1322ba4e58ecSGerrit Renker * The case coverage > packet length is handled by send module. */ 1323ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 1324b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 1325ba4e58ecSGerrit Renker return -ENOPROTOOPT; 1326ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Illegal coverage: use default (8) */ 1327ba4e58ecSGerrit Renker val = 8; 1328ba4e58ecSGerrit Renker up->pcslen = val; 1329ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_SEND_CC; 1330ba4e58ecSGerrit Renker break; 1331ba4e58ecSGerrit Renker 1332ba4e58ecSGerrit Renker /* The receiver specifies a minimum checksum coverage value. To make 1333ba4e58ecSGerrit Renker * sense, this should be set to at least 8 (as done below). If zero is 1334ba4e58ecSGerrit Renker * used, this again means full checksum coverage. */ 1335ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 1336b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 1337ba4e58ecSGerrit Renker return -ENOPROTOOPT; 1338ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Avoid silly minimal values. */ 1339ba4e58ecSGerrit Renker val = 8; 1340ba4e58ecSGerrit Renker up->pcrlen = val; 1341ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_RECV_CC; 1342ba4e58ecSGerrit Renker break; 1343ba4e58ecSGerrit Renker 13441da177e4SLinus Torvalds default: 13451da177e4SLinus Torvalds err = -ENOPROTOOPT; 13461da177e4SLinus Torvalds break; 13476516c655SStephen Hemminger } 13481da177e4SLinus Torvalds 13491da177e4SLinus Torvalds return err; 13501da177e4SLinus Torvalds } 13511da177e4SLinus Torvalds 1352*db8dac20SDavid S. Miller int udp_setsockopt(struct sock *sk, int level, int optname, 1353*db8dac20SDavid S. Miller char __user *optval, int optlen) 1354*db8dac20SDavid S. Miller { 1355*db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1356*db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1357*db8dac20SDavid S. Miller udp_push_pending_frames); 1358*db8dac20SDavid S. Miller return ip_setsockopt(sk, level, optname, optval, optlen); 1359*db8dac20SDavid S. Miller } 1360*db8dac20SDavid S. Miller 1361*db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1362*db8dac20SDavid S. Miller int compat_udp_setsockopt(struct sock *sk, int level, int optname, 1363*db8dac20SDavid S. Miller char __user *optval, int optlen) 1364*db8dac20SDavid S. Miller { 1365*db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1366*db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1367*db8dac20SDavid S. Miller udp_push_pending_frames); 1368*db8dac20SDavid S. Miller return compat_ip_setsockopt(sk, level, optname, optval, optlen); 1369*db8dac20SDavid S. Miller } 1370*db8dac20SDavid S. Miller #endif 1371*db8dac20SDavid S. Miller 13724c0a6cb0SGerrit Renker int udp_lib_getsockopt(struct sock *sk, int level, int optname, 13731da177e4SLinus Torvalds char __user *optval, int __user *optlen) 13741da177e4SLinus Torvalds { 13751da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 13761da177e4SLinus Torvalds int val, len; 13771da177e4SLinus Torvalds 13781da177e4SLinus Torvalds if (get_user(len,optlen)) 13791da177e4SLinus Torvalds return -EFAULT; 13801da177e4SLinus Torvalds 13811da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(int)); 13821da177e4SLinus Torvalds 13831da177e4SLinus Torvalds if (len < 0) 13841da177e4SLinus Torvalds return -EINVAL; 13851da177e4SLinus Torvalds 13861da177e4SLinus Torvalds switch (optname) { 13871da177e4SLinus Torvalds case UDP_CORK: 13881da177e4SLinus Torvalds val = up->corkflag; 13891da177e4SLinus Torvalds break; 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds case UDP_ENCAP: 13921da177e4SLinus Torvalds val = up->encap_type; 13931da177e4SLinus Torvalds break; 13941da177e4SLinus Torvalds 1395ba4e58ecSGerrit Renker /* The following two cannot be changed on UDP sockets, the return is 1396ba4e58ecSGerrit Renker * always 0 (which corresponds to the full checksum coverage of UDP). */ 1397ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 1398ba4e58ecSGerrit Renker val = up->pcslen; 1399ba4e58ecSGerrit Renker break; 1400ba4e58ecSGerrit Renker 1401ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 1402ba4e58ecSGerrit Renker val = up->pcrlen; 1403ba4e58ecSGerrit Renker break; 1404ba4e58ecSGerrit Renker 14051da177e4SLinus Torvalds default: 14061da177e4SLinus Torvalds return -ENOPROTOOPT; 14076516c655SStephen Hemminger } 14081da177e4SLinus Torvalds 14091da177e4SLinus Torvalds if (put_user(len, optlen)) 14101da177e4SLinus Torvalds return -EFAULT; 14111da177e4SLinus Torvalds if (copy_to_user(optval, &val,len)) 14121da177e4SLinus Torvalds return -EFAULT; 14131da177e4SLinus Torvalds return 0; 14141da177e4SLinus Torvalds } 14151da177e4SLinus Torvalds 1416*db8dac20SDavid S. Miller int udp_getsockopt(struct sock *sk, int level, int optname, 1417*db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 1418*db8dac20SDavid S. Miller { 1419*db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1420*db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1421*db8dac20SDavid S. Miller return ip_getsockopt(sk, level, optname, optval, optlen); 1422*db8dac20SDavid S. Miller } 1423*db8dac20SDavid S. Miller 1424*db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1425*db8dac20SDavid S. Miller int compat_udp_getsockopt(struct sock *sk, int level, int optname, 1426*db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 1427*db8dac20SDavid S. Miller { 1428*db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1429*db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1430*db8dac20SDavid S. Miller return compat_ip_getsockopt(sk, level, optname, optval, optlen); 1431*db8dac20SDavid S. Miller } 1432*db8dac20SDavid S. Miller #endif 14331da177e4SLinus Torvalds /** 14341da177e4SLinus Torvalds * udp_poll - wait for a UDP event. 14351da177e4SLinus Torvalds * @file - file struct 14361da177e4SLinus Torvalds * @sock - socket 14371da177e4SLinus Torvalds * @wait - poll table 14381da177e4SLinus Torvalds * 14391da177e4SLinus Torvalds * This is same as datagram poll, except for the special case of 14401da177e4SLinus Torvalds * blocking sockets. If application is using a blocking fd 14411da177e4SLinus Torvalds * and a packet with checksum error is in the queue; 14421da177e4SLinus Torvalds * then it could get return from select indicating data available 14431da177e4SLinus Torvalds * but then block when reading it. Add special case code 14441da177e4SLinus Torvalds * to work around these arguably broken applications. 14451da177e4SLinus Torvalds */ 14461da177e4SLinus Torvalds unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait) 14471da177e4SLinus Torvalds { 14481da177e4SLinus Torvalds unsigned int mask = datagram_poll(file, sock, wait); 14491da177e4SLinus Torvalds struct sock *sk = sock->sk; 1450ba4e58ecSGerrit Renker int is_lite = IS_UDPLITE(sk); 14511da177e4SLinus Torvalds 14521da177e4SLinus Torvalds /* Check for false positives due to checksum errors */ 14531da177e4SLinus Torvalds if ( (mask & POLLRDNORM) && 14541da177e4SLinus Torvalds !(file->f_flags & O_NONBLOCK) && 14551da177e4SLinus Torvalds !(sk->sk_shutdown & RCV_SHUTDOWN)){ 14561da177e4SLinus Torvalds struct sk_buff_head *rcvq = &sk->sk_receive_queue; 14571da177e4SLinus Torvalds struct sk_buff *skb; 14581da177e4SLinus Torvalds 1459208d8984SHerbert Xu spin_lock_bh(&rcvq->lock); 1460759e5d00SHerbert Xu while ((skb = skb_peek(rcvq)) != NULL && 1461759e5d00SHerbert Xu udp_lib_checksum_complete(skb)) { 1462ba4e58ecSGerrit Renker UDP_INC_STATS_BH(UDP_MIB_INERRORS, is_lite); 14631da177e4SLinus Torvalds __skb_unlink(skb, rcvq); 14641da177e4SLinus Torvalds kfree_skb(skb); 14651da177e4SLinus Torvalds } 1466208d8984SHerbert Xu spin_unlock_bh(&rcvq->lock); 14671da177e4SLinus Torvalds 14681da177e4SLinus Torvalds /* nothing to see, move along */ 14691da177e4SLinus Torvalds if (skb == NULL) 14701da177e4SLinus Torvalds mask &= ~(POLLIN | POLLRDNORM); 14711da177e4SLinus Torvalds } 14721da177e4SLinus Torvalds 14731da177e4SLinus Torvalds return mask; 14741da177e4SLinus Torvalds 14751da177e4SLinus Torvalds } 14761da177e4SLinus Torvalds 1477*db8dac20SDavid S. Miller DEFINE_PROTO_INUSE(udp) 1478*db8dac20SDavid S. Miller 1479*db8dac20SDavid S. Miller struct proto udp_prot = { 1480*db8dac20SDavid S. Miller .name = "UDP", 1481*db8dac20SDavid S. Miller .owner = THIS_MODULE, 1482*db8dac20SDavid S. Miller .close = udp_lib_close, 1483*db8dac20SDavid S. Miller .connect = ip4_datagram_connect, 1484*db8dac20SDavid S. Miller .disconnect = udp_disconnect, 1485*db8dac20SDavid S. Miller .ioctl = udp_ioctl, 1486*db8dac20SDavid S. Miller .destroy = udp_destroy_sock, 1487*db8dac20SDavid S. Miller .setsockopt = udp_setsockopt, 1488*db8dac20SDavid S. Miller .getsockopt = udp_getsockopt, 1489*db8dac20SDavid S. Miller .sendmsg = udp_sendmsg, 1490*db8dac20SDavid S. Miller .recvmsg = udp_recvmsg, 1491*db8dac20SDavid S. Miller .sendpage = udp_sendpage, 1492*db8dac20SDavid S. Miller .backlog_rcv = udp_queue_rcv_skb, 1493*db8dac20SDavid S. Miller .hash = udp_lib_hash, 1494*db8dac20SDavid S. Miller .unhash = udp_lib_unhash, 1495*db8dac20SDavid S. Miller .get_port = udp_v4_get_port, 1496*db8dac20SDavid S. Miller .memory_allocated = &udp_memory_allocated, 1497*db8dac20SDavid S. Miller .sysctl_mem = sysctl_udp_mem, 1498*db8dac20SDavid S. Miller .sysctl_wmem = &sysctl_udp_wmem_min, 1499*db8dac20SDavid S. Miller .sysctl_rmem = &sysctl_udp_rmem_min, 1500*db8dac20SDavid S. Miller .obj_size = sizeof(struct udp_sock), 1501*db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1502*db8dac20SDavid S. Miller .compat_setsockopt = compat_udp_setsockopt, 1503*db8dac20SDavid S. Miller .compat_getsockopt = compat_udp_getsockopt, 1504*db8dac20SDavid S. Miller #endif 1505*db8dac20SDavid S. Miller REF_PROTO_INUSE(udp) 1506*db8dac20SDavid S. Miller }; 15071da177e4SLinus Torvalds 15081da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 15091da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 15101da177e4SLinus Torvalds 15111da177e4SLinus Torvalds static struct sock *udp_get_first(struct seq_file *seq) 15121da177e4SLinus Torvalds { 15131da177e4SLinus Torvalds struct sock *sk; 15141da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 15151da177e4SLinus Torvalds 15161da177e4SLinus Torvalds for (state->bucket = 0; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) { 15171da177e4SLinus Torvalds struct hlist_node *node; 1518ba4e58ecSGerrit Renker sk_for_each(sk, node, state->hashtable + state->bucket) { 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; 15311da177e4SLinus Torvalds 15321da177e4SLinus Torvalds do { 15331da177e4SLinus Torvalds sk = sk_next(sk); 15341da177e4SLinus Torvalds try_again: 15351da177e4SLinus Torvalds ; 15361da177e4SLinus Torvalds } while (sk && sk->sk_family != state->family); 15371da177e4SLinus Torvalds 15381da177e4SLinus Torvalds if (!sk && ++state->bucket < UDP_HTABLE_SIZE) { 1539ba4e58ecSGerrit Renker sk = sk_head(state->hashtable + state->bucket); 15401da177e4SLinus Torvalds goto try_again; 15411da177e4SLinus Torvalds } 15421da177e4SLinus Torvalds return sk; 15431da177e4SLinus Torvalds } 15441da177e4SLinus Torvalds 15451da177e4SLinus Torvalds static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos) 15461da177e4SLinus Torvalds { 15471da177e4SLinus Torvalds struct sock *sk = udp_get_first(seq); 15481da177e4SLinus Torvalds 15491da177e4SLinus Torvalds if (sk) 15501da177e4SLinus Torvalds while (pos && (sk = udp_get_next(seq, sk)) != NULL) 15511da177e4SLinus Torvalds --pos; 15521da177e4SLinus Torvalds return pos ? NULL : sk; 15531da177e4SLinus Torvalds } 15541da177e4SLinus Torvalds 15551da177e4SLinus Torvalds static void *udp_seq_start(struct seq_file *seq, loff_t *pos) 15569a429c49SEric Dumazet __acquires(udp_hash_lock) 15571da177e4SLinus Torvalds { 15581da177e4SLinus Torvalds read_lock(&udp_hash_lock); 15591da177e4SLinus Torvalds return *pos ? udp_get_idx(seq, *pos-1) : (void *)1; 15601da177e4SLinus Torvalds } 15611da177e4SLinus Torvalds 15621da177e4SLinus Torvalds static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos) 15631da177e4SLinus Torvalds { 15641da177e4SLinus Torvalds struct sock *sk; 15651da177e4SLinus Torvalds 15661da177e4SLinus Torvalds if (v == (void *)1) 15671da177e4SLinus Torvalds sk = udp_get_idx(seq, 0); 15681da177e4SLinus Torvalds else 15691da177e4SLinus Torvalds sk = udp_get_next(seq, v); 15701da177e4SLinus Torvalds 15711da177e4SLinus Torvalds ++*pos; 15721da177e4SLinus Torvalds return sk; 15731da177e4SLinus Torvalds } 15741da177e4SLinus Torvalds 15751da177e4SLinus Torvalds static void udp_seq_stop(struct seq_file *seq, void *v) 15769a429c49SEric Dumazet __releases(udp_hash_lock) 15771da177e4SLinus Torvalds { 15781da177e4SLinus Torvalds read_unlock(&udp_hash_lock); 15791da177e4SLinus Torvalds } 15801da177e4SLinus Torvalds 15811da177e4SLinus Torvalds static int udp_seq_open(struct inode *inode, struct file *file) 15821da177e4SLinus Torvalds { 15831da177e4SLinus Torvalds struct udp_seq_afinfo *afinfo = PDE(inode)->data; 15841da177e4SLinus Torvalds struct seq_file *seq; 15851da177e4SLinus Torvalds int rc = -ENOMEM; 15860da974f4SPanagiotis Issaris struct udp_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL); 15871da177e4SLinus Torvalds 15881da177e4SLinus Torvalds if (!s) 15891da177e4SLinus Torvalds goto out; 15901da177e4SLinus Torvalds s->family = afinfo->family; 1591ba4e58ecSGerrit Renker s->hashtable = afinfo->hashtable; 15921da177e4SLinus Torvalds s->seq_ops.start = udp_seq_start; 15931da177e4SLinus Torvalds s->seq_ops.next = udp_seq_next; 15941da177e4SLinus Torvalds s->seq_ops.show = afinfo->seq_show; 15951da177e4SLinus Torvalds s->seq_ops.stop = udp_seq_stop; 15961da177e4SLinus Torvalds 15971da177e4SLinus Torvalds rc = seq_open(file, &s->seq_ops); 15981da177e4SLinus Torvalds if (rc) 15991da177e4SLinus Torvalds goto out_kfree; 16001da177e4SLinus Torvalds 16011da177e4SLinus Torvalds seq = file->private_data; 16021da177e4SLinus Torvalds seq->private = s; 16031da177e4SLinus Torvalds out: 16041da177e4SLinus Torvalds return rc; 16051da177e4SLinus Torvalds out_kfree: 16061da177e4SLinus Torvalds kfree(s); 16071da177e4SLinus Torvalds goto out; 16081da177e4SLinus Torvalds } 16091da177e4SLinus Torvalds 16101da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 16111da177e4SLinus Torvalds int udp_proc_register(struct udp_seq_afinfo *afinfo) 16121da177e4SLinus Torvalds { 16131da177e4SLinus Torvalds struct proc_dir_entry *p; 16141da177e4SLinus Torvalds int rc = 0; 16151da177e4SLinus Torvalds 16161da177e4SLinus Torvalds if (!afinfo) 16171da177e4SLinus Torvalds return -EINVAL; 16181da177e4SLinus Torvalds afinfo->seq_fops->owner = afinfo->owner; 16191da177e4SLinus Torvalds afinfo->seq_fops->open = udp_seq_open; 16201da177e4SLinus Torvalds afinfo->seq_fops->read = seq_read; 16211da177e4SLinus Torvalds afinfo->seq_fops->llseek = seq_lseek; 16221da177e4SLinus Torvalds afinfo->seq_fops->release = seq_release_private; 16231da177e4SLinus Torvalds 1624457c4cbcSEric W. Biederman p = proc_net_fops_create(&init_net, afinfo->name, S_IRUGO, afinfo->seq_fops); 16251da177e4SLinus Torvalds if (p) 16261da177e4SLinus Torvalds p->data = afinfo; 16271da177e4SLinus Torvalds else 16281da177e4SLinus Torvalds rc = -ENOMEM; 16291da177e4SLinus Torvalds return rc; 16301da177e4SLinus Torvalds } 16311da177e4SLinus Torvalds 16321da177e4SLinus Torvalds void udp_proc_unregister(struct udp_seq_afinfo *afinfo) 16331da177e4SLinus Torvalds { 16341da177e4SLinus Torvalds if (!afinfo) 16351da177e4SLinus Torvalds return; 1636457c4cbcSEric W. Biederman proc_net_remove(&init_net, afinfo->name); 16371da177e4SLinus Torvalds memset(afinfo->seq_fops, 0, sizeof(*afinfo->seq_fops)); 16381da177e4SLinus Torvalds } 1639*db8dac20SDavid S. Miller 1640*db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 1641*db8dac20SDavid S. Miller static void udp4_format_sock(struct sock *sp, char *tmpbuf, int bucket) 1642*db8dac20SDavid S. Miller { 1643*db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sp); 1644*db8dac20SDavid S. Miller __be32 dest = inet->daddr; 1645*db8dac20SDavid S. Miller __be32 src = inet->rcv_saddr; 1646*db8dac20SDavid S. Miller __u16 destp = ntohs(inet->dport); 1647*db8dac20SDavid S. Miller __u16 srcp = ntohs(inet->sport); 1648*db8dac20SDavid S. Miller 1649*db8dac20SDavid S. Miller sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X" 1650*db8dac20SDavid S. Miller " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p", 1651*db8dac20SDavid S. Miller bucket, src, srcp, dest, destp, sp->sk_state, 1652*db8dac20SDavid S. Miller atomic_read(&sp->sk_wmem_alloc), 1653*db8dac20SDavid S. Miller atomic_read(&sp->sk_rmem_alloc), 1654*db8dac20SDavid S. Miller 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp), 1655*db8dac20SDavid S. Miller atomic_read(&sp->sk_refcnt), sp); 1656*db8dac20SDavid S. Miller } 1657*db8dac20SDavid S. Miller 1658*db8dac20SDavid S. Miller int udp4_seq_show(struct seq_file *seq, void *v) 1659*db8dac20SDavid S. Miller { 1660*db8dac20SDavid S. Miller if (v == SEQ_START_TOKEN) 1661*db8dac20SDavid S. Miller seq_printf(seq, "%-127s\n", 1662*db8dac20SDavid S. Miller " sl local_address rem_address st tx_queue " 1663*db8dac20SDavid S. Miller "rx_queue tr tm->when retrnsmt uid timeout " 1664*db8dac20SDavid S. Miller "inode"); 1665*db8dac20SDavid S. Miller else { 1666*db8dac20SDavid S. Miller char tmpbuf[129]; 1667*db8dac20SDavid S. Miller struct udp_iter_state *state = seq->private; 1668*db8dac20SDavid S. Miller 1669*db8dac20SDavid S. Miller udp4_format_sock(v, tmpbuf, state->bucket); 1670*db8dac20SDavid S. Miller seq_printf(seq, "%-127s\n", tmpbuf); 1671*db8dac20SDavid S. Miller } 1672*db8dac20SDavid S. Miller return 0; 1673*db8dac20SDavid S. Miller } 1674*db8dac20SDavid S. Miller 1675*db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 1676*db8dac20SDavid S. Miller static struct file_operations udp4_seq_fops; 1677*db8dac20SDavid S. Miller static struct udp_seq_afinfo udp4_seq_afinfo = { 1678*db8dac20SDavid S. Miller .owner = THIS_MODULE, 1679*db8dac20SDavid S. Miller .name = "udp", 1680*db8dac20SDavid S. Miller .family = AF_INET, 1681*db8dac20SDavid S. Miller .hashtable = udp_hash, 1682*db8dac20SDavid S. Miller .seq_show = udp4_seq_show, 1683*db8dac20SDavid S. Miller .seq_fops = &udp4_seq_fops, 1684*db8dac20SDavid S. Miller }; 1685*db8dac20SDavid S. Miller 1686*db8dac20SDavid S. Miller int __init udp4_proc_init(void) 1687*db8dac20SDavid S. Miller { 1688*db8dac20SDavid S. Miller return udp_proc_register(&udp4_seq_afinfo); 1689*db8dac20SDavid S. Miller } 1690*db8dac20SDavid S. Miller 1691*db8dac20SDavid S. Miller void udp4_proc_exit(void) 1692*db8dac20SDavid S. Miller { 1693*db8dac20SDavid S. Miller udp_proc_unregister(&udp4_seq_afinfo); 1694*db8dac20SDavid S. Miller } 16951da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 16961da177e4SLinus Torvalds 169795766fffSHideo Aoki void __init udp_init(void) 169895766fffSHideo Aoki { 169995766fffSHideo Aoki unsigned long limit; 170095766fffSHideo Aoki 170195766fffSHideo Aoki /* Set the pressure threshold up by the same strategy of TCP. It is a 170295766fffSHideo Aoki * fraction of global memory that is up to 1/2 at 256 MB, decreasing 170395766fffSHideo Aoki * toward zero with the amount of memory, with a floor of 128 pages. 170495766fffSHideo Aoki */ 170595766fffSHideo Aoki limit = min(nr_all_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT); 170695766fffSHideo Aoki limit = (limit * (nr_all_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11); 170795766fffSHideo Aoki limit = max(limit, 128UL); 170895766fffSHideo Aoki sysctl_udp_mem[0] = limit / 4 * 3; 170995766fffSHideo Aoki sysctl_udp_mem[1] = limit; 171095766fffSHideo Aoki sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2; 171195766fffSHideo Aoki 171295766fffSHideo Aoki sysctl_udp_rmem_min = SK_MEM_QUANTUM; 171395766fffSHideo Aoki sysctl_udp_wmem_min = SK_MEM_QUANTUM; 171495766fffSHideo Aoki } 171595766fffSHideo Aoki 17161da177e4SLinus Torvalds EXPORT_SYMBOL(udp_disconnect); 17171da177e4SLinus Torvalds EXPORT_SYMBOL(udp_hash); 17181da177e4SLinus Torvalds EXPORT_SYMBOL(udp_hash_lock); 17191da177e4SLinus Torvalds EXPORT_SYMBOL(udp_ioctl); 172025030a7fSGerrit Renker EXPORT_SYMBOL(udp_get_port); 1721*db8dac20SDavid S. Miller EXPORT_SYMBOL(udp_prot); 1722*db8dac20SDavid S. Miller EXPORT_SYMBOL(udp_sendmsg); 17234c0a6cb0SGerrit Renker EXPORT_SYMBOL(udp_lib_getsockopt); 17244c0a6cb0SGerrit Renker EXPORT_SYMBOL(udp_lib_setsockopt); 17251da177e4SLinus Torvalds EXPORT_SYMBOL(udp_poll); 17261da177e4SLinus Torvalds 17271da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 17281da177e4SLinus Torvalds EXPORT_SYMBOL(udp_proc_register); 17291da177e4SLinus Torvalds EXPORT_SYMBOL(udp_proc_unregister); 17301da177e4SLinus Torvalds #endif 1731