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)]) 140878628fbSYOSHIFUJI Hideaki if (net_eq(sock_net(sk), net) && sk->sk_hash == num) 141fc038410SDavid S. Miller return 1; 14225030a7fSGerrit Renker return 0; 14325030a7fSGerrit Renker } 14425030a7fSGerrit Renker 14525030a7fSGerrit Renker /** 1466ba5a3c5SPavel Emelyanov * 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 150df2bc459SDavid S. Miller * @saddr_comp: AF-dependent comparison of bound local IP addresses 15125030a7fSGerrit Renker */ 1526ba5a3c5SPavel Emelyanov int udp_lib_get_port(struct sock *sk, unsigned short snum, 153df2bc459SDavid S. Miller int (*saddr_comp)(const struct sock *sk1, 154df2bc459SDavid S. Miller const struct sock *sk2 ) ) 1551da177e4SLinus Torvalds { 1566ba5a3c5SPavel Emelyanov struct hlist_head *udptable = sk->sk_prot->h.udp_hash; 1571da177e4SLinus Torvalds struct hlist_node *node; 15825030a7fSGerrit Renker struct hlist_head *head; 1591da177e4SLinus Torvalds struct sock *sk2; 16025030a7fSGerrit Renker int error = 1; 1613b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds write_lock_bh(&udp_hash_lock); 1641da177e4SLinus Torvalds 16532c1da70SStephen Hemminger if (!snum) { 166a25de534SAnton Arapov int i, low, high, remaining; 16732c1da70SStephen Hemminger unsigned rover, best, best_size_so_far; 1681da177e4SLinus Torvalds 169227b60f5SStephen Hemminger inet_get_local_port_range(&low, &high); 170a25de534SAnton Arapov remaining = (high - low) + 1; 171227b60f5SStephen Hemminger 17232c1da70SStephen Hemminger best_size_so_far = UINT_MAX; 173a25de534SAnton Arapov best = rover = net_random() % remaining + low; 17432c1da70SStephen Hemminger 17532c1da70SStephen Hemminger /* 1st pass: look for empty (or shortest) hash chain */ 17632c1da70SStephen Hemminger for (i = 0; i < UDP_HTABLE_SIZE; i++) { 17732c1da70SStephen Hemminger int size = 0; 17832c1da70SStephen Hemminger 17932c1da70SStephen Hemminger head = &udptable[rover & (UDP_HTABLE_SIZE - 1)]; 18032c1da70SStephen Hemminger if (hlist_empty(head)) 1811da177e4SLinus Torvalds goto gotit; 18232c1da70SStephen Hemminger 1835c668704SDavid S. Miller sk_for_each(sk2, node, head) { 1845c668704SDavid S. Miller if (++size >= best_size_so_far) 1855c668704SDavid S. Miller goto next; 1865c668704SDavid S. Miller } 1871da177e4SLinus Torvalds best_size_so_far = size; 18832c1da70SStephen Hemminger best = rover; 1895c668704SDavid S. Miller next: 19032c1da70SStephen Hemminger /* fold back if end of range */ 19132c1da70SStephen Hemminger if (++rover > high) 19232c1da70SStephen Hemminger rover = low + ((rover - low) 19332c1da70SStephen Hemminger & (UDP_HTABLE_SIZE - 1)); 19432c1da70SStephen Hemminger 19532c1da70SStephen Hemminger 1961da177e4SLinus Torvalds } 19732c1da70SStephen Hemminger 19832c1da70SStephen Hemminger /* 2nd pass: find hole in shortest hash chain */ 19932c1da70SStephen Hemminger rover = best; 20032c1da70SStephen Hemminger for (i = 0; i < (1 << 16) / UDP_HTABLE_SIZE; i++) { 201fa4d3c62SPavel Emelyanov if (! __udp_lib_lport_inuse(net, rover, udptable)) 20232c1da70SStephen Hemminger goto gotit; 20332c1da70SStephen Hemminger rover += UDP_HTABLE_SIZE; 20432c1da70SStephen Hemminger if (rover > high) 20532c1da70SStephen Hemminger rover = low + ((rover - low) 20632c1da70SStephen Hemminger & (UDP_HTABLE_SIZE - 1)); 2071da177e4SLinus Torvalds } 20832c1da70SStephen Hemminger 20932c1da70SStephen Hemminger 21032c1da70SStephen Hemminger /* All ports in use! */ 2111da177e4SLinus Torvalds goto fail; 21232c1da70SStephen Hemminger 2131da177e4SLinus Torvalds gotit: 21432c1da70SStephen Hemminger snum = rover; 2151da177e4SLinus Torvalds } else { 216df2bc459SDavid S. Miller head = &udptable[snum & (UDP_HTABLE_SIZE - 1)]; 2171da177e4SLinus Torvalds 21825030a7fSGerrit Renker sk_for_each(sk2, node, head) 219df2bc459SDavid S. Miller if (sk2->sk_hash == snum && 2201da177e4SLinus Torvalds sk2 != sk && 221878628fbSYOSHIFUJI Hideaki net_eq(sock_net(sk2), net) && 22225030a7fSGerrit Renker (!sk2->sk_reuse || !sk->sk_reuse) && 223df2bc459SDavid S. Miller (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if 224df2bc459SDavid S. Miller || sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 225df2bc459SDavid S. Miller (*saddr_comp)(sk, sk2) ) 2261da177e4SLinus Torvalds goto fail; 2271da177e4SLinus Torvalds } 22832c1da70SStephen Hemminger 22925030a7fSGerrit Renker inet_sk(sk)->num = snum; 230df2bc459SDavid S. Miller sk->sk_hash = snum; 2311da177e4SLinus Torvalds if (sk_unhashed(sk)) { 232df2bc459SDavid S. Miller head = &udptable[snum & (UDP_HTABLE_SIZE - 1)]; 23325030a7fSGerrit Renker sk_add_node(sk, head); 23465f76517SEric Dumazet sock_prot_inuse_add(sk->sk_prot, 1); 2351da177e4SLinus Torvalds } 23625030a7fSGerrit Renker error = 0; 2371da177e4SLinus Torvalds fail: 2381da177e4SLinus Torvalds write_unlock_bh(&udp_hash_lock); 23925030a7fSGerrit Renker return error; 2401da177e4SLinus Torvalds } 2411da177e4SLinus Torvalds 2426ba5a3c5SPavel Emelyanov static int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2) 243db8dac20SDavid S. Miller { 244db8dac20SDavid S. Miller struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2); 245db8dac20SDavid S. Miller 246db8dac20SDavid S. Miller return ( !ipv6_only_sock(sk2) && 247db8dac20SDavid S. Miller (!inet1->rcv_saddr || !inet2->rcv_saddr || 248db8dac20SDavid S. Miller inet1->rcv_saddr == inet2->rcv_saddr )); 249db8dac20SDavid S. Miller } 250db8dac20SDavid S. Miller 2516ba5a3c5SPavel Emelyanov int udp_v4_get_port(struct sock *sk, unsigned short snum) 252db8dac20SDavid S. Miller { 2536ba5a3c5SPavel Emelyanov return udp_lib_get_port(sk, snum, ipv4_rcv_saddr_equal); 254db8dac20SDavid S. Miller } 255db8dac20SDavid S. Miller 256db8dac20SDavid S. Miller /* UDP is nearly always wildcards out the wazoo, it makes no sense to try 257db8dac20SDavid S. Miller * harder than this. -DaveM 258db8dac20SDavid S. Miller */ 259db8dac20SDavid S. Miller static struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, 260db8dac20SDavid S. Miller __be16 sport, __be32 daddr, __be16 dport, 261db8dac20SDavid S. Miller int dif, struct hlist_head udptable[]) 262db8dac20SDavid S. Miller { 263db8dac20SDavid S. Miller struct sock *sk, *result = NULL; 264db8dac20SDavid S. Miller struct hlist_node *node; 265db8dac20SDavid S. Miller unsigned short hnum = ntohs(dport); 266db8dac20SDavid S. Miller int badness = -1; 267db8dac20SDavid S. Miller 268db8dac20SDavid S. Miller read_lock(&udp_hash_lock); 269db8dac20SDavid S. Miller sk_for_each(sk, node, &udptable[hnum & (UDP_HTABLE_SIZE - 1)]) { 270db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 271db8dac20SDavid S. Miller 272878628fbSYOSHIFUJI Hideaki if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum && 273db8dac20SDavid S. Miller !ipv6_only_sock(sk)) { 274db8dac20SDavid S. Miller int score = (sk->sk_family == PF_INET ? 1 : 0); 275db8dac20SDavid S. Miller if (inet->rcv_saddr) { 276db8dac20SDavid S. Miller if (inet->rcv_saddr != daddr) 277db8dac20SDavid S. Miller continue; 278db8dac20SDavid S. Miller score+=2; 279db8dac20SDavid S. Miller } 280db8dac20SDavid S. Miller if (inet->daddr) { 281db8dac20SDavid S. Miller if (inet->daddr != saddr) 282db8dac20SDavid S. Miller continue; 283db8dac20SDavid S. Miller score+=2; 284db8dac20SDavid S. Miller } 285db8dac20SDavid S. Miller if (inet->dport) { 286db8dac20SDavid S. Miller if (inet->dport != sport) 287db8dac20SDavid S. Miller continue; 288db8dac20SDavid S. Miller score+=2; 289db8dac20SDavid S. Miller } 290db8dac20SDavid S. Miller if (sk->sk_bound_dev_if) { 291db8dac20SDavid S. Miller if (sk->sk_bound_dev_if != dif) 292db8dac20SDavid S. Miller continue; 293db8dac20SDavid S. Miller score+=2; 294db8dac20SDavid S. Miller } 295db8dac20SDavid S. Miller if (score == 9) { 296db8dac20SDavid S. Miller result = sk; 297db8dac20SDavid S. Miller break; 298db8dac20SDavid S. Miller } else if (score > badness) { 299db8dac20SDavid S. Miller result = sk; 300db8dac20SDavid S. Miller badness = score; 301db8dac20SDavid S. Miller } 302db8dac20SDavid S. Miller } 303db8dac20SDavid S. Miller } 304db8dac20SDavid S. Miller if (result) 305db8dac20SDavid S. Miller sock_hold(result); 306db8dac20SDavid S. Miller read_unlock(&udp_hash_lock); 307db8dac20SDavid S. Miller return result; 308db8dac20SDavid S. Miller } 309db8dac20SDavid S. Miller 310db8dac20SDavid S. Miller static inline struct sock *udp_v4_mcast_next(struct sock *sk, 311db8dac20SDavid S. Miller __be16 loc_port, __be32 loc_addr, 312db8dac20SDavid S. Miller __be16 rmt_port, __be32 rmt_addr, 313db8dac20SDavid S. Miller int dif) 314db8dac20SDavid S. Miller { 315db8dac20SDavid S. Miller struct hlist_node *node; 316db8dac20SDavid S. Miller struct sock *s = sk; 317db8dac20SDavid S. Miller unsigned short hnum = ntohs(loc_port); 318db8dac20SDavid S. Miller 319db8dac20SDavid S. Miller sk_for_each_from(s, node) { 320db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(s); 321db8dac20SDavid S. Miller 322db8dac20SDavid S. Miller if (s->sk_hash != hnum || 323db8dac20SDavid S. Miller (inet->daddr && inet->daddr != rmt_addr) || 324db8dac20SDavid S. Miller (inet->dport != rmt_port && inet->dport) || 325db8dac20SDavid S. Miller (inet->rcv_saddr && inet->rcv_saddr != loc_addr) || 326db8dac20SDavid S. Miller ipv6_only_sock(s) || 327db8dac20SDavid S. Miller (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)) 328db8dac20SDavid S. Miller continue; 329db8dac20SDavid S. Miller if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif)) 330db8dac20SDavid S. Miller continue; 331db8dac20SDavid S. Miller goto found; 332db8dac20SDavid S. Miller } 333db8dac20SDavid S. Miller s = NULL; 334db8dac20SDavid S. Miller found: 335db8dac20SDavid S. Miller return s; 336db8dac20SDavid S. Miller } 337db8dac20SDavid S. Miller 338db8dac20SDavid S. Miller /* 339db8dac20SDavid S. Miller * This routine is called by the ICMP module when it gets some 340db8dac20SDavid S. Miller * sort of error condition. If err < 0 then the socket should 341db8dac20SDavid S. Miller * be closed and the error returned to the user. If err > 0 342db8dac20SDavid S. Miller * it's just the icmp type << 8 | icmp code. 343db8dac20SDavid S. Miller * Header points to the ip header of the error packet. We move 344db8dac20SDavid S. Miller * on past this. Then (as it used to claim before adjustment) 345db8dac20SDavid S. Miller * header points to the first 8 bytes of the udp header. We need 346db8dac20SDavid S. Miller * to find the appropriate port. 347db8dac20SDavid S. Miller */ 348db8dac20SDavid S. Miller 349db8dac20SDavid S. Miller void __udp4_lib_err(struct sk_buff *skb, u32 info, struct hlist_head udptable[]) 350db8dac20SDavid S. Miller { 351db8dac20SDavid S. Miller struct inet_sock *inet; 352db8dac20SDavid S. Miller struct iphdr *iph = (struct iphdr*)skb->data; 353db8dac20SDavid S. Miller struct udphdr *uh = (struct udphdr*)(skb->data+(iph->ihl<<2)); 354db8dac20SDavid S. Miller const int type = icmp_hdr(skb)->type; 355db8dac20SDavid S. Miller const int code = icmp_hdr(skb)->code; 356db8dac20SDavid S. Miller struct sock *sk; 357db8dac20SDavid S. Miller int harderr; 358db8dac20SDavid S. Miller int err; 359db8dac20SDavid S. Miller 360c346dca1SYOSHIFUJI Hideaki sk = __udp4_lib_lookup(dev_net(skb->dev), iph->daddr, uh->dest, 361db8dac20SDavid S. Miller iph->saddr, uh->source, skb->dev->ifindex, udptable); 362db8dac20SDavid S. Miller if (sk == NULL) { 363db8dac20SDavid S. Miller ICMP_INC_STATS_BH(ICMP_MIB_INERRORS); 364db8dac20SDavid S. Miller return; /* No socket for error */ 365db8dac20SDavid S. Miller } 366db8dac20SDavid S. Miller 367db8dac20SDavid S. Miller err = 0; 368db8dac20SDavid S. Miller harderr = 0; 369db8dac20SDavid S. Miller inet = inet_sk(sk); 370db8dac20SDavid S. Miller 371db8dac20SDavid S. Miller switch (type) { 372db8dac20SDavid S. Miller default: 373db8dac20SDavid S. Miller case ICMP_TIME_EXCEEDED: 374db8dac20SDavid S. Miller err = EHOSTUNREACH; 375db8dac20SDavid S. Miller break; 376db8dac20SDavid S. Miller case ICMP_SOURCE_QUENCH: 377db8dac20SDavid S. Miller goto out; 378db8dac20SDavid S. Miller case ICMP_PARAMETERPROB: 379db8dac20SDavid S. Miller err = EPROTO; 380db8dac20SDavid S. Miller harderr = 1; 381db8dac20SDavid S. Miller break; 382db8dac20SDavid S. Miller case ICMP_DEST_UNREACH: 383db8dac20SDavid S. Miller if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */ 384db8dac20SDavid S. Miller if (inet->pmtudisc != IP_PMTUDISC_DONT) { 385db8dac20SDavid S. Miller err = EMSGSIZE; 386db8dac20SDavid S. Miller harderr = 1; 387db8dac20SDavid S. Miller break; 388db8dac20SDavid S. Miller } 389db8dac20SDavid S. Miller goto out; 390db8dac20SDavid S. Miller } 391db8dac20SDavid S. Miller err = EHOSTUNREACH; 392db8dac20SDavid S. Miller if (code <= NR_ICMP_UNREACH) { 393db8dac20SDavid S. Miller harderr = icmp_err_convert[code].fatal; 394db8dac20SDavid S. Miller err = icmp_err_convert[code].errno; 395db8dac20SDavid S. Miller } 396db8dac20SDavid S. Miller break; 397db8dac20SDavid S. Miller } 398db8dac20SDavid S. Miller 399db8dac20SDavid S. Miller /* 400db8dac20SDavid S. Miller * RFC1122: OK. Passes ICMP errors back to application, as per 401db8dac20SDavid S. Miller * 4.1.3.3. 402db8dac20SDavid S. Miller */ 403db8dac20SDavid S. Miller if (!inet->recverr) { 404db8dac20SDavid S. Miller if (!harderr || sk->sk_state != TCP_ESTABLISHED) 405db8dac20SDavid S. Miller goto out; 406db8dac20SDavid S. Miller } else { 407db8dac20SDavid S. Miller ip_icmp_error(sk, skb, err, uh->dest, info, (u8*)(uh+1)); 408db8dac20SDavid S. Miller } 409db8dac20SDavid S. Miller sk->sk_err = err; 410db8dac20SDavid S. Miller sk->sk_error_report(sk); 411db8dac20SDavid S. Miller out: 412db8dac20SDavid S. Miller sock_put(sk); 413db8dac20SDavid S. Miller } 414db8dac20SDavid S. Miller 415db8dac20SDavid S. Miller void udp_err(struct sk_buff *skb, u32 info) 416db8dac20SDavid S. Miller { 417db8dac20SDavid S. Miller __udp4_lib_err(skb, info, udp_hash); 418db8dac20SDavid S. Miller } 419db8dac20SDavid S. Miller 420db8dac20SDavid S. Miller /* 421db8dac20SDavid S. Miller * Throw away all pending data and cancel the corking. Socket is locked. 422db8dac20SDavid S. Miller */ 423db8dac20SDavid S. Miller static void udp_flush_pending_frames(struct sock *sk) 424db8dac20SDavid S. Miller { 425db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 426db8dac20SDavid S. Miller 427db8dac20SDavid S. Miller if (up->pending) { 428db8dac20SDavid S. Miller up->len = 0; 429db8dac20SDavid S. Miller up->pending = 0; 430db8dac20SDavid S. Miller ip_flush_pending_frames(sk); 431db8dac20SDavid S. Miller } 432db8dac20SDavid S. Miller } 433db8dac20SDavid S. Miller 434db8dac20SDavid S. Miller /** 435db8dac20SDavid S. Miller * udp4_hwcsum_outgoing - handle outgoing HW checksumming 436db8dac20SDavid S. Miller * @sk: socket we are sending on 437db8dac20SDavid S. Miller * @skb: sk_buff containing the filled-in UDP header 438db8dac20SDavid S. Miller * (checksum field must be zeroed out) 439db8dac20SDavid S. Miller */ 440db8dac20SDavid S. Miller static void udp4_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb, 441db8dac20SDavid S. Miller __be32 src, __be32 dst, int len ) 442db8dac20SDavid S. Miller { 443db8dac20SDavid S. Miller unsigned int offset; 444db8dac20SDavid S. Miller struct udphdr *uh = udp_hdr(skb); 445db8dac20SDavid S. Miller __wsum csum = 0; 446db8dac20SDavid S. Miller 447db8dac20SDavid S. Miller if (skb_queue_len(&sk->sk_write_queue) == 1) { 448db8dac20SDavid S. Miller /* 449db8dac20SDavid S. Miller * Only one fragment on the socket. 450db8dac20SDavid S. Miller */ 451db8dac20SDavid S. Miller skb->csum_start = skb_transport_header(skb) - skb->head; 452db8dac20SDavid S. Miller skb->csum_offset = offsetof(struct udphdr, check); 453db8dac20SDavid S. Miller uh->check = ~csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, 0); 454db8dac20SDavid S. Miller } else { 455db8dac20SDavid S. Miller /* 456db8dac20SDavid S. Miller * HW-checksum won't work as there are two or more 457db8dac20SDavid S. Miller * fragments on the socket so that all csums of sk_buffs 458db8dac20SDavid S. Miller * should be together 459db8dac20SDavid S. Miller */ 460db8dac20SDavid S. Miller offset = skb_transport_offset(skb); 461db8dac20SDavid S. Miller skb->csum = skb_checksum(skb, offset, skb->len - offset, 0); 462db8dac20SDavid S. Miller 463db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_NONE; 464db8dac20SDavid S. Miller 465db8dac20SDavid S. Miller skb_queue_walk(&sk->sk_write_queue, skb) { 466db8dac20SDavid S. Miller csum = csum_add(csum, skb->csum); 467db8dac20SDavid S. Miller } 468db8dac20SDavid S. Miller 469db8dac20SDavid S. Miller uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum); 470db8dac20SDavid S. Miller if (uh->check == 0) 471db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 472db8dac20SDavid S. Miller } 473db8dac20SDavid S. Miller } 474db8dac20SDavid S. Miller 475db8dac20SDavid S. Miller /* 476db8dac20SDavid S. Miller * Push out all pending data as one UDP datagram. Socket is locked. 477db8dac20SDavid S. Miller */ 478db8dac20SDavid S. Miller static int udp_push_pending_frames(struct sock *sk) 479db8dac20SDavid S. Miller { 480db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 481db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 482db8dac20SDavid S. Miller struct flowi *fl = &inet->cork.fl; 483db8dac20SDavid S. Miller struct sk_buff *skb; 484db8dac20SDavid S. Miller struct udphdr *uh; 485db8dac20SDavid S. Miller int err = 0; 486db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 487db8dac20SDavid S. Miller __wsum csum = 0; 488db8dac20SDavid S. Miller 489db8dac20SDavid S. Miller /* Grab the skbuff where UDP header space exists. */ 490db8dac20SDavid S. Miller if ((skb = skb_peek(&sk->sk_write_queue)) == NULL) 491db8dac20SDavid S. Miller goto out; 492db8dac20SDavid S. Miller 493db8dac20SDavid S. Miller /* 494db8dac20SDavid S. Miller * Create a UDP header 495db8dac20SDavid S. Miller */ 496db8dac20SDavid S. Miller uh = udp_hdr(skb); 497db8dac20SDavid S. Miller uh->source = fl->fl_ip_sport; 498db8dac20SDavid S. Miller uh->dest = fl->fl_ip_dport; 499db8dac20SDavid S. Miller uh->len = htons(up->len); 500db8dac20SDavid S. Miller uh->check = 0; 501db8dac20SDavid S. Miller 502db8dac20SDavid S. Miller if (is_udplite) /* UDP-Lite */ 503db8dac20SDavid S. Miller csum = udplite_csum_outgoing(sk, skb); 504db8dac20SDavid S. Miller 505db8dac20SDavid S. Miller else if (sk->sk_no_check == UDP_CSUM_NOXMIT) { /* UDP csum disabled */ 506db8dac20SDavid S. Miller 507db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_NONE; 508db8dac20SDavid S. Miller goto send; 509db8dac20SDavid S. Miller 510db8dac20SDavid S. Miller } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 511db8dac20SDavid S. Miller 512db8dac20SDavid S. Miller udp4_hwcsum_outgoing(sk, skb, fl->fl4_src,fl->fl4_dst, up->len); 513db8dac20SDavid S. Miller goto send; 514db8dac20SDavid S. Miller 515db8dac20SDavid S. Miller } else /* `normal' UDP */ 516db8dac20SDavid S. Miller csum = udp_csum_outgoing(sk, skb); 517db8dac20SDavid S. Miller 518db8dac20SDavid S. Miller /* add protocol-dependent pseudo-header */ 519db8dac20SDavid S. Miller uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst, up->len, 520db8dac20SDavid S. Miller sk->sk_protocol, csum ); 521db8dac20SDavid S. Miller if (uh->check == 0) 522db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 523db8dac20SDavid S. Miller 524db8dac20SDavid S. Miller send: 525db8dac20SDavid S. Miller err = ip_push_pending_frames(sk); 526db8dac20SDavid S. Miller out: 527db8dac20SDavid S. Miller up->len = 0; 528db8dac20SDavid S. Miller up->pending = 0; 529db8dac20SDavid S. Miller if (!err) 530db8dac20SDavid S. Miller UDP_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS, is_udplite); 531db8dac20SDavid S. Miller return err; 532db8dac20SDavid S. Miller } 533db8dac20SDavid S. Miller 534db8dac20SDavid S. Miller int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 535db8dac20SDavid S. Miller size_t len) 536db8dac20SDavid S. Miller { 537db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 538db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 539db8dac20SDavid S. Miller int ulen = len; 540db8dac20SDavid S. Miller struct ipcm_cookie ipc; 541db8dac20SDavid S. Miller struct rtable *rt = NULL; 542db8dac20SDavid S. Miller int free = 0; 543db8dac20SDavid S. Miller int connected = 0; 544db8dac20SDavid S. Miller __be32 daddr, faddr, saddr; 545db8dac20SDavid S. Miller __be16 dport; 546db8dac20SDavid S. Miller u8 tos; 547db8dac20SDavid S. Miller int err, is_udplite = IS_UDPLITE(sk); 548db8dac20SDavid S. Miller int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; 549db8dac20SDavid S. Miller int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 550db8dac20SDavid S. Miller 551db8dac20SDavid S. Miller if (len > 0xFFFF) 552db8dac20SDavid S. Miller return -EMSGSIZE; 553db8dac20SDavid S. Miller 554db8dac20SDavid S. Miller /* 555db8dac20SDavid S. Miller * Check the flags. 556db8dac20SDavid S. Miller */ 557db8dac20SDavid S. Miller 558db8dac20SDavid S. Miller if (msg->msg_flags&MSG_OOB) /* Mirror BSD error message compatibility */ 559db8dac20SDavid S. Miller return -EOPNOTSUPP; 560db8dac20SDavid S. Miller 561db8dac20SDavid S. Miller ipc.opt = NULL; 562db8dac20SDavid S. Miller 563db8dac20SDavid S. Miller if (up->pending) { 564db8dac20SDavid S. Miller /* 565db8dac20SDavid S. Miller * There are pending frames. 566db8dac20SDavid S. Miller * The socket lock must be held while it's corked. 567db8dac20SDavid S. Miller */ 568db8dac20SDavid S. Miller lock_sock(sk); 569db8dac20SDavid S. Miller if (likely(up->pending)) { 570db8dac20SDavid S. Miller if (unlikely(up->pending != AF_INET)) { 571db8dac20SDavid S. Miller release_sock(sk); 572db8dac20SDavid S. Miller return -EINVAL; 573db8dac20SDavid S. Miller } 574db8dac20SDavid S. Miller goto do_append_data; 575db8dac20SDavid S. Miller } 576db8dac20SDavid S. Miller release_sock(sk); 577db8dac20SDavid S. Miller } 578db8dac20SDavid S. Miller ulen += sizeof(struct udphdr); 579db8dac20SDavid S. Miller 580db8dac20SDavid S. Miller /* 581db8dac20SDavid S. Miller * Get and verify the address. 582db8dac20SDavid S. Miller */ 583db8dac20SDavid S. Miller if (msg->msg_name) { 584db8dac20SDavid S. Miller struct sockaddr_in * usin = (struct sockaddr_in*)msg->msg_name; 585db8dac20SDavid S. Miller if (msg->msg_namelen < sizeof(*usin)) 586db8dac20SDavid S. Miller return -EINVAL; 587db8dac20SDavid S. Miller if (usin->sin_family != AF_INET) { 588db8dac20SDavid S. Miller if (usin->sin_family != AF_UNSPEC) 589db8dac20SDavid S. Miller return -EAFNOSUPPORT; 590db8dac20SDavid S. Miller } 591db8dac20SDavid S. Miller 592db8dac20SDavid S. Miller daddr = usin->sin_addr.s_addr; 593db8dac20SDavid S. Miller dport = usin->sin_port; 594db8dac20SDavid S. Miller if (dport == 0) 595db8dac20SDavid S. Miller return -EINVAL; 596db8dac20SDavid S. Miller } else { 597db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 598db8dac20SDavid S. Miller return -EDESTADDRREQ; 599db8dac20SDavid S. Miller daddr = inet->daddr; 600db8dac20SDavid S. Miller dport = inet->dport; 601db8dac20SDavid S. Miller /* Open fast path for connected socket. 602db8dac20SDavid S. Miller Route will not be used, if at least one option is set. 603db8dac20SDavid S. Miller */ 604db8dac20SDavid S. Miller connected = 1; 605db8dac20SDavid S. Miller } 606db8dac20SDavid S. Miller ipc.addr = inet->saddr; 607db8dac20SDavid S. Miller 608db8dac20SDavid S. Miller ipc.oif = sk->sk_bound_dev_if; 609db8dac20SDavid S. Miller if (msg->msg_controllen) { 6103b1e0a65SYOSHIFUJI Hideaki err = ip_cmsg_send(sock_net(sk), msg, &ipc); 611db8dac20SDavid S. Miller if (err) 612db8dac20SDavid S. Miller return err; 613db8dac20SDavid S. Miller if (ipc.opt) 614db8dac20SDavid S. Miller free = 1; 615db8dac20SDavid S. Miller connected = 0; 616db8dac20SDavid S. Miller } 617db8dac20SDavid S. Miller if (!ipc.opt) 618db8dac20SDavid S. Miller ipc.opt = inet->opt; 619db8dac20SDavid S. Miller 620db8dac20SDavid S. Miller saddr = ipc.addr; 621db8dac20SDavid S. Miller ipc.addr = faddr = daddr; 622db8dac20SDavid S. Miller 623db8dac20SDavid S. Miller if (ipc.opt && ipc.opt->srr) { 624db8dac20SDavid S. Miller if (!daddr) 625db8dac20SDavid S. Miller return -EINVAL; 626db8dac20SDavid S. Miller faddr = ipc.opt->faddr; 627db8dac20SDavid S. Miller connected = 0; 628db8dac20SDavid S. Miller } 629db8dac20SDavid S. Miller tos = RT_TOS(inet->tos); 630db8dac20SDavid S. Miller if (sock_flag(sk, SOCK_LOCALROUTE) || 631db8dac20SDavid S. Miller (msg->msg_flags & MSG_DONTROUTE) || 632db8dac20SDavid S. Miller (ipc.opt && ipc.opt->is_strictroute)) { 633db8dac20SDavid S. Miller tos |= RTO_ONLINK; 634db8dac20SDavid S. Miller connected = 0; 635db8dac20SDavid S. Miller } 636db8dac20SDavid S. Miller 637db8dac20SDavid S. Miller if (ipv4_is_multicast(daddr)) { 638db8dac20SDavid S. Miller if (!ipc.oif) 639db8dac20SDavid S. Miller ipc.oif = inet->mc_index; 640db8dac20SDavid S. Miller if (!saddr) 641db8dac20SDavid S. Miller saddr = inet->mc_addr; 642db8dac20SDavid S. Miller connected = 0; 643db8dac20SDavid S. Miller } 644db8dac20SDavid S. Miller 645db8dac20SDavid S. Miller if (connected) 646db8dac20SDavid S. Miller rt = (struct rtable*)sk_dst_check(sk, 0); 647db8dac20SDavid S. Miller 648db8dac20SDavid S. Miller if (rt == NULL) { 649db8dac20SDavid S. Miller struct flowi fl = { .oif = ipc.oif, 650db8dac20SDavid S. Miller .nl_u = { .ip4_u = 651db8dac20SDavid S. Miller { .daddr = faddr, 652db8dac20SDavid S. Miller .saddr = saddr, 653db8dac20SDavid S. Miller .tos = tos } }, 654db8dac20SDavid S. Miller .proto = sk->sk_protocol, 655db8dac20SDavid S. Miller .uli_u = { .ports = 656db8dac20SDavid S. Miller { .sport = inet->sport, 657db8dac20SDavid S. Miller .dport = dport } } }; 658db8dac20SDavid S. Miller security_sk_classify_flow(sk, &fl); 6593b1e0a65SYOSHIFUJI Hideaki err = ip_route_output_flow(sock_net(sk), &rt, &fl, sk, 1); 660db8dac20SDavid S. Miller if (err) { 661db8dac20SDavid S. Miller if (err == -ENETUNREACH) 662db8dac20SDavid S. Miller IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES); 663db8dac20SDavid S. Miller goto out; 664db8dac20SDavid S. Miller } 665db8dac20SDavid S. Miller 666db8dac20SDavid S. Miller err = -EACCES; 667db8dac20SDavid S. Miller if ((rt->rt_flags & RTCF_BROADCAST) && 668db8dac20SDavid S. Miller !sock_flag(sk, SOCK_BROADCAST)) 669db8dac20SDavid S. Miller goto out; 670db8dac20SDavid S. Miller if (connected) 671db8dac20SDavid S. Miller sk_dst_set(sk, dst_clone(&rt->u.dst)); 672db8dac20SDavid S. Miller } 673db8dac20SDavid S. Miller 674db8dac20SDavid S. Miller if (msg->msg_flags&MSG_CONFIRM) 675db8dac20SDavid S. Miller goto do_confirm; 676db8dac20SDavid S. Miller back_from_confirm: 677db8dac20SDavid S. Miller 678db8dac20SDavid S. Miller saddr = rt->rt_src; 679db8dac20SDavid S. Miller if (!ipc.addr) 680db8dac20SDavid S. Miller daddr = ipc.addr = rt->rt_dst; 681db8dac20SDavid S. Miller 682db8dac20SDavid S. Miller lock_sock(sk); 683db8dac20SDavid S. Miller if (unlikely(up->pending)) { 684db8dac20SDavid S. Miller /* The socket is already corked while preparing it. */ 685db8dac20SDavid S. Miller /* ... which is an evident application bug. --ANK */ 686db8dac20SDavid S. Miller release_sock(sk); 687db8dac20SDavid S. Miller 688db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n"); 689db8dac20SDavid S. Miller err = -EINVAL; 690db8dac20SDavid S. Miller goto out; 691db8dac20SDavid S. Miller } 692db8dac20SDavid S. Miller /* 693db8dac20SDavid S. Miller * Now cork the socket to pend data. 694db8dac20SDavid S. Miller */ 695db8dac20SDavid S. Miller inet->cork.fl.fl4_dst = daddr; 696db8dac20SDavid S. Miller inet->cork.fl.fl_ip_dport = dport; 697db8dac20SDavid S. Miller inet->cork.fl.fl4_src = saddr; 698db8dac20SDavid S. Miller inet->cork.fl.fl_ip_sport = inet->sport; 699db8dac20SDavid S. Miller up->pending = AF_INET; 700db8dac20SDavid S. Miller 701db8dac20SDavid S. Miller do_append_data: 702db8dac20SDavid S. Miller up->len += ulen; 703db8dac20SDavid S. Miller getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 704db8dac20SDavid S. Miller err = ip_append_data(sk, getfrag, msg->msg_iov, ulen, 705db8dac20SDavid S. Miller sizeof(struct udphdr), &ipc, rt, 706db8dac20SDavid S. Miller corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); 707db8dac20SDavid S. Miller if (err) 708db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 709db8dac20SDavid S. Miller else if (!corkreq) 710db8dac20SDavid S. Miller err = udp_push_pending_frames(sk); 711db8dac20SDavid S. Miller else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 712db8dac20SDavid S. Miller up->pending = 0; 713db8dac20SDavid S. Miller release_sock(sk); 714db8dac20SDavid S. Miller 715db8dac20SDavid S. Miller out: 716db8dac20SDavid S. Miller ip_rt_put(rt); 717db8dac20SDavid S. Miller if (free) 718db8dac20SDavid S. Miller kfree(ipc.opt); 719db8dac20SDavid S. Miller if (!err) 720db8dac20SDavid S. Miller return len; 721db8dac20SDavid S. Miller /* 722db8dac20SDavid S. Miller * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 723db8dac20SDavid S. Miller * ENOBUFS might not be good (it's not tunable per se), but otherwise 724db8dac20SDavid S. Miller * we don't have a good statistic (IpOutDiscards but it can be too many 725db8dac20SDavid S. Miller * things). We could add another new stat but at least for now that 726db8dac20SDavid S. Miller * seems like overkill. 727db8dac20SDavid S. Miller */ 728db8dac20SDavid S. Miller if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 729db8dac20SDavid S. Miller UDP_INC_STATS_USER(UDP_MIB_SNDBUFERRORS, is_udplite); 730db8dac20SDavid S. Miller } 731db8dac20SDavid S. Miller return err; 732db8dac20SDavid S. Miller 733db8dac20SDavid S. Miller do_confirm: 734db8dac20SDavid S. Miller dst_confirm(&rt->u.dst); 735db8dac20SDavid S. Miller if (!(msg->msg_flags&MSG_PROBE) || len) 736db8dac20SDavid S. Miller goto back_from_confirm; 737db8dac20SDavid S. Miller err = 0; 738db8dac20SDavid S. Miller goto out; 739db8dac20SDavid S. Miller } 740db8dac20SDavid S. Miller 741db8dac20SDavid S. Miller int udp_sendpage(struct sock *sk, struct page *page, int offset, 742db8dac20SDavid S. Miller size_t size, int flags) 743db8dac20SDavid S. Miller { 744db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 745db8dac20SDavid S. Miller int ret; 746db8dac20SDavid S. Miller 747db8dac20SDavid S. Miller if (!up->pending) { 748db8dac20SDavid S. Miller struct msghdr msg = { .msg_flags = flags|MSG_MORE }; 749db8dac20SDavid S. Miller 750db8dac20SDavid S. Miller /* Call udp_sendmsg to specify destination address which 751db8dac20SDavid S. Miller * sendpage interface can't pass. 752db8dac20SDavid S. Miller * This will succeed only when the socket is connected. 753db8dac20SDavid S. Miller */ 754db8dac20SDavid S. Miller ret = udp_sendmsg(NULL, sk, &msg, 0); 755db8dac20SDavid S. Miller if (ret < 0) 756db8dac20SDavid S. Miller return ret; 757db8dac20SDavid S. Miller } 758db8dac20SDavid S. Miller 759db8dac20SDavid S. Miller lock_sock(sk); 760db8dac20SDavid S. Miller 761db8dac20SDavid S. Miller if (unlikely(!up->pending)) { 762db8dac20SDavid S. Miller release_sock(sk); 763db8dac20SDavid S. Miller 764db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n"); 765db8dac20SDavid S. Miller return -EINVAL; 766db8dac20SDavid S. Miller } 767db8dac20SDavid S. Miller 768db8dac20SDavid S. Miller ret = ip_append_page(sk, page, offset, size, flags); 769db8dac20SDavid S. Miller if (ret == -EOPNOTSUPP) { 770db8dac20SDavid S. Miller release_sock(sk); 771db8dac20SDavid S. Miller return sock_no_sendpage(sk->sk_socket, page, offset, 772db8dac20SDavid S. Miller size, flags); 773db8dac20SDavid S. Miller } 774db8dac20SDavid S. Miller if (ret < 0) { 775db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 776db8dac20SDavid S. Miller goto out; 777db8dac20SDavid S. Miller } 778db8dac20SDavid S. Miller 779db8dac20SDavid S. Miller up->len += size; 780db8dac20SDavid S. Miller if (!(up->corkflag || (flags&MSG_MORE))) 781db8dac20SDavid S. Miller ret = udp_push_pending_frames(sk); 782db8dac20SDavid S. Miller if (!ret) 783db8dac20SDavid S. Miller ret = size; 784db8dac20SDavid S. Miller out: 785db8dac20SDavid S. Miller release_sock(sk); 786db8dac20SDavid S. Miller return ret; 787db8dac20SDavid S. Miller } 788db8dac20SDavid S. Miller 7891da177e4SLinus Torvalds /* 7901da177e4SLinus Torvalds * IOCTL requests applicable to the UDP protocol 7911da177e4SLinus Torvalds */ 7921da177e4SLinus Torvalds 7931da177e4SLinus Torvalds int udp_ioctl(struct sock *sk, int cmd, unsigned long arg) 7941da177e4SLinus Torvalds { 7956516c655SStephen Hemminger switch (cmd) { 7961da177e4SLinus Torvalds case SIOCOUTQ: 7971da177e4SLinus Torvalds { 7981da177e4SLinus Torvalds int amount = atomic_read(&sk->sk_wmem_alloc); 7991da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 8001da177e4SLinus Torvalds } 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds case SIOCINQ: 8031da177e4SLinus Torvalds { 8041da177e4SLinus Torvalds struct sk_buff *skb; 8051da177e4SLinus Torvalds unsigned long amount; 8061da177e4SLinus Torvalds 8071da177e4SLinus Torvalds amount = 0; 808208d8984SHerbert Xu spin_lock_bh(&sk->sk_receive_queue.lock); 8091da177e4SLinus Torvalds skb = skb_peek(&sk->sk_receive_queue); 8101da177e4SLinus Torvalds if (skb != NULL) { 8111da177e4SLinus Torvalds /* 8121da177e4SLinus Torvalds * We will only return the amount 8131da177e4SLinus Torvalds * of this packet since that is all 8141da177e4SLinus Torvalds * that will be read. 8151da177e4SLinus Torvalds */ 8161da177e4SLinus Torvalds amount = skb->len - sizeof(struct udphdr); 8171da177e4SLinus Torvalds } 818208d8984SHerbert Xu spin_unlock_bh(&sk->sk_receive_queue.lock); 8191da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds default: 8231da177e4SLinus Torvalds return -ENOIOCTLCMD; 8241da177e4SLinus Torvalds } 8256516c655SStephen Hemminger 8266516c655SStephen Hemminger return 0; 8271da177e4SLinus Torvalds } 8281da177e4SLinus Torvalds 829db8dac20SDavid S. Miller /* 830db8dac20SDavid S. Miller * This should be easy, if there is something there we 831db8dac20SDavid S. Miller * return it, otherwise we block. 832db8dac20SDavid S. Miller */ 833db8dac20SDavid S. Miller 834db8dac20SDavid S. Miller int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 835db8dac20SDavid S. Miller size_t len, int noblock, int flags, int *addr_len) 836db8dac20SDavid S. Miller { 837db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 838db8dac20SDavid S. Miller struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name; 839db8dac20SDavid S. Miller struct sk_buff *skb; 840db8dac20SDavid S. Miller unsigned int ulen, copied; 841db8dac20SDavid S. Miller int peeked; 842db8dac20SDavid S. Miller int err; 843db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 844db8dac20SDavid S. Miller 845db8dac20SDavid S. Miller /* 846db8dac20SDavid S. Miller * Check any passed addresses 847db8dac20SDavid S. Miller */ 848db8dac20SDavid S. Miller if (addr_len) 849db8dac20SDavid S. Miller *addr_len=sizeof(*sin); 850db8dac20SDavid S. Miller 851db8dac20SDavid S. Miller if (flags & MSG_ERRQUEUE) 852db8dac20SDavid S. Miller return ip_recv_error(sk, msg, len); 853db8dac20SDavid S. Miller 854db8dac20SDavid S. Miller try_again: 855db8dac20SDavid S. Miller skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), 856db8dac20SDavid S. Miller &peeked, &err); 857db8dac20SDavid S. Miller if (!skb) 858db8dac20SDavid S. Miller goto out; 859db8dac20SDavid S. Miller 860db8dac20SDavid S. Miller ulen = skb->len - sizeof(struct udphdr); 861db8dac20SDavid S. Miller copied = len; 862db8dac20SDavid S. Miller if (copied > ulen) 863db8dac20SDavid S. Miller copied = ulen; 864db8dac20SDavid S. Miller else if (copied < ulen) 865db8dac20SDavid S. Miller msg->msg_flags |= MSG_TRUNC; 866db8dac20SDavid S. Miller 867db8dac20SDavid S. Miller /* 868db8dac20SDavid S. Miller * If checksum is needed at all, try to do it while copying the 869db8dac20SDavid S. Miller * data. If the data is truncated, or if we only want a partial 870db8dac20SDavid S. Miller * coverage checksum (UDP-Lite), do it before the copy. 871db8dac20SDavid S. Miller */ 872db8dac20SDavid S. Miller 873db8dac20SDavid S. Miller if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) { 874db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 875db8dac20SDavid S. Miller goto csum_copy_err; 876db8dac20SDavid S. Miller } 877db8dac20SDavid S. Miller 878db8dac20SDavid S. Miller if (skb_csum_unnecessary(skb)) 879db8dac20SDavid S. Miller err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), 880db8dac20SDavid S. Miller msg->msg_iov, copied ); 881db8dac20SDavid S. Miller else { 882db8dac20SDavid S. Miller err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov); 883db8dac20SDavid S. Miller 884db8dac20SDavid S. Miller if (err == -EINVAL) 885db8dac20SDavid S. Miller goto csum_copy_err; 886db8dac20SDavid S. Miller } 887db8dac20SDavid S. Miller 888db8dac20SDavid S. Miller if (err) 889db8dac20SDavid S. Miller goto out_free; 890db8dac20SDavid S. Miller 891db8dac20SDavid S. Miller if (!peeked) 892db8dac20SDavid S. Miller UDP_INC_STATS_USER(UDP_MIB_INDATAGRAMS, is_udplite); 893db8dac20SDavid S. Miller 894db8dac20SDavid S. Miller sock_recv_timestamp(msg, sk, skb); 895db8dac20SDavid S. Miller 896db8dac20SDavid S. Miller /* Copy the address. */ 897db8dac20SDavid S. Miller if (sin) 898db8dac20SDavid S. Miller { 899db8dac20SDavid S. Miller sin->sin_family = AF_INET; 900db8dac20SDavid S. Miller sin->sin_port = udp_hdr(skb)->source; 901db8dac20SDavid S. Miller sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 902db8dac20SDavid S. Miller memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 903db8dac20SDavid S. Miller } 904db8dac20SDavid S. Miller if (inet->cmsg_flags) 905db8dac20SDavid S. Miller ip_cmsg_recv(msg, skb); 906db8dac20SDavid S. Miller 907db8dac20SDavid S. Miller err = copied; 908db8dac20SDavid S. Miller if (flags & MSG_TRUNC) 909db8dac20SDavid S. Miller err = ulen; 910db8dac20SDavid S. Miller 911db8dac20SDavid S. Miller out_free: 912db8dac20SDavid S. Miller lock_sock(sk); 913db8dac20SDavid S. Miller skb_free_datagram(sk, skb); 914db8dac20SDavid S. Miller release_sock(sk); 915db8dac20SDavid S. Miller out: 916db8dac20SDavid S. Miller return err; 917db8dac20SDavid S. Miller 918db8dac20SDavid S. Miller csum_copy_err: 919db8dac20SDavid S. Miller lock_sock(sk); 920db8dac20SDavid S. Miller if (!skb_kill_datagram(sk, skb, flags)) 921db8dac20SDavid S. Miller UDP_INC_STATS_USER(UDP_MIB_INERRORS, is_udplite); 922db8dac20SDavid S. Miller release_sock(sk); 923db8dac20SDavid S. Miller 924db8dac20SDavid S. Miller if (noblock) 925db8dac20SDavid S. Miller return -EAGAIN; 926db8dac20SDavid S. Miller goto try_again; 927db8dac20SDavid S. Miller } 928db8dac20SDavid S. Miller 929db8dac20SDavid S. Miller 9301da177e4SLinus Torvalds int udp_disconnect(struct sock *sk, int flags) 9311da177e4SLinus Torvalds { 9321da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 9331da177e4SLinus Torvalds /* 9341da177e4SLinus Torvalds * 1003.1g - break association. 9351da177e4SLinus Torvalds */ 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds sk->sk_state = TCP_CLOSE; 9381da177e4SLinus Torvalds inet->daddr = 0; 9391da177e4SLinus Torvalds inet->dport = 0; 9401da177e4SLinus Torvalds sk->sk_bound_dev_if = 0; 9411da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) 9421da177e4SLinus Torvalds inet_reset_saddr(sk); 9431da177e4SLinus Torvalds 9441da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) { 9451da177e4SLinus Torvalds sk->sk_prot->unhash(sk); 9461da177e4SLinus Torvalds inet->sport = 0; 9471da177e4SLinus Torvalds } 9481da177e4SLinus Torvalds sk_dst_reset(sk); 9491da177e4SLinus Torvalds return 0; 9501da177e4SLinus Torvalds } 9511da177e4SLinus Torvalds 952db8dac20SDavid S. Miller /* returns: 953db8dac20SDavid S. Miller * -1: error 954db8dac20SDavid S. Miller * 0: success 955db8dac20SDavid S. Miller * >0: "udp encap" protocol resubmission 956db8dac20SDavid S. Miller * 957db8dac20SDavid S. Miller * Note that in the success and error cases, the skb is assumed to 958db8dac20SDavid S. Miller * have either been requeued or freed. 959db8dac20SDavid S. Miller */ 960db8dac20SDavid S. Miller int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb) 961db8dac20SDavid S. Miller { 962db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 963db8dac20SDavid S. Miller int rc; 964db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 965db8dac20SDavid S. Miller 966db8dac20SDavid S. Miller /* 967db8dac20SDavid S. Miller * Charge it to the socket, dropping if the queue is full. 968db8dac20SDavid S. Miller */ 969db8dac20SDavid S. Miller if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) 970db8dac20SDavid S. Miller goto drop; 971db8dac20SDavid S. Miller nf_reset(skb); 972db8dac20SDavid S. Miller 973db8dac20SDavid S. Miller if (up->encap_type) { 974db8dac20SDavid S. Miller /* 975db8dac20SDavid S. Miller * This is an encapsulation socket so pass the skb to 976db8dac20SDavid S. Miller * the socket's udp_encap_rcv() hook. Otherwise, just 977db8dac20SDavid S. Miller * fall through and pass this up the UDP socket. 978db8dac20SDavid S. Miller * up->encap_rcv() returns the following value: 979db8dac20SDavid S. Miller * =0 if skb was successfully passed to the encap 980db8dac20SDavid S. Miller * handler or was discarded by it. 981db8dac20SDavid S. Miller * >0 if skb should be passed on to UDP. 982db8dac20SDavid S. Miller * <0 if skb should be resubmitted as proto -N 983db8dac20SDavid S. Miller */ 984db8dac20SDavid S. Miller 985db8dac20SDavid S. Miller /* if we're overly short, let UDP handle it */ 986db8dac20SDavid S. Miller if (skb->len > sizeof(struct udphdr) && 987db8dac20SDavid S. Miller up->encap_rcv != NULL) { 988db8dac20SDavid S. Miller int ret; 989db8dac20SDavid S. Miller 990db8dac20SDavid S. Miller ret = (*up->encap_rcv)(sk, skb); 991db8dac20SDavid S. Miller if (ret <= 0) { 992db8dac20SDavid S. Miller UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS, 993db8dac20SDavid S. Miller is_udplite); 994db8dac20SDavid S. Miller return -ret; 995db8dac20SDavid S. Miller } 996db8dac20SDavid S. Miller } 997db8dac20SDavid S. Miller 998db8dac20SDavid S. Miller /* FALLTHROUGH -- it's a UDP Packet */ 999db8dac20SDavid S. Miller } 1000db8dac20SDavid S. Miller 1001db8dac20SDavid S. Miller /* 1002db8dac20SDavid S. Miller * UDP-Lite specific tests, ignored on UDP sockets 1003db8dac20SDavid S. Miller */ 1004db8dac20SDavid S. Miller if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 1005db8dac20SDavid S. Miller 1006db8dac20SDavid S. Miller /* 1007db8dac20SDavid S. Miller * MIB statistics other than incrementing the error count are 1008db8dac20SDavid S. Miller * disabled for the following two types of errors: these depend 1009db8dac20SDavid S. Miller * on the application settings, not on the functioning of the 1010db8dac20SDavid S. Miller * protocol stack as such. 1011db8dac20SDavid S. Miller * 1012db8dac20SDavid S. Miller * RFC 3828 here recommends (sec 3.3): "There should also be a 1013db8dac20SDavid S. Miller * way ... to ... at least let the receiving application block 1014db8dac20SDavid S. Miller * delivery of packets with coverage values less than a value 1015db8dac20SDavid S. Miller * provided by the application." 1016db8dac20SDavid S. Miller */ 1017db8dac20SDavid S. Miller if (up->pcrlen == 0) { /* full coverage was set */ 1018db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_WARNING "UDPLITE: partial coverage " 1019db8dac20SDavid S. Miller "%d while full coverage %d requested\n", 1020db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, skb->len); 1021db8dac20SDavid S. Miller goto drop; 1022db8dac20SDavid S. Miller } 1023db8dac20SDavid S. Miller /* The next case involves violating the min. coverage requested 1024db8dac20SDavid S. Miller * by the receiver. This is subtle: if receiver wants x and x is 1025db8dac20SDavid S. Miller * greater than the buffersize/MTU then receiver will complain 1026db8dac20SDavid S. Miller * that it wants x while sender emits packets of smaller size y. 1027db8dac20SDavid S. Miller * Therefore the above ...()->partial_cov statement is essential. 1028db8dac20SDavid S. Miller */ 1029db8dac20SDavid S. Miller if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 1030db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_WARNING 1031db8dac20SDavid S. Miller "UDPLITE: coverage %d too small, need min %d\n", 1032db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, up->pcrlen); 1033db8dac20SDavid S. Miller goto drop; 1034db8dac20SDavid S. Miller } 1035db8dac20SDavid S. Miller } 1036db8dac20SDavid S. Miller 1037db8dac20SDavid S. Miller if (sk->sk_filter) { 1038db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 1039db8dac20SDavid S. Miller goto drop; 1040db8dac20SDavid S. Miller } 1041db8dac20SDavid S. Miller 1042db8dac20SDavid S. Miller if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) { 1043db8dac20SDavid S. Miller /* Note that an ENOMEM error is charged twice */ 1044db8dac20SDavid S. Miller if (rc == -ENOMEM) 1045db8dac20SDavid S. Miller UDP_INC_STATS_BH(UDP_MIB_RCVBUFERRORS, is_udplite); 1046db8dac20SDavid S. Miller goto drop; 1047db8dac20SDavid S. Miller } 1048db8dac20SDavid S. Miller 1049db8dac20SDavid S. Miller return 0; 1050db8dac20SDavid S. Miller 1051db8dac20SDavid S. Miller drop: 1052db8dac20SDavid S. Miller UDP_INC_STATS_BH(UDP_MIB_INERRORS, is_udplite); 1053db8dac20SDavid S. Miller kfree_skb(skb); 1054db8dac20SDavid S. Miller return -1; 1055db8dac20SDavid S. Miller } 1056db8dac20SDavid S. Miller 1057db8dac20SDavid S. Miller /* 1058db8dac20SDavid S. Miller * Multicasts and broadcasts go to each listener. 1059db8dac20SDavid S. Miller * 1060db8dac20SDavid S. Miller * Note: called only from the BH handler context, 1061db8dac20SDavid S. Miller * so we don't need to lock the hashes. 1062db8dac20SDavid S. Miller */ 1063db8dac20SDavid S. Miller static int __udp4_lib_mcast_deliver(struct sk_buff *skb, 1064db8dac20SDavid S. Miller struct udphdr *uh, 1065db8dac20SDavid S. Miller __be32 saddr, __be32 daddr, 1066db8dac20SDavid S. Miller struct hlist_head udptable[]) 1067db8dac20SDavid S. Miller { 1068db8dac20SDavid S. Miller struct sock *sk; 1069db8dac20SDavid S. Miller int dif; 1070db8dac20SDavid S. Miller 1071db8dac20SDavid S. Miller read_lock(&udp_hash_lock); 1072db8dac20SDavid S. Miller sk = sk_head(&udptable[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]); 1073db8dac20SDavid S. Miller dif = skb->dev->ifindex; 1074db8dac20SDavid S. Miller sk = udp_v4_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif); 1075db8dac20SDavid S. Miller if (sk) { 1076db8dac20SDavid S. Miller struct sock *sknext = NULL; 1077db8dac20SDavid S. Miller 1078db8dac20SDavid S. Miller do { 1079db8dac20SDavid S. Miller struct sk_buff *skb1 = skb; 1080db8dac20SDavid S. Miller 1081db8dac20SDavid S. Miller sknext = udp_v4_mcast_next(sk_next(sk), uh->dest, daddr, 1082db8dac20SDavid S. Miller uh->source, saddr, dif); 1083db8dac20SDavid S. Miller if (sknext) 1084db8dac20SDavid S. Miller skb1 = skb_clone(skb, GFP_ATOMIC); 1085db8dac20SDavid S. Miller 1086db8dac20SDavid S. Miller if (skb1) { 1087db8dac20SDavid S. Miller int ret = 0; 1088db8dac20SDavid S. Miller 1089db8dac20SDavid S. Miller bh_lock_sock_nested(sk); 1090db8dac20SDavid S. Miller if (!sock_owned_by_user(sk)) 1091db8dac20SDavid S. Miller ret = udp_queue_rcv_skb(sk, skb1); 1092db8dac20SDavid S. Miller else 1093db8dac20SDavid S. Miller sk_add_backlog(sk, skb1); 1094db8dac20SDavid S. Miller bh_unlock_sock(sk); 1095db8dac20SDavid S. Miller 1096db8dac20SDavid S. Miller if (ret > 0) 1097db8dac20SDavid S. Miller /* we should probably re-process instead 1098db8dac20SDavid S. Miller * of dropping packets here. */ 1099db8dac20SDavid S. Miller kfree_skb(skb1); 1100db8dac20SDavid S. Miller } 1101db8dac20SDavid S. Miller sk = sknext; 1102db8dac20SDavid S. Miller } while (sknext); 1103db8dac20SDavid S. Miller } else 1104db8dac20SDavid S. Miller kfree_skb(skb); 1105db8dac20SDavid S. Miller read_unlock(&udp_hash_lock); 1106db8dac20SDavid S. Miller return 0; 1107db8dac20SDavid S. Miller } 1108db8dac20SDavid S. Miller 1109db8dac20SDavid S. Miller /* Initialize UDP checksum. If exited with zero value (success), 1110db8dac20SDavid S. Miller * CHECKSUM_UNNECESSARY means, that no more checks are required. 1111db8dac20SDavid S. Miller * Otherwise, csum completion requires chacksumming packet body, 1112db8dac20SDavid S. Miller * including udp header and folding it to skb->csum. 1113db8dac20SDavid S. Miller */ 1114db8dac20SDavid S. Miller static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh, 1115db8dac20SDavid S. Miller int proto) 1116db8dac20SDavid S. Miller { 1117db8dac20SDavid S. Miller const struct iphdr *iph; 1118db8dac20SDavid S. Miller int err; 1119db8dac20SDavid S. Miller 1120db8dac20SDavid S. Miller UDP_SKB_CB(skb)->partial_cov = 0; 1121db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov = skb->len; 1122db8dac20SDavid S. Miller 1123db8dac20SDavid S. Miller if (proto == IPPROTO_UDPLITE) { 1124db8dac20SDavid S. Miller err = udplite_checksum_init(skb, uh); 1125db8dac20SDavid S. Miller if (err) 1126db8dac20SDavid S. Miller return err; 1127db8dac20SDavid S. Miller } 1128db8dac20SDavid S. Miller 1129db8dac20SDavid S. Miller iph = ip_hdr(skb); 1130db8dac20SDavid S. Miller if (uh->check == 0) { 1131db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_UNNECESSARY; 1132db8dac20SDavid S. Miller } else if (skb->ip_summed == CHECKSUM_COMPLETE) { 1133db8dac20SDavid S. Miller if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len, 1134db8dac20SDavid S. Miller proto, skb->csum)) 1135db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_UNNECESSARY; 1136db8dac20SDavid S. Miller } 1137db8dac20SDavid S. Miller if (!skb_csum_unnecessary(skb)) 1138db8dac20SDavid S. Miller skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, 1139db8dac20SDavid S. Miller skb->len, proto, 0); 1140db8dac20SDavid S. Miller /* Probably, we should checksum udp header (it should be in cache 1141db8dac20SDavid S. Miller * in any case) and data in tiny packets (< rx copybreak). 1142db8dac20SDavid S. Miller */ 1143db8dac20SDavid S. Miller 1144db8dac20SDavid S. Miller return 0; 1145db8dac20SDavid S. Miller } 1146db8dac20SDavid S. Miller 1147db8dac20SDavid S. Miller /* 1148db8dac20SDavid S. Miller * All we need to do is get the socket, and then do a checksum. 1149db8dac20SDavid S. Miller */ 1150db8dac20SDavid S. Miller 1151db8dac20SDavid S. Miller int __udp4_lib_rcv(struct sk_buff *skb, struct hlist_head udptable[], 1152db8dac20SDavid S. Miller int proto) 1153db8dac20SDavid S. Miller { 1154db8dac20SDavid S. Miller struct sock *sk; 1155db8dac20SDavid S. Miller struct udphdr *uh = udp_hdr(skb); 1156db8dac20SDavid S. Miller unsigned short ulen; 1157db8dac20SDavid S. Miller struct rtable *rt = (struct rtable*)skb->dst; 1158db8dac20SDavid S. Miller __be32 saddr = ip_hdr(skb)->saddr; 1159db8dac20SDavid S. Miller __be32 daddr = ip_hdr(skb)->daddr; 1160db8dac20SDavid S. Miller 1161db8dac20SDavid S. Miller /* 1162db8dac20SDavid S. Miller * Validate the packet. 1163db8dac20SDavid S. Miller */ 1164db8dac20SDavid S. Miller if (!pskb_may_pull(skb, sizeof(struct udphdr))) 1165db8dac20SDavid S. Miller goto drop; /* No space for header. */ 1166db8dac20SDavid S. Miller 1167db8dac20SDavid S. Miller ulen = ntohs(uh->len); 1168db8dac20SDavid S. Miller if (ulen > skb->len) 1169db8dac20SDavid S. Miller goto short_packet; 1170db8dac20SDavid S. Miller 1171db8dac20SDavid S. Miller if (proto == IPPROTO_UDP) { 1172db8dac20SDavid S. Miller /* UDP validates ulen. */ 1173db8dac20SDavid S. Miller if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen)) 1174db8dac20SDavid S. Miller goto short_packet; 1175db8dac20SDavid S. Miller uh = udp_hdr(skb); 1176db8dac20SDavid S. Miller } 1177db8dac20SDavid S. Miller 1178db8dac20SDavid S. Miller if (udp4_csum_init(skb, uh, proto)) 1179db8dac20SDavid S. Miller goto csum_error; 1180db8dac20SDavid S. Miller 1181db8dac20SDavid S. Miller if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST)) 1182db8dac20SDavid S. Miller return __udp4_lib_mcast_deliver(skb, uh, saddr, daddr, udptable); 1183db8dac20SDavid S. Miller 1184c346dca1SYOSHIFUJI Hideaki sk = __udp4_lib_lookup(dev_net(skb->dev), saddr, uh->source, daddr, 1185db8dac20SDavid S. Miller uh->dest, inet_iif(skb), udptable); 1186db8dac20SDavid S. Miller 1187db8dac20SDavid S. Miller if (sk != NULL) { 1188db8dac20SDavid S. Miller int ret = 0; 1189db8dac20SDavid S. Miller bh_lock_sock_nested(sk); 1190db8dac20SDavid S. Miller if (!sock_owned_by_user(sk)) 1191db8dac20SDavid S. Miller ret = udp_queue_rcv_skb(sk, skb); 1192db8dac20SDavid S. Miller else 1193db8dac20SDavid S. Miller sk_add_backlog(sk, skb); 1194db8dac20SDavid S. Miller bh_unlock_sock(sk); 1195db8dac20SDavid S. Miller sock_put(sk); 1196db8dac20SDavid S. Miller 1197db8dac20SDavid S. Miller /* a return value > 0 means to resubmit the input, but 1198db8dac20SDavid S. Miller * it wants the return to be -protocol, or 0 1199db8dac20SDavid S. Miller */ 1200db8dac20SDavid S. Miller if (ret > 0) 1201db8dac20SDavid S. Miller return -ret; 1202db8dac20SDavid S. Miller return 0; 1203db8dac20SDavid S. Miller } 1204db8dac20SDavid S. Miller 1205db8dac20SDavid S. Miller if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) 1206db8dac20SDavid S. Miller goto drop; 1207db8dac20SDavid S. Miller nf_reset(skb); 1208db8dac20SDavid S. Miller 1209db8dac20SDavid S. Miller /* No socket. Drop packet silently, if checksum is wrong */ 1210db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 1211db8dac20SDavid S. Miller goto csum_error; 1212db8dac20SDavid S. Miller 1213db8dac20SDavid S. Miller UDP_INC_STATS_BH(UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); 1214db8dac20SDavid S. Miller icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); 1215db8dac20SDavid S. Miller 1216db8dac20SDavid S. Miller /* 1217db8dac20SDavid S. Miller * Hmm. We got an UDP packet to a port to which we 1218db8dac20SDavid S. Miller * don't wanna listen. Ignore it. 1219db8dac20SDavid S. Miller */ 1220db8dac20SDavid S. Miller kfree_skb(skb); 1221db8dac20SDavid S. Miller return 0; 1222db8dac20SDavid S. Miller 1223db8dac20SDavid S. Miller short_packet: 1224db8dac20SDavid 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", 1225db8dac20SDavid S. Miller proto == IPPROTO_UDPLITE ? "-Lite" : "", 1226db8dac20SDavid S. Miller NIPQUAD(saddr), 1227db8dac20SDavid S. Miller ntohs(uh->source), 1228db8dac20SDavid S. Miller ulen, 1229db8dac20SDavid S. Miller skb->len, 1230db8dac20SDavid S. Miller NIPQUAD(daddr), 1231db8dac20SDavid S. Miller ntohs(uh->dest)); 1232db8dac20SDavid S. Miller goto drop; 1233db8dac20SDavid S. Miller 1234db8dac20SDavid S. Miller csum_error: 1235db8dac20SDavid S. Miller /* 1236db8dac20SDavid S. Miller * RFC1122: OK. Discards the bad packet silently (as far as 1237db8dac20SDavid S. Miller * the network is concerned, anyway) as per 4.1.3.4 (MUST). 1238db8dac20SDavid S. Miller */ 1239db8dac20SDavid 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", 1240db8dac20SDavid S. Miller proto == IPPROTO_UDPLITE ? "-Lite" : "", 1241db8dac20SDavid S. Miller NIPQUAD(saddr), 1242db8dac20SDavid S. Miller ntohs(uh->source), 1243db8dac20SDavid S. Miller NIPQUAD(daddr), 1244db8dac20SDavid S. Miller ntohs(uh->dest), 1245db8dac20SDavid S. Miller ulen); 1246db8dac20SDavid S. Miller drop: 1247db8dac20SDavid S. Miller UDP_INC_STATS_BH(UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 1248db8dac20SDavid S. Miller kfree_skb(skb); 1249db8dac20SDavid S. Miller return 0; 1250db8dac20SDavid S. Miller } 1251db8dac20SDavid S. Miller 1252db8dac20SDavid S. Miller int udp_rcv(struct sk_buff *skb) 1253db8dac20SDavid S. Miller { 1254db8dac20SDavid S. Miller return __udp4_lib_rcv(skb, udp_hash, IPPROTO_UDP); 1255db8dac20SDavid S. Miller } 1256db8dac20SDavid S. Miller 1257db8dac20SDavid S. Miller int udp_destroy_sock(struct sock *sk) 1258db8dac20SDavid S. Miller { 1259db8dac20SDavid S. Miller lock_sock(sk); 1260db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 1261db8dac20SDavid S. Miller release_sock(sk); 1262db8dac20SDavid S. Miller return 0; 1263db8dac20SDavid S. Miller } 1264db8dac20SDavid S. Miller 12651da177e4SLinus Torvalds /* 12661da177e4SLinus Torvalds * Socket option code for UDP 12671da177e4SLinus Torvalds */ 12684c0a6cb0SGerrit Renker int udp_lib_setsockopt(struct sock *sk, int level, int optname, 12694c0a6cb0SGerrit Renker char __user *optval, int optlen, 12704c0a6cb0SGerrit Renker int (*push_pending_frames)(struct sock *)) 12711da177e4SLinus Torvalds { 12721da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 12731da177e4SLinus Torvalds int val; 12741da177e4SLinus Torvalds int err = 0; 1275b2bf1e26SWang Chen int is_udplite = IS_UDPLITE(sk); 12761da177e4SLinus Torvalds 12771da177e4SLinus Torvalds if (optlen<sizeof(int)) 12781da177e4SLinus Torvalds return -EINVAL; 12791da177e4SLinus Torvalds 12801da177e4SLinus Torvalds if (get_user(val, (int __user *)optval)) 12811da177e4SLinus Torvalds return -EFAULT; 12821da177e4SLinus Torvalds 12831da177e4SLinus Torvalds switch (optname) { 12841da177e4SLinus Torvalds case UDP_CORK: 12851da177e4SLinus Torvalds if (val != 0) { 12861da177e4SLinus Torvalds up->corkflag = 1; 12871da177e4SLinus Torvalds } else { 12881da177e4SLinus Torvalds up->corkflag = 0; 12891da177e4SLinus Torvalds lock_sock(sk); 12904c0a6cb0SGerrit Renker (*push_pending_frames)(sk); 12911da177e4SLinus Torvalds release_sock(sk); 12921da177e4SLinus Torvalds } 12931da177e4SLinus Torvalds break; 12941da177e4SLinus Torvalds 12951da177e4SLinus Torvalds case UDP_ENCAP: 12961da177e4SLinus Torvalds switch (val) { 12971da177e4SLinus Torvalds case 0: 12981da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP: 12991da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP_NON_IKE: 1300067b207bSJames Chapman up->encap_rcv = xfrm4_udp_encap_rcv; 1301067b207bSJames Chapman /* FALLTHROUGH */ 1302342f0234SJames Chapman case UDP_ENCAP_L2TPINUDP: 13031da177e4SLinus Torvalds up->encap_type = val; 13041da177e4SLinus Torvalds break; 13051da177e4SLinus Torvalds default: 13061da177e4SLinus Torvalds err = -ENOPROTOOPT; 13071da177e4SLinus Torvalds break; 13081da177e4SLinus Torvalds } 13091da177e4SLinus Torvalds break; 13101da177e4SLinus Torvalds 1311ba4e58ecSGerrit Renker /* 1312ba4e58ecSGerrit Renker * UDP-Lite's partial checksum coverage (RFC 3828). 1313ba4e58ecSGerrit Renker */ 1314ba4e58ecSGerrit Renker /* The sender sets actual checksum coverage length via this option. 1315ba4e58ecSGerrit Renker * The case coverage > packet length is handled by send module. */ 1316ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 1317b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 1318ba4e58ecSGerrit Renker return -ENOPROTOOPT; 1319ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Illegal coverage: use default (8) */ 1320ba4e58ecSGerrit Renker val = 8; 1321ba4e58ecSGerrit Renker up->pcslen = val; 1322ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_SEND_CC; 1323ba4e58ecSGerrit Renker break; 1324ba4e58ecSGerrit Renker 1325ba4e58ecSGerrit Renker /* The receiver specifies a minimum checksum coverage value. To make 1326ba4e58ecSGerrit Renker * sense, this should be set to at least 8 (as done below). If zero is 1327ba4e58ecSGerrit Renker * used, this again means full checksum coverage. */ 1328ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 1329b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 1330ba4e58ecSGerrit Renker return -ENOPROTOOPT; 1331ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Avoid silly minimal values. */ 1332ba4e58ecSGerrit Renker val = 8; 1333ba4e58ecSGerrit Renker up->pcrlen = val; 1334ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_RECV_CC; 1335ba4e58ecSGerrit Renker break; 1336ba4e58ecSGerrit Renker 13371da177e4SLinus Torvalds default: 13381da177e4SLinus Torvalds err = -ENOPROTOOPT; 13391da177e4SLinus Torvalds break; 13406516c655SStephen Hemminger } 13411da177e4SLinus Torvalds 13421da177e4SLinus Torvalds return err; 13431da177e4SLinus Torvalds } 13441da177e4SLinus Torvalds 1345db8dac20SDavid S. Miller int udp_setsockopt(struct sock *sk, int level, int optname, 1346db8dac20SDavid S. Miller char __user *optval, int optlen) 1347db8dac20SDavid S. Miller { 1348db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1349db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1350db8dac20SDavid S. Miller udp_push_pending_frames); 1351db8dac20SDavid S. Miller return ip_setsockopt(sk, level, optname, optval, optlen); 1352db8dac20SDavid S. Miller } 1353db8dac20SDavid S. Miller 1354db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1355db8dac20SDavid S. Miller int compat_udp_setsockopt(struct sock *sk, int level, int optname, 1356db8dac20SDavid S. Miller char __user *optval, int optlen) 1357db8dac20SDavid S. Miller { 1358db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1359db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1360db8dac20SDavid S. Miller udp_push_pending_frames); 1361db8dac20SDavid S. Miller return compat_ip_setsockopt(sk, level, optname, optval, optlen); 1362db8dac20SDavid S. Miller } 1363db8dac20SDavid S. Miller #endif 1364db8dac20SDavid S. Miller 13654c0a6cb0SGerrit Renker int udp_lib_getsockopt(struct sock *sk, int level, int optname, 13661da177e4SLinus Torvalds char __user *optval, int __user *optlen) 13671da177e4SLinus Torvalds { 13681da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 13691da177e4SLinus Torvalds int val, len; 13701da177e4SLinus Torvalds 13711da177e4SLinus Torvalds if (get_user(len,optlen)) 13721da177e4SLinus Torvalds return -EFAULT; 13731da177e4SLinus Torvalds 13741da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(int)); 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds if (len < 0) 13771da177e4SLinus Torvalds return -EINVAL; 13781da177e4SLinus Torvalds 13791da177e4SLinus Torvalds switch (optname) { 13801da177e4SLinus Torvalds case UDP_CORK: 13811da177e4SLinus Torvalds val = up->corkflag; 13821da177e4SLinus Torvalds break; 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds case UDP_ENCAP: 13851da177e4SLinus Torvalds val = up->encap_type; 13861da177e4SLinus Torvalds break; 13871da177e4SLinus Torvalds 1388ba4e58ecSGerrit Renker /* The following two cannot be changed on UDP sockets, the return is 1389ba4e58ecSGerrit Renker * always 0 (which corresponds to the full checksum coverage of UDP). */ 1390ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 1391ba4e58ecSGerrit Renker val = up->pcslen; 1392ba4e58ecSGerrit Renker break; 1393ba4e58ecSGerrit Renker 1394ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 1395ba4e58ecSGerrit Renker val = up->pcrlen; 1396ba4e58ecSGerrit Renker break; 1397ba4e58ecSGerrit Renker 13981da177e4SLinus Torvalds default: 13991da177e4SLinus Torvalds return -ENOPROTOOPT; 14006516c655SStephen Hemminger } 14011da177e4SLinus Torvalds 14021da177e4SLinus Torvalds if (put_user(len, optlen)) 14031da177e4SLinus Torvalds return -EFAULT; 14041da177e4SLinus Torvalds if (copy_to_user(optval, &val,len)) 14051da177e4SLinus Torvalds return -EFAULT; 14061da177e4SLinus Torvalds return 0; 14071da177e4SLinus Torvalds } 14081da177e4SLinus Torvalds 1409db8dac20SDavid S. Miller int udp_getsockopt(struct sock *sk, int level, int optname, 1410db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 1411db8dac20SDavid S. Miller { 1412db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1413db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1414db8dac20SDavid S. Miller return ip_getsockopt(sk, level, optname, optval, optlen); 1415db8dac20SDavid S. Miller } 1416db8dac20SDavid S. Miller 1417db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1418db8dac20SDavid S. Miller int compat_udp_getsockopt(struct sock *sk, int level, int optname, 1419db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 1420db8dac20SDavid S. Miller { 1421db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1422db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1423db8dac20SDavid S. Miller return compat_ip_getsockopt(sk, level, optname, optval, optlen); 1424db8dac20SDavid S. Miller } 1425db8dac20SDavid S. Miller #endif 14261da177e4SLinus Torvalds /** 14271da177e4SLinus Torvalds * udp_poll - wait for a UDP event. 14281da177e4SLinus Torvalds * @file - file struct 14291da177e4SLinus Torvalds * @sock - socket 14301da177e4SLinus Torvalds * @wait - poll table 14311da177e4SLinus Torvalds * 14321da177e4SLinus Torvalds * This is same as datagram poll, except for the special case of 14331da177e4SLinus Torvalds * blocking sockets. If application is using a blocking fd 14341da177e4SLinus Torvalds * and a packet with checksum error is in the queue; 14351da177e4SLinus Torvalds * then it could get return from select indicating data available 14361da177e4SLinus Torvalds * but then block when reading it. Add special case code 14371da177e4SLinus Torvalds * to work around these arguably broken applications. 14381da177e4SLinus Torvalds */ 14391da177e4SLinus Torvalds unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait) 14401da177e4SLinus Torvalds { 14411da177e4SLinus Torvalds unsigned int mask = datagram_poll(file, sock, wait); 14421da177e4SLinus Torvalds struct sock *sk = sock->sk; 1443ba4e58ecSGerrit Renker int is_lite = IS_UDPLITE(sk); 14441da177e4SLinus Torvalds 14451da177e4SLinus Torvalds /* Check for false positives due to checksum errors */ 14461da177e4SLinus Torvalds if ( (mask & POLLRDNORM) && 14471da177e4SLinus Torvalds !(file->f_flags & O_NONBLOCK) && 14481da177e4SLinus Torvalds !(sk->sk_shutdown & RCV_SHUTDOWN)){ 14491da177e4SLinus Torvalds struct sk_buff_head *rcvq = &sk->sk_receive_queue; 14501da177e4SLinus Torvalds struct sk_buff *skb; 14511da177e4SLinus Torvalds 1452208d8984SHerbert Xu spin_lock_bh(&rcvq->lock); 1453759e5d00SHerbert Xu while ((skb = skb_peek(rcvq)) != NULL && 1454759e5d00SHerbert Xu udp_lib_checksum_complete(skb)) { 1455ba4e58ecSGerrit Renker UDP_INC_STATS_BH(UDP_MIB_INERRORS, is_lite); 14561da177e4SLinus Torvalds __skb_unlink(skb, rcvq); 14571da177e4SLinus Torvalds kfree_skb(skb); 14581da177e4SLinus Torvalds } 1459208d8984SHerbert Xu spin_unlock_bh(&rcvq->lock); 14601da177e4SLinus Torvalds 14611da177e4SLinus Torvalds /* nothing to see, move along */ 14621da177e4SLinus Torvalds if (skb == NULL) 14631da177e4SLinus Torvalds mask &= ~(POLLIN | POLLRDNORM); 14641da177e4SLinus Torvalds } 14651da177e4SLinus Torvalds 14661da177e4SLinus Torvalds return mask; 14671da177e4SLinus Torvalds 14681da177e4SLinus Torvalds } 14691da177e4SLinus Torvalds 1470db8dac20SDavid S. Miller struct proto udp_prot = { 1471db8dac20SDavid S. Miller .name = "UDP", 1472db8dac20SDavid S. Miller .owner = THIS_MODULE, 1473db8dac20SDavid S. Miller .close = udp_lib_close, 1474db8dac20SDavid S. Miller .connect = ip4_datagram_connect, 1475db8dac20SDavid S. Miller .disconnect = udp_disconnect, 1476db8dac20SDavid S. Miller .ioctl = udp_ioctl, 1477db8dac20SDavid S. Miller .destroy = udp_destroy_sock, 1478db8dac20SDavid S. Miller .setsockopt = udp_setsockopt, 1479db8dac20SDavid S. Miller .getsockopt = udp_getsockopt, 1480db8dac20SDavid S. Miller .sendmsg = udp_sendmsg, 1481db8dac20SDavid S. Miller .recvmsg = udp_recvmsg, 1482db8dac20SDavid S. Miller .sendpage = udp_sendpage, 1483db8dac20SDavid S. Miller .backlog_rcv = udp_queue_rcv_skb, 1484db8dac20SDavid S. Miller .hash = udp_lib_hash, 1485db8dac20SDavid S. Miller .unhash = udp_lib_unhash, 1486db8dac20SDavid S. Miller .get_port = udp_v4_get_port, 1487db8dac20SDavid S. Miller .memory_allocated = &udp_memory_allocated, 1488db8dac20SDavid S. Miller .sysctl_mem = sysctl_udp_mem, 1489db8dac20SDavid S. Miller .sysctl_wmem = &sysctl_udp_wmem_min, 1490db8dac20SDavid S. Miller .sysctl_rmem = &sysctl_udp_rmem_min, 1491db8dac20SDavid S. Miller .obj_size = sizeof(struct udp_sock), 14926ba5a3c5SPavel Emelyanov .h.udp_hash = udp_hash, 1493db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1494db8dac20SDavid S. Miller .compat_setsockopt = compat_udp_setsockopt, 1495db8dac20SDavid S. Miller .compat_getsockopt = compat_udp_getsockopt, 1496db8dac20SDavid S. Miller #endif 1497db8dac20SDavid S. Miller }; 14981da177e4SLinus Torvalds 14991da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 15001da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 15011da177e4SLinus Torvalds 15021da177e4SLinus Torvalds static struct sock *udp_get_first(struct seq_file *seq) 15031da177e4SLinus Torvalds { 15041da177e4SLinus Torvalds struct sock *sk; 15051da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 1506*6f191efeSDenis V. Lunev struct net *net = seq_file_net(seq); 15071da177e4SLinus Torvalds 15081da177e4SLinus Torvalds for (state->bucket = 0; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) { 15091da177e4SLinus Torvalds struct hlist_node *node; 1510ba4e58ecSGerrit Renker sk_for_each(sk, node, state->hashtable + state->bucket) { 1511878628fbSYOSHIFUJI Hideaki if (!net_eq(sock_net(sk), net)) 1512a91275efSDaniel Lezcano continue; 15131da177e4SLinus Torvalds if (sk->sk_family == state->family) 15141da177e4SLinus Torvalds goto found; 15151da177e4SLinus Torvalds } 15161da177e4SLinus Torvalds } 15171da177e4SLinus Torvalds sk = NULL; 15181da177e4SLinus Torvalds found: 15191da177e4SLinus Torvalds return sk; 15201da177e4SLinus Torvalds } 15211da177e4SLinus Torvalds 15221da177e4SLinus Torvalds static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk) 15231da177e4SLinus Torvalds { 15241da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 1525*6f191efeSDenis V. Lunev struct net *net = seq_file_net(seq); 15261da177e4SLinus Torvalds 15271da177e4SLinus Torvalds do { 15281da177e4SLinus Torvalds sk = sk_next(sk); 15291da177e4SLinus Torvalds try_again: 15301da177e4SLinus Torvalds ; 1531878628fbSYOSHIFUJI Hideaki } while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family)); 15321da177e4SLinus Torvalds 15331da177e4SLinus Torvalds if (!sk && ++state->bucket < UDP_HTABLE_SIZE) { 1534ba4e58ecSGerrit Renker sk = sk_head(state->hashtable + state->bucket); 15351da177e4SLinus Torvalds goto try_again; 15361da177e4SLinus Torvalds } 15371da177e4SLinus Torvalds return sk; 15381da177e4SLinus Torvalds } 15391da177e4SLinus Torvalds 15401da177e4SLinus Torvalds static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos) 15411da177e4SLinus Torvalds { 15421da177e4SLinus Torvalds struct sock *sk = udp_get_first(seq); 15431da177e4SLinus Torvalds 15441da177e4SLinus Torvalds if (sk) 15451da177e4SLinus Torvalds while (pos && (sk = udp_get_next(seq, sk)) != NULL) 15461da177e4SLinus Torvalds --pos; 15471da177e4SLinus Torvalds return pos ? NULL : sk; 15481da177e4SLinus Torvalds } 15491da177e4SLinus Torvalds 15501da177e4SLinus Torvalds static void *udp_seq_start(struct seq_file *seq, loff_t *pos) 15519a429c49SEric Dumazet __acquires(udp_hash_lock) 15521da177e4SLinus Torvalds { 15531da177e4SLinus Torvalds read_lock(&udp_hash_lock); 15541da177e4SLinus Torvalds return *pos ? udp_get_idx(seq, *pos-1) : (void *)1; 15551da177e4SLinus Torvalds } 15561da177e4SLinus Torvalds 15571da177e4SLinus Torvalds static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos) 15581da177e4SLinus Torvalds { 15591da177e4SLinus Torvalds struct sock *sk; 15601da177e4SLinus Torvalds 15611da177e4SLinus Torvalds if (v == (void *)1) 15621da177e4SLinus Torvalds sk = udp_get_idx(seq, 0); 15631da177e4SLinus Torvalds else 15641da177e4SLinus Torvalds sk = udp_get_next(seq, v); 15651da177e4SLinus Torvalds 15661da177e4SLinus Torvalds ++*pos; 15671da177e4SLinus Torvalds return sk; 15681da177e4SLinus Torvalds } 15691da177e4SLinus Torvalds 15701da177e4SLinus Torvalds static void udp_seq_stop(struct seq_file *seq, void *v) 15719a429c49SEric Dumazet __releases(udp_hash_lock) 15721da177e4SLinus Torvalds { 15731da177e4SLinus Torvalds read_unlock(&udp_hash_lock); 15741da177e4SLinus Torvalds } 15751da177e4SLinus Torvalds 15761da177e4SLinus Torvalds static int udp_seq_open(struct inode *inode, struct file *file) 15771da177e4SLinus Torvalds { 15781da177e4SLinus Torvalds struct udp_seq_afinfo *afinfo = PDE(inode)->data; 15791da177e4SLinus Torvalds struct seq_file *seq; 1580a91275efSDaniel Lezcano struct net *net; 15811da177e4SLinus Torvalds int rc = -ENOMEM; 15820da974f4SPanagiotis Issaris struct udp_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL); 15831da177e4SLinus Torvalds 15841da177e4SLinus Torvalds if (!s) 15851da177e4SLinus Torvalds goto out; 1586a91275efSDaniel Lezcano 1587a91275efSDaniel Lezcano rc = -ENXIO; 1588a91275efSDaniel Lezcano net = get_proc_net(inode); 1589a91275efSDaniel Lezcano if (!net) 1590a91275efSDaniel Lezcano goto out_kfree; 1591a91275efSDaniel Lezcano 15921da177e4SLinus Torvalds s->family = afinfo->family; 1593ba4e58ecSGerrit Renker s->hashtable = afinfo->hashtable; 15941da177e4SLinus Torvalds s->seq_ops.start = udp_seq_start; 15951da177e4SLinus Torvalds s->seq_ops.next = udp_seq_next; 15961da177e4SLinus Torvalds s->seq_ops.show = afinfo->seq_show; 15971da177e4SLinus Torvalds s->seq_ops.stop = udp_seq_stop; 1598*6f191efeSDenis V. Lunev s->p.net = net; 15991da177e4SLinus Torvalds 16001da177e4SLinus Torvalds rc = seq_open(file, &s->seq_ops); 16011da177e4SLinus Torvalds if (rc) 1602a91275efSDaniel Lezcano goto out_put_net; 16031da177e4SLinus Torvalds 16041da177e4SLinus Torvalds seq = file->private_data; 16051da177e4SLinus Torvalds seq->private = s; 16061da177e4SLinus Torvalds out: 16071da177e4SLinus Torvalds return rc; 1608a91275efSDaniel Lezcano out_put_net: 1609a91275efSDaniel Lezcano put_net(net); 16101da177e4SLinus Torvalds out_kfree: 16111da177e4SLinus Torvalds kfree(s); 16121da177e4SLinus Torvalds goto out; 16131da177e4SLinus Torvalds } 16141da177e4SLinus Torvalds 1615a91275efSDaniel Lezcano static int udp_seq_release(struct inode *inode, struct file *file) 1616a91275efSDaniel Lezcano { 1617a91275efSDaniel Lezcano struct seq_file *seq = file->private_data; 1618a91275efSDaniel Lezcano struct udp_iter_state *s = seq->private; 1619a91275efSDaniel Lezcano 1620*6f191efeSDenis V. Lunev put_net(s->p.net); 1621a91275efSDaniel Lezcano seq_release_private(inode, file); 1622a91275efSDaniel Lezcano return 0; 1623a91275efSDaniel Lezcano } 1624a91275efSDaniel Lezcano 16251da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 16260c96d8c5SDaniel Lezcano int udp_proc_register(struct net *net, struct udp_seq_afinfo *afinfo) 16271da177e4SLinus Torvalds { 16281da177e4SLinus Torvalds struct proc_dir_entry *p; 16291da177e4SLinus Torvalds int rc = 0; 16301da177e4SLinus Torvalds 16311da177e4SLinus Torvalds if (!afinfo) 16321da177e4SLinus Torvalds return -EINVAL; 16331da177e4SLinus Torvalds afinfo->seq_fops->owner = afinfo->owner; 16341da177e4SLinus Torvalds afinfo->seq_fops->open = udp_seq_open; 16351da177e4SLinus Torvalds afinfo->seq_fops->read = seq_read; 16361da177e4SLinus Torvalds afinfo->seq_fops->llseek = seq_lseek; 1637a91275efSDaniel Lezcano afinfo->seq_fops->release = udp_seq_release; 16381da177e4SLinus Torvalds 16390c96d8c5SDaniel Lezcano p = proc_net_fops_create(net, afinfo->name, S_IRUGO, afinfo->seq_fops); 16401da177e4SLinus Torvalds if (p) 16411da177e4SLinus Torvalds p->data = afinfo; 16421da177e4SLinus Torvalds else 16431da177e4SLinus Torvalds rc = -ENOMEM; 16441da177e4SLinus Torvalds return rc; 16451da177e4SLinus Torvalds } 16461da177e4SLinus Torvalds 16470c96d8c5SDaniel Lezcano void udp_proc_unregister(struct net *net, struct udp_seq_afinfo *afinfo) 16481da177e4SLinus Torvalds { 16491da177e4SLinus Torvalds if (!afinfo) 16501da177e4SLinus Torvalds return; 16510c96d8c5SDaniel Lezcano proc_net_remove(net, afinfo->name); 16521da177e4SLinus Torvalds memset(afinfo->seq_fops, 0, sizeof(*afinfo->seq_fops)); 16531da177e4SLinus Torvalds } 1654db8dac20SDavid S. Miller 1655db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 1656db8dac20SDavid S. Miller static void udp4_format_sock(struct sock *sp, char *tmpbuf, int bucket) 1657db8dac20SDavid S. Miller { 1658db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sp); 1659db8dac20SDavid S. Miller __be32 dest = inet->daddr; 1660db8dac20SDavid S. Miller __be32 src = inet->rcv_saddr; 1661db8dac20SDavid S. Miller __u16 destp = ntohs(inet->dport); 1662db8dac20SDavid S. Miller __u16 srcp = ntohs(inet->sport); 1663db8dac20SDavid S. Miller 1664db8dac20SDavid S. Miller sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X" 1665db8dac20SDavid S. Miller " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p", 1666db8dac20SDavid S. Miller bucket, src, srcp, dest, destp, sp->sk_state, 1667db8dac20SDavid S. Miller atomic_read(&sp->sk_wmem_alloc), 1668db8dac20SDavid S. Miller atomic_read(&sp->sk_rmem_alloc), 1669db8dac20SDavid S. Miller 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp), 1670db8dac20SDavid S. Miller atomic_read(&sp->sk_refcnt), sp); 1671db8dac20SDavid S. Miller } 1672db8dac20SDavid S. Miller 1673db8dac20SDavid S. Miller int udp4_seq_show(struct seq_file *seq, void *v) 1674db8dac20SDavid S. Miller { 1675db8dac20SDavid S. Miller if (v == SEQ_START_TOKEN) 1676db8dac20SDavid S. Miller seq_printf(seq, "%-127s\n", 1677db8dac20SDavid S. Miller " sl local_address rem_address st tx_queue " 1678db8dac20SDavid S. Miller "rx_queue tr tm->when retrnsmt uid timeout " 1679db8dac20SDavid S. Miller "inode"); 1680db8dac20SDavid S. Miller else { 1681db8dac20SDavid S. Miller char tmpbuf[129]; 1682db8dac20SDavid S. Miller struct udp_iter_state *state = seq->private; 1683db8dac20SDavid S. Miller 1684db8dac20SDavid S. Miller udp4_format_sock(v, tmpbuf, state->bucket); 1685db8dac20SDavid S. Miller seq_printf(seq, "%-127s\n", tmpbuf); 1686db8dac20SDavid S. Miller } 1687db8dac20SDavid S. Miller return 0; 1688db8dac20SDavid S. Miller } 1689db8dac20SDavid S. Miller 1690db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 1691db8dac20SDavid S. Miller static struct file_operations udp4_seq_fops; 1692db8dac20SDavid S. Miller static struct udp_seq_afinfo udp4_seq_afinfo = { 1693db8dac20SDavid S. Miller .owner = THIS_MODULE, 1694db8dac20SDavid S. Miller .name = "udp", 1695db8dac20SDavid S. Miller .family = AF_INET, 1696db8dac20SDavid S. Miller .hashtable = udp_hash, 1697db8dac20SDavid S. Miller .seq_show = udp4_seq_show, 1698db8dac20SDavid S. Miller .seq_fops = &udp4_seq_fops, 1699db8dac20SDavid S. Miller }; 1700db8dac20SDavid S. Miller 170115439febSPavel Emelyanov static int udp4_proc_init_net(struct net *net) 170215439febSPavel Emelyanov { 170315439febSPavel Emelyanov return udp_proc_register(net, &udp4_seq_afinfo); 170415439febSPavel Emelyanov } 170515439febSPavel Emelyanov 170615439febSPavel Emelyanov static void udp4_proc_exit_net(struct net *net) 170715439febSPavel Emelyanov { 170815439febSPavel Emelyanov udp_proc_unregister(net, &udp4_seq_afinfo); 170915439febSPavel Emelyanov } 171015439febSPavel Emelyanov 171115439febSPavel Emelyanov static struct pernet_operations udp4_net_ops = { 171215439febSPavel Emelyanov .init = udp4_proc_init_net, 171315439febSPavel Emelyanov .exit = udp4_proc_exit_net, 171415439febSPavel Emelyanov }; 171515439febSPavel Emelyanov 1716db8dac20SDavid S. Miller int __init udp4_proc_init(void) 1717db8dac20SDavid S. Miller { 171815439febSPavel Emelyanov return register_pernet_subsys(&udp4_net_ops); 1719db8dac20SDavid S. Miller } 1720db8dac20SDavid S. Miller 1721db8dac20SDavid S. Miller void udp4_proc_exit(void) 1722db8dac20SDavid S. Miller { 172315439febSPavel Emelyanov unregister_pernet_subsys(&udp4_net_ops); 1724db8dac20SDavid S. Miller } 17251da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 17261da177e4SLinus Torvalds 172795766fffSHideo Aoki void __init udp_init(void) 172895766fffSHideo Aoki { 172995766fffSHideo Aoki unsigned long limit; 173095766fffSHideo Aoki 173195766fffSHideo Aoki /* Set the pressure threshold up by the same strategy of TCP. It is a 173295766fffSHideo Aoki * fraction of global memory that is up to 1/2 at 256 MB, decreasing 173395766fffSHideo Aoki * toward zero with the amount of memory, with a floor of 128 pages. 173495766fffSHideo Aoki */ 173595766fffSHideo Aoki limit = min(nr_all_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT); 173695766fffSHideo Aoki limit = (limit * (nr_all_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11); 173795766fffSHideo Aoki limit = max(limit, 128UL); 173895766fffSHideo Aoki sysctl_udp_mem[0] = limit / 4 * 3; 173995766fffSHideo Aoki sysctl_udp_mem[1] = limit; 174095766fffSHideo Aoki sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2; 174195766fffSHideo Aoki 174295766fffSHideo Aoki sysctl_udp_rmem_min = SK_MEM_QUANTUM; 174395766fffSHideo Aoki sysctl_udp_wmem_min = SK_MEM_QUANTUM; 174495766fffSHideo Aoki } 174595766fffSHideo Aoki 17461da177e4SLinus Torvalds EXPORT_SYMBOL(udp_disconnect); 17471da177e4SLinus Torvalds EXPORT_SYMBOL(udp_hash); 17481da177e4SLinus Torvalds EXPORT_SYMBOL(udp_hash_lock); 17491da177e4SLinus Torvalds EXPORT_SYMBOL(udp_ioctl); 1750db8dac20SDavid S. Miller EXPORT_SYMBOL(udp_prot); 1751db8dac20SDavid S. Miller EXPORT_SYMBOL(udp_sendmsg); 17524c0a6cb0SGerrit Renker EXPORT_SYMBOL(udp_lib_getsockopt); 17534c0a6cb0SGerrit Renker EXPORT_SYMBOL(udp_lib_setsockopt); 17541da177e4SLinus Torvalds EXPORT_SYMBOL(udp_poll); 17556ba5a3c5SPavel Emelyanov EXPORT_SYMBOL(udp_lib_get_port); 17561da177e4SLinus Torvalds 17571da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 17581da177e4SLinus Torvalds EXPORT_SYMBOL(udp_proc_register); 17591da177e4SLinus Torvalds EXPORT_SYMBOL(udp_proc_unregister); 17601da177e4SLinus Torvalds #endif 1761