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 249db8dac20SDavid S. Miller int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2) 250db8dac20SDavid S. Miller { 251db8dac20SDavid S. Miller struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2); 252db8dac20SDavid S. Miller 253db8dac20SDavid S. Miller return ( !ipv6_only_sock(sk2) && 254db8dac20SDavid S. Miller (!inet1->rcv_saddr || !inet2->rcv_saddr || 255db8dac20SDavid S. Miller inet1->rcv_saddr == inet2->rcv_saddr )); 256db8dac20SDavid S. Miller } 257db8dac20SDavid S. Miller 258db8dac20SDavid S. Miller static inline int udp_v4_get_port(struct sock *sk, unsigned short snum) 259db8dac20SDavid S. Miller { 260db8dac20SDavid S. Miller return udp_get_port(sk, snum, ipv4_rcv_saddr_equal); 261db8dac20SDavid S. Miller } 262db8dac20SDavid S. Miller 263db8dac20SDavid S. Miller /* UDP is nearly always wildcards out the wazoo, it makes no sense to try 264db8dac20SDavid S. Miller * harder than this. -DaveM 265db8dac20SDavid S. Miller */ 266db8dac20SDavid S. Miller static struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, 267db8dac20SDavid S. Miller __be16 sport, __be32 daddr, __be16 dport, 268db8dac20SDavid S. Miller int dif, struct hlist_head udptable[]) 269db8dac20SDavid S. Miller { 270db8dac20SDavid S. Miller struct sock *sk, *result = NULL; 271db8dac20SDavid S. Miller struct hlist_node *node; 272db8dac20SDavid S. Miller unsigned short hnum = ntohs(dport); 273db8dac20SDavid S. Miller int badness = -1; 274db8dac20SDavid S. Miller 275db8dac20SDavid S. Miller read_lock(&udp_hash_lock); 276db8dac20SDavid S. Miller sk_for_each(sk, node, &udptable[hnum & (UDP_HTABLE_SIZE - 1)]) { 277db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 278db8dac20SDavid S. Miller 279db8dac20SDavid S. Miller if (sk->sk_net == net && sk->sk_hash == hnum && 280db8dac20SDavid S. Miller !ipv6_only_sock(sk)) { 281db8dac20SDavid S. Miller int score = (sk->sk_family == PF_INET ? 1 : 0); 282db8dac20SDavid S. Miller if (inet->rcv_saddr) { 283db8dac20SDavid S. Miller if (inet->rcv_saddr != daddr) 284db8dac20SDavid S. Miller continue; 285db8dac20SDavid S. Miller score+=2; 286db8dac20SDavid S. Miller } 287db8dac20SDavid S. Miller if (inet->daddr) { 288db8dac20SDavid S. Miller if (inet->daddr != saddr) 289db8dac20SDavid S. Miller continue; 290db8dac20SDavid S. Miller score+=2; 291db8dac20SDavid S. Miller } 292db8dac20SDavid S. Miller if (inet->dport) { 293db8dac20SDavid S. Miller if (inet->dport != sport) 294db8dac20SDavid S. Miller continue; 295db8dac20SDavid S. Miller score+=2; 296db8dac20SDavid S. Miller } 297db8dac20SDavid S. Miller if (sk->sk_bound_dev_if) { 298db8dac20SDavid S. Miller if (sk->sk_bound_dev_if != dif) 299db8dac20SDavid S. Miller continue; 300db8dac20SDavid S. Miller score+=2; 301db8dac20SDavid S. Miller } 302db8dac20SDavid S. Miller if (score == 9) { 303db8dac20SDavid S. Miller result = sk; 304db8dac20SDavid S. Miller break; 305db8dac20SDavid S. Miller } else if (score > badness) { 306db8dac20SDavid S. Miller result = sk; 307db8dac20SDavid S. Miller badness = score; 308db8dac20SDavid S. Miller } 309db8dac20SDavid S. Miller } 310db8dac20SDavid S. Miller } 311db8dac20SDavid S. Miller if (result) 312db8dac20SDavid S. Miller sock_hold(result); 313db8dac20SDavid S. Miller read_unlock(&udp_hash_lock); 314db8dac20SDavid S. Miller return result; 315db8dac20SDavid S. Miller } 316db8dac20SDavid S. Miller 317db8dac20SDavid S. Miller static inline struct sock *udp_v4_mcast_next(struct sock *sk, 318db8dac20SDavid S. Miller __be16 loc_port, __be32 loc_addr, 319db8dac20SDavid S. Miller __be16 rmt_port, __be32 rmt_addr, 320db8dac20SDavid S. Miller int dif) 321db8dac20SDavid S. Miller { 322db8dac20SDavid S. Miller struct hlist_node *node; 323db8dac20SDavid S. Miller struct sock *s = sk; 324db8dac20SDavid S. Miller unsigned short hnum = ntohs(loc_port); 325db8dac20SDavid S. Miller 326db8dac20SDavid S. Miller sk_for_each_from(s, node) { 327db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(s); 328db8dac20SDavid S. Miller 329db8dac20SDavid S. Miller if (s->sk_hash != hnum || 330db8dac20SDavid S. Miller (inet->daddr && inet->daddr != rmt_addr) || 331db8dac20SDavid S. Miller (inet->dport != rmt_port && inet->dport) || 332db8dac20SDavid S. Miller (inet->rcv_saddr && inet->rcv_saddr != loc_addr) || 333db8dac20SDavid S. Miller ipv6_only_sock(s) || 334db8dac20SDavid S. Miller (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)) 335db8dac20SDavid S. Miller continue; 336db8dac20SDavid S. Miller if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif)) 337db8dac20SDavid S. Miller continue; 338db8dac20SDavid S. Miller goto found; 339db8dac20SDavid S. Miller } 340db8dac20SDavid S. Miller s = NULL; 341db8dac20SDavid S. Miller found: 342db8dac20SDavid S. Miller return s; 343db8dac20SDavid S. Miller } 344db8dac20SDavid S. Miller 345db8dac20SDavid S. Miller /* 346db8dac20SDavid S. Miller * This routine is called by the ICMP module when it gets some 347db8dac20SDavid S. Miller * sort of error condition. If err < 0 then the socket should 348db8dac20SDavid S. Miller * be closed and the error returned to the user. If err > 0 349db8dac20SDavid S. Miller * it's just the icmp type << 8 | icmp code. 350db8dac20SDavid S. Miller * Header points to the ip header of the error packet. We move 351db8dac20SDavid S. Miller * on past this. Then (as it used to claim before adjustment) 352db8dac20SDavid S. Miller * header points to the first 8 bytes of the udp header. We need 353db8dac20SDavid S. Miller * to find the appropriate port. 354db8dac20SDavid S. Miller */ 355db8dac20SDavid S. Miller 356db8dac20SDavid S. Miller void __udp4_lib_err(struct sk_buff *skb, u32 info, struct hlist_head udptable[]) 357db8dac20SDavid S. Miller { 358db8dac20SDavid S. Miller struct inet_sock *inet; 359db8dac20SDavid S. Miller struct iphdr *iph = (struct iphdr*)skb->data; 360db8dac20SDavid S. Miller struct udphdr *uh = (struct udphdr*)(skb->data+(iph->ihl<<2)); 361db8dac20SDavid S. Miller const int type = icmp_hdr(skb)->type; 362db8dac20SDavid S. Miller const int code = icmp_hdr(skb)->code; 363db8dac20SDavid S. Miller struct sock *sk; 364db8dac20SDavid S. Miller int harderr; 365db8dac20SDavid S. Miller int err; 366db8dac20SDavid S. Miller 367db8dac20SDavid S. Miller sk = __udp4_lib_lookup(skb->dev->nd_net, iph->daddr, uh->dest, 368db8dac20SDavid S. Miller iph->saddr, uh->source, skb->dev->ifindex, udptable); 369db8dac20SDavid S. Miller if (sk == NULL) { 370db8dac20SDavid S. Miller ICMP_INC_STATS_BH(ICMP_MIB_INERRORS); 371db8dac20SDavid S. Miller return; /* No socket for error */ 372db8dac20SDavid S. Miller } 373db8dac20SDavid S. Miller 374db8dac20SDavid S. Miller err = 0; 375db8dac20SDavid S. Miller harderr = 0; 376db8dac20SDavid S. Miller inet = inet_sk(sk); 377db8dac20SDavid S. Miller 378db8dac20SDavid S. Miller switch (type) { 379db8dac20SDavid S. Miller default: 380db8dac20SDavid S. Miller case ICMP_TIME_EXCEEDED: 381db8dac20SDavid S. Miller err = EHOSTUNREACH; 382db8dac20SDavid S. Miller break; 383db8dac20SDavid S. Miller case ICMP_SOURCE_QUENCH: 384db8dac20SDavid S. Miller goto out; 385db8dac20SDavid S. Miller case ICMP_PARAMETERPROB: 386db8dac20SDavid S. Miller err = EPROTO; 387db8dac20SDavid S. Miller harderr = 1; 388db8dac20SDavid S. Miller break; 389db8dac20SDavid S. Miller case ICMP_DEST_UNREACH: 390db8dac20SDavid S. Miller if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */ 391db8dac20SDavid S. Miller if (inet->pmtudisc != IP_PMTUDISC_DONT) { 392db8dac20SDavid S. Miller err = EMSGSIZE; 393db8dac20SDavid S. Miller harderr = 1; 394db8dac20SDavid S. Miller break; 395db8dac20SDavid S. Miller } 396db8dac20SDavid S. Miller goto out; 397db8dac20SDavid S. Miller } 398db8dac20SDavid S. Miller err = EHOSTUNREACH; 399db8dac20SDavid S. Miller if (code <= NR_ICMP_UNREACH) { 400db8dac20SDavid S. Miller harderr = icmp_err_convert[code].fatal; 401db8dac20SDavid S. Miller err = icmp_err_convert[code].errno; 402db8dac20SDavid S. Miller } 403db8dac20SDavid S. Miller break; 404db8dac20SDavid S. Miller } 405db8dac20SDavid S. Miller 406db8dac20SDavid S. Miller /* 407db8dac20SDavid S. Miller * RFC1122: OK. Passes ICMP errors back to application, as per 408db8dac20SDavid S. Miller * 4.1.3.3. 409db8dac20SDavid S. Miller */ 410db8dac20SDavid S. Miller if (!inet->recverr) { 411db8dac20SDavid S. Miller if (!harderr || sk->sk_state != TCP_ESTABLISHED) 412db8dac20SDavid S. Miller goto out; 413db8dac20SDavid S. Miller } else { 414db8dac20SDavid S. Miller ip_icmp_error(sk, skb, err, uh->dest, info, (u8*)(uh+1)); 415db8dac20SDavid S. Miller } 416db8dac20SDavid S. Miller sk->sk_err = err; 417db8dac20SDavid S. Miller sk->sk_error_report(sk); 418db8dac20SDavid S. Miller out: 419db8dac20SDavid S. Miller sock_put(sk); 420db8dac20SDavid S. Miller } 421db8dac20SDavid S. Miller 422db8dac20SDavid S. Miller void udp_err(struct sk_buff *skb, u32 info) 423db8dac20SDavid S. Miller { 424db8dac20SDavid S. Miller __udp4_lib_err(skb, info, udp_hash); 425db8dac20SDavid S. Miller } 426db8dac20SDavid S. Miller 427db8dac20SDavid S. Miller /* 428db8dac20SDavid S. Miller * Throw away all pending data and cancel the corking. Socket is locked. 429db8dac20SDavid S. Miller */ 430db8dac20SDavid S. Miller static void udp_flush_pending_frames(struct sock *sk) 431db8dac20SDavid S. Miller { 432db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 433db8dac20SDavid S. Miller 434db8dac20SDavid S. Miller if (up->pending) { 435db8dac20SDavid S. Miller up->len = 0; 436db8dac20SDavid S. Miller up->pending = 0; 437db8dac20SDavid S. Miller ip_flush_pending_frames(sk); 438db8dac20SDavid S. Miller } 439db8dac20SDavid S. Miller } 440db8dac20SDavid S. Miller 441db8dac20SDavid S. Miller /** 442db8dac20SDavid S. Miller * udp4_hwcsum_outgoing - handle outgoing HW checksumming 443db8dac20SDavid S. Miller * @sk: socket we are sending on 444db8dac20SDavid S. Miller * @skb: sk_buff containing the filled-in UDP header 445db8dac20SDavid S. Miller * (checksum field must be zeroed out) 446db8dac20SDavid S. Miller */ 447db8dac20SDavid S. Miller static void udp4_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb, 448db8dac20SDavid S. Miller __be32 src, __be32 dst, int len ) 449db8dac20SDavid S. Miller { 450db8dac20SDavid S. Miller unsigned int offset; 451db8dac20SDavid S. Miller struct udphdr *uh = udp_hdr(skb); 452db8dac20SDavid S. Miller __wsum csum = 0; 453db8dac20SDavid S. Miller 454db8dac20SDavid S. Miller if (skb_queue_len(&sk->sk_write_queue) == 1) { 455db8dac20SDavid S. Miller /* 456db8dac20SDavid S. Miller * Only one fragment on the socket. 457db8dac20SDavid S. Miller */ 458db8dac20SDavid S. Miller skb->csum_start = skb_transport_header(skb) - skb->head; 459db8dac20SDavid S. Miller skb->csum_offset = offsetof(struct udphdr, check); 460db8dac20SDavid S. Miller uh->check = ~csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, 0); 461db8dac20SDavid S. Miller } else { 462db8dac20SDavid S. Miller /* 463db8dac20SDavid S. Miller * HW-checksum won't work as there are two or more 464db8dac20SDavid S. Miller * fragments on the socket so that all csums of sk_buffs 465db8dac20SDavid S. Miller * should be together 466db8dac20SDavid S. Miller */ 467db8dac20SDavid S. Miller offset = skb_transport_offset(skb); 468db8dac20SDavid S. Miller skb->csum = skb_checksum(skb, offset, skb->len - offset, 0); 469db8dac20SDavid S. Miller 470db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_NONE; 471db8dac20SDavid S. Miller 472db8dac20SDavid S. Miller skb_queue_walk(&sk->sk_write_queue, skb) { 473db8dac20SDavid S. Miller csum = csum_add(csum, skb->csum); 474db8dac20SDavid S. Miller } 475db8dac20SDavid S. Miller 476db8dac20SDavid S. Miller uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum); 477db8dac20SDavid S. Miller if (uh->check == 0) 478db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 479db8dac20SDavid S. Miller } 480db8dac20SDavid S. Miller } 481db8dac20SDavid S. Miller 482db8dac20SDavid S. Miller /* 483db8dac20SDavid S. Miller * Push out all pending data as one UDP datagram. Socket is locked. 484db8dac20SDavid S. Miller */ 485db8dac20SDavid S. Miller static int udp_push_pending_frames(struct sock *sk) 486db8dac20SDavid S. Miller { 487db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 488db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 489db8dac20SDavid S. Miller struct flowi *fl = &inet->cork.fl; 490db8dac20SDavid S. Miller struct sk_buff *skb; 491db8dac20SDavid S. Miller struct udphdr *uh; 492db8dac20SDavid S. Miller int err = 0; 493db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 494db8dac20SDavid S. Miller __wsum csum = 0; 495db8dac20SDavid S. Miller 496db8dac20SDavid S. Miller /* Grab the skbuff where UDP header space exists. */ 497db8dac20SDavid S. Miller if ((skb = skb_peek(&sk->sk_write_queue)) == NULL) 498db8dac20SDavid S. Miller goto out; 499db8dac20SDavid S. Miller 500db8dac20SDavid S. Miller /* 501db8dac20SDavid S. Miller * Create a UDP header 502db8dac20SDavid S. Miller */ 503db8dac20SDavid S. Miller uh = udp_hdr(skb); 504db8dac20SDavid S. Miller uh->source = fl->fl_ip_sport; 505db8dac20SDavid S. Miller uh->dest = fl->fl_ip_dport; 506db8dac20SDavid S. Miller uh->len = htons(up->len); 507db8dac20SDavid S. Miller uh->check = 0; 508db8dac20SDavid S. Miller 509db8dac20SDavid S. Miller if (is_udplite) /* UDP-Lite */ 510db8dac20SDavid S. Miller csum = udplite_csum_outgoing(sk, skb); 511db8dac20SDavid S. Miller 512db8dac20SDavid S. Miller else if (sk->sk_no_check == UDP_CSUM_NOXMIT) { /* UDP csum disabled */ 513db8dac20SDavid S. Miller 514db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_NONE; 515db8dac20SDavid S. Miller goto send; 516db8dac20SDavid S. Miller 517db8dac20SDavid S. Miller } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 518db8dac20SDavid S. Miller 519db8dac20SDavid S. Miller udp4_hwcsum_outgoing(sk, skb, fl->fl4_src,fl->fl4_dst, up->len); 520db8dac20SDavid S. Miller goto send; 521db8dac20SDavid S. Miller 522db8dac20SDavid S. Miller } else /* `normal' UDP */ 523db8dac20SDavid S. Miller csum = udp_csum_outgoing(sk, skb); 524db8dac20SDavid S. Miller 525db8dac20SDavid S. Miller /* add protocol-dependent pseudo-header */ 526db8dac20SDavid S. Miller uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst, up->len, 527db8dac20SDavid S. Miller sk->sk_protocol, csum ); 528db8dac20SDavid S. Miller if (uh->check == 0) 529db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 530db8dac20SDavid S. Miller 531db8dac20SDavid S. Miller send: 532db8dac20SDavid S. Miller err = ip_push_pending_frames(sk); 533db8dac20SDavid S. Miller out: 534db8dac20SDavid S. Miller up->len = 0; 535db8dac20SDavid S. Miller up->pending = 0; 536db8dac20SDavid S. Miller if (!err) 537db8dac20SDavid S. Miller UDP_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS, is_udplite); 538db8dac20SDavid S. Miller return err; 539db8dac20SDavid S. Miller } 540db8dac20SDavid S. Miller 541db8dac20SDavid S. Miller int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 542db8dac20SDavid S. Miller size_t len) 543db8dac20SDavid S. Miller { 544db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 545db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 546db8dac20SDavid S. Miller int ulen = len; 547db8dac20SDavid S. Miller struct ipcm_cookie ipc; 548db8dac20SDavid S. Miller struct rtable *rt = NULL; 549db8dac20SDavid S. Miller int free = 0; 550db8dac20SDavid S. Miller int connected = 0; 551db8dac20SDavid S. Miller __be32 daddr, faddr, saddr; 552db8dac20SDavid S. Miller __be16 dport; 553db8dac20SDavid S. Miller u8 tos; 554db8dac20SDavid S. Miller int err, is_udplite = IS_UDPLITE(sk); 555db8dac20SDavid S. Miller int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; 556db8dac20SDavid S. Miller int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 557db8dac20SDavid S. Miller 558db8dac20SDavid S. Miller if (len > 0xFFFF) 559db8dac20SDavid S. Miller return -EMSGSIZE; 560db8dac20SDavid S. Miller 561db8dac20SDavid S. Miller /* 562db8dac20SDavid S. Miller * Check the flags. 563db8dac20SDavid S. Miller */ 564db8dac20SDavid S. Miller 565db8dac20SDavid S. Miller if (msg->msg_flags&MSG_OOB) /* Mirror BSD error message compatibility */ 566db8dac20SDavid S. Miller return -EOPNOTSUPP; 567db8dac20SDavid S. Miller 568db8dac20SDavid S. Miller ipc.opt = NULL; 569db8dac20SDavid S. Miller 570db8dac20SDavid S. Miller if (up->pending) { 571db8dac20SDavid S. Miller /* 572db8dac20SDavid S. Miller * There are pending frames. 573db8dac20SDavid S. Miller * The socket lock must be held while it's corked. 574db8dac20SDavid S. Miller */ 575db8dac20SDavid S. Miller lock_sock(sk); 576db8dac20SDavid S. Miller if (likely(up->pending)) { 577db8dac20SDavid S. Miller if (unlikely(up->pending != AF_INET)) { 578db8dac20SDavid S. Miller release_sock(sk); 579db8dac20SDavid S. Miller return -EINVAL; 580db8dac20SDavid S. Miller } 581db8dac20SDavid S. Miller goto do_append_data; 582db8dac20SDavid S. Miller } 583db8dac20SDavid S. Miller release_sock(sk); 584db8dac20SDavid S. Miller } 585db8dac20SDavid S. Miller ulen += sizeof(struct udphdr); 586db8dac20SDavid S. Miller 587db8dac20SDavid S. Miller /* 588db8dac20SDavid S. Miller * Get and verify the address. 589db8dac20SDavid S. Miller */ 590db8dac20SDavid S. Miller if (msg->msg_name) { 591db8dac20SDavid S. Miller struct sockaddr_in * usin = (struct sockaddr_in*)msg->msg_name; 592db8dac20SDavid S. Miller if (msg->msg_namelen < sizeof(*usin)) 593db8dac20SDavid S. Miller return -EINVAL; 594db8dac20SDavid S. Miller if (usin->sin_family != AF_INET) { 595db8dac20SDavid S. Miller if (usin->sin_family != AF_UNSPEC) 596db8dac20SDavid S. Miller return -EAFNOSUPPORT; 597db8dac20SDavid S. Miller } 598db8dac20SDavid S. Miller 599db8dac20SDavid S. Miller daddr = usin->sin_addr.s_addr; 600db8dac20SDavid S. Miller dport = usin->sin_port; 601db8dac20SDavid S. Miller if (dport == 0) 602db8dac20SDavid S. Miller return -EINVAL; 603db8dac20SDavid S. Miller } else { 604db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 605db8dac20SDavid S. Miller return -EDESTADDRREQ; 606db8dac20SDavid S. Miller daddr = inet->daddr; 607db8dac20SDavid S. Miller dport = inet->dport; 608db8dac20SDavid S. Miller /* Open fast path for connected socket. 609db8dac20SDavid S. Miller Route will not be used, if at least one option is set. 610db8dac20SDavid S. Miller */ 611db8dac20SDavid S. Miller connected = 1; 612db8dac20SDavid S. Miller } 613db8dac20SDavid S. Miller ipc.addr = inet->saddr; 614db8dac20SDavid S. Miller 615db8dac20SDavid S. Miller ipc.oif = sk->sk_bound_dev_if; 616db8dac20SDavid S. Miller if (msg->msg_controllen) { 617db8dac20SDavid S. Miller err = ip_cmsg_send(msg, &ipc); 618db8dac20SDavid S. Miller if (err) 619db8dac20SDavid S. Miller return err; 620db8dac20SDavid S. Miller if (ipc.opt) 621db8dac20SDavid S. Miller free = 1; 622db8dac20SDavid S. Miller connected = 0; 623db8dac20SDavid S. Miller } 624db8dac20SDavid S. Miller if (!ipc.opt) 625db8dac20SDavid S. Miller ipc.opt = inet->opt; 626db8dac20SDavid S. Miller 627db8dac20SDavid S. Miller saddr = ipc.addr; 628db8dac20SDavid S. Miller ipc.addr = faddr = daddr; 629db8dac20SDavid S. Miller 630db8dac20SDavid S. Miller if (ipc.opt && ipc.opt->srr) { 631db8dac20SDavid S. Miller if (!daddr) 632db8dac20SDavid S. Miller return -EINVAL; 633db8dac20SDavid S. Miller faddr = ipc.opt->faddr; 634db8dac20SDavid S. Miller connected = 0; 635db8dac20SDavid S. Miller } 636db8dac20SDavid S. Miller tos = RT_TOS(inet->tos); 637db8dac20SDavid S. Miller if (sock_flag(sk, SOCK_LOCALROUTE) || 638db8dac20SDavid S. Miller (msg->msg_flags & MSG_DONTROUTE) || 639db8dac20SDavid S. Miller (ipc.opt && ipc.opt->is_strictroute)) { 640db8dac20SDavid S. Miller tos |= RTO_ONLINK; 641db8dac20SDavid S. Miller connected = 0; 642db8dac20SDavid S. Miller } 643db8dac20SDavid S. Miller 644db8dac20SDavid S. Miller if (ipv4_is_multicast(daddr)) { 645db8dac20SDavid S. Miller if (!ipc.oif) 646db8dac20SDavid S. Miller ipc.oif = inet->mc_index; 647db8dac20SDavid S. Miller if (!saddr) 648db8dac20SDavid S. Miller saddr = inet->mc_addr; 649db8dac20SDavid S. Miller connected = 0; 650db8dac20SDavid S. Miller } 651db8dac20SDavid S. Miller 652db8dac20SDavid S. Miller if (connected) 653db8dac20SDavid S. Miller rt = (struct rtable*)sk_dst_check(sk, 0); 654db8dac20SDavid S. Miller 655db8dac20SDavid S. Miller if (rt == NULL) { 656db8dac20SDavid S. Miller struct flowi fl = { .oif = ipc.oif, 657db8dac20SDavid S. Miller .nl_u = { .ip4_u = 658db8dac20SDavid S. Miller { .daddr = faddr, 659db8dac20SDavid S. Miller .saddr = saddr, 660db8dac20SDavid S. Miller .tos = tos } }, 661db8dac20SDavid S. Miller .proto = sk->sk_protocol, 662db8dac20SDavid S. Miller .uli_u = { .ports = 663db8dac20SDavid S. Miller { .sport = inet->sport, 664db8dac20SDavid S. Miller .dport = dport } } }; 665db8dac20SDavid S. Miller security_sk_classify_flow(sk, &fl); 666db8dac20SDavid S. Miller err = ip_route_output_flow(&init_net, &rt, &fl, sk, 1); 667db8dac20SDavid S. Miller if (err) { 668db8dac20SDavid S. Miller if (err == -ENETUNREACH) 669db8dac20SDavid S. Miller IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES); 670db8dac20SDavid S. Miller goto out; 671db8dac20SDavid S. Miller } 672db8dac20SDavid S. Miller 673db8dac20SDavid S. Miller err = -EACCES; 674db8dac20SDavid S. Miller if ((rt->rt_flags & RTCF_BROADCAST) && 675db8dac20SDavid S. Miller !sock_flag(sk, SOCK_BROADCAST)) 676db8dac20SDavid S. Miller goto out; 677db8dac20SDavid S. Miller if (connected) 678db8dac20SDavid S. Miller sk_dst_set(sk, dst_clone(&rt->u.dst)); 679db8dac20SDavid S. Miller } 680db8dac20SDavid S. Miller 681db8dac20SDavid S. Miller if (msg->msg_flags&MSG_CONFIRM) 682db8dac20SDavid S. Miller goto do_confirm; 683db8dac20SDavid S. Miller back_from_confirm: 684db8dac20SDavid S. Miller 685db8dac20SDavid S. Miller saddr = rt->rt_src; 686db8dac20SDavid S. Miller if (!ipc.addr) 687db8dac20SDavid S. Miller daddr = ipc.addr = rt->rt_dst; 688db8dac20SDavid S. Miller 689db8dac20SDavid S. Miller lock_sock(sk); 690db8dac20SDavid S. Miller if (unlikely(up->pending)) { 691db8dac20SDavid S. Miller /* The socket is already corked while preparing it. */ 692db8dac20SDavid S. Miller /* ... which is an evident application bug. --ANK */ 693db8dac20SDavid S. Miller release_sock(sk); 694db8dac20SDavid S. Miller 695db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n"); 696db8dac20SDavid S. Miller err = -EINVAL; 697db8dac20SDavid S. Miller goto out; 698db8dac20SDavid S. Miller } 699db8dac20SDavid S. Miller /* 700db8dac20SDavid S. Miller * Now cork the socket to pend data. 701db8dac20SDavid S. Miller */ 702db8dac20SDavid S. Miller inet->cork.fl.fl4_dst = daddr; 703db8dac20SDavid S. Miller inet->cork.fl.fl_ip_dport = dport; 704db8dac20SDavid S. Miller inet->cork.fl.fl4_src = saddr; 705db8dac20SDavid S. Miller inet->cork.fl.fl_ip_sport = inet->sport; 706db8dac20SDavid S. Miller up->pending = AF_INET; 707db8dac20SDavid S. Miller 708db8dac20SDavid S. Miller do_append_data: 709db8dac20SDavid S. Miller up->len += ulen; 710db8dac20SDavid S. Miller getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 711db8dac20SDavid S. Miller err = ip_append_data(sk, getfrag, msg->msg_iov, ulen, 712db8dac20SDavid S. Miller sizeof(struct udphdr), &ipc, rt, 713db8dac20SDavid S. Miller corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); 714db8dac20SDavid S. Miller if (err) 715db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 716db8dac20SDavid S. Miller else if (!corkreq) 717db8dac20SDavid S. Miller err = udp_push_pending_frames(sk); 718db8dac20SDavid S. Miller else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 719db8dac20SDavid S. Miller up->pending = 0; 720db8dac20SDavid S. Miller release_sock(sk); 721db8dac20SDavid S. Miller 722db8dac20SDavid S. Miller out: 723db8dac20SDavid S. Miller ip_rt_put(rt); 724db8dac20SDavid S. Miller if (free) 725db8dac20SDavid S. Miller kfree(ipc.opt); 726db8dac20SDavid S. Miller if (!err) 727db8dac20SDavid S. Miller return len; 728db8dac20SDavid S. Miller /* 729db8dac20SDavid S. Miller * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 730db8dac20SDavid S. Miller * ENOBUFS might not be good (it's not tunable per se), but otherwise 731db8dac20SDavid S. Miller * we don't have a good statistic (IpOutDiscards but it can be too many 732db8dac20SDavid S. Miller * things). We could add another new stat but at least for now that 733db8dac20SDavid S. Miller * seems like overkill. 734db8dac20SDavid S. Miller */ 735db8dac20SDavid S. Miller if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 736db8dac20SDavid S. Miller UDP_INC_STATS_USER(UDP_MIB_SNDBUFERRORS, is_udplite); 737db8dac20SDavid S. Miller } 738db8dac20SDavid S. Miller return err; 739db8dac20SDavid S. Miller 740db8dac20SDavid S. Miller do_confirm: 741db8dac20SDavid S. Miller dst_confirm(&rt->u.dst); 742db8dac20SDavid S. Miller if (!(msg->msg_flags&MSG_PROBE) || len) 743db8dac20SDavid S. Miller goto back_from_confirm; 744db8dac20SDavid S. Miller err = 0; 745db8dac20SDavid S. Miller goto out; 746db8dac20SDavid S. Miller } 747db8dac20SDavid S. Miller 748db8dac20SDavid S. Miller int udp_sendpage(struct sock *sk, struct page *page, int offset, 749db8dac20SDavid S. Miller size_t size, int flags) 750db8dac20SDavid S. Miller { 751db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 752db8dac20SDavid S. Miller int ret; 753db8dac20SDavid S. Miller 754db8dac20SDavid S. Miller if (!up->pending) { 755db8dac20SDavid S. Miller struct msghdr msg = { .msg_flags = flags|MSG_MORE }; 756db8dac20SDavid S. Miller 757db8dac20SDavid S. Miller /* Call udp_sendmsg to specify destination address which 758db8dac20SDavid S. Miller * sendpage interface can't pass. 759db8dac20SDavid S. Miller * This will succeed only when the socket is connected. 760db8dac20SDavid S. Miller */ 761db8dac20SDavid S. Miller ret = udp_sendmsg(NULL, sk, &msg, 0); 762db8dac20SDavid S. Miller if (ret < 0) 763db8dac20SDavid S. Miller return ret; 764db8dac20SDavid S. Miller } 765db8dac20SDavid S. Miller 766db8dac20SDavid S. Miller lock_sock(sk); 767db8dac20SDavid S. Miller 768db8dac20SDavid S. Miller if (unlikely(!up->pending)) { 769db8dac20SDavid S. Miller release_sock(sk); 770db8dac20SDavid S. Miller 771db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n"); 772db8dac20SDavid S. Miller return -EINVAL; 773db8dac20SDavid S. Miller } 774db8dac20SDavid S. Miller 775db8dac20SDavid S. Miller ret = ip_append_page(sk, page, offset, size, flags); 776db8dac20SDavid S. Miller if (ret == -EOPNOTSUPP) { 777db8dac20SDavid S. Miller release_sock(sk); 778db8dac20SDavid S. Miller return sock_no_sendpage(sk->sk_socket, page, offset, 779db8dac20SDavid S. Miller size, flags); 780db8dac20SDavid S. Miller } 781db8dac20SDavid S. Miller if (ret < 0) { 782db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 783db8dac20SDavid S. Miller goto out; 784db8dac20SDavid S. Miller } 785db8dac20SDavid S. Miller 786db8dac20SDavid S. Miller up->len += size; 787db8dac20SDavid S. Miller if (!(up->corkflag || (flags&MSG_MORE))) 788db8dac20SDavid S. Miller ret = udp_push_pending_frames(sk); 789db8dac20SDavid S. Miller if (!ret) 790db8dac20SDavid S. Miller ret = size; 791db8dac20SDavid S. Miller out: 792db8dac20SDavid S. Miller release_sock(sk); 793db8dac20SDavid S. Miller return ret; 794db8dac20SDavid S. Miller } 795db8dac20SDavid 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 836db8dac20SDavid S. Miller /* 837db8dac20SDavid S. Miller * This should be easy, if there is something there we 838db8dac20SDavid S. Miller * return it, otherwise we block. 839db8dac20SDavid S. Miller */ 840db8dac20SDavid S. Miller 841db8dac20SDavid S. Miller int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 842db8dac20SDavid S. Miller size_t len, int noblock, int flags, int *addr_len) 843db8dac20SDavid S. Miller { 844db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 845db8dac20SDavid S. Miller struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name; 846db8dac20SDavid S. Miller struct sk_buff *skb; 847db8dac20SDavid S. Miller unsigned int ulen, copied; 848db8dac20SDavid S. Miller int peeked; 849db8dac20SDavid S. Miller int err; 850db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 851db8dac20SDavid S. Miller 852db8dac20SDavid S. Miller /* 853db8dac20SDavid S. Miller * Check any passed addresses 854db8dac20SDavid S. Miller */ 855db8dac20SDavid S. Miller if (addr_len) 856db8dac20SDavid S. Miller *addr_len=sizeof(*sin); 857db8dac20SDavid S. Miller 858db8dac20SDavid S. Miller if (flags & MSG_ERRQUEUE) 859db8dac20SDavid S. Miller return ip_recv_error(sk, msg, len); 860db8dac20SDavid S. Miller 861db8dac20SDavid S. Miller try_again: 862db8dac20SDavid S. Miller skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), 863db8dac20SDavid S. Miller &peeked, &err); 864db8dac20SDavid S. Miller if (!skb) 865db8dac20SDavid S. Miller goto out; 866db8dac20SDavid S. Miller 867db8dac20SDavid S. Miller ulen = skb->len - sizeof(struct udphdr); 868db8dac20SDavid S. Miller copied = len; 869db8dac20SDavid S. Miller if (copied > ulen) 870db8dac20SDavid S. Miller copied = ulen; 871db8dac20SDavid S. Miller else if (copied < ulen) 872db8dac20SDavid S. Miller msg->msg_flags |= MSG_TRUNC; 873db8dac20SDavid S. Miller 874db8dac20SDavid S. Miller /* 875db8dac20SDavid S. Miller * If checksum is needed at all, try to do it while copying the 876db8dac20SDavid S. Miller * data. If the data is truncated, or if we only want a partial 877db8dac20SDavid S. Miller * coverage checksum (UDP-Lite), do it before the copy. 878db8dac20SDavid S. Miller */ 879db8dac20SDavid S. Miller 880db8dac20SDavid S. Miller if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) { 881db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 882db8dac20SDavid S. Miller goto csum_copy_err; 883db8dac20SDavid S. Miller } 884db8dac20SDavid S. Miller 885db8dac20SDavid S. Miller if (skb_csum_unnecessary(skb)) 886db8dac20SDavid S. Miller err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), 887db8dac20SDavid S. Miller msg->msg_iov, copied ); 888db8dac20SDavid S. Miller else { 889db8dac20SDavid S. Miller err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov); 890db8dac20SDavid S. Miller 891db8dac20SDavid S. Miller if (err == -EINVAL) 892db8dac20SDavid S. Miller goto csum_copy_err; 893db8dac20SDavid S. Miller } 894db8dac20SDavid S. Miller 895db8dac20SDavid S. Miller if (err) 896db8dac20SDavid S. Miller goto out_free; 897db8dac20SDavid S. Miller 898db8dac20SDavid S. Miller if (!peeked) 899db8dac20SDavid S. Miller UDP_INC_STATS_USER(UDP_MIB_INDATAGRAMS, is_udplite); 900db8dac20SDavid S. Miller 901db8dac20SDavid S. Miller sock_recv_timestamp(msg, sk, skb); 902db8dac20SDavid S. Miller 903db8dac20SDavid S. Miller /* Copy the address. */ 904db8dac20SDavid S. Miller if (sin) 905db8dac20SDavid S. Miller { 906db8dac20SDavid S. Miller sin->sin_family = AF_INET; 907db8dac20SDavid S. Miller sin->sin_port = udp_hdr(skb)->source; 908db8dac20SDavid S. Miller sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 909db8dac20SDavid S. Miller memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 910db8dac20SDavid S. Miller } 911db8dac20SDavid S. Miller if (inet->cmsg_flags) 912db8dac20SDavid S. Miller ip_cmsg_recv(msg, skb); 913db8dac20SDavid S. Miller 914db8dac20SDavid S. Miller err = copied; 915db8dac20SDavid S. Miller if (flags & MSG_TRUNC) 916db8dac20SDavid S. Miller err = ulen; 917db8dac20SDavid S. Miller 918db8dac20SDavid S. Miller out_free: 919db8dac20SDavid S. Miller lock_sock(sk); 920db8dac20SDavid S. Miller skb_free_datagram(sk, skb); 921db8dac20SDavid S. Miller release_sock(sk); 922db8dac20SDavid S. Miller out: 923db8dac20SDavid S. Miller return err; 924db8dac20SDavid S. Miller 925db8dac20SDavid S. Miller csum_copy_err: 926db8dac20SDavid S. Miller lock_sock(sk); 927db8dac20SDavid S. Miller if (!skb_kill_datagram(sk, skb, flags)) 928db8dac20SDavid S. Miller UDP_INC_STATS_USER(UDP_MIB_INERRORS, is_udplite); 929db8dac20SDavid S. Miller release_sock(sk); 930db8dac20SDavid S. Miller 931db8dac20SDavid S. Miller if (noblock) 932db8dac20SDavid S. Miller return -EAGAIN; 933db8dac20SDavid S. Miller goto try_again; 934db8dac20SDavid S. Miller } 935db8dac20SDavid S. Miller 936db8dac20SDavid 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 959db8dac20SDavid S. Miller /* returns: 960db8dac20SDavid S. Miller * -1: error 961db8dac20SDavid S. Miller * 0: success 962db8dac20SDavid S. Miller * >0: "udp encap" protocol resubmission 963db8dac20SDavid S. Miller * 964db8dac20SDavid S. Miller * Note that in the success and error cases, the skb is assumed to 965db8dac20SDavid S. Miller * have either been requeued or freed. 966db8dac20SDavid S. Miller */ 967db8dac20SDavid S. Miller int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb) 968db8dac20SDavid S. Miller { 969db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 970db8dac20SDavid S. Miller int rc; 971db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 972db8dac20SDavid S. Miller 973db8dac20SDavid S. Miller /* 974db8dac20SDavid S. Miller * Charge it to the socket, dropping if the queue is full. 975db8dac20SDavid S. Miller */ 976db8dac20SDavid S. Miller if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) 977db8dac20SDavid S. Miller goto drop; 978db8dac20SDavid S. Miller nf_reset(skb); 979db8dac20SDavid S. Miller 980db8dac20SDavid S. Miller if (up->encap_type) { 981db8dac20SDavid S. Miller /* 982db8dac20SDavid S. Miller * This is an encapsulation socket so pass the skb to 983db8dac20SDavid S. Miller * the socket's udp_encap_rcv() hook. Otherwise, just 984db8dac20SDavid S. Miller * fall through and pass this up the UDP socket. 985db8dac20SDavid S. Miller * up->encap_rcv() returns the following value: 986db8dac20SDavid S. Miller * =0 if skb was successfully passed to the encap 987db8dac20SDavid S. Miller * handler or was discarded by it. 988db8dac20SDavid S. Miller * >0 if skb should be passed on to UDP. 989db8dac20SDavid S. Miller * <0 if skb should be resubmitted as proto -N 990db8dac20SDavid S. Miller */ 991db8dac20SDavid S. Miller 992db8dac20SDavid S. Miller /* if we're overly short, let UDP handle it */ 993db8dac20SDavid S. Miller if (skb->len > sizeof(struct udphdr) && 994db8dac20SDavid S. Miller up->encap_rcv != NULL) { 995db8dac20SDavid S. Miller int ret; 996db8dac20SDavid S. Miller 997db8dac20SDavid S. Miller ret = (*up->encap_rcv)(sk, skb); 998db8dac20SDavid S. Miller if (ret <= 0) { 999db8dac20SDavid S. Miller UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS, 1000db8dac20SDavid S. Miller is_udplite); 1001db8dac20SDavid S. Miller return -ret; 1002db8dac20SDavid S. Miller } 1003db8dac20SDavid S. Miller } 1004db8dac20SDavid S. Miller 1005db8dac20SDavid S. Miller /* FALLTHROUGH -- it's a UDP Packet */ 1006db8dac20SDavid S. Miller } 1007db8dac20SDavid S. Miller 1008db8dac20SDavid S. Miller /* 1009db8dac20SDavid S. Miller * UDP-Lite specific tests, ignored on UDP sockets 1010db8dac20SDavid S. Miller */ 1011db8dac20SDavid S. Miller if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 1012db8dac20SDavid S. Miller 1013db8dac20SDavid S. Miller /* 1014db8dac20SDavid S. Miller * MIB statistics other than incrementing the error count are 1015db8dac20SDavid S. Miller * disabled for the following two types of errors: these depend 1016db8dac20SDavid S. Miller * on the application settings, not on the functioning of the 1017db8dac20SDavid S. Miller * protocol stack as such. 1018db8dac20SDavid S. Miller * 1019db8dac20SDavid S. Miller * RFC 3828 here recommends (sec 3.3): "There should also be a 1020db8dac20SDavid S. Miller * way ... to ... at least let the receiving application block 1021db8dac20SDavid S. Miller * delivery of packets with coverage values less than a value 1022db8dac20SDavid S. Miller * provided by the application." 1023db8dac20SDavid S. Miller */ 1024db8dac20SDavid S. Miller if (up->pcrlen == 0) { /* full coverage was set */ 1025db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_WARNING "UDPLITE: partial coverage " 1026db8dac20SDavid S. Miller "%d while full coverage %d requested\n", 1027db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, skb->len); 1028db8dac20SDavid S. Miller goto drop; 1029db8dac20SDavid S. Miller } 1030db8dac20SDavid S. Miller /* The next case involves violating the min. coverage requested 1031db8dac20SDavid S. Miller * by the receiver. This is subtle: if receiver wants x and x is 1032db8dac20SDavid S. Miller * greater than the buffersize/MTU then receiver will complain 1033db8dac20SDavid S. Miller * that it wants x while sender emits packets of smaller size y. 1034db8dac20SDavid S. Miller * Therefore the above ...()->partial_cov statement is essential. 1035db8dac20SDavid S. Miller */ 1036db8dac20SDavid S. Miller if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 1037db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_WARNING 1038db8dac20SDavid S. Miller "UDPLITE: coverage %d too small, need min %d\n", 1039db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, up->pcrlen); 1040db8dac20SDavid S. Miller goto drop; 1041db8dac20SDavid S. Miller } 1042db8dac20SDavid S. Miller } 1043db8dac20SDavid S. Miller 1044db8dac20SDavid S. Miller if (sk->sk_filter) { 1045db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 1046db8dac20SDavid S. Miller goto drop; 1047db8dac20SDavid S. Miller } 1048db8dac20SDavid S. Miller 1049db8dac20SDavid S. Miller if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) { 1050db8dac20SDavid S. Miller /* Note that an ENOMEM error is charged twice */ 1051db8dac20SDavid S. Miller if (rc == -ENOMEM) 1052db8dac20SDavid S. Miller UDP_INC_STATS_BH(UDP_MIB_RCVBUFERRORS, is_udplite); 1053db8dac20SDavid S. Miller goto drop; 1054db8dac20SDavid S. Miller } 1055db8dac20SDavid S. Miller 1056db8dac20SDavid S. Miller return 0; 1057db8dac20SDavid S. Miller 1058db8dac20SDavid S. Miller drop: 1059db8dac20SDavid S. Miller UDP_INC_STATS_BH(UDP_MIB_INERRORS, is_udplite); 1060db8dac20SDavid S. Miller kfree_skb(skb); 1061db8dac20SDavid S. Miller return -1; 1062db8dac20SDavid S. Miller } 1063db8dac20SDavid S. Miller 1064db8dac20SDavid S. Miller /* 1065db8dac20SDavid S. Miller * Multicasts and broadcasts go to each listener. 1066db8dac20SDavid S. Miller * 1067db8dac20SDavid S. Miller * Note: called only from the BH handler context, 1068db8dac20SDavid S. Miller * so we don't need to lock the hashes. 1069db8dac20SDavid S. Miller */ 1070db8dac20SDavid S. Miller static int __udp4_lib_mcast_deliver(struct sk_buff *skb, 1071db8dac20SDavid S. Miller struct udphdr *uh, 1072db8dac20SDavid S. Miller __be32 saddr, __be32 daddr, 1073db8dac20SDavid S. Miller struct hlist_head udptable[]) 1074db8dac20SDavid S. Miller { 1075db8dac20SDavid S. Miller struct sock *sk; 1076db8dac20SDavid S. Miller int dif; 1077db8dac20SDavid S. Miller 1078db8dac20SDavid S. Miller read_lock(&udp_hash_lock); 1079db8dac20SDavid S. Miller sk = sk_head(&udptable[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]); 1080db8dac20SDavid S. Miller dif = skb->dev->ifindex; 1081db8dac20SDavid S. Miller sk = udp_v4_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif); 1082db8dac20SDavid S. Miller if (sk) { 1083db8dac20SDavid S. Miller struct sock *sknext = NULL; 1084db8dac20SDavid S. Miller 1085db8dac20SDavid S. Miller do { 1086db8dac20SDavid S. Miller struct sk_buff *skb1 = skb; 1087db8dac20SDavid S. Miller 1088db8dac20SDavid S. Miller sknext = udp_v4_mcast_next(sk_next(sk), uh->dest, daddr, 1089db8dac20SDavid S. Miller uh->source, saddr, dif); 1090db8dac20SDavid S. Miller if (sknext) 1091db8dac20SDavid S. Miller skb1 = skb_clone(skb, GFP_ATOMIC); 1092db8dac20SDavid S. Miller 1093db8dac20SDavid S. Miller if (skb1) { 1094db8dac20SDavid S. Miller int ret = 0; 1095db8dac20SDavid S. Miller 1096db8dac20SDavid S. Miller bh_lock_sock_nested(sk); 1097db8dac20SDavid S. Miller if (!sock_owned_by_user(sk)) 1098db8dac20SDavid S. Miller ret = udp_queue_rcv_skb(sk, skb1); 1099db8dac20SDavid S. Miller else 1100db8dac20SDavid S. Miller sk_add_backlog(sk, skb1); 1101db8dac20SDavid S. Miller bh_unlock_sock(sk); 1102db8dac20SDavid S. Miller 1103db8dac20SDavid S. Miller if (ret > 0) 1104db8dac20SDavid S. Miller /* we should probably re-process instead 1105db8dac20SDavid S. Miller * of dropping packets here. */ 1106db8dac20SDavid S. Miller kfree_skb(skb1); 1107db8dac20SDavid S. Miller } 1108db8dac20SDavid S. Miller sk = sknext; 1109db8dac20SDavid S. Miller } while (sknext); 1110db8dac20SDavid S. Miller } else 1111db8dac20SDavid S. Miller kfree_skb(skb); 1112db8dac20SDavid S. Miller read_unlock(&udp_hash_lock); 1113db8dac20SDavid S. Miller return 0; 1114db8dac20SDavid S. Miller } 1115db8dac20SDavid S. Miller 1116db8dac20SDavid S. Miller /* Initialize UDP checksum. If exited with zero value (success), 1117db8dac20SDavid S. Miller * CHECKSUM_UNNECESSARY means, that no more checks are required. 1118db8dac20SDavid S. Miller * Otherwise, csum completion requires chacksumming packet body, 1119db8dac20SDavid S. Miller * including udp header and folding it to skb->csum. 1120db8dac20SDavid S. Miller */ 1121db8dac20SDavid S. Miller static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh, 1122db8dac20SDavid S. Miller int proto) 1123db8dac20SDavid S. Miller { 1124db8dac20SDavid S. Miller const struct iphdr *iph; 1125db8dac20SDavid S. Miller int err; 1126db8dac20SDavid S. Miller 1127db8dac20SDavid S. Miller UDP_SKB_CB(skb)->partial_cov = 0; 1128db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov = skb->len; 1129db8dac20SDavid S. Miller 1130db8dac20SDavid S. Miller if (proto == IPPROTO_UDPLITE) { 1131db8dac20SDavid S. Miller err = udplite_checksum_init(skb, uh); 1132db8dac20SDavid S. Miller if (err) 1133db8dac20SDavid S. Miller return err; 1134db8dac20SDavid S. Miller } 1135db8dac20SDavid S. Miller 1136db8dac20SDavid S. Miller iph = ip_hdr(skb); 1137db8dac20SDavid S. Miller if (uh->check == 0) { 1138db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_UNNECESSARY; 1139db8dac20SDavid S. Miller } else if (skb->ip_summed == CHECKSUM_COMPLETE) { 1140db8dac20SDavid S. Miller if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len, 1141db8dac20SDavid S. Miller proto, skb->csum)) 1142db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_UNNECESSARY; 1143db8dac20SDavid S. Miller } 1144db8dac20SDavid S. Miller if (!skb_csum_unnecessary(skb)) 1145db8dac20SDavid S. Miller skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, 1146db8dac20SDavid S. Miller skb->len, proto, 0); 1147db8dac20SDavid S. Miller /* Probably, we should checksum udp header (it should be in cache 1148db8dac20SDavid S. Miller * in any case) and data in tiny packets (< rx copybreak). 1149db8dac20SDavid S. Miller */ 1150db8dac20SDavid S. Miller 1151db8dac20SDavid S. Miller return 0; 1152db8dac20SDavid S. Miller } 1153db8dac20SDavid S. Miller 1154db8dac20SDavid S. Miller /* 1155db8dac20SDavid S. Miller * All we need to do is get the socket, and then do a checksum. 1156db8dac20SDavid S. Miller */ 1157db8dac20SDavid S. Miller 1158db8dac20SDavid S. Miller int __udp4_lib_rcv(struct sk_buff *skb, struct hlist_head udptable[], 1159db8dac20SDavid S. Miller int proto) 1160db8dac20SDavid S. Miller { 1161db8dac20SDavid S. Miller struct sock *sk; 1162db8dac20SDavid S. Miller struct udphdr *uh = udp_hdr(skb); 1163db8dac20SDavid S. Miller unsigned short ulen; 1164db8dac20SDavid S. Miller struct rtable *rt = (struct rtable*)skb->dst; 1165db8dac20SDavid S. Miller __be32 saddr = ip_hdr(skb)->saddr; 1166db8dac20SDavid S. Miller __be32 daddr = ip_hdr(skb)->daddr; 1167db8dac20SDavid S. Miller 1168db8dac20SDavid S. Miller /* 1169db8dac20SDavid S. Miller * Validate the packet. 1170db8dac20SDavid S. Miller */ 1171db8dac20SDavid S. Miller if (!pskb_may_pull(skb, sizeof(struct udphdr))) 1172db8dac20SDavid S. Miller goto drop; /* No space for header. */ 1173db8dac20SDavid S. Miller 1174db8dac20SDavid S. Miller ulen = ntohs(uh->len); 1175db8dac20SDavid S. Miller if (ulen > skb->len) 1176db8dac20SDavid S. Miller goto short_packet; 1177db8dac20SDavid S. Miller 1178db8dac20SDavid S. Miller if (proto == IPPROTO_UDP) { 1179db8dac20SDavid S. Miller /* UDP validates ulen. */ 1180db8dac20SDavid S. Miller if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen)) 1181db8dac20SDavid S. Miller goto short_packet; 1182db8dac20SDavid S. Miller uh = udp_hdr(skb); 1183db8dac20SDavid S. Miller } 1184db8dac20SDavid S. Miller 1185db8dac20SDavid S. Miller if (udp4_csum_init(skb, uh, proto)) 1186db8dac20SDavid S. Miller goto csum_error; 1187db8dac20SDavid S. Miller 1188db8dac20SDavid S. Miller if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST)) 1189db8dac20SDavid S. Miller return __udp4_lib_mcast_deliver(skb, uh, saddr, daddr, udptable); 1190db8dac20SDavid S. Miller 1191db8dac20SDavid S. Miller sk = __udp4_lib_lookup(skb->dev->nd_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: 1231db8dac20SDavid 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", 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 */ 1246db8dac20SDavid 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", 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 1264db8dac20SDavid S. Miller int 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 return 0; 1270db8dac20SDavid S. Miller } 1271db8dac20SDavid 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 1352db8dac20SDavid S. Miller int udp_setsockopt(struct sock *sk, int level, int optname, 1353db8dac20SDavid S. Miller char __user *optval, int optlen) 1354db8dac20SDavid S. Miller { 1355db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1356db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1357db8dac20SDavid S. Miller udp_push_pending_frames); 1358db8dac20SDavid S. Miller return ip_setsockopt(sk, level, optname, optval, optlen); 1359db8dac20SDavid S. Miller } 1360db8dac20SDavid S. Miller 1361db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1362db8dac20SDavid S. Miller int compat_udp_setsockopt(struct sock *sk, int level, int optname, 1363db8dac20SDavid S. Miller char __user *optval, int optlen) 1364db8dac20SDavid S. Miller { 1365db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1366db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1367db8dac20SDavid S. Miller udp_push_pending_frames); 1368db8dac20SDavid S. Miller return compat_ip_setsockopt(sk, level, optname, optval, optlen); 1369db8dac20SDavid S. Miller } 1370db8dac20SDavid S. Miller #endif 1371db8dac20SDavid 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 1416db8dac20SDavid S. Miller int udp_getsockopt(struct sock *sk, int level, int optname, 1417db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 1418db8dac20SDavid S. Miller { 1419db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1420db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1421db8dac20SDavid S. Miller return ip_getsockopt(sk, level, optname, optval, optlen); 1422db8dac20SDavid S. Miller } 1423db8dac20SDavid S. Miller 1424db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1425db8dac20SDavid S. Miller int compat_udp_getsockopt(struct sock *sk, int level, int optname, 1426db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 1427db8dac20SDavid S. Miller { 1428db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1429db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1430db8dac20SDavid S. Miller return compat_ip_getsockopt(sk, level, optname, optval, optlen); 1431db8dac20SDavid S. Miller } 1432db8dac20SDavid 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 1477db8dac20SDavid S. Miller DEFINE_PROTO_INUSE(udp) 1478db8dac20SDavid S. Miller 1479db8dac20SDavid S. Miller struct proto udp_prot = { 1480db8dac20SDavid S. Miller .name = "UDP", 1481db8dac20SDavid S. Miller .owner = THIS_MODULE, 1482db8dac20SDavid S. Miller .close = udp_lib_close, 1483db8dac20SDavid S. Miller .connect = ip4_datagram_connect, 1484db8dac20SDavid S. Miller .disconnect = udp_disconnect, 1485db8dac20SDavid S. Miller .ioctl = udp_ioctl, 1486db8dac20SDavid S. Miller .destroy = udp_destroy_sock, 1487db8dac20SDavid S. Miller .setsockopt = udp_setsockopt, 1488db8dac20SDavid S. Miller .getsockopt = udp_getsockopt, 1489db8dac20SDavid S. Miller .sendmsg = udp_sendmsg, 1490db8dac20SDavid S. Miller .recvmsg = udp_recvmsg, 1491db8dac20SDavid S. Miller .sendpage = udp_sendpage, 1492db8dac20SDavid S. Miller .backlog_rcv = udp_queue_rcv_skb, 1493db8dac20SDavid S. Miller .hash = udp_lib_hash, 1494db8dac20SDavid S. Miller .unhash = udp_lib_unhash, 1495db8dac20SDavid S. Miller .get_port = udp_v4_get_port, 1496db8dac20SDavid S. Miller .memory_allocated = &udp_memory_allocated, 1497db8dac20SDavid S. Miller .sysctl_mem = sysctl_udp_mem, 1498db8dac20SDavid S. Miller .sysctl_wmem = &sysctl_udp_wmem_min, 1499db8dac20SDavid S. Miller .sysctl_rmem = &sysctl_udp_rmem_min, 1500db8dac20SDavid S. Miller .obj_size = sizeof(struct udp_sock), 1501db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1502db8dac20SDavid S. Miller .compat_setsockopt = compat_udp_setsockopt, 1503db8dac20SDavid S. Miller .compat_getsockopt = compat_udp_getsockopt, 1504db8dac20SDavid S. Miller #endif 1505db8dac20SDavid S. Miller REF_PROTO_INUSE(udp) 1506db8dac20SDavid 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; 1515*a91275efSDaniel Lezcano struct net *net = state->net; 15161da177e4SLinus Torvalds 15171da177e4SLinus Torvalds for (state->bucket = 0; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) { 15181da177e4SLinus Torvalds struct hlist_node *node; 1519ba4e58ecSGerrit Renker sk_for_each(sk, node, state->hashtable + state->bucket) { 1520*a91275efSDaniel Lezcano if (sk->sk_net != net) 1521*a91275efSDaniel Lezcano continue; 15221da177e4SLinus Torvalds if (sk->sk_family == state->family) 15231da177e4SLinus Torvalds goto found; 15241da177e4SLinus Torvalds } 15251da177e4SLinus Torvalds } 15261da177e4SLinus Torvalds sk = NULL; 15271da177e4SLinus Torvalds found: 15281da177e4SLinus Torvalds return sk; 15291da177e4SLinus Torvalds } 15301da177e4SLinus Torvalds 15311da177e4SLinus Torvalds static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk) 15321da177e4SLinus Torvalds { 15331da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 1534*a91275efSDaniel Lezcano struct net *net = state->net; 15351da177e4SLinus Torvalds 15361da177e4SLinus Torvalds do { 15371da177e4SLinus Torvalds sk = sk_next(sk); 15381da177e4SLinus Torvalds try_again: 15391da177e4SLinus Torvalds ; 1540*a91275efSDaniel Lezcano } while (sk && sk->sk_net != net && sk->sk_family != state->family); 15411da177e4SLinus Torvalds 15421da177e4SLinus Torvalds if (!sk && ++state->bucket < UDP_HTABLE_SIZE) { 1543ba4e58ecSGerrit Renker sk = sk_head(state->hashtable + state->bucket); 15441da177e4SLinus Torvalds goto try_again; 15451da177e4SLinus Torvalds } 15461da177e4SLinus Torvalds return sk; 15471da177e4SLinus Torvalds } 15481da177e4SLinus Torvalds 15491da177e4SLinus Torvalds static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos) 15501da177e4SLinus Torvalds { 15511da177e4SLinus Torvalds struct sock *sk = udp_get_first(seq); 15521da177e4SLinus Torvalds 15531da177e4SLinus Torvalds if (sk) 15541da177e4SLinus Torvalds while (pos && (sk = udp_get_next(seq, sk)) != NULL) 15551da177e4SLinus Torvalds --pos; 15561da177e4SLinus Torvalds return pos ? NULL : sk; 15571da177e4SLinus Torvalds } 15581da177e4SLinus Torvalds 15591da177e4SLinus Torvalds static void *udp_seq_start(struct seq_file *seq, loff_t *pos) 15609a429c49SEric Dumazet __acquires(udp_hash_lock) 15611da177e4SLinus Torvalds { 15621da177e4SLinus Torvalds read_lock(&udp_hash_lock); 15631da177e4SLinus Torvalds return *pos ? udp_get_idx(seq, *pos-1) : (void *)1; 15641da177e4SLinus Torvalds } 15651da177e4SLinus Torvalds 15661da177e4SLinus Torvalds static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos) 15671da177e4SLinus Torvalds { 15681da177e4SLinus Torvalds struct sock *sk; 15691da177e4SLinus Torvalds 15701da177e4SLinus Torvalds if (v == (void *)1) 15711da177e4SLinus Torvalds sk = udp_get_idx(seq, 0); 15721da177e4SLinus Torvalds else 15731da177e4SLinus Torvalds sk = udp_get_next(seq, v); 15741da177e4SLinus Torvalds 15751da177e4SLinus Torvalds ++*pos; 15761da177e4SLinus Torvalds return sk; 15771da177e4SLinus Torvalds } 15781da177e4SLinus Torvalds 15791da177e4SLinus Torvalds static void udp_seq_stop(struct seq_file *seq, void *v) 15809a429c49SEric Dumazet __releases(udp_hash_lock) 15811da177e4SLinus Torvalds { 15821da177e4SLinus Torvalds read_unlock(&udp_hash_lock); 15831da177e4SLinus Torvalds } 15841da177e4SLinus Torvalds 15851da177e4SLinus Torvalds static int udp_seq_open(struct inode *inode, struct file *file) 15861da177e4SLinus Torvalds { 15871da177e4SLinus Torvalds struct udp_seq_afinfo *afinfo = PDE(inode)->data; 15881da177e4SLinus Torvalds struct seq_file *seq; 1589*a91275efSDaniel Lezcano struct net *net; 15901da177e4SLinus Torvalds int rc = -ENOMEM; 15910da974f4SPanagiotis Issaris struct udp_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL); 15921da177e4SLinus Torvalds 15931da177e4SLinus Torvalds if (!s) 15941da177e4SLinus Torvalds goto out; 1595*a91275efSDaniel Lezcano 1596*a91275efSDaniel Lezcano rc = -ENXIO; 1597*a91275efSDaniel Lezcano net = get_proc_net(inode); 1598*a91275efSDaniel Lezcano if (!net) 1599*a91275efSDaniel Lezcano goto out_kfree; 1600*a91275efSDaniel Lezcano 16011da177e4SLinus Torvalds s->family = afinfo->family; 1602ba4e58ecSGerrit Renker s->hashtable = afinfo->hashtable; 16031da177e4SLinus Torvalds s->seq_ops.start = udp_seq_start; 16041da177e4SLinus Torvalds s->seq_ops.next = udp_seq_next; 16051da177e4SLinus Torvalds s->seq_ops.show = afinfo->seq_show; 16061da177e4SLinus Torvalds s->seq_ops.stop = udp_seq_stop; 1607*a91275efSDaniel Lezcano s->net = net; 16081da177e4SLinus Torvalds 16091da177e4SLinus Torvalds rc = seq_open(file, &s->seq_ops); 16101da177e4SLinus Torvalds if (rc) 1611*a91275efSDaniel Lezcano goto out_put_net; 16121da177e4SLinus Torvalds 16131da177e4SLinus Torvalds seq = file->private_data; 16141da177e4SLinus Torvalds seq->private = s; 16151da177e4SLinus Torvalds out: 16161da177e4SLinus Torvalds return rc; 1617*a91275efSDaniel Lezcano out_put_net: 1618*a91275efSDaniel Lezcano put_net(net); 16191da177e4SLinus Torvalds out_kfree: 16201da177e4SLinus Torvalds kfree(s); 16211da177e4SLinus Torvalds goto out; 16221da177e4SLinus Torvalds } 16231da177e4SLinus Torvalds 1624*a91275efSDaniel Lezcano static int udp_seq_release(struct inode *inode, struct file *file) 1625*a91275efSDaniel Lezcano { 1626*a91275efSDaniel Lezcano struct seq_file *seq = file->private_data; 1627*a91275efSDaniel Lezcano struct udp_iter_state *s = seq->private; 1628*a91275efSDaniel Lezcano 1629*a91275efSDaniel Lezcano put_net(s->net); 1630*a91275efSDaniel Lezcano seq_release_private(inode, file); 1631*a91275efSDaniel Lezcano return 0; 1632*a91275efSDaniel Lezcano } 1633*a91275efSDaniel Lezcano 16341da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 16351da177e4SLinus Torvalds int udp_proc_register(struct udp_seq_afinfo *afinfo) 16361da177e4SLinus Torvalds { 16371da177e4SLinus Torvalds struct proc_dir_entry *p; 16381da177e4SLinus Torvalds int rc = 0; 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds if (!afinfo) 16411da177e4SLinus Torvalds return -EINVAL; 16421da177e4SLinus Torvalds afinfo->seq_fops->owner = afinfo->owner; 16431da177e4SLinus Torvalds afinfo->seq_fops->open = udp_seq_open; 16441da177e4SLinus Torvalds afinfo->seq_fops->read = seq_read; 16451da177e4SLinus Torvalds afinfo->seq_fops->llseek = seq_lseek; 1646*a91275efSDaniel Lezcano afinfo->seq_fops->release = udp_seq_release; 16471da177e4SLinus Torvalds 1648457c4cbcSEric W. Biederman p = proc_net_fops_create(&init_net, afinfo->name, S_IRUGO, afinfo->seq_fops); 16491da177e4SLinus Torvalds if (p) 16501da177e4SLinus Torvalds p->data = afinfo; 16511da177e4SLinus Torvalds else 16521da177e4SLinus Torvalds rc = -ENOMEM; 16531da177e4SLinus Torvalds return rc; 16541da177e4SLinus Torvalds } 16551da177e4SLinus Torvalds 16561da177e4SLinus Torvalds void udp_proc_unregister(struct udp_seq_afinfo *afinfo) 16571da177e4SLinus Torvalds { 16581da177e4SLinus Torvalds if (!afinfo) 16591da177e4SLinus Torvalds return; 1660457c4cbcSEric W. Biederman proc_net_remove(&init_net, afinfo->name); 16611da177e4SLinus Torvalds memset(afinfo->seq_fops, 0, sizeof(*afinfo->seq_fops)); 16621da177e4SLinus Torvalds } 1663db8dac20SDavid S. Miller 1664db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 1665db8dac20SDavid S. Miller static void udp4_format_sock(struct sock *sp, char *tmpbuf, int bucket) 1666db8dac20SDavid S. Miller { 1667db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sp); 1668db8dac20SDavid S. Miller __be32 dest = inet->daddr; 1669db8dac20SDavid S. Miller __be32 src = inet->rcv_saddr; 1670db8dac20SDavid S. Miller __u16 destp = ntohs(inet->dport); 1671db8dac20SDavid S. Miller __u16 srcp = ntohs(inet->sport); 1672db8dac20SDavid S. Miller 1673db8dac20SDavid S. Miller sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X" 1674db8dac20SDavid S. Miller " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p", 1675db8dac20SDavid S. Miller bucket, src, srcp, dest, destp, sp->sk_state, 1676db8dac20SDavid S. Miller atomic_read(&sp->sk_wmem_alloc), 1677db8dac20SDavid S. Miller atomic_read(&sp->sk_rmem_alloc), 1678db8dac20SDavid S. Miller 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp), 1679db8dac20SDavid S. Miller atomic_read(&sp->sk_refcnt), sp); 1680db8dac20SDavid S. Miller } 1681db8dac20SDavid S. Miller 1682db8dac20SDavid S. Miller int udp4_seq_show(struct seq_file *seq, void *v) 1683db8dac20SDavid S. Miller { 1684db8dac20SDavid S. Miller if (v == SEQ_START_TOKEN) 1685db8dac20SDavid S. Miller seq_printf(seq, "%-127s\n", 1686db8dac20SDavid S. Miller " sl local_address rem_address st tx_queue " 1687db8dac20SDavid S. Miller "rx_queue tr tm->when retrnsmt uid timeout " 1688db8dac20SDavid S. Miller "inode"); 1689db8dac20SDavid S. Miller else { 1690db8dac20SDavid S. Miller char tmpbuf[129]; 1691db8dac20SDavid S. Miller struct udp_iter_state *state = seq->private; 1692db8dac20SDavid S. Miller 1693db8dac20SDavid S. Miller udp4_format_sock(v, tmpbuf, state->bucket); 1694db8dac20SDavid S. Miller seq_printf(seq, "%-127s\n", tmpbuf); 1695db8dac20SDavid S. Miller } 1696db8dac20SDavid S. Miller return 0; 1697db8dac20SDavid S. Miller } 1698db8dac20SDavid S. Miller 1699db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 1700db8dac20SDavid S. Miller static struct file_operations udp4_seq_fops; 1701db8dac20SDavid S. Miller static struct udp_seq_afinfo udp4_seq_afinfo = { 1702db8dac20SDavid S. Miller .owner = THIS_MODULE, 1703db8dac20SDavid S. Miller .name = "udp", 1704db8dac20SDavid S. Miller .family = AF_INET, 1705db8dac20SDavid S. Miller .hashtable = udp_hash, 1706db8dac20SDavid S. Miller .seq_show = udp4_seq_show, 1707db8dac20SDavid S. Miller .seq_fops = &udp4_seq_fops, 1708db8dac20SDavid S. Miller }; 1709db8dac20SDavid S. Miller 1710db8dac20SDavid S. Miller int __init udp4_proc_init(void) 1711db8dac20SDavid S. Miller { 1712db8dac20SDavid S. Miller return udp_proc_register(&udp4_seq_afinfo); 1713db8dac20SDavid S. Miller } 1714db8dac20SDavid S. Miller 1715db8dac20SDavid S. Miller void udp4_proc_exit(void) 1716db8dac20SDavid S. Miller { 1717db8dac20SDavid S. Miller udp_proc_unregister(&udp4_seq_afinfo); 1718db8dac20SDavid S. Miller } 17191da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 17201da177e4SLinus Torvalds 172195766fffSHideo Aoki void __init udp_init(void) 172295766fffSHideo Aoki { 172395766fffSHideo Aoki unsigned long limit; 172495766fffSHideo Aoki 172595766fffSHideo Aoki /* Set the pressure threshold up by the same strategy of TCP. It is a 172695766fffSHideo Aoki * fraction of global memory that is up to 1/2 at 256 MB, decreasing 172795766fffSHideo Aoki * toward zero with the amount of memory, with a floor of 128 pages. 172895766fffSHideo Aoki */ 172995766fffSHideo Aoki limit = min(nr_all_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT); 173095766fffSHideo Aoki limit = (limit * (nr_all_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11); 173195766fffSHideo Aoki limit = max(limit, 128UL); 173295766fffSHideo Aoki sysctl_udp_mem[0] = limit / 4 * 3; 173395766fffSHideo Aoki sysctl_udp_mem[1] = limit; 173495766fffSHideo Aoki sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2; 173595766fffSHideo Aoki 173695766fffSHideo Aoki sysctl_udp_rmem_min = SK_MEM_QUANTUM; 173795766fffSHideo Aoki sysctl_udp_wmem_min = SK_MEM_QUANTUM; 173895766fffSHideo Aoki } 173995766fffSHideo Aoki 17401da177e4SLinus Torvalds EXPORT_SYMBOL(udp_disconnect); 17411da177e4SLinus Torvalds EXPORT_SYMBOL(udp_hash); 17421da177e4SLinus Torvalds EXPORT_SYMBOL(udp_hash_lock); 17431da177e4SLinus Torvalds EXPORT_SYMBOL(udp_ioctl); 174425030a7fSGerrit Renker EXPORT_SYMBOL(udp_get_port); 1745db8dac20SDavid S. Miller EXPORT_SYMBOL(udp_prot); 1746db8dac20SDavid S. Miller EXPORT_SYMBOL(udp_sendmsg); 17474c0a6cb0SGerrit Renker EXPORT_SYMBOL(udp_lib_getsockopt); 17484c0a6cb0SGerrit Renker EXPORT_SYMBOL(udp_lib_setsockopt); 17491da177e4SLinus Torvalds EXPORT_SYMBOL(udp_poll); 17501da177e4SLinus Torvalds 17511da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 17521da177e4SLinus Torvalds EXPORT_SYMBOL(udp_proc_register); 17531da177e4SLinus Torvalds EXPORT_SYMBOL(udp_proc_unregister); 17541da177e4SLinus Torvalds #endif 1755