11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 31da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 41da177e4SLinus Torvalds * interface as the means of communication with the user level. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * The User Datagram Protocol (UDP). 71da177e4SLinus Torvalds * 802c30a84SJesper Juhl * Authors: Ross Biro 91da177e4SLinus Torvalds * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 101da177e4SLinus Torvalds * Arnt Gulbrandsen, <agulbra@nvg.unit.no> 11113aa838SAlan Cox * Alan Cox, <alan@lxorguk.ukuu.org.uk> 121da177e4SLinus Torvalds * Hirokazu Takahashi, <taka@valinux.co.jp> 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * Fixes: 151da177e4SLinus Torvalds * Alan Cox : verify_area() calls 161da177e4SLinus Torvalds * Alan Cox : stopped close while in use off icmp 171da177e4SLinus Torvalds * messages. Not a fix but a botch that 181da177e4SLinus Torvalds * for udp at least is 'valid'. 191da177e4SLinus Torvalds * Alan Cox : Fixed icmp handling properly 201da177e4SLinus Torvalds * Alan Cox : Correct error for oversized datagrams 211da177e4SLinus Torvalds * Alan Cox : Tidied select() semantics. 221da177e4SLinus Torvalds * Alan Cox : udp_err() fixed properly, also now 231da177e4SLinus Torvalds * select and read wake correctly on errors 241da177e4SLinus Torvalds * Alan Cox : udp_send verify_area moved to avoid mem leak 251da177e4SLinus Torvalds * Alan Cox : UDP can count its memory 261da177e4SLinus Torvalds * Alan Cox : send to an unknown connection causes 271da177e4SLinus Torvalds * an ECONNREFUSED off the icmp, but 281da177e4SLinus Torvalds * does NOT close. 291da177e4SLinus Torvalds * Alan Cox : Switched to new sk_buff handlers. No more backlog! 301da177e4SLinus Torvalds * Alan Cox : Using generic datagram code. Even smaller and the PEEK 311da177e4SLinus Torvalds * bug no longer crashes it. 321da177e4SLinus Torvalds * Fred Van Kempen : Net2e support for sk->broadcast. 331da177e4SLinus Torvalds * Alan Cox : Uses skb_free_datagram 341da177e4SLinus Torvalds * Alan Cox : Added get/set sockopt support. 351da177e4SLinus Torvalds * Alan Cox : Broadcasting without option set returns EACCES. 361da177e4SLinus Torvalds * Alan Cox : No wakeup calls. Instead we now use the callbacks. 371da177e4SLinus Torvalds * Alan Cox : Use ip_tos and ip_ttl 381da177e4SLinus Torvalds * Alan Cox : SNMP Mibs 391da177e4SLinus Torvalds * Alan Cox : MSG_DONTROUTE, and 0.0.0.0 support. 401da177e4SLinus Torvalds * Matt Dillon : UDP length checks. 411da177e4SLinus Torvalds * Alan Cox : Smarter af_inet used properly. 421da177e4SLinus Torvalds * Alan Cox : Use new kernel side addressing. 431da177e4SLinus Torvalds * Alan Cox : Incorrect return on truncated datagram receive. 441da177e4SLinus Torvalds * Arnt Gulbrandsen : New udp_send and stuff 451da177e4SLinus Torvalds * Alan Cox : Cache last socket 461da177e4SLinus Torvalds * Alan Cox : Route cache 471da177e4SLinus Torvalds * Jon Peatfield : Minor efficiency fix to sendto(). 481da177e4SLinus Torvalds * Mike Shaver : RFC1122 checks. 491da177e4SLinus Torvalds * Alan Cox : Nonblocking error fix. 501da177e4SLinus Torvalds * Willy Konynenberg : Transparent proxying support. 511da177e4SLinus Torvalds * Mike McLagan : Routing by source 521da177e4SLinus Torvalds * David S. Miller : New socket lookup architecture. 531da177e4SLinus Torvalds * Last socket cache retained as it 541da177e4SLinus Torvalds * does have a high hit rate. 551da177e4SLinus Torvalds * Olaf Kirch : Don't linearise iovec on sendmsg. 561da177e4SLinus Torvalds * Andi Kleen : Some cleanups, cache destination entry 571da177e4SLinus Torvalds * for connect. 581da177e4SLinus Torvalds * Vitaly E. Lavrov : Transparent proxy revived after year coma. 591da177e4SLinus Torvalds * Melvin Smith : Check msg_name not msg_namelen in sendto(), 601da177e4SLinus Torvalds * return ENOTCONN for unconnected sockets (POSIX) 611da177e4SLinus Torvalds * Janos Farkas : don't deliver multi/broadcasts to a different 621da177e4SLinus Torvalds * bound-to-device socket 631da177e4SLinus Torvalds * Hirokazu Takahashi : HW checksumming for outgoing UDP 641da177e4SLinus Torvalds * datagrams. 651da177e4SLinus Torvalds * Hirokazu Takahashi : sendfile() on UDP works now. 661da177e4SLinus Torvalds * Arnaldo C. Melo : convert /proc/net/udp to seq_file 671da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which 681da177e4SLinus Torvalds * Alexey Kuznetsov: allow both IPv4 and IPv6 sockets to bind 691da177e4SLinus Torvalds * a single port at the same time. 701da177e4SLinus Torvalds * Derek Atkins <derek@ihtfp.com>: Add Encapulation Support 71342f0234SJames Chapman * James Chapman : Add L2TP encapsulation type. 721da177e4SLinus Torvalds * 731da177e4SLinus Torvalds * 741da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 751da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 761da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 771da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 781da177e4SLinus Torvalds */ 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds #include <asm/system.h> 811da177e4SLinus Torvalds #include <asm/uaccess.h> 821da177e4SLinus Torvalds #include <asm/ioctls.h> 8395766fffSHideo Aoki #include <linux/bootmem.h> 848203efb3SEric Dumazet #include <linux/highmem.h> 858203efb3SEric Dumazet #include <linux/swap.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 109645ca708SEric Dumazet struct udp_table udp_table; 110645ca708SEric Dumazet EXPORT_SYMBOL(udp_table); 1111da177e4SLinus Torvalds 11295766fffSHideo Aoki int sysctl_udp_mem[3] __read_mostly; 11395766fffSHideo Aoki int sysctl_udp_rmem_min __read_mostly; 11495766fffSHideo Aoki int sysctl_udp_wmem_min __read_mostly; 11595766fffSHideo Aoki 11695766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_mem); 11795766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_rmem_min); 11895766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_wmem_min); 11995766fffSHideo Aoki 12095766fffSHideo Aoki atomic_t udp_memory_allocated; 12195766fffSHideo Aoki EXPORT_SYMBOL(udp_memory_allocated); 12295766fffSHideo Aoki 12398322f22SEric Dumazet #define PORTS_PER_CHAIN (65536 / UDP_HTABLE_SIZE) 12498322f22SEric Dumazet 125f24d43c0SEric Dumazet static int udp_lib_lport_inuse(struct net *net, __u16 num, 126645ca708SEric Dumazet const struct udp_hslot *hslot, 12798322f22SEric Dumazet unsigned long *bitmap, 128f24d43c0SEric Dumazet struct sock *sk, 129f24d43c0SEric Dumazet int (*saddr_comp)(const struct sock *sk1, 130f24d43c0SEric Dumazet const struct sock *sk2)) 13125030a7fSGerrit Renker { 132f24d43c0SEric Dumazet struct sock *sk2; 13388ab1932SEric Dumazet struct hlist_nulls_node *node; 13425030a7fSGerrit Renker 13588ab1932SEric Dumazet sk_nulls_for_each(sk2, node, &hslot->head) 136f24d43c0SEric Dumazet if (net_eq(sock_net(sk2), net) && 137f24d43c0SEric Dumazet sk2 != sk && 13898322f22SEric Dumazet (bitmap || sk2->sk_hash == num) && 139f24d43c0SEric Dumazet (!sk2->sk_reuse || !sk->sk_reuse) && 140f24d43c0SEric Dumazet (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if 141f24d43c0SEric Dumazet || sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 14298322f22SEric Dumazet (*saddr_comp)(sk, sk2)) { 14398322f22SEric Dumazet if (bitmap) 14498322f22SEric Dumazet __set_bit(sk2->sk_hash / UDP_HTABLE_SIZE, 14598322f22SEric Dumazet bitmap); 14698322f22SEric Dumazet else 147fc038410SDavid S. Miller return 1; 14898322f22SEric Dumazet } 14925030a7fSGerrit Renker return 0; 15025030a7fSGerrit Renker } 15125030a7fSGerrit Renker 15225030a7fSGerrit Renker /** 1536ba5a3c5SPavel Emelyanov * udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6 15425030a7fSGerrit Renker * 15525030a7fSGerrit Renker * @sk: socket struct in question 15625030a7fSGerrit Renker * @snum: port number to look up 157df2bc459SDavid S. Miller * @saddr_comp: AF-dependent comparison of bound local IP addresses 15825030a7fSGerrit Renker */ 1596ba5a3c5SPavel Emelyanov int udp_lib_get_port(struct sock *sk, unsigned short snum, 160df2bc459SDavid S. Miller int (*saddr_comp)(const struct sock *sk1, 161df2bc459SDavid S. Miller const struct sock *sk2 ) ) 1621da177e4SLinus Torvalds { 163645ca708SEric Dumazet struct udp_hslot *hslot; 164645ca708SEric Dumazet struct udp_table *udptable = sk->sk_prot->h.udp_table; 16525030a7fSGerrit Renker int error = 1; 1663b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 1671da177e4SLinus Torvalds 16832c1da70SStephen Hemminger if (!snum) { 1699088c560SEric Dumazet int low, high, remaining; 1709088c560SEric Dumazet unsigned rand; 17198322f22SEric Dumazet unsigned short first, last; 17298322f22SEric Dumazet DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN); 1731da177e4SLinus Torvalds 174227b60f5SStephen Hemminger inet_get_local_port_range(&low, &high); 175a25de534SAnton Arapov remaining = (high - low) + 1; 176227b60f5SStephen Hemminger 1779088c560SEric Dumazet rand = net_random(); 17898322f22SEric Dumazet first = (((u64)rand * remaining) >> 32) + low; 17998322f22SEric Dumazet /* 18098322f22SEric Dumazet * force rand to be an odd multiple of UDP_HTABLE_SIZE 18198322f22SEric Dumazet */ 18298322f22SEric Dumazet rand = (rand | 1) * UDP_HTABLE_SIZE; 18398322f22SEric Dumazet for (last = first + UDP_HTABLE_SIZE; first != last; first++) { 18498322f22SEric Dumazet hslot = &udptable->hash[udp_hashfn(net, first)]; 18598322f22SEric Dumazet bitmap_zero(bitmap, PORTS_PER_CHAIN); 186645ca708SEric Dumazet spin_lock_bh(&hslot->lock); 18798322f22SEric Dumazet udp_lib_lport_inuse(net, snum, hslot, bitmap, sk, 18898322f22SEric Dumazet saddr_comp); 18998322f22SEric Dumazet 19098322f22SEric Dumazet snum = first; 19198322f22SEric Dumazet /* 19298322f22SEric Dumazet * Iterate on all possible values of snum for this hash. 19398322f22SEric Dumazet * Using steps of an odd multiple of UDP_HTABLE_SIZE 19498322f22SEric Dumazet * give us randomization and full range coverage. 19598322f22SEric Dumazet */ 1969088c560SEric Dumazet do { 19798322f22SEric Dumazet if (low <= snum && snum <= high && 19898322f22SEric Dumazet !test_bit(snum / UDP_HTABLE_SIZE, bitmap)) 19998322f22SEric Dumazet goto found; 20098322f22SEric Dumazet snum += rand; 20198322f22SEric Dumazet } while (snum != first); 20298322f22SEric Dumazet spin_unlock_bh(&hslot->lock); 2039088c560SEric Dumazet } 20498322f22SEric Dumazet goto fail; 205645ca708SEric Dumazet } else { 206645ca708SEric Dumazet hslot = &udptable->hash[udp_hashfn(net, snum)]; 207645ca708SEric Dumazet spin_lock_bh(&hslot->lock); 20898322f22SEric Dumazet if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk, saddr_comp)) 209645ca708SEric Dumazet goto fail_unlock; 210645ca708SEric Dumazet } 21198322f22SEric Dumazet found: 21225030a7fSGerrit Renker inet_sk(sk)->num = snum; 213df2bc459SDavid S. Miller sk->sk_hash = snum; 2141da177e4SLinus Torvalds if (sk_unhashed(sk)) { 21588ab1932SEric Dumazet sk_nulls_add_node_rcu(sk, &hslot->head); 216c29a0bc4SPavel Emelyanov sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 2171da177e4SLinus Torvalds } 21825030a7fSGerrit Renker error = 0; 219645ca708SEric Dumazet fail_unlock: 220645ca708SEric Dumazet spin_unlock_bh(&hslot->lock); 2211da177e4SLinus Torvalds fail: 22225030a7fSGerrit Renker return error; 2231da177e4SLinus Torvalds } 2241da177e4SLinus Torvalds 225499923c7SVlad Yasevich static int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2) 226db8dac20SDavid S. Miller { 227db8dac20SDavid S. Miller struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2); 228db8dac20SDavid S. Miller 229db8dac20SDavid S. Miller return ( !ipv6_only_sock(sk2) && 230db8dac20SDavid S. Miller (!inet1->rcv_saddr || !inet2->rcv_saddr || 231db8dac20SDavid S. Miller inet1->rcv_saddr == inet2->rcv_saddr )); 232db8dac20SDavid S. Miller } 233db8dac20SDavid S. Miller 2346ba5a3c5SPavel Emelyanov int udp_v4_get_port(struct sock *sk, unsigned short snum) 235db8dac20SDavid S. Miller { 2366ba5a3c5SPavel Emelyanov return udp_lib_get_port(sk, snum, ipv4_rcv_saddr_equal); 237db8dac20SDavid S. Miller } 238db8dac20SDavid S. Miller 239645ca708SEric Dumazet static inline int compute_score(struct sock *sk, struct net *net, __be32 saddr, 240645ca708SEric Dumazet unsigned short hnum, 241645ca708SEric Dumazet __be16 sport, __be32 daddr, __be16 dport, int dif) 242645ca708SEric Dumazet { 243645ca708SEric Dumazet int score = -1; 244645ca708SEric Dumazet 245645ca708SEric Dumazet if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum && 246645ca708SEric Dumazet !ipv6_only_sock(sk)) { 247645ca708SEric Dumazet struct inet_sock *inet = inet_sk(sk); 248645ca708SEric Dumazet 249645ca708SEric Dumazet score = (sk->sk_family == PF_INET ? 1 : 0); 250645ca708SEric Dumazet if (inet->rcv_saddr) { 251645ca708SEric Dumazet if (inet->rcv_saddr != daddr) 252645ca708SEric Dumazet return -1; 253645ca708SEric Dumazet score += 2; 254645ca708SEric Dumazet } 255645ca708SEric Dumazet if (inet->daddr) { 256645ca708SEric Dumazet if (inet->daddr != saddr) 257645ca708SEric Dumazet return -1; 258645ca708SEric Dumazet score += 2; 259645ca708SEric Dumazet } 260645ca708SEric Dumazet if (inet->dport) { 261645ca708SEric Dumazet if (inet->dport != sport) 262645ca708SEric Dumazet return -1; 263645ca708SEric Dumazet score += 2; 264645ca708SEric Dumazet } 265645ca708SEric Dumazet if (sk->sk_bound_dev_if) { 266645ca708SEric Dumazet if (sk->sk_bound_dev_if != dif) 267645ca708SEric Dumazet return -1; 268645ca708SEric Dumazet score += 2; 269645ca708SEric Dumazet } 270645ca708SEric Dumazet } 271645ca708SEric Dumazet return score; 272645ca708SEric Dumazet } 273645ca708SEric Dumazet 274db8dac20SDavid S. Miller /* UDP is nearly always wildcards out the wazoo, it makes no sense to try 275db8dac20SDavid S. Miller * harder than this. -DaveM 276db8dac20SDavid S. Miller */ 277db8dac20SDavid S. Miller static struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, 278db8dac20SDavid S. Miller __be16 sport, __be32 daddr, __be16 dport, 279645ca708SEric Dumazet int dif, struct udp_table *udptable) 280db8dac20SDavid S. Miller { 281271b72c7SEric Dumazet struct sock *sk, *result; 28288ab1932SEric Dumazet struct hlist_nulls_node *node; 283db8dac20SDavid S. Miller unsigned short hnum = ntohs(dport); 284645ca708SEric Dumazet unsigned int hash = udp_hashfn(net, hnum); 285645ca708SEric Dumazet struct udp_hslot *hslot = &udptable->hash[hash]; 286271b72c7SEric Dumazet int score, badness; 287db8dac20SDavid S. Miller 288271b72c7SEric Dumazet rcu_read_lock(); 289271b72c7SEric Dumazet begin: 290271b72c7SEric Dumazet result = NULL; 291271b72c7SEric Dumazet badness = -1; 29288ab1932SEric Dumazet sk_nulls_for_each_rcu(sk, node, &hslot->head) { 293645ca708SEric Dumazet score = compute_score(sk, net, saddr, hnum, sport, 294645ca708SEric Dumazet daddr, dport, dif); 295645ca708SEric Dumazet if (score > badness) { 296db8dac20SDavid S. Miller result = sk; 297db8dac20SDavid S. Miller badness = score; 298db8dac20SDavid S. Miller } 299db8dac20SDavid S. Miller } 30088ab1932SEric Dumazet /* 30188ab1932SEric Dumazet * if the nulls value we got at the end of this lookup is 30288ab1932SEric Dumazet * not the expected one, we must restart lookup. 30388ab1932SEric Dumazet * We probably met an item that was moved to another chain. 30488ab1932SEric Dumazet */ 30588ab1932SEric Dumazet if (get_nulls_value(node) != hash) 30688ab1932SEric Dumazet goto begin; 30788ab1932SEric Dumazet 308271b72c7SEric Dumazet if (result) { 309271b72c7SEric Dumazet if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt))) 310271b72c7SEric Dumazet result = NULL; 311271b72c7SEric Dumazet else if (unlikely(compute_score(result, net, saddr, hnum, sport, 312271b72c7SEric Dumazet daddr, dport, dif) < badness)) { 313271b72c7SEric Dumazet sock_put(result); 314271b72c7SEric Dumazet goto begin; 315271b72c7SEric Dumazet } 316271b72c7SEric Dumazet } 317271b72c7SEric Dumazet rcu_read_unlock(); 318db8dac20SDavid S. Miller return result; 319db8dac20SDavid S. Miller } 320db8dac20SDavid S. Miller 321607c4aafSKOVACS Krisztian static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb, 322607c4aafSKOVACS Krisztian __be16 sport, __be16 dport, 323645ca708SEric Dumazet struct udp_table *udptable) 324607c4aafSKOVACS Krisztian { 32523542618SKOVACS Krisztian struct sock *sk; 326607c4aafSKOVACS Krisztian const struct iphdr *iph = ip_hdr(skb); 327607c4aafSKOVACS Krisztian 32823542618SKOVACS Krisztian if (unlikely(sk = skb_steal_sock(skb))) 32923542618SKOVACS Krisztian return sk; 33023542618SKOVACS Krisztian else 331adf30907SEric Dumazet return __udp4_lib_lookup(dev_net(skb_dst(skb)->dev), iph->saddr, sport, 332607c4aafSKOVACS Krisztian iph->daddr, dport, inet_iif(skb), 333607c4aafSKOVACS Krisztian udptable); 334607c4aafSKOVACS Krisztian } 335607c4aafSKOVACS Krisztian 336bcd41303SKOVACS Krisztian struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport, 337bcd41303SKOVACS Krisztian __be32 daddr, __be16 dport, int dif) 338bcd41303SKOVACS Krisztian { 339645ca708SEric Dumazet return __udp4_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table); 340bcd41303SKOVACS Krisztian } 341bcd41303SKOVACS Krisztian EXPORT_SYMBOL_GPL(udp4_lib_lookup); 342bcd41303SKOVACS Krisztian 343920a4611SEric Dumazet static inline struct sock *udp_v4_mcast_next(struct net *net, struct sock *sk, 344db8dac20SDavid S. Miller __be16 loc_port, __be32 loc_addr, 345db8dac20SDavid S. Miller __be16 rmt_port, __be32 rmt_addr, 346db8dac20SDavid S. Miller int dif) 347db8dac20SDavid S. Miller { 34888ab1932SEric Dumazet struct hlist_nulls_node *node; 349db8dac20SDavid S. Miller struct sock *s = sk; 350db8dac20SDavid S. Miller unsigned short hnum = ntohs(loc_port); 351db8dac20SDavid S. Miller 35288ab1932SEric Dumazet sk_nulls_for_each_from(s, node) { 353db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(s); 354db8dac20SDavid S. Miller 355920a4611SEric Dumazet if (!net_eq(sock_net(s), net) || 356920a4611SEric Dumazet s->sk_hash != hnum || 357db8dac20SDavid S. Miller (inet->daddr && inet->daddr != rmt_addr) || 358db8dac20SDavid S. Miller (inet->dport != rmt_port && inet->dport) || 359db8dac20SDavid S. Miller (inet->rcv_saddr && inet->rcv_saddr != loc_addr) || 360db8dac20SDavid S. Miller ipv6_only_sock(s) || 361db8dac20SDavid S. Miller (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)) 362db8dac20SDavid S. Miller continue; 363db8dac20SDavid S. Miller if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif)) 364db8dac20SDavid S. Miller continue; 365db8dac20SDavid S. Miller goto found; 366db8dac20SDavid S. Miller } 367db8dac20SDavid S. Miller s = NULL; 368db8dac20SDavid S. Miller found: 369db8dac20SDavid S. Miller return s; 370db8dac20SDavid S. Miller } 371db8dac20SDavid S. Miller 372db8dac20SDavid S. Miller /* 373db8dac20SDavid S. Miller * This routine is called by the ICMP module when it gets some 374db8dac20SDavid S. Miller * sort of error condition. If err < 0 then the socket should 375db8dac20SDavid S. Miller * be closed and the error returned to the user. If err > 0 376db8dac20SDavid S. Miller * it's just the icmp type << 8 | icmp code. 377db8dac20SDavid S. Miller * Header points to the ip header of the error packet. We move 378db8dac20SDavid S. Miller * on past this. Then (as it used to claim before adjustment) 379db8dac20SDavid S. Miller * header points to the first 8 bytes of the udp header. We need 380db8dac20SDavid S. Miller * to find the appropriate port. 381db8dac20SDavid S. Miller */ 382db8dac20SDavid S. Miller 383645ca708SEric Dumazet void __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable) 384db8dac20SDavid S. Miller { 385db8dac20SDavid S. Miller struct inet_sock *inet; 386db8dac20SDavid S. Miller struct iphdr *iph = (struct iphdr*)skb->data; 387db8dac20SDavid S. Miller struct udphdr *uh = (struct udphdr*)(skb->data+(iph->ihl<<2)); 388db8dac20SDavid S. Miller const int type = icmp_hdr(skb)->type; 389db8dac20SDavid S. Miller const int code = icmp_hdr(skb)->code; 390db8dac20SDavid S. Miller struct sock *sk; 391db8dac20SDavid S. Miller int harderr; 392db8dac20SDavid S. Miller int err; 393fd54d716SPavel Emelyanov struct net *net = dev_net(skb->dev); 394db8dac20SDavid S. Miller 395fd54d716SPavel Emelyanov sk = __udp4_lib_lookup(net, iph->daddr, uh->dest, 396db8dac20SDavid S. Miller iph->saddr, uh->source, skb->dev->ifindex, udptable); 397db8dac20SDavid S. Miller if (sk == NULL) { 398dcfc23caSPavel Emelyanov ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS); 399db8dac20SDavid S. Miller return; /* No socket for error */ 400db8dac20SDavid S. Miller } 401db8dac20SDavid S. Miller 402db8dac20SDavid S. Miller err = 0; 403db8dac20SDavid S. Miller harderr = 0; 404db8dac20SDavid S. Miller inet = inet_sk(sk); 405db8dac20SDavid S. Miller 406db8dac20SDavid S. Miller switch (type) { 407db8dac20SDavid S. Miller default: 408db8dac20SDavid S. Miller case ICMP_TIME_EXCEEDED: 409db8dac20SDavid S. Miller err = EHOSTUNREACH; 410db8dac20SDavid S. Miller break; 411db8dac20SDavid S. Miller case ICMP_SOURCE_QUENCH: 412db8dac20SDavid S. Miller goto out; 413db8dac20SDavid S. Miller case ICMP_PARAMETERPROB: 414db8dac20SDavid S. Miller err = EPROTO; 415db8dac20SDavid S. Miller harderr = 1; 416db8dac20SDavid S. Miller break; 417db8dac20SDavid S. Miller case ICMP_DEST_UNREACH: 418db8dac20SDavid S. Miller if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */ 419db8dac20SDavid S. Miller if (inet->pmtudisc != IP_PMTUDISC_DONT) { 420db8dac20SDavid S. Miller err = EMSGSIZE; 421db8dac20SDavid S. Miller harderr = 1; 422db8dac20SDavid S. Miller break; 423db8dac20SDavid S. Miller } 424db8dac20SDavid S. Miller goto out; 425db8dac20SDavid S. Miller } 426db8dac20SDavid S. Miller err = EHOSTUNREACH; 427db8dac20SDavid S. Miller if (code <= NR_ICMP_UNREACH) { 428db8dac20SDavid S. Miller harderr = icmp_err_convert[code].fatal; 429db8dac20SDavid S. Miller err = icmp_err_convert[code].errno; 430db8dac20SDavid S. Miller } 431db8dac20SDavid S. Miller break; 432db8dac20SDavid S. Miller } 433db8dac20SDavid S. Miller 434db8dac20SDavid S. Miller /* 435db8dac20SDavid S. Miller * RFC1122: OK. Passes ICMP errors back to application, as per 436db8dac20SDavid S. Miller * 4.1.3.3. 437db8dac20SDavid S. Miller */ 438db8dac20SDavid S. Miller if (!inet->recverr) { 439db8dac20SDavid S. Miller if (!harderr || sk->sk_state != TCP_ESTABLISHED) 440db8dac20SDavid S. Miller goto out; 441db8dac20SDavid S. Miller } else { 442db8dac20SDavid S. Miller ip_icmp_error(sk, skb, err, uh->dest, info, (u8*)(uh+1)); 443db8dac20SDavid S. Miller } 444db8dac20SDavid S. Miller sk->sk_err = err; 445db8dac20SDavid S. Miller sk->sk_error_report(sk); 446db8dac20SDavid S. Miller out: 447db8dac20SDavid S. Miller sock_put(sk); 448db8dac20SDavid S. Miller } 449db8dac20SDavid S. Miller 450db8dac20SDavid S. Miller void udp_err(struct sk_buff *skb, u32 info) 451db8dac20SDavid S. Miller { 452645ca708SEric Dumazet __udp4_lib_err(skb, info, &udp_table); 453db8dac20SDavid S. Miller } 454db8dac20SDavid S. Miller 455db8dac20SDavid S. Miller /* 456db8dac20SDavid S. Miller * Throw away all pending data and cancel the corking. Socket is locked. 457db8dac20SDavid S. Miller */ 45836d926b9SDenis V. Lunev void udp_flush_pending_frames(struct sock *sk) 459db8dac20SDavid S. Miller { 460db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 461db8dac20SDavid S. Miller 462db8dac20SDavid S. Miller if (up->pending) { 463db8dac20SDavid S. Miller up->len = 0; 464db8dac20SDavid S. Miller up->pending = 0; 465db8dac20SDavid S. Miller ip_flush_pending_frames(sk); 466db8dac20SDavid S. Miller } 467db8dac20SDavid S. Miller } 46836d926b9SDenis V. Lunev EXPORT_SYMBOL(udp_flush_pending_frames); 469db8dac20SDavid S. Miller 470db8dac20SDavid S. Miller /** 471db8dac20SDavid S. Miller * udp4_hwcsum_outgoing - handle outgoing HW checksumming 472db8dac20SDavid S. Miller * @sk: socket we are sending on 473db8dac20SDavid S. Miller * @skb: sk_buff containing the filled-in UDP header 474db8dac20SDavid S. Miller * (checksum field must be zeroed out) 475db8dac20SDavid S. Miller */ 476db8dac20SDavid S. Miller static void udp4_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb, 477db8dac20SDavid S. Miller __be32 src, __be32 dst, int len ) 478db8dac20SDavid S. Miller { 479db8dac20SDavid S. Miller unsigned int offset; 480db8dac20SDavid S. Miller struct udphdr *uh = udp_hdr(skb); 481db8dac20SDavid S. Miller __wsum csum = 0; 482db8dac20SDavid S. Miller 483db8dac20SDavid S. Miller if (skb_queue_len(&sk->sk_write_queue) == 1) { 484db8dac20SDavid S. Miller /* 485db8dac20SDavid S. Miller * Only one fragment on the socket. 486db8dac20SDavid S. Miller */ 487db8dac20SDavid S. Miller skb->csum_start = skb_transport_header(skb) - skb->head; 488db8dac20SDavid S. Miller skb->csum_offset = offsetof(struct udphdr, check); 489db8dac20SDavid S. Miller uh->check = ~csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, 0); 490db8dac20SDavid S. Miller } else { 491db8dac20SDavid S. Miller /* 492db8dac20SDavid S. Miller * HW-checksum won't work as there are two or more 493db8dac20SDavid S. Miller * fragments on the socket so that all csums of sk_buffs 494db8dac20SDavid S. Miller * should be together 495db8dac20SDavid S. Miller */ 496db8dac20SDavid S. Miller offset = skb_transport_offset(skb); 497db8dac20SDavid S. Miller skb->csum = skb_checksum(skb, offset, skb->len - offset, 0); 498db8dac20SDavid S. Miller 499db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_NONE; 500db8dac20SDavid S. Miller 501db8dac20SDavid S. Miller skb_queue_walk(&sk->sk_write_queue, skb) { 502db8dac20SDavid S. Miller csum = csum_add(csum, skb->csum); 503db8dac20SDavid S. Miller } 504db8dac20SDavid S. Miller 505db8dac20SDavid S. Miller uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum); 506db8dac20SDavid S. Miller if (uh->check == 0) 507db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 508db8dac20SDavid S. Miller } 509db8dac20SDavid S. Miller } 510db8dac20SDavid S. Miller 511db8dac20SDavid S. Miller /* 512db8dac20SDavid S. Miller * Push out all pending data as one UDP datagram. Socket is locked. 513db8dac20SDavid S. Miller */ 514db8dac20SDavid S. Miller static int udp_push_pending_frames(struct sock *sk) 515db8dac20SDavid S. Miller { 516db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 517db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 518db8dac20SDavid S. Miller struct flowi *fl = &inet->cork.fl; 519db8dac20SDavid S. Miller struct sk_buff *skb; 520db8dac20SDavid S. Miller struct udphdr *uh; 521db8dac20SDavid S. Miller int err = 0; 522db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 523db8dac20SDavid S. Miller __wsum csum = 0; 524db8dac20SDavid S. Miller 525db8dac20SDavid S. Miller /* Grab the skbuff where UDP header space exists. */ 526db8dac20SDavid S. Miller if ((skb = skb_peek(&sk->sk_write_queue)) == NULL) 527db8dac20SDavid S. Miller goto out; 528db8dac20SDavid S. Miller 529db8dac20SDavid S. Miller /* 530db8dac20SDavid S. Miller * Create a UDP header 531db8dac20SDavid S. Miller */ 532db8dac20SDavid S. Miller uh = udp_hdr(skb); 533db8dac20SDavid S. Miller uh->source = fl->fl_ip_sport; 534db8dac20SDavid S. Miller uh->dest = fl->fl_ip_dport; 535db8dac20SDavid S. Miller uh->len = htons(up->len); 536db8dac20SDavid S. Miller uh->check = 0; 537db8dac20SDavid S. Miller 538db8dac20SDavid S. Miller if (is_udplite) /* UDP-Lite */ 539db8dac20SDavid S. Miller csum = udplite_csum_outgoing(sk, skb); 540db8dac20SDavid S. Miller 541db8dac20SDavid S. Miller else if (sk->sk_no_check == UDP_CSUM_NOXMIT) { /* UDP csum disabled */ 542db8dac20SDavid S. Miller 543db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_NONE; 544db8dac20SDavid S. Miller goto send; 545db8dac20SDavid S. Miller 546db8dac20SDavid S. Miller } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 547db8dac20SDavid S. Miller 548db8dac20SDavid S. Miller udp4_hwcsum_outgoing(sk, skb, fl->fl4_src,fl->fl4_dst, up->len); 549db8dac20SDavid S. Miller goto send; 550db8dac20SDavid S. Miller 551db8dac20SDavid S. Miller } else /* `normal' UDP */ 552db8dac20SDavid S. Miller csum = udp_csum_outgoing(sk, skb); 553db8dac20SDavid S. Miller 554db8dac20SDavid S. Miller /* add protocol-dependent pseudo-header */ 555db8dac20SDavid S. Miller uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst, up->len, 556db8dac20SDavid S. Miller sk->sk_protocol, csum ); 557db8dac20SDavid S. Miller if (uh->check == 0) 558db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 559db8dac20SDavid S. Miller 560db8dac20SDavid S. Miller send: 561db8dac20SDavid S. Miller err = ip_push_pending_frames(sk); 562db8dac20SDavid S. Miller out: 563db8dac20SDavid S. Miller up->len = 0; 564db8dac20SDavid S. Miller up->pending = 0; 565db8dac20SDavid S. Miller if (!err) 566629ca23cSPavel Emelyanov UDP_INC_STATS_USER(sock_net(sk), 567629ca23cSPavel Emelyanov UDP_MIB_OUTDATAGRAMS, is_udplite); 568db8dac20SDavid S. Miller return err; 569db8dac20SDavid S. Miller } 570db8dac20SDavid S. Miller 571db8dac20SDavid S. Miller int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 572db8dac20SDavid S. Miller size_t len) 573db8dac20SDavid S. Miller { 574db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 575db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 576db8dac20SDavid S. Miller int ulen = len; 577db8dac20SDavid S. Miller struct ipcm_cookie ipc; 578db8dac20SDavid S. Miller struct rtable *rt = NULL; 579db8dac20SDavid S. Miller int free = 0; 580db8dac20SDavid S. Miller int connected = 0; 581db8dac20SDavid S. Miller __be32 daddr, faddr, saddr; 582db8dac20SDavid S. Miller __be16 dport; 583db8dac20SDavid S. Miller u8 tos; 584db8dac20SDavid S. Miller int err, is_udplite = IS_UDPLITE(sk); 585db8dac20SDavid S. Miller int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; 586db8dac20SDavid S. Miller int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 587db8dac20SDavid S. Miller 588db8dac20SDavid S. Miller if (len > 0xFFFF) 589db8dac20SDavid S. Miller return -EMSGSIZE; 590db8dac20SDavid S. Miller 591db8dac20SDavid S. Miller /* 592db8dac20SDavid S. Miller * Check the flags. 593db8dac20SDavid S. Miller */ 594db8dac20SDavid S. Miller 595db8dac20SDavid S. Miller if (msg->msg_flags&MSG_OOB) /* Mirror BSD error message compatibility */ 596db8dac20SDavid S. Miller return -EOPNOTSUPP; 597db8dac20SDavid S. Miller 598db8dac20SDavid S. Miller ipc.opt = NULL; 59951f31cabSPatrick Ohly ipc.shtx.flags = 0; 600db8dac20SDavid S. Miller 601db8dac20SDavid S. Miller if (up->pending) { 602db8dac20SDavid S. Miller /* 603db8dac20SDavid S. Miller * There are pending frames. 604db8dac20SDavid S. Miller * The socket lock must be held while it's corked. 605db8dac20SDavid S. Miller */ 606db8dac20SDavid S. Miller lock_sock(sk); 607db8dac20SDavid S. Miller if (likely(up->pending)) { 608db8dac20SDavid S. Miller if (unlikely(up->pending != AF_INET)) { 609db8dac20SDavid S. Miller release_sock(sk); 610db8dac20SDavid S. Miller return -EINVAL; 611db8dac20SDavid S. Miller } 612db8dac20SDavid S. Miller goto do_append_data; 613db8dac20SDavid S. Miller } 614db8dac20SDavid S. Miller release_sock(sk); 615db8dac20SDavid S. Miller } 616db8dac20SDavid S. Miller ulen += sizeof(struct udphdr); 617db8dac20SDavid S. Miller 618db8dac20SDavid S. Miller /* 619db8dac20SDavid S. Miller * Get and verify the address. 620db8dac20SDavid S. Miller */ 621db8dac20SDavid S. Miller if (msg->msg_name) { 622db8dac20SDavid S. Miller struct sockaddr_in * usin = (struct sockaddr_in*)msg->msg_name; 623db8dac20SDavid S. Miller if (msg->msg_namelen < sizeof(*usin)) 624db8dac20SDavid S. Miller return -EINVAL; 625db8dac20SDavid S. Miller if (usin->sin_family != AF_INET) { 626db8dac20SDavid S. Miller if (usin->sin_family != AF_UNSPEC) 627db8dac20SDavid S. Miller return -EAFNOSUPPORT; 628db8dac20SDavid S. Miller } 629db8dac20SDavid S. Miller 630db8dac20SDavid S. Miller daddr = usin->sin_addr.s_addr; 631db8dac20SDavid S. Miller dport = usin->sin_port; 632db8dac20SDavid S. Miller if (dport == 0) 633db8dac20SDavid S. Miller return -EINVAL; 634db8dac20SDavid S. Miller } else { 635db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 636db8dac20SDavid S. Miller return -EDESTADDRREQ; 637db8dac20SDavid S. Miller daddr = inet->daddr; 638db8dac20SDavid S. Miller dport = inet->dport; 639db8dac20SDavid S. Miller /* Open fast path for connected socket. 640db8dac20SDavid S. Miller Route will not be used, if at least one option is set. 641db8dac20SDavid S. Miller */ 642db8dac20SDavid S. Miller connected = 1; 643db8dac20SDavid S. Miller } 644db8dac20SDavid S. Miller ipc.addr = inet->saddr; 645db8dac20SDavid S. Miller 646db8dac20SDavid S. Miller ipc.oif = sk->sk_bound_dev_if; 64751f31cabSPatrick Ohly err = sock_tx_timestamp(msg, sk, &ipc.shtx); 64851f31cabSPatrick Ohly if (err) 64951f31cabSPatrick Ohly return err; 650db8dac20SDavid S. Miller if (msg->msg_controllen) { 6513b1e0a65SYOSHIFUJI Hideaki err = ip_cmsg_send(sock_net(sk), msg, &ipc); 652db8dac20SDavid S. Miller if (err) 653db8dac20SDavid S. Miller return err; 654db8dac20SDavid S. Miller if (ipc.opt) 655db8dac20SDavid S. Miller free = 1; 656db8dac20SDavid S. Miller connected = 0; 657db8dac20SDavid S. Miller } 658db8dac20SDavid S. Miller if (!ipc.opt) 659db8dac20SDavid S. Miller ipc.opt = inet->opt; 660db8dac20SDavid S. Miller 661db8dac20SDavid S. Miller saddr = ipc.addr; 662db8dac20SDavid S. Miller ipc.addr = faddr = daddr; 663db8dac20SDavid S. Miller 664db8dac20SDavid S. Miller if (ipc.opt && ipc.opt->srr) { 665db8dac20SDavid S. Miller if (!daddr) 666db8dac20SDavid S. Miller return -EINVAL; 667db8dac20SDavid S. Miller faddr = ipc.opt->faddr; 668db8dac20SDavid S. Miller connected = 0; 669db8dac20SDavid S. Miller } 670db8dac20SDavid S. Miller tos = RT_TOS(inet->tos); 671db8dac20SDavid S. Miller if (sock_flag(sk, SOCK_LOCALROUTE) || 672db8dac20SDavid S. Miller (msg->msg_flags & MSG_DONTROUTE) || 673db8dac20SDavid S. Miller (ipc.opt && ipc.opt->is_strictroute)) { 674db8dac20SDavid S. Miller tos |= RTO_ONLINK; 675db8dac20SDavid S. Miller connected = 0; 676db8dac20SDavid S. Miller } 677db8dac20SDavid S. Miller 678db8dac20SDavid S. Miller if (ipv4_is_multicast(daddr)) { 679db8dac20SDavid S. Miller if (!ipc.oif) 680db8dac20SDavid S. Miller ipc.oif = inet->mc_index; 681db8dac20SDavid S. Miller if (!saddr) 682db8dac20SDavid S. Miller saddr = inet->mc_addr; 683db8dac20SDavid S. Miller connected = 0; 684db8dac20SDavid S. Miller } 685db8dac20SDavid S. Miller 686db8dac20SDavid S. Miller if (connected) 687db8dac20SDavid S. Miller rt = (struct rtable*)sk_dst_check(sk, 0); 688db8dac20SDavid S. Miller 689db8dac20SDavid S. Miller if (rt == NULL) { 690db8dac20SDavid S. Miller struct flowi fl = { .oif = ipc.oif, 691db8dac20SDavid S. Miller .nl_u = { .ip4_u = 692db8dac20SDavid S. Miller { .daddr = faddr, 693db8dac20SDavid S. Miller .saddr = saddr, 694db8dac20SDavid S. Miller .tos = tos } }, 695db8dac20SDavid S. Miller .proto = sk->sk_protocol, 696a134f85cSBalazs Scheidler .flags = inet_sk_flowi_flags(sk), 697db8dac20SDavid S. Miller .uli_u = { .ports = 698db8dac20SDavid S. Miller { .sport = inet->sport, 699db8dac20SDavid S. Miller .dport = dport } } }; 70084a3aa00SPavel Emelyanov struct net *net = sock_net(sk); 70184a3aa00SPavel Emelyanov 702db8dac20SDavid S. Miller security_sk_classify_flow(sk, &fl); 70384a3aa00SPavel Emelyanov err = ip_route_output_flow(net, &rt, &fl, sk, 1); 704db8dac20SDavid S. Miller if (err) { 705db8dac20SDavid S. Miller if (err == -ENETUNREACH) 7067c73a6faSPavel Emelyanov IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES); 707db8dac20SDavid S. Miller goto out; 708db8dac20SDavid S. Miller } 709db8dac20SDavid S. Miller 710db8dac20SDavid S. Miller err = -EACCES; 711db8dac20SDavid S. Miller if ((rt->rt_flags & RTCF_BROADCAST) && 712db8dac20SDavid S. Miller !sock_flag(sk, SOCK_BROADCAST)) 713db8dac20SDavid S. Miller goto out; 714db8dac20SDavid S. Miller if (connected) 715db8dac20SDavid S. Miller sk_dst_set(sk, dst_clone(&rt->u.dst)); 716db8dac20SDavid S. Miller } 717db8dac20SDavid S. Miller 718db8dac20SDavid S. Miller if (msg->msg_flags&MSG_CONFIRM) 719db8dac20SDavid S. Miller goto do_confirm; 720db8dac20SDavid S. Miller back_from_confirm: 721db8dac20SDavid S. Miller 722db8dac20SDavid S. Miller saddr = rt->rt_src; 723db8dac20SDavid S. Miller if (!ipc.addr) 724db8dac20SDavid S. Miller daddr = ipc.addr = rt->rt_dst; 725db8dac20SDavid S. Miller 726db8dac20SDavid S. Miller lock_sock(sk); 727db8dac20SDavid S. Miller if (unlikely(up->pending)) { 728db8dac20SDavid S. Miller /* The socket is already corked while preparing it. */ 729db8dac20SDavid S. Miller /* ... which is an evident application bug. --ANK */ 730db8dac20SDavid S. Miller release_sock(sk); 731db8dac20SDavid S. Miller 732db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n"); 733db8dac20SDavid S. Miller err = -EINVAL; 734db8dac20SDavid S. Miller goto out; 735db8dac20SDavid S. Miller } 736db8dac20SDavid S. Miller /* 737db8dac20SDavid S. Miller * Now cork the socket to pend data. 738db8dac20SDavid S. Miller */ 739db8dac20SDavid S. Miller inet->cork.fl.fl4_dst = daddr; 740db8dac20SDavid S. Miller inet->cork.fl.fl_ip_dport = dport; 741db8dac20SDavid S. Miller inet->cork.fl.fl4_src = saddr; 742db8dac20SDavid S. Miller inet->cork.fl.fl_ip_sport = inet->sport; 743db8dac20SDavid S. Miller up->pending = AF_INET; 744db8dac20SDavid S. Miller 745db8dac20SDavid S. Miller do_append_data: 746db8dac20SDavid S. Miller up->len += ulen; 747db8dac20SDavid S. Miller getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 748db8dac20SDavid S. Miller err = ip_append_data(sk, getfrag, msg->msg_iov, ulen, 7492e77d89bSEric Dumazet sizeof(struct udphdr), &ipc, &rt, 750db8dac20SDavid S. Miller corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); 751db8dac20SDavid S. Miller if (err) 752db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 753db8dac20SDavid S. Miller else if (!corkreq) 754db8dac20SDavid S. Miller err = udp_push_pending_frames(sk); 755db8dac20SDavid S. Miller else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 756db8dac20SDavid S. Miller up->pending = 0; 757db8dac20SDavid S. Miller release_sock(sk); 758db8dac20SDavid S. Miller 759db8dac20SDavid S. Miller out: 760db8dac20SDavid S. Miller ip_rt_put(rt); 761db8dac20SDavid S. Miller if (free) 762db8dac20SDavid S. Miller kfree(ipc.opt); 763db8dac20SDavid S. Miller if (!err) 764db8dac20SDavid S. Miller return len; 765db8dac20SDavid S. Miller /* 766db8dac20SDavid S. Miller * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 767db8dac20SDavid S. Miller * ENOBUFS might not be good (it's not tunable per se), but otherwise 768db8dac20SDavid S. Miller * we don't have a good statistic (IpOutDiscards but it can be too many 769db8dac20SDavid S. Miller * things). We could add another new stat but at least for now that 770db8dac20SDavid S. Miller * seems like overkill. 771db8dac20SDavid S. Miller */ 772db8dac20SDavid S. Miller if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 773629ca23cSPavel Emelyanov UDP_INC_STATS_USER(sock_net(sk), 774629ca23cSPavel Emelyanov UDP_MIB_SNDBUFERRORS, is_udplite); 775db8dac20SDavid S. Miller } 776db8dac20SDavid S. Miller return err; 777db8dac20SDavid S. Miller 778db8dac20SDavid S. Miller do_confirm: 779db8dac20SDavid S. Miller dst_confirm(&rt->u.dst); 780db8dac20SDavid S. Miller if (!(msg->msg_flags&MSG_PROBE) || len) 781db8dac20SDavid S. Miller goto back_from_confirm; 782db8dac20SDavid S. Miller err = 0; 783db8dac20SDavid S. Miller goto out; 784db8dac20SDavid S. Miller } 785db8dac20SDavid S. Miller 786db8dac20SDavid S. Miller int udp_sendpage(struct sock *sk, struct page *page, int offset, 787db8dac20SDavid S. Miller size_t size, int flags) 788db8dac20SDavid S. Miller { 789db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 790db8dac20SDavid S. Miller int ret; 791db8dac20SDavid S. Miller 792db8dac20SDavid S. Miller if (!up->pending) { 793db8dac20SDavid S. Miller struct msghdr msg = { .msg_flags = flags|MSG_MORE }; 794db8dac20SDavid S. Miller 795db8dac20SDavid S. Miller /* Call udp_sendmsg to specify destination address which 796db8dac20SDavid S. Miller * sendpage interface can't pass. 797db8dac20SDavid S. Miller * This will succeed only when the socket is connected. 798db8dac20SDavid S. Miller */ 799db8dac20SDavid S. Miller ret = udp_sendmsg(NULL, sk, &msg, 0); 800db8dac20SDavid S. Miller if (ret < 0) 801db8dac20SDavid S. Miller return ret; 802db8dac20SDavid S. Miller } 803db8dac20SDavid S. Miller 804db8dac20SDavid S. Miller lock_sock(sk); 805db8dac20SDavid S. Miller 806db8dac20SDavid S. Miller if (unlikely(!up->pending)) { 807db8dac20SDavid S. Miller release_sock(sk); 808db8dac20SDavid S. Miller 809db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n"); 810db8dac20SDavid S. Miller return -EINVAL; 811db8dac20SDavid S. Miller } 812db8dac20SDavid S. Miller 813db8dac20SDavid S. Miller ret = ip_append_page(sk, page, offset, size, flags); 814db8dac20SDavid S. Miller if (ret == -EOPNOTSUPP) { 815db8dac20SDavid S. Miller release_sock(sk); 816db8dac20SDavid S. Miller return sock_no_sendpage(sk->sk_socket, page, offset, 817db8dac20SDavid S. Miller size, flags); 818db8dac20SDavid S. Miller } 819db8dac20SDavid S. Miller if (ret < 0) { 820db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 821db8dac20SDavid S. Miller goto out; 822db8dac20SDavid S. Miller } 823db8dac20SDavid S. Miller 824db8dac20SDavid S. Miller up->len += size; 825db8dac20SDavid S. Miller if (!(up->corkflag || (flags&MSG_MORE))) 826db8dac20SDavid S. Miller ret = udp_push_pending_frames(sk); 827db8dac20SDavid S. Miller if (!ret) 828db8dac20SDavid S. Miller ret = size; 829db8dac20SDavid S. Miller out: 830db8dac20SDavid S. Miller release_sock(sk); 831db8dac20SDavid S. Miller return ret; 832db8dac20SDavid S. Miller } 833db8dac20SDavid S. Miller 8341da177e4SLinus Torvalds /* 8351da177e4SLinus Torvalds * IOCTL requests applicable to the UDP protocol 8361da177e4SLinus Torvalds */ 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds int udp_ioctl(struct sock *sk, int cmd, unsigned long arg) 8391da177e4SLinus Torvalds { 8406516c655SStephen Hemminger switch (cmd) { 8411da177e4SLinus Torvalds case SIOCOUTQ: 8421da177e4SLinus Torvalds { 84331e6d363SEric Dumazet int amount = sk_wmem_alloc_get(sk); 84431e6d363SEric Dumazet 8451da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 8461da177e4SLinus Torvalds } 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds case SIOCINQ: 8491da177e4SLinus Torvalds { 8501da177e4SLinus Torvalds struct sk_buff *skb; 8511da177e4SLinus Torvalds unsigned long amount; 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds amount = 0; 854208d8984SHerbert Xu spin_lock_bh(&sk->sk_receive_queue.lock); 8551da177e4SLinus Torvalds skb = skb_peek(&sk->sk_receive_queue); 8561da177e4SLinus Torvalds if (skb != NULL) { 8571da177e4SLinus Torvalds /* 8581da177e4SLinus Torvalds * We will only return the amount 8591da177e4SLinus Torvalds * of this packet since that is all 8601da177e4SLinus Torvalds * that will be read. 8611da177e4SLinus Torvalds */ 8621da177e4SLinus Torvalds amount = skb->len - sizeof(struct udphdr); 8631da177e4SLinus Torvalds } 864208d8984SHerbert Xu spin_unlock_bh(&sk->sk_receive_queue.lock); 8651da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 8661da177e4SLinus Torvalds } 8671da177e4SLinus Torvalds 8681da177e4SLinus Torvalds default: 8691da177e4SLinus Torvalds return -ENOIOCTLCMD; 8701da177e4SLinus Torvalds } 8716516c655SStephen Hemminger 8726516c655SStephen Hemminger return 0; 8731da177e4SLinus Torvalds } 8741da177e4SLinus Torvalds 875db8dac20SDavid S. Miller /* 876db8dac20SDavid S. Miller * This should be easy, if there is something there we 877db8dac20SDavid S. Miller * return it, otherwise we block. 878db8dac20SDavid S. Miller */ 879db8dac20SDavid S. Miller 880db8dac20SDavid S. Miller int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 881db8dac20SDavid S. Miller size_t len, int noblock, int flags, int *addr_len) 882db8dac20SDavid S. Miller { 883db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 884db8dac20SDavid S. Miller struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name; 885db8dac20SDavid S. Miller struct sk_buff *skb; 886db8dac20SDavid S. Miller unsigned int ulen, copied; 887db8dac20SDavid S. Miller int peeked; 888db8dac20SDavid S. Miller int err; 889db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 890db8dac20SDavid S. Miller 891db8dac20SDavid S. Miller /* 892db8dac20SDavid S. Miller * Check any passed addresses 893db8dac20SDavid S. Miller */ 894db8dac20SDavid S. Miller if (addr_len) 895db8dac20SDavid S. Miller *addr_len=sizeof(*sin); 896db8dac20SDavid S. Miller 897db8dac20SDavid S. Miller if (flags & MSG_ERRQUEUE) 898db8dac20SDavid S. Miller return ip_recv_error(sk, msg, len); 899db8dac20SDavid S. Miller 900db8dac20SDavid S. Miller try_again: 901db8dac20SDavid S. Miller skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), 902db8dac20SDavid S. Miller &peeked, &err); 903db8dac20SDavid S. Miller if (!skb) 904db8dac20SDavid S. Miller goto out; 905db8dac20SDavid S. Miller 906db8dac20SDavid S. Miller ulen = skb->len - sizeof(struct udphdr); 907db8dac20SDavid S. Miller copied = len; 908db8dac20SDavid S. Miller if (copied > ulen) 909db8dac20SDavid S. Miller copied = ulen; 910db8dac20SDavid S. Miller else if (copied < ulen) 911db8dac20SDavid S. Miller msg->msg_flags |= MSG_TRUNC; 912db8dac20SDavid S. Miller 913db8dac20SDavid S. Miller /* 914db8dac20SDavid S. Miller * If checksum is needed at all, try to do it while copying the 915db8dac20SDavid S. Miller * data. If the data is truncated, or if we only want a partial 916db8dac20SDavid S. Miller * coverage checksum (UDP-Lite), do it before the copy. 917db8dac20SDavid S. Miller */ 918db8dac20SDavid S. Miller 919db8dac20SDavid S. Miller if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) { 920db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 921db8dac20SDavid S. Miller goto csum_copy_err; 922db8dac20SDavid S. Miller } 923db8dac20SDavid S. Miller 924db8dac20SDavid S. Miller if (skb_csum_unnecessary(skb)) 925db8dac20SDavid S. Miller err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), 926db8dac20SDavid S. Miller msg->msg_iov, copied ); 927db8dac20SDavid S. Miller else { 928db8dac20SDavid S. Miller err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov); 929db8dac20SDavid S. Miller 930db8dac20SDavid S. Miller if (err == -EINVAL) 931db8dac20SDavid S. Miller goto csum_copy_err; 932db8dac20SDavid S. Miller } 933db8dac20SDavid S. Miller 934db8dac20SDavid S. Miller if (err) 935db8dac20SDavid S. Miller goto out_free; 936db8dac20SDavid S. Miller 937db8dac20SDavid S. Miller if (!peeked) 938629ca23cSPavel Emelyanov UDP_INC_STATS_USER(sock_net(sk), 939629ca23cSPavel Emelyanov UDP_MIB_INDATAGRAMS, is_udplite); 940db8dac20SDavid S. Miller 941db8dac20SDavid S. Miller sock_recv_timestamp(msg, sk, skb); 942db8dac20SDavid S. Miller 943db8dac20SDavid S. Miller /* Copy the address. */ 944db8dac20SDavid S. Miller if (sin) 945db8dac20SDavid S. Miller { 946db8dac20SDavid S. Miller sin->sin_family = AF_INET; 947db8dac20SDavid S. Miller sin->sin_port = udp_hdr(skb)->source; 948db8dac20SDavid S. Miller sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 949db8dac20SDavid S. Miller memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 950db8dac20SDavid S. Miller } 951db8dac20SDavid S. Miller if (inet->cmsg_flags) 952db8dac20SDavid S. Miller ip_cmsg_recv(msg, skb); 953db8dac20SDavid S. Miller 954db8dac20SDavid S. Miller err = copied; 955db8dac20SDavid S. Miller if (flags & MSG_TRUNC) 956db8dac20SDavid S. Miller err = ulen; 957db8dac20SDavid S. Miller 958db8dac20SDavid S. Miller out_free: 959db8dac20SDavid S. Miller lock_sock(sk); 960db8dac20SDavid S. Miller skb_free_datagram(sk, skb); 961db8dac20SDavid S. Miller release_sock(sk); 962db8dac20SDavid S. Miller out: 963db8dac20SDavid S. Miller return err; 964db8dac20SDavid S. Miller 965db8dac20SDavid S. Miller csum_copy_err: 966db8dac20SDavid S. Miller lock_sock(sk); 967db8dac20SDavid S. Miller if (!skb_kill_datagram(sk, skb, flags)) 968629ca23cSPavel Emelyanov UDP_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 969db8dac20SDavid S. Miller release_sock(sk); 970db8dac20SDavid S. Miller 971db8dac20SDavid S. Miller if (noblock) 972db8dac20SDavid S. Miller return -EAGAIN; 973db8dac20SDavid S. Miller goto try_again; 974db8dac20SDavid S. Miller } 975db8dac20SDavid S. Miller 976db8dac20SDavid S. Miller 9771da177e4SLinus Torvalds int udp_disconnect(struct sock *sk, int flags) 9781da177e4SLinus Torvalds { 9791da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 9801da177e4SLinus Torvalds /* 9811da177e4SLinus Torvalds * 1003.1g - break association. 9821da177e4SLinus Torvalds */ 9831da177e4SLinus Torvalds 9841da177e4SLinus Torvalds sk->sk_state = TCP_CLOSE; 9851da177e4SLinus Torvalds inet->daddr = 0; 9861da177e4SLinus Torvalds inet->dport = 0; 9871da177e4SLinus Torvalds sk->sk_bound_dev_if = 0; 9881da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) 9891da177e4SLinus Torvalds inet_reset_saddr(sk); 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) { 9921da177e4SLinus Torvalds sk->sk_prot->unhash(sk); 9931da177e4SLinus Torvalds inet->sport = 0; 9941da177e4SLinus Torvalds } 9951da177e4SLinus Torvalds sk_dst_reset(sk); 9961da177e4SLinus Torvalds return 0; 9971da177e4SLinus Torvalds } 9981da177e4SLinus Torvalds 999645ca708SEric Dumazet void udp_lib_unhash(struct sock *sk) 1000645ca708SEric Dumazet { 1001723b4610SEric Dumazet if (sk_hashed(sk)) { 1002645ca708SEric Dumazet struct udp_table *udptable = sk->sk_prot->h.udp_table; 1003645ca708SEric Dumazet unsigned int hash = udp_hashfn(sock_net(sk), sk->sk_hash); 1004645ca708SEric Dumazet struct udp_hslot *hslot = &udptable->hash[hash]; 1005645ca708SEric Dumazet 1006c8db3fecSEric Dumazet spin_lock_bh(&hslot->lock); 100788ab1932SEric Dumazet if (sk_nulls_del_node_init_rcu(sk)) { 1008645ca708SEric Dumazet inet_sk(sk)->num = 0; 1009645ca708SEric Dumazet sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 1010645ca708SEric Dumazet } 1011c8db3fecSEric Dumazet spin_unlock_bh(&hslot->lock); 1012645ca708SEric Dumazet } 1013723b4610SEric Dumazet } 1014645ca708SEric Dumazet EXPORT_SYMBOL(udp_lib_unhash); 1015645ca708SEric Dumazet 101693821778SHerbert Xu static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 101793821778SHerbert Xu { 101893821778SHerbert Xu int is_udplite = IS_UDPLITE(sk); 101993821778SHerbert Xu int rc; 102093821778SHerbert Xu 102193821778SHerbert Xu if ((rc = sock_queue_rcv_skb(sk, skb)) < 0) { 102293821778SHerbert Xu /* Note that an ENOMEM error is charged twice */ 1023e408b8dcSEric Dumazet if (rc == -ENOMEM) { 102493821778SHerbert Xu UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS, 102593821778SHerbert Xu is_udplite); 1026e408b8dcSEric Dumazet atomic_inc(&sk->sk_drops); 1027e408b8dcSEric Dumazet } 102893821778SHerbert Xu goto drop; 102993821778SHerbert Xu } 103093821778SHerbert Xu 103193821778SHerbert Xu return 0; 103293821778SHerbert Xu 103393821778SHerbert Xu drop: 103493821778SHerbert Xu UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 103593821778SHerbert Xu kfree_skb(skb); 103693821778SHerbert Xu return -1; 103793821778SHerbert Xu } 103893821778SHerbert Xu 1039db8dac20SDavid S. Miller /* returns: 1040db8dac20SDavid S. Miller * -1: error 1041db8dac20SDavid S. Miller * 0: success 1042db8dac20SDavid S. Miller * >0: "udp encap" protocol resubmission 1043db8dac20SDavid S. Miller * 1044db8dac20SDavid S. Miller * Note that in the success and error cases, the skb is assumed to 1045db8dac20SDavid S. Miller * have either been requeued or freed. 1046db8dac20SDavid S. Miller */ 1047db8dac20SDavid S. Miller int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb) 1048db8dac20SDavid S. Miller { 1049db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 1050db8dac20SDavid S. Miller int rc; 1051db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 1052db8dac20SDavid S. Miller 1053db8dac20SDavid S. Miller /* 1054db8dac20SDavid S. Miller * Charge it to the socket, dropping if the queue is full. 1055db8dac20SDavid S. Miller */ 1056db8dac20SDavid S. Miller if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) 1057db8dac20SDavid S. Miller goto drop; 1058db8dac20SDavid S. Miller nf_reset(skb); 1059db8dac20SDavid S. Miller 1060db8dac20SDavid S. Miller if (up->encap_type) { 1061db8dac20SDavid S. Miller /* 1062db8dac20SDavid S. Miller * This is an encapsulation socket so pass the skb to 1063db8dac20SDavid S. Miller * the socket's udp_encap_rcv() hook. Otherwise, just 1064db8dac20SDavid S. Miller * fall through and pass this up the UDP socket. 1065db8dac20SDavid S. Miller * up->encap_rcv() returns the following value: 1066db8dac20SDavid S. Miller * =0 if skb was successfully passed to the encap 1067db8dac20SDavid S. Miller * handler or was discarded by it. 1068db8dac20SDavid S. Miller * >0 if skb should be passed on to UDP. 1069db8dac20SDavid S. Miller * <0 if skb should be resubmitted as proto -N 1070db8dac20SDavid S. Miller */ 1071db8dac20SDavid S. Miller 1072db8dac20SDavid S. Miller /* if we're overly short, let UDP handle it */ 1073db8dac20SDavid S. Miller if (skb->len > sizeof(struct udphdr) && 1074db8dac20SDavid S. Miller up->encap_rcv != NULL) { 1075db8dac20SDavid S. Miller int ret; 1076db8dac20SDavid S. Miller 1077db8dac20SDavid S. Miller ret = (*up->encap_rcv)(sk, skb); 1078db8dac20SDavid S. Miller if (ret <= 0) { 10790283328eSPavel Emelyanov UDP_INC_STATS_BH(sock_net(sk), 10800283328eSPavel Emelyanov UDP_MIB_INDATAGRAMS, 1081db8dac20SDavid S. Miller is_udplite); 1082db8dac20SDavid S. Miller return -ret; 1083db8dac20SDavid S. Miller } 1084db8dac20SDavid S. Miller } 1085db8dac20SDavid S. Miller 1086db8dac20SDavid S. Miller /* FALLTHROUGH -- it's a UDP Packet */ 1087db8dac20SDavid S. Miller } 1088db8dac20SDavid S. Miller 1089db8dac20SDavid S. Miller /* 1090db8dac20SDavid S. Miller * UDP-Lite specific tests, ignored on UDP sockets 1091db8dac20SDavid S. Miller */ 1092db8dac20SDavid S. Miller if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 1093db8dac20SDavid S. Miller 1094db8dac20SDavid S. Miller /* 1095db8dac20SDavid S. Miller * MIB statistics other than incrementing the error count are 1096db8dac20SDavid S. Miller * disabled for the following two types of errors: these depend 1097db8dac20SDavid S. Miller * on the application settings, not on the functioning of the 1098db8dac20SDavid S. Miller * protocol stack as such. 1099db8dac20SDavid S. Miller * 1100db8dac20SDavid S. Miller * RFC 3828 here recommends (sec 3.3): "There should also be a 1101db8dac20SDavid S. Miller * way ... to ... at least let the receiving application block 1102db8dac20SDavid S. Miller * delivery of packets with coverage values less than a value 1103db8dac20SDavid S. Miller * provided by the application." 1104db8dac20SDavid S. Miller */ 1105db8dac20SDavid S. Miller if (up->pcrlen == 0) { /* full coverage was set */ 1106db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_WARNING "UDPLITE: partial coverage " 1107db8dac20SDavid S. Miller "%d while full coverage %d requested\n", 1108db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, skb->len); 1109db8dac20SDavid S. Miller goto drop; 1110db8dac20SDavid S. Miller } 1111db8dac20SDavid S. Miller /* The next case involves violating the min. coverage requested 1112db8dac20SDavid S. Miller * by the receiver. This is subtle: if receiver wants x and x is 1113db8dac20SDavid S. Miller * greater than the buffersize/MTU then receiver will complain 1114db8dac20SDavid S. Miller * that it wants x while sender emits packets of smaller size y. 1115db8dac20SDavid S. Miller * Therefore the above ...()->partial_cov statement is essential. 1116db8dac20SDavid S. Miller */ 1117db8dac20SDavid S. Miller if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 1118db8dac20SDavid S. Miller LIMIT_NETDEBUG(KERN_WARNING 1119db8dac20SDavid S. Miller "UDPLITE: coverage %d too small, need min %d\n", 1120db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, up->pcrlen); 1121db8dac20SDavid S. Miller goto drop; 1122db8dac20SDavid S. Miller } 1123db8dac20SDavid S. Miller } 1124db8dac20SDavid S. Miller 1125db8dac20SDavid S. Miller if (sk->sk_filter) { 1126db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 1127db8dac20SDavid S. Miller goto drop; 1128db8dac20SDavid S. Miller } 1129db8dac20SDavid S. Miller 113093821778SHerbert Xu rc = 0; 1131db8dac20SDavid S. Miller 113293821778SHerbert Xu bh_lock_sock(sk); 113393821778SHerbert Xu if (!sock_owned_by_user(sk)) 113493821778SHerbert Xu rc = __udp_queue_rcv_skb(sk, skb); 113593821778SHerbert Xu else 113693821778SHerbert Xu sk_add_backlog(sk, skb); 113793821778SHerbert Xu bh_unlock_sock(sk); 113893821778SHerbert Xu 113993821778SHerbert Xu return rc; 1140db8dac20SDavid S. Miller 1141db8dac20SDavid S. Miller drop: 11420283328eSPavel Emelyanov UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 1143db8dac20SDavid S. Miller kfree_skb(skb); 1144db8dac20SDavid S. Miller return -1; 1145db8dac20SDavid S. Miller } 1146db8dac20SDavid S. Miller 1147db8dac20SDavid S. Miller /* 1148db8dac20SDavid S. Miller * Multicasts and broadcasts go to each listener. 1149db8dac20SDavid S. Miller * 1150db8dac20SDavid S. Miller * Note: called only from the BH handler context, 1151db8dac20SDavid S. Miller * so we don't need to lock the hashes. 1152db8dac20SDavid S. Miller */ 1153e3163493SPavel Emelyanov static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb, 1154db8dac20SDavid S. Miller struct udphdr *uh, 1155db8dac20SDavid S. Miller __be32 saddr, __be32 daddr, 1156645ca708SEric Dumazet struct udp_table *udptable) 1157db8dac20SDavid S. Miller { 1158db8dac20SDavid S. Miller struct sock *sk; 1159645ca708SEric Dumazet struct udp_hslot *hslot = &udptable->hash[udp_hashfn(net, ntohs(uh->dest))]; 1160db8dac20SDavid S. Miller int dif; 1161db8dac20SDavid S. Miller 1162645ca708SEric Dumazet spin_lock(&hslot->lock); 116388ab1932SEric Dumazet sk = sk_nulls_head(&hslot->head); 1164db8dac20SDavid S. Miller dif = skb->dev->ifindex; 1165920a4611SEric Dumazet sk = udp_v4_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif); 1166db8dac20SDavid S. Miller if (sk) { 1167db8dac20SDavid S. Miller struct sock *sknext = NULL; 1168db8dac20SDavid S. Miller 1169db8dac20SDavid S. Miller do { 1170db8dac20SDavid S. Miller struct sk_buff *skb1 = skb; 1171db8dac20SDavid S. Miller 117288ab1932SEric Dumazet sknext = udp_v4_mcast_next(net, sk_nulls_next(sk), uh->dest, 1173920a4611SEric Dumazet daddr, uh->source, saddr, 1174920a4611SEric Dumazet dif); 1175db8dac20SDavid S. Miller if (sknext) 1176db8dac20SDavid S. Miller skb1 = skb_clone(skb, GFP_ATOMIC); 1177db8dac20SDavid S. Miller 1178db8dac20SDavid S. Miller if (skb1) { 117993821778SHerbert Xu int ret = udp_queue_rcv_skb(sk, skb1); 1180db8dac20SDavid S. Miller if (ret > 0) 1181db8dac20SDavid S. Miller /* we should probably re-process instead 1182db8dac20SDavid S. Miller * of dropping packets here. */ 1183db8dac20SDavid S. Miller kfree_skb(skb1); 1184db8dac20SDavid S. Miller } 1185db8dac20SDavid S. Miller sk = sknext; 1186db8dac20SDavid S. Miller } while (sknext); 1187db8dac20SDavid S. Miller } else 1188ead2ceb0SNeil Horman consume_skb(skb); 1189645ca708SEric Dumazet spin_unlock(&hslot->lock); 1190db8dac20SDavid S. Miller return 0; 1191db8dac20SDavid S. Miller } 1192db8dac20SDavid S. Miller 1193db8dac20SDavid S. Miller /* Initialize UDP checksum. If exited with zero value (success), 1194db8dac20SDavid S. Miller * CHECKSUM_UNNECESSARY means, that no more checks are required. 1195db8dac20SDavid S. Miller * Otherwise, csum completion requires chacksumming packet body, 1196db8dac20SDavid S. Miller * including udp header and folding it to skb->csum. 1197db8dac20SDavid S. Miller */ 1198db8dac20SDavid S. Miller static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh, 1199db8dac20SDavid S. Miller int proto) 1200db8dac20SDavid S. Miller { 1201db8dac20SDavid S. Miller const struct iphdr *iph; 1202db8dac20SDavid S. Miller int err; 1203db8dac20SDavid S. Miller 1204db8dac20SDavid S. Miller UDP_SKB_CB(skb)->partial_cov = 0; 1205db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov = skb->len; 1206db8dac20SDavid S. Miller 1207db8dac20SDavid S. Miller if (proto == IPPROTO_UDPLITE) { 1208db8dac20SDavid S. Miller err = udplite_checksum_init(skb, uh); 1209db8dac20SDavid S. Miller if (err) 1210db8dac20SDavid S. Miller return err; 1211db8dac20SDavid S. Miller } 1212db8dac20SDavid S. Miller 1213db8dac20SDavid S. Miller iph = ip_hdr(skb); 1214db8dac20SDavid S. Miller if (uh->check == 0) { 1215db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_UNNECESSARY; 1216db8dac20SDavid S. Miller } else if (skb->ip_summed == CHECKSUM_COMPLETE) { 1217db8dac20SDavid S. Miller if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len, 1218db8dac20SDavid S. Miller proto, skb->csum)) 1219db8dac20SDavid S. Miller skb->ip_summed = CHECKSUM_UNNECESSARY; 1220db8dac20SDavid S. Miller } 1221db8dac20SDavid S. Miller if (!skb_csum_unnecessary(skb)) 1222db8dac20SDavid S. Miller skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, 1223db8dac20SDavid S. Miller skb->len, proto, 0); 1224db8dac20SDavid S. Miller /* Probably, we should checksum udp header (it should be in cache 1225db8dac20SDavid S. Miller * in any case) and data in tiny packets (< rx copybreak). 1226db8dac20SDavid S. Miller */ 1227db8dac20SDavid S. Miller 1228db8dac20SDavid S. Miller return 0; 1229db8dac20SDavid S. Miller } 1230db8dac20SDavid S. Miller 1231db8dac20SDavid S. Miller /* 1232db8dac20SDavid S. Miller * All we need to do is get the socket, and then do a checksum. 1233db8dac20SDavid S. Miller */ 1234db8dac20SDavid S. Miller 1235645ca708SEric Dumazet int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable, 1236db8dac20SDavid S. Miller int proto) 1237db8dac20SDavid S. Miller { 1238db8dac20SDavid S. Miller struct sock *sk; 12397b5e56f9SJesper Dangaard Brouer struct udphdr *uh; 1240db8dac20SDavid S. Miller unsigned short ulen; 1241adf30907SEric Dumazet struct rtable *rt = skb_rtable(skb); 12422783ef23SJesper Dangaard Brouer __be32 saddr, daddr; 12430283328eSPavel Emelyanov struct net *net = dev_net(skb->dev); 1244db8dac20SDavid S. Miller 1245db8dac20SDavid S. Miller /* 1246db8dac20SDavid S. Miller * Validate the packet. 1247db8dac20SDavid S. Miller */ 1248db8dac20SDavid S. Miller if (!pskb_may_pull(skb, sizeof(struct udphdr))) 1249db8dac20SDavid S. Miller goto drop; /* No space for header. */ 1250db8dac20SDavid S. Miller 12517b5e56f9SJesper Dangaard Brouer uh = udp_hdr(skb); 1252db8dac20SDavid S. Miller ulen = ntohs(uh->len); 1253db8dac20SDavid S. Miller if (ulen > skb->len) 1254db8dac20SDavid S. Miller goto short_packet; 1255db8dac20SDavid S. Miller 1256db8dac20SDavid S. Miller if (proto == IPPROTO_UDP) { 1257db8dac20SDavid S. Miller /* UDP validates ulen. */ 1258db8dac20SDavid S. Miller if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen)) 1259db8dac20SDavid S. Miller goto short_packet; 1260db8dac20SDavid S. Miller uh = udp_hdr(skb); 1261db8dac20SDavid S. Miller } 1262db8dac20SDavid S. Miller 1263db8dac20SDavid S. Miller if (udp4_csum_init(skb, uh, proto)) 1264db8dac20SDavid S. Miller goto csum_error; 1265db8dac20SDavid S. Miller 12662783ef23SJesper Dangaard Brouer saddr = ip_hdr(skb)->saddr; 12672783ef23SJesper Dangaard Brouer daddr = ip_hdr(skb)->daddr; 12682783ef23SJesper Dangaard Brouer 1269db8dac20SDavid S. Miller if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST)) 1270e3163493SPavel Emelyanov return __udp4_lib_mcast_deliver(net, skb, uh, 1271e3163493SPavel Emelyanov saddr, daddr, udptable); 1272db8dac20SDavid S. Miller 1273607c4aafSKOVACS Krisztian sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable); 1274db8dac20SDavid S. Miller 1275db8dac20SDavid S. Miller if (sk != NULL) { 127693821778SHerbert Xu int ret = udp_queue_rcv_skb(sk, skb); 1277db8dac20SDavid S. Miller sock_put(sk); 1278db8dac20SDavid S. Miller 1279db8dac20SDavid S. Miller /* a return value > 0 means to resubmit the input, but 1280db8dac20SDavid S. Miller * it wants the return to be -protocol, or 0 1281db8dac20SDavid S. Miller */ 1282db8dac20SDavid S. Miller if (ret > 0) 1283db8dac20SDavid S. Miller return -ret; 1284db8dac20SDavid S. Miller return 0; 1285db8dac20SDavid S. Miller } 1286db8dac20SDavid S. Miller 1287db8dac20SDavid S. Miller if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) 1288db8dac20SDavid S. Miller goto drop; 1289db8dac20SDavid S. Miller nf_reset(skb); 1290db8dac20SDavid S. Miller 1291db8dac20SDavid S. Miller /* No socket. Drop packet silently, if checksum is wrong */ 1292db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 1293db8dac20SDavid S. Miller goto csum_error; 1294db8dac20SDavid S. Miller 12950283328eSPavel Emelyanov UDP_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); 1296db8dac20SDavid S. Miller icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); 1297db8dac20SDavid S. Miller 1298db8dac20SDavid S. Miller /* 1299db8dac20SDavid S. Miller * Hmm. We got an UDP packet to a port to which we 1300db8dac20SDavid S. Miller * don't wanna listen. Ignore it. 1301db8dac20SDavid S. Miller */ 1302db8dac20SDavid S. Miller kfree_skb(skb); 1303db8dac20SDavid S. Miller return 0; 1304db8dac20SDavid S. Miller 1305db8dac20SDavid S. Miller short_packet: 1306673d57e7SHarvey Harrison LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n", 1307db8dac20SDavid S. Miller proto == IPPROTO_UDPLITE ? "-Lite" : "", 1308673d57e7SHarvey Harrison &saddr, 1309db8dac20SDavid S. Miller ntohs(uh->source), 1310db8dac20SDavid S. Miller ulen, 1311db8dac20SDavid S. Miller skb->len, 1312673d57e7SHarvey Harrison &daddr, 1313db8dac20SDavid S. Miller ntohs(uh->dest)); 1314db8dac20SDavid S. Miller goto drop; 1315db8dac20SDavid S. Miller 1316db8dac20SDavid S. Miller csum_error: 1317db8dac20SDavid S. Miller /* 1318db8dac20SDavid S. Miller * RFC1122: OK. Discards the bad packet silently (as far as 1319db8dac20SDavid S. Miller * the network is concerned, anyway) as per 4.1.3.4 (MUST). 1320db8dac20SDavid S. Miller */ 1321673d57e7SHarvey Harrison LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n", 1322db8dac20SDavid S. Miller proto == IPPROTO_UDPLITE ? "-Lite" : "", 1323673d57e7SHarvey Harrison &saddr, 1324db8dac20SDavid S. Miller ntohs(uh->source), 1325673d57e7SHarvey Harrison &daddr, 1326db8dac20SDavid S. Miller ntohs(uh->dest), 1327db8dac20SDavid S. Miller ulen); 1328db8dac20SDavid S. Miller drop: 13290283328eSPavel Emelyanov UDP_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 1330db8dac20SDavid S. Miller kfree_skb(skb); 1331db8dac20SDavid S. Miller return 0; 1332db8dac20SDavid S. Miller } 1333db8dac20SDavid S. Miller 1334db8dac20SDavid S. Miller int udp_rcv(struct sk_buff *skb) 1335db8dac20SDavid S. Miller { 1336645ca708SEric Dumazet return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP); 1337db8dac20SDavid S. Miller } 1338db8dac20SDavid S. Miller 13397d06b2e0SBrian Haley void udp_destroy_sock(struct sock *sk) 1340db8dac20SDavid S. Miller { 1341db8dac20SDavid S. Miller lock_sock(sk); 1342db8dac20SDavid S. Miller udp_flush_pending_frames(sk); 1343db8dac20SDavid S. Miller release_sock(sk); 1344db8dac20SDavid S. Miller } 1345db8dac20SDavid S. Miller 13461da177e4SLinus Torvalds /* 13471da177e4SLinus Torvalds * Socket option code for UDP 13481da177e4SLinus Torvalds */ 13494c0a6cb0SGerrit Renker int udp_lib_setsockopt(struct sock *sk, int level, int optname, 13504c0a6cb0SGerrit Renker char __user *optval, int optlen, 13514c0a6cb0SGerrit Renker int (*push_pending_frames)(struct sock *)) 13521da177e4SLinus Torvalds { 13531da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 13541da177e4SLinus Torvalds int val; 13551da177e4SLinus Torvalds int err = 0; 1356b2bf1e26SWang Chen int is_udplite = IS_UDPLITE(sk); 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds if (optlen<sizeof(int)) 13591da177e4SLinus Torvalds return -EINVAL; 13601da177e4SLinus Torvalds 13611da177e4SLinus Torvalds if (get_user(val, (int __user *)optval)) 13621da177e4SLinus Torvalds return -EFAULT; 13631da177e4SLinus Torvalds 13641da177e4SLinus Torvalds switch (optname) { 13651da177e4SLinus Torvalds case UDP_CORK: 13661da177e4SLinus Torvalds if (val != 0) { 13671da177e4SLinus Torvalds up->corkflag = 1; 13681da177e4SLinus Torvalds } else { 13691da177e4SLinus Torvalds up->corkflag = 0; 13701da177e4SLinus Torvalds lock_sock(sk); 13714c0a6cb0SGerrit Renker (*push_pending_frames)(sk); 13721da177e4SLinus Torvalds release_sock(sk); 13731da177e4SLinus Torvalds } 13741da177e4SLinus Torvalds break; 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds case UDP_ENCAP: 13771da177e4SLinus Torvalds switch (val) { 13781da177e4SLinus Torvalds case 0: 13791da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP: 13801da177e4SLinus Torvalds case UDP_ENCAP_ESPINUDP_NON_IKE: 1381067b207bSJames Chapman up->encap_rcv = xfrm4_udp_encap_rcv; 1382067b207bSJames Chapman /* FALLTHROUGH */ 1383342f0234SJames Chapman case UDP_ENCAP_L2TPINUDP: 13841da177e4SLinus Torvalds up->encap_type = val; 13851da177e4SLinus Torvalds break; 13861da177e4SLinus Torvalds default: 13871da177e4SLinus Torvalds err = -ENOPROTOOPT; 13881da177e4SLinus Torvalds break; 13891da177e4SLinus Torvalds } 13901da177e4SLinus Torvalds break; 13911da177e4SLinus Torvalds 1392ba4e58ecSGerrit Renker /* 1393ba4e58ecSGerrit Renker * UDP-Lite's partial checksum coverage (RFC 3828). 1394ba4e58ecSGerrit Renker */ 1395ba4e58ecSGerrit Renker /* The sender sets actual checksum coverage length via this option. 1396ba4e58ecSGerrit Renker * The case coverage > packet length is handled by send module. */ 1397ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 1398b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 1399ba4e58ecSGerrit Renker return -ENOPROTOOPT; 1400ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Illegal coverage: use default (8) */ 1401ba4e58ecSGerrit Renker val = 8; 140247112e25SGerrit Renker else if (val > USHORT_MAX) 140347112e25SGerrit Renker val = USHORT_MAX; 1404ba4e58ecSGerrit Renker up->pcslen = val; 1405ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_SEND_CC; 1406ba4e58ecSGerrit Renker break; 1407ba4e58ecSGerrit Renker 1408ba4e58ecSGerrit Renker /* The receiver specifies a minimum checksum coverage value. To make 1409ba4e58ecSGerrit Renker * sense, this should be set to at least 8 (as done below). If zero is 1410ba4e58ecSGerrit Renker * used, this again means full checksum coverage. */ 1411ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 1412b2bf1e26SWang Chen if (!is_udplite) /* Disable the option on UDP sockets */ 1413ba4e58ecSGerrit Renker return -ENOPROTOOPT; 1414ba4e58ecSGerrit Renker if (val != 0 && val < 8) /* Avoid silly minimal values. */ 1415ba4e58ecSGerrit Renker val = 8; 141647112e25SGerrit Renker else if (val > USHORT_MAX) 141747112e25SGerrit Renker val = USHORT_MAX; 1418ba4e58ecSGerrit Renker up->pcrlen = val; 1419ba4e58ecSGerrit Renker up->pcflag |= UDPLITE_RECV_CC; 1420ba4e58ecSGerrit Renker break; 1421ba4e58ecSGerrit Renker 14221da177e4SLinus Torvalds default: 14231da177e4SLinus Torvalds err = -ENOPROTOOPT; 14241da177e4SLinus Torvalds break; 14256516c655SStephen Hemminger } 14261da177e4SLinus Torvalds 14271da177e4SLinus Torvalds return err; 14281da177e4SLinus Torvalds } 14291da177e4SLinus Torvalds 1430db8dac20SDavid S. Miller int udp_setsockopt(struct sock *sk, int level, int optname, 1431db8dac20SDavid S. Miller char __user *optval, int optlen) 1432db8dac20SDavid S. Miller { 1433db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1434db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1435db8dac20SDavid S. Miller udp_push_pending_frames); 1436db8dac20SDavid S. Miller return ip_setsockopt(sk, level, optname, optval, optlen); 1437db8dac20SDavid S. Miller } 1438db8dac20SDavid S. Miller 1439db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1440db8dac20SDavid S. Miller int compat_udp_setsockopt(struct sock *sk, int level, int optname, 1441db8dac20SDavid S. Miller char __user *optval, int optlen) 1442db8dac20SDavid S. Miller { 1443db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1444db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1445db8dac20SDavid S. Miller udp_push_pending_frames); 1446db8dac20SDavid S. Miller return compat_ip_setsockopt(sk, level, optname, optval, optlen); 1447db8dac20SDavid S. Miller } 1448db8dac20SDavid S. Miller #endif 1449db8dac20SDavid S. Miller 14504c0a6cb0SGerrit Renker int udp_lib_getsockopt(struct sock *sk, int level, int optname, 14511da177e4SLinus Torvalds char __user *optval, int __user *optlen) 14521da177e4SLinus Torvalds { 14531da177e4SLinus Torvalds struct udp_sock *up = udp_sk(sk); 14541da177e4SLinus Torvalds int val, len; 14551da177e4SLinus Torvalds 14561da177e4SLinus Torvalds if (get_user(len,optlen)) 14571da177e4SLinus Torvalds return -EFAULT; 14581da177e4SLinus Torvalds 14591da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(int)); 14601da177e4SLinus Torvalds 14611da177e4SLinus Torvalds if (len < 0) 14621da177e4SLinus Torvalds return -EINVAL; 14631da177e4SLinus Torvalds 14641da177e4SLinus Torvalds switch (optname) { 14651da177e4SLinus Torvalds case UDP_CORK: 14661da177e4SLinus Torvalds val = up->corkflag; 14671da177e4SLinus Torvalds break; 14681da177e4SLinus Torvalds 14691da177e4SLinus Torvalds case UDP_ENCAP: 14701da177e4SLinus Torvalds val = up->encap_type; 14711da177e4SLinus Torvalds break; 14721da177e4SLinus Torvalds 1473ba4e58ecSGerrit Renker /* The following two cannot be changed on UDP sockets, the return is 1474ba4e58ecSGerrit Renker * always 0 (which corresponds to the full checksum coverage of UDP). */ 1475ba4e58ecSGerrit Renker case UDPLITE_SEND_CSCOV: 1476ba4e58ecSGerrit Renker val = up->pcslen; 1477ba4e58ecSGerrit Renker break; 1478ba4e58ecSGerrit Renker 1479ba4e58ecSGerrit Renker case UDPLITE_RECV_CSCOV: 1480ba4e58ecSGerrit Renker val = up->pcrlen; 1481ba4e58ecSGerrit Renker break; 1482ba4e58ecSGerrit Renker 14831da177e4SLinus Torvalds default: 14841da177e4SLinus Torvalds return -ENOPROTOOPT; 14856516c655SStephen Hemminger } 14861da177e4SLinus Torvalds 14871da177e4SLinus Torvalds if (put_user(len, optlen)) 14881da177e4SLinus Torvalds return -EFAULT; 14891da177e4SLinus Torvalds if (copy_to_user(optval, &val,len)) 14901da177e4SLinus Torvalds return -EFAULT; 14911da177e4SLinus Torvalds return 0; 14921da177e4SLinus Torvalds } 14931da177e4SLinus Torvalds 1494db8dac20SDavid S. Miller int udp_getsockopt(struct sock *sk, int level, int optname, 1495db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 1496db8dac20SDavid S. Miller { 1497db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1498db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1499db8dac20SDavid S. Miller return ip_getsockopt(sk, level, optname, optval, optlen); 1500db8dac20SDavid S. Miller } 1501db8dac20SDavid S. Miller 1502db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1503db8dac20SDavid S. Miller int compat_udp_getsockopt(struct sock *sk, int level, int optname, 1504db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 1505db8dac20SDavid S. Miller { 1506db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1507db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1508db8dac20SDavid S. Miller return compat_ip_getsockopt(sk, level, optname, optval, optlen); 1509db8dac20SDavid S. Miller } 1510db8dac20SDavid S. Miller #endif 15111da177e4SLinus Torvalds /** 15121da177e4SLinus Torvalds * udp_poll - wait for a UDP event. 15131da177e4SLinus Torvalds * @file - file struct 15141da177e4SLinus Torvalds * @sock - socket 15151da177e4SLinus Torvalds * @wait - poll table 15161da177e4SLinus Torvalds * 15171da177e4SLinus Torvalds * This is same as datagram poll, except for the special case of 15181da177e4SLinus Torvalds * blocking sockets. If application is using a blocking fd 15191da177e4SLinus Torvalds * and a packet with checksum error is in the queue; 15201da177e4SLinus Torvalds * then it could get return from select indicating data available 15211da177e4SLinus Torvalds * but then block when reading it. Add special case code 15221da177e4SLinus Torvalds * to work around these arguably broken applications. 15231da177e4SLinus Torvalds */ 15241da177e4SLinus Torvalds unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait) 15251da177e4SLinus Torvalds { 15261da177e4SLinus Torvalds unsigned int mask = datagram_poll(file, sock, wait); 15271da177e4SLinus Torvalds struct sock *sk = sock->sk; 1528ba4e58ecSGerrit Renker int is_lite = IS_UDPLITE(sk); 15291da177e4SLinus Torvalds 15301da177e4SLinus Torvalds /* Check for false positives due to checksum errors */ 15311da177e4SLinus Torvalds if ( (mask & POLLRDNORM) && 15321da177e4SLinus Torvalds !(file->f_flags & O_NONBLOCK) && 15331da177e4SLinus Torvalds !(sk->sk_shutdown & RCV_SHUTDOWN)){ 15341da177e4SLinus Torvalds struct sk_buff_head *rcvq = &sk->sk_receive_queue; 15351da177e4SLinus Torvalds struct sk_buff *skb; 15361da177e4SLinus Torvalds 1537208d8984SHerbert Xu spin_lock_bh(&rcvq->lock); 1538759e5d00SHerbert Xu while ((skb = skb_peek(rcvq)) != NULL && 1539759e5d00SHerbert Xu udp_lib_checksum_complete(skb)) { 15400283328eSPavel Emelyanov UDP_INC_STATS_BH(sock_net(sk), 15410283328eSPavel Emelyanov UDP_MIB_INERRORS, is_lite); 15421da177e4SLinus Torvalds __skb_unlink(skb, rcvq); 15431da177e4SLinus Torvalds kfree_skb(skb); 15441da177e4SLinus Torvalds } 1545208d8984SHerbert Xu spin_unlock_bh(&rcvq->lock); 15461da177e4SLinus Torvalds 15471da177e4SLinus Torvalds /* nothing to see, move along */ 15481da177e4SLinus Torvalds if (skb == NULL) 15491da177e4SLinus Torvalds mask &= ~(POLLIN | POLLRDNORM); 15501da177e4SLinus Torvalds } 15511da177e4SLinus Torvalds 15521da177e4SLinus Torvalds return mask; 15531da177e4SLinus Torvalds 15541da177e4SLinus Torvalds } 15551da177e4SLinus Torvalds 1556db8dac20SDavid S. Miller struct proto udp_prot = { 1557db8dac20SDavid S. Miller .name = "UDP", 1558db8dac20SDavid S. Miller .owner = THIS_MODULE, 1559db8dac20SDavid S. Miller .close = udp_lib_close, 1560db8dac20SDavid S. Miller .connect = ip4_datagram_connect, 1561db8dac20SDavid S. Miller .disconnect = udp_disconnect, 1562db8dac20SDavid S. Miller .ioctl = udp_ioctl, 1563db8dac20SDavid S. Miller .destroy = udp_destroy_sock, 1564db8dac20SDavid S. Miller .setsockopt = udp_setsockopt, 1565db8dac20SDavid S. Miller .getsockopt = udp_getsockopt, 1566db8dac20SDavid S. Miller .sendmsg = udp_sendmsg, 1567db8dac20SDavid S. Miller .recvmsg = udp_recvmsg, 1568db8dac20SDavid S. Miller .sendpage = udp_sendpage, 156993821778SHerbert Xu .backlog_rcv = __udp_queue_rcv_skb, 1570db8dac20SDavid S. Miller .hash = udp_lib_hash, 1571db8dac20SDavid S. Miller .unhash = udp_lib_unhash, 1572db8dac20SDavid S. Miller .get_port = udp_v4_get_port, 1573db8dac20SDavid S. Miller .memory_allocated = &udp_memory_allocated, 1574db8dac20SDavid S. Miller .sysctl_mem = sysctl_udp_mem, 1575db8dac20SDavid S. Miller .sysctl_wmem = &sysctl_udp_wmem_min, 1576db8dac20SDavid S. Miller .sysctl_rmem = &sysctl_udp_rmem_min, 1577db8dac20SDavid S. Miller .obj_size = sizeof(struct udp_sock), 1578271b72c7SEric Dumazet .slab_flags = SLAB_DESTROY_BY_RCU, 1579645ca708SEric Dumazet .h.udp_table = &udp_table, 1580db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1581db8dac20SDavid S. Miller .compat_setsockopt = compat_udp_setsockopt, 1582db8dac20SDavid S. Miller .compat_getsockopt = compat_udp_getsockopt, 1583db8dac20SDavid S. Miller #endif 1584db8dac20SDavid S. Miller }; 15851da177e4SLinus Torvalds 15861da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 15871da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 15881da177e4SLinus Torvalds 1589645ca708SEric Dumazet static struct sock *udp_get_first(struct seq_file *seq, int start) 15901da177e4SLinus Torvalds { 15911da177e4SLinus Torvalds struct sock *sk; 15921da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 15936f191efeSDenis V. Lunev struct net *net = seq_file_net(seq); 15941da177e4SLinus Torvalds 1595645ca708SEric Dumazet for (state->bucket = start; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) { 159688ab1932SEric Dumazet struct hlist_nulls_node *node; 1597645ca708SEric Dumazet struct udp_hslot *hslot = &state->udp_table->hash[state->bucket]; 1598645ca708SEric Dumazet spin_lock_bh(&hslot->lock); 159988ab1932SEric Dumazet sk_nulls_for_each(sk, node, &hslot->head) { 1600878628fbSYOSHIFUJI Hideaki if (!net_eq(sock_net(sk), net)) 1601a91275efSDaniel Lezcano continue; 16021da177e4SLinus Torvalds if (sk->sk_family == state->family) 16031da177e4SLinus Torvalds goto found; 16041da177e4SLinus Torvalds } 1605645ca708SEric Dumazet spin_unlock_bh(&hslot->lock); 16061da177e4SLinus Torvalds } 16071da177e4SLinus Torvalds sk = NULL; 16081da177e4SLinus Torvalds found: 16091da177e4SLinus Torvalds return sk; 16101da177e4SLinus Torvalds } 16111da177e4SLinus Torvalds 16121da177e4SLinus Torvalds static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk) 16131da177e4SLinus Torvalds { 16141da177e4SLinus Torvalds struct udp_iter_state *state = seq->private; 16156f191efeSDenis V. Lunev struct net *net = seq_file_net(seq); 16161da177e4SLinus Torvalds 16171da177e4SLinus Torvalds do { 161888ab1932SEric Dumazet sk = sk_nulls_next(sk); 1619878628fbSYOSHIFUJI Hideaki } while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family)); 16201da177e4SLinus Torvalds 1621645ca708SEric Dumazet if (!sk) { 162230842f29SVitaly Mayatskikh if (state->bucket < UDP_HTABLE_SIZE) 1623f52b5054SEric Dumazet spin_unlock_bh(&state->udp_table->hash[state->bucket].lock); 1624645ca708SEric Dumazet return udp_get_first(seq, state->bucket + 1); 16251da177e4SLinus Torvalds } 16261da177e4SLinus Torvalds return sk; 16271da177e4SLinus Torvalds } 16281da177e4SLinus Torvalds 16291da177e4SLinus Torvalds static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos) 16301da177e4SLinus Torvalds { 1631645ca708SEric Dumazet struct sock *sk = udp_get_first(seq, 0); 16321da177e4SLinus Torvalds 16331da177e4SLinus Torvalds if (sk) 16341da177e4SLinus Torvalds while (pos && (sk = udp_get_next(seq, sk)) != NULL) 16351da177e4SLinus Torvalds --pos; 16361da177e4SLinus Torvalds return pos ? NULL : sk; 16371da177e4SLinus Torvalds } 16381da177e4SLinus Torvalds 16391da177e4SLinus Torvalds static void *udp_seq_start(struct seq_file *seq, loff_t *pos) 16401da177e4SLinus Torvalds { 164130842f29SVitaly Mayatskikh struct udp_iter_state *state = seq->private; 164230842f29SVitaly Mayatskikh state->bucket = UDP_HTABLE_SIZE; 164330842f29SVitaly Mayatskikh 1644b50660f1SYOSHIFUJI Hideaki return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN; 16451da177e4SLinus Torvalds } 16461da177e4SLinus Torvalds 16471da177e4SLinus Torvalds static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos) 16481da177e4SLinus Torvalds { 16491da177e4SLinus Torvalds struct sock *sk; 16501da177e4SLinus Torvalds 1651b50660f1SYOSHIFUJI Hideaki if (v == SEQ_START_TOKEN) 16521da177e4SLinus Torvalds sk = udp_get_idx(seq, 0); 16531da177e4SLinus Torvalds else 16541da177e4SLinus Torvalds sk = udp_get_next(seq, v); 16551da177e4SLinus Torvalds 16561da177e4SLinus Torvalds ++*pos; 16571da177e4SLinus Torvalds return sk; 16581da177e4SLinus Torvalds } 16591da177e4SLinus Torvalds 16601da177e4SLinus Torvalds static void udp_seq_stop(struct seq_file *seq, void *v) 16611da177e4SLinus Torvalds { 1662645ca708SEric Dumazet struct udp_iter_state *state = seq->private; 1663645ca708SEric Dumazet 1664645ca708SEric Dumazet if (state->bucket < UDP_HTABLE_SIZE) 1665645ca708SEric Dumazet spin_unlock_bh(&state->udp_table->hash[state->bucket].lock); 16661da177e4SLinus Torvalds } 16671da177e4SLinus Torvalds 16681da177e4SLinus Torvalds static int udp_seq_open(struct inode *inode, struct file *file) 16691da177e4SLinus Torvalds { 16701da177e4SLinus Torvalds struct udp_seq_afinfo *afinfo = PDE(inode)->data; 1671a2be75c1SDenis V. Lunev struct udp_iter_state *s; 1672a2be75c1SDenis V. Lunev int err; 16731da177e4SLinus Torvalds 1674a2be75c1SDenis V. Lunev err = seq_open_net(inode, file, &afinfo->seq_ops, 1675a2be75c1SDenis V. Lunev sizeof(struct udp_iter_state)); 1676a2be75c1SDenis V. Lunev if (err < 0) 1677a2be75c1SDenis V. Lunev return err; 1678a91275efSDaniel Lezcano 1679a2be75c1SDenis V. Lunev s = ((struct seq_file *)file->private_data)->private; 16801da177e4SLinus Torvalds s->family = afinfo->family; 1681645ca708SEric Dumazet s->udp_table = afinfo->udp_table; 1682a2be75c1SDenis V. Lunev return err; 1683a91275efSDaniel Lezcano } 1684a91275efSDaniel Lezcano 16851da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 16860c96d8c5SDaniel Lezcano int udp_proc_register(struct net *net, struct udp_seq_afinfo *afinfo) 16871da177e4SLinus Torvalds { 16881da177e4SLinus Torvalds struct proc_dir_entry *p; 16891da177e4SLinus Torvalds int rc = 0; 16901da177e4SLinus Torvalds 16913ba9441bSDenis V. Lunev afinfo->seq_fops.open = udp_seq_open; 16923ba9441bSDenis V. Lunev afinfo->seq_fops.read = seq_read; 16933ba9441bSDenis V. Lunev afinfo->seq_fops.llseek = seq_lseek; 16943ba9441bSDenis V. Lunev afinfo->seq_fops.release = seq_release_net; 16951da177e4SLinus Torvalds 1696dda61925SDenis V. Lunev afinfo->seq_ops.start = udp_seq_start; 1697dda61925SDenis V. Lunev afinfo->seq_ops.next = udp_seq_next; 1698dda61925SDenis V. Lunev afinfo->seq_ops.stop = udp_seq_stop; 1699dda61925SDenis V. Lunev 170084841c3cSDenis V. Lunev p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net, 170184841c3cSDenis V. Lunev &afinfo->seq_fops, afinfo); 170284841c3cSDenis V. Lunev if (!p) 17031da177e4SLinus Torvalds rc = -ENOMEM; 17041da177e4SLinus Torvalds return rc; 17051da177e4SLinus Torvalds } 17061da177e4SLinus Torvalds 17070c96d8c5SDaniel Lezcano void udp_proc_unregister(struct net *net, struct udp_seq_afinfo *afinfo) 17081da177e4SLinus Torvalds { 17090c96d8c5SDaniel Lezcano proc_net_remove(net, afinfo->name); 17101da177e4SLinus Torvalds } 1711db8dac20SDavid S. Miller 1712db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 17135e659e4cSPavel Emelyanov static void udp4_format_sock(struct sock *sp, struct seq_file *f, 17145e659e4cSPavel Emelyanov int bucket, int *len) 1715db8dac20SDavid S. Miller { 1716db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sp); 1717db8dac20SDavid S. Miller __be32 dest = inet->daddr; 1718db8dac20SDavid S. Miller __be32 src = inet->rcv_saddr; 1719db8dac20SDavid S. Miller __u16 destp = ntohs(inet->dport); 1720db8dac20SDavid S. Miller __u16 srcp = ntohs(inet->sport); 1721db8dac20SDavid S. Miller 17225e659e4cSPavel Emelyanov seq_printf(f, "%4d: %08X:%04X %08X:%04X" 1723cb61cb9bSEric Dumazet " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d%n", 1724db8dac20SDavid S. Miller bucket, src, srcp, dest, destp, sp->sk_state, 172531e6d363SEric Dumazet sk_wmem_alloc_get(sp), 172631e6d363SEric Dumazet sk_rmem_alloc_get(sp), 1727db8dac20SDavid S. Miller 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp), 1728cb61cb9bSEric Dumazet atomic_read(&sp->sk_refcnt), sp, 1729cb61cb9bSEric Dumazet atomic_read(&sp->sk_drops), len); 1730db8dac20SDavid S. Miller } 1731db8dac20SDavid S. Miller 1732db8dac20SDavid S. Miller int udp4_seq_show(struct seq_file *seq, void *v) 1733db8dac20SDavid S. Miller { 1734db8dac20SDavid S. Miller if (v == SEQ_START_TOKEN) 1735db8dac20SDavid S. Miller seq_printf(seq, "%-127s\n", 1736db8dac20SDavid S. Miller " sl local_address rem_address st tx_queue " 1737db8dac20SDavid S. Miller "rx_queue tr tm->when retrnsmt uid timeout " 1738cb61cb9bSEric Dumazet "inode ref pointer drops"); 1739db8dac20SDavid S. Miller else { 1740db8dac20SDavid S. Miller struct udp_iter_state *state = seq->private; 17415e659e4cSPavel Emelyanov int len; 1742db8dac20SDavid S. Miller 17435e659e4cSPavel Emelyanov udp4_format_sock(v, seq, state->bucket, &len); 17445e659e4cSPavel Emelyanov seq_printf(seq, "%*s\n", 127 - len ,""); 1745db8dac20SDavid S. Miller } 1746db8dac20SDavid S. Miller return 0; 1747db8dac20SDavid S. Miller } 1748db8dac20SDavid S. Miller 1749db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 1750db8dac20SDavid S. Miller static struct udp_seq_afinfo udp4_seq_afinfo = { 1751db8dac20SDavid S. Miller .name = "udp", 1752db8dac20SDavid S. Miller .family = AF_INET, 1753645ca708SEric Dumazet .udp_table = &udp_table, 17544ad96d39SDenis V. Lunev .seq_fops = { 17554ad96d39SDenis V. Lunev .owner = THIS_MODULE, 17564ad96d39SDenis V. Lunev }, 1757dda61925SDenis V. Lunev .seq_ops = { 1758dda61925SDenis V. Lunev .show = udp4_seq_show, 1759dda61925SDenis V. Lunev }, 1760db8dac20SDavid S. Miller }; 1761db8dac20SDavid S. Miller 176215439febSPavel Emelyanov static int udp4_proc_init_net(struct net *net) 176315439febSPavel Emelyanov { 176415439febSPavel Emelyanov return udp_proc_register(net, &udp4_seq_afinfo); 176515439febSPavel Emelyanov } 176615439febSPavel Emelyanov 176715439febSPavel Emelyanov static void udp4_proc_exit_net(struct net *net) 176815439febSPavel Emelyanov { 176915439febSPavel Emelyanov udp_proc_unregister(net, &udp4_seq_afinfo); 177015439febSPavel Emelyanov } 177115439febSPavel Emelyanov 177215439febSPavel Emelyanov static struct pernet_operations udp4_net_ops = { 177315439febSPavel Emelyanov .init = udp4_proc_init_net, 177415439febSPavel Emelyanov .exit = udp4_proc_exit_net, 177515439febSPavel Emelyanov }; 177615439febSPavel Emelyanov 1777db8dac20SDavid S. Miller int __init udp4_proc_init(void) 1778db8dac20SDavid S. Miller { 177915439febSPavel Emelyanov return register_pernet_subsys(&udp4_net_ops); 1780db8dac20SDavid S. Miller } 1781db8dac20SDavid S. Miller 1782db8dac20SDavid S. Miller void udp4_proc_exit(void) 1783db8dac20SDavid S. Miller { 178415439febSPavel Emelyanov unregister_pernet_subsys(&udp4_net_ops); 1785db8dac20SDavid S. Miller } 17861da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 17871da177e4SLinus Torvalds 1788645ca708SEric Dumazet void __init udp_table_init(struct udp_table *table) 1789645ca708SEric Dumazet { 1790645ca708SEric Dumazet int i; 1791645ca708SEric Dumazet 1792645ca708SEric Dumazet for (i = 0; i < UDP_HTABLE_SIZE; i++) { 179388ab1932SEric Dumazet INIT_HLIST_NULLS_HEAD(&table->hash[i].head, i); 1794645ca708SEric Dumazet spin_lock_init(&table->hash[i].lock); 1795645ca708SEric Dumazet } 1796645ca708SEric Dumazet } 1797645ca708SEric Dumazet 179895766fffSHideo Aoki void __init udp_init(void) 179995766fffSHideo Aoki { 18008203efb3SEric Dumazet unsigned long nr_pages, limit; 180195766fffSHideo Aoki 1802645ca708SEric Dumazet udp_table_init(&udp_table); 180395766fffSHideo Aoki /* Set the pressure threshold up by the same strategy of TCP. It is a 180495766fffSHideo Aoki * fraction of global memory that is up to 1/2 at 256 MB, decreasing 180595766fffSHideo Aoki * toward zero with the amount of memory, with a floor of 128 pages. 180695766fffSHideo Aoki */ 18078203efb3SEric Dumazet nr_pages = totalram_pages - totalhigh_pages; 18088203efb3SEric Dumazet limit = min(nr_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT); 18098203efb3SEric Dumazet limit = (limit * (nr_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11); 181095766fffSHideo Aoki limit = max(limit, 128UL); 181195766fffSHideo Aoki sysctl_udp_mem[0] = limit / 4 * 3; 181295766fffSHideo Aoki sysctl_udp_mem[1] = limit; 181395766fffSHideo Aoki sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2; 181495766fffSHideo Aoki 181595766fffSHideo Aoki sysctl_udp_rmem_min = SK_MEM_QUANTUM; 181695766fffSHideo Aoki sysctl_udp_wmem_min = SK_MEM_QUANTUM; 181795766fffSHideo Aoki } 181895766fffSHideo Aoki 1819*d7ca4cc0SSridhar Samudrala int udp4_ufo_send_check(struct sk_buff *skb) 1820*d7ca4cc0SSridhar Samudrala { 1821*d7ca4cc0SSridhar Samudrala const struct iphdr *iph; 1822*d7ca4cc0SSridhar Samudrala struct udphdr *uh; 1823*d7ca4cc0SSridhar Samudrala 1824*d7ca4cc0SSridhar Samudrala if (!pskb_may_pull(skb, sizeof(*uh))) 1825*d7ca4cc0SSridhar Samudrala return -EINVAL; 1826*d7ca4cc0SSridhar Samudrala 1827*d7ca4cc0SSridhar Samudrala iph = ip_hdr(skb); 1828*d7ca4cc0SSridhar Samudrala uh = udp_hdr(skb); 1829*d7ca4cc0SSridhar Samudrala 1830*d7ca4cc0SSridhar Samudrala uh->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len, 1831*d7ca4cc0SSridhar Samudrala IPPROTO_UDP, 0); 1832*d7ca4cc0SSridhar Samudrala skb->csum_start = skb_transport_header(skb) - skb->head; 1833*d7ca4cc0SSridhar Samudrala skb->csum_offset = offsetof(struct udphdr, check); 1834*d7ca4cc0SSridhar Samudrala skb->ip_summed = CHECKSUM_PARTIAL; 1835*d7ca4cc0SSridhar Samudrala return 0; 1836*d7ca4cc0SSridhar Samudrala } 1837*d7ca4cc0SSridhar Samudrala 1838*d7ca4cc0SSridhar Samudrala struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb, int features) 1839*d7ca4cc0SSridhar Samudrala { 1840*d7ca4cc0SSridhar Samudrala struct sk_buff *segs = ERR_PTR(-EINVAL); 1841*d7ca4cc0SSridhar Samudrala unsigned int mss; 1842*d7ca4cc0SSridhar Samudrala int offset; 1843*d7ca4cc0SSridhar Samudrala __wsum csum; 1844*d7ca4cc0SSridhar Samudrala 1845*d7ca4cc0SSridhar Samudrala mss = skb_shinfo(skb)->gso_size; 1846*d7ca4cc0SSridhar Samudrala if (unlikely(skb->len <= mss)) 1847*d7ca4cc0SSridhar Samudrala goto out; 1848*d7ca4cc0SSridhar Samudrala 1849*d7ca4cc0SSridhar Samudrala if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) { 1850*d7ca4cc0SSridhar Samudrala /* Packet is from an untrusted source, reset gso_segs. */ 1851*d7ca4cc0SSridhar Samudrala int type = skb_shinfo(skb)->gso_type; 1852*d7ca4cc0SSridhar Samudrala 1853*d7ca4cc0SSridhar Samudrala if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY) || 1854*d7ca4cc0SSridhar Samudrala !(type & (SKB_GSO_UDP)))) 1855*d7ca4cc0SSridhar Samudrala goto out; 1856*d7ca4cc0SSridhar Samudrala 1857*d7ca4cc0SSridhar Samudrala skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss); 1858*d7ca4cc0SSridhar Samudrala 1859*d7ca4cc0SSridhar Samudrala segs = NULL; 1860*d7ca4cc0SSridhar Samudrala goto out; 1861*d7ca4cc0SSridhar Samudrala } 1862*d7ca4cc0SSridhar Samudrala 1863*d7ca4cc0SSridhar Samudrala /* Do software UFO. Complete and fill in the UDP checksum as HW cannot 1864*d7ca4cc0SSridhar Samudrala * do checksum of UDP packets sent as multiple IP fragments. 1865*d7ca4cc0SSridhar Samudrala */ 1866*d7ca4cc0SSridhar Samudrala offset = skb->csum_start - skb_headroom(skb); 1867*d7ca4cc0SSridhar Samudrala csum = skb_checksum(skb, offset, skb->len- offset, 0); 1868*d7ca4cc0SSridhar Samudrala offset += skb->csum_offset; 1869*d7ca4cc0SSridhar Samudrala *(__sum16 *)(skb->data + offset) = csum_fold(csum); 1870*d7ca4cc0SSridhar Samudrala skb->ip_summed = CHECKSUM_NONE; 1871*d7ca4cc0SSridhar Samudrala 1872*d7ca4cc0SSridhar Samudrala /* Fragment the skb. IP headers of the fragments are updated in 1873*d7ca4cc0SSridhar Samudrala * inet_gso_segment() 1874*d7ca4cc0SSridhar Samudrala */ 1875*d7ca4cc0SSridhar Samudrala segs = skb_segment(skb, features); 1876*d7ca4cc0SSridhar Samudrala out: 1877*d7ca4cc0SSridhar Samudrala return segs; 1878*d7ca4cc0SSridhar Samudrala } 1879*d7ca4cc0SSridhar Samudrala 18801da177e4SLinus Torvalds EXPORT_SYMBOL(udp_disconnect); 18811da177e4SLinus Torvalds EXPORT_SYMBOL(udp_ioctl); 1882db8dac20SDavid S. Miller EXPORT_SYMBOL(udp_prot); 1883db8dac20SDavid S. Miller EXPORT_SYMBOL(udp_sendmsg); 18844c0a6cb0SGerrit Renker EXPORT_SYMBOL(udp_lib_getsockopt); 18854c0a6cb0SGerrit Renker EXPORT_SYMBOL(udp_lib_setsockopt); 18861da177e4SLinus Torvalds EXPORT_SYMBOL(udp_poll); 18876ba5a3c5SPavel Emelyanov EXPORT_SYMBOL(udp_lib_get_port); 18881da177e4SLinus Torvalds 18891da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 18901da177e4SLinus Torvalds EXPORT_SYMBOL(udp_proc_register); 18911da177e4SLinus Torvalds EXPORT_SYMBOL(udp_proc_unregister); 18921da177e4SLinus Torvalds #endif 1893