udp.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * The User Datagram Protocol (UDP).
  7. *
  8. * Authors: Ross Biro
  9. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  10. * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  11. * Alan Cox, <alan@lxorguk.ukuu.org.uk>
  12. * Hirokazu Takahashi, <taka@valinux.co.jp>
  13. *
  14. * Fixes:
  15. * Alan Cox : verify_area() calls
  16. * Alan Cox : stopped close while in use off icmp
  17. * messages. Not a fix but a botch that
  18. * for udp at least is 'valid'.
  19. * Alan Cox : Fixed icmp handling properly
  20. * Alan Cox : Correct error for oversized datagrams
  21. * Alan Cox : Tidied select() semantics.
  22. * Alan Cox : udp_err() fixed properly, also now
  23. * select and read wake correctly on errors
  24. * Alan Cox : udp_send verify_area moved to avoid mem leak
  25. * Alan Cox : UDP can count its memory
  26. * Alan Cox : send to an unknown connection causes
  27. * an ECONNREFUSED off the icmp, but
  28. * does NOT close.
  29. * Alan Cox : Switched to new sk_buff handlers. No more backlog!
  30. * Alan Cox : Using generic datagram code. Even smaller and the PEEK
  31. * bug no longer crashes it.
  32. * Fred Van Kempen : Net2e support for sk->broadcast.
  33. * Alan Cox : Uses skb_free_datagram
  34. * Alan Cox : Added get/set sockopt support.
  35. * Alan Cox : Broadcasting without option set returns EACCES.
  36. * Alan Cox : No wakeup calls. Instead we now use the callbacks.
  37. * Alan Cox : Use ip_tos and ip_ttl
  38. * Alan Cox : SNMP Mibs
  39. * Alan Cox : MSG_DONTROUTE, and 0.0.0.0 support.
  40. * Matt Dillon : UDP length checks.
  41. * Alan Cox : Smarter af_inet used properly.
  42. * Alan Cox : Use new kernel side addressing.
  43. * Alan Cox : Incorrect return on truncated datagram receive.
  44. * Arnt Gulbrandsen : New udp_send and stuff
  45. * Alan Cox : Cache last socket
  46. * Alan Cox : Route cache
  47. * Jon Peatfield : Minor efficiency fix to sendto().
  48. * Mike Shaver : RFC1122 checks.
  49. * Alan Cox : Nonblocking error fix.
  50. * Willy Konynenberg : Transparent proxying support.
  51. * Mike McLagan : Routing by source
  52. * David S. Miller : New socket lookup architecture.
  53. * Last socket cache retained as it
  54. * does have a high hit rate.
  55. * Olaf Kirch : Don't linearise iovec on sendmsg.
  56. * Andi Kleen : Some cleanups, cache destination entry
  57. * for connect.
  58. * Vitaly E. Lavrov : Transparent proxy revived after year coma.
  59. * Melvin Smith : Check msg_name not msg_namelen in sendto(),
  60. * return ENOTCONN for unconnected sockets (POSIX)
  61. * Janos Farkas : don't deliver multi/broadcasts to a different
  62. * bound-to-device socket
  63. * Hirokazu Takahashi : HW checksumming for outgoing UDP
  64. * datagrams.
  65. * Hirokazu Takahashi : sendfile() on UDP works now.
  66. * Arnaldo C. Melo : convert /proc/net/udp to seq_file
  67. * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
  68. * Alexey Kuznetsov: allow both IPv4 and IPv6 sockets to bind
  69. * a single port at the same time.
  70. * Derek Atkins <derek@ihtfp.com>: Add Encapulation Support
  71. * James Chapman : Add L2TP encapsulation type.
  72. *
  73. *
  74. * This program is free software; you can redistribute it and/or
  75. * modify it under the terms of the GNU General Public License
  76. * as published by the Free Software Foundation; either version
  77. * 2 of the License, or (at your option) any later version.
  78. */
  79. #define pr_fmt(fmt) "UDP: " fmt
  80. #include <linux/uaccess.h>
  81. #include <asm/ioctls.h>
  82. #include <linux/memblock.h>
  83. #include <linux/highmem.h>
  84. #include <linux/swap.h>
  85. #include <linux/types.h>
  86. #include <linux/fcntl.h>
  87. #include <linux/module.h>
  88. #include <linux/socket.h>
  89. #include <linux/sockios.h>
  90. #include <linux/igmp.h>
  91. #include <linux/inetdevice.h>
  92. #include <linux/in.h>
  93. #include <linux/errno.h>
  94. #include <linux/timer.h>
  95. #include <linux/mm.h>
  96. #include <linux/inet.h>
  97. #include <linux/netdevice.h>
  98. #include <linux/slab.h>
  99. #include <net/tcp_states.h>
  100. #include <linux/skbuff.h>
  101. #include <linux/proc_fs.h>
  102. #include <linux/seq_file.h>
  103. #include <net/net_namespace.h>
  104. #include <net/icmp.h>
  105. #include <net/inet_hashtables.h>
  106. #include <net/route.h>
  107. #include <net/checksum.h>
  108. #include <net/xfrm.h>
  109. #include <trace/events/udp.h>
  110. #include <linux/static_key.h>
  111. #include <trace/events/skb.h>
  112. #include <net/busy_poll.h>
  113. #include "udp_impl.h"
  114. #include <net/sock_reuseport.h>
  115. #include <net/addrconf.h>
  116. struct udp_table udp_table __read_mostly;
  117. EXPORT_SYMBOL(udp_table);
  118. long sysctl_udp_mem[3] __read_mostly;
  119. EXPORT_SYMBOL(sysctl_udp_mem);
  120. atomic_long_t udp_memory_allocated;
  121. EXPORT_SYMBOL(udp_memory_allocated);
  122. #define MAX_UDP_PORTS 65536
  123. #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN)
  124. /* IPCB reference means this can not be used from early demux */
  125. static bool udp_lib_exact_dif_match(struct net *net, struct sk_buff *skb)
  126. {
  127. #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
  128. if (!net->ipv4.sysctl_udp_l3mdev_accept &&
  129. skb && ipv4_l3mdev_skb(IPCB(skb)->flags))
  130. return true;
  131. #endif
  132. return false;
  133. }
  134. static int udp_lib_lport_inuse(struct net *net, __u16 num,
  135. const struct udp_hslot *hslot,
  136. unsigned long *bitmap,
  137. struct sock *sk, unsigned int log)
  138. {
  139. struct sock *sk2;
  140. kuid_t uid = sock_i_uid(sk);
  141. sk_for_each(sk2, &hslot->head) {
  142. if (net_eq(sock_net(sk2), net) &&
  143. sk2 != sk &&
  144. (bitmap || udp_sk(sk2)->udp_port_hash == num) &&
  145. (!sk2->sk_reuse || !sk->sk_reuse) &&
  146. (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
  147. sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
  148. inet_rcv_saddr_equal(sk, sk2, true)) {
  149. if (sk2->sk_reuseport && sk->sk_reuseport &&
  150. !rcu_access_pointer(sk->sk_reuseport_cb) &&
  151. uid_eq(uid, sock_i_uid(sk2))) {
  152. if (!bitmap)
  153. return 0;
  154. } else {
  155. if (!bitmap)
  156. return 1;
  157. __set_bit(udp_sk(sk2)->udp_port_hash >> log,
  158. bitmap);
  159. }
  160. }
  161. }
  162. return 0;
  163. }
  164. /*
  165. * Note: we still hold spinlock of primary hash chain, so no other writer
  166. * can insert/delete a socket with local_port == num
  167. */
  168. static int udp_lib_lport_inuse2(struct net *net, __u16 num,
  169. struct udp_hslot *hslot2,
  170. struct sock *sk)
  171. {
  172. struct sock *sk2;
  173. kuid_t uid = sock_i_uid(sk);
  174. int res = 0;
  175. spin_lock(&hslot2->lock);
  176. udp_portaddr_for_each_entry(sk2, &hslot2->head) {
  177. if (net_eq(sock_net(sk2), net) &&
  178. sk2 != sk &&
  179. (udp_sk(sk2)->udp_port_hash == num) &&
  180. (!sk2->sk_reuse || !sk->sk_reuse) &&
  181. (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
  182. sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
  183. inet_rcv_saddr_equal(sk, sk2, true)) {
  184. if (sk2->sk_reuseport && sk->sk_reuseport &&
  185. !rcu_access_pointer(sk->sk_reuseport_cb) &&
  186. uid_eq(uid, sock_i_uid(sk2))) {
  187. res = 0;
  188. } else {
  189. res = 1;
  190. }
  191. break;
  192. }
  193. }
  194. spin_unlock(&hslot2->lock);
  195. return res;
  196. }
  197. static int udp_reuseport_add_sock(struct sock *sk, struct udp_hslot *hslot)
  198. {
  199. struct net *net = sock_net(sk);
  200. kuid_t uid = sock_i_uid(sk);
  201. struct sock *sk2;
  202. sk_for_each(sk2, &hslot->head) {
  203. if (net_eq(sock_net(sk2), net) &&
  204. sk2 != sk &&
  205. sk2->sk_family == sk->sk_family &&
  206. ipv6_only_sock(sk2) == ipv6_only_sock(sk) &&
  207. (udp_sk(sk2)->udp_port_hash == udp_sk(sk)->udp_port_hash) &&
  208. (sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
  209. sk2->sk_reuseport && uid_eq(uid, sock_i_uid(sk2)) &&
  210. inet_rcv_saddr_equal(sk, sk2, false)) {
  211. return reuseport_add_sock(sk, sk2,
  212. inet_rcv_saddr_any(sk));
  213. }
  214. }
  215. return reuseport_alloc(sk, inet_rcv_saddr_any(sk));
  216. }
  217. /**
  218. * udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6
  219. *
  220. * @sk: socket struct in question
  221. * @snum: port number to look up
  222. * @hash2_nulladdr: AF-dependent hash value in secondary hash chains,
  223. * with NULL address
  224. */
  225. int udp_lib_get_port(struct sock *sk, unsigned short snum,
  226. unsigned int hash2_nulladdr)
  227. {
  228. struct udp_hslot *hslot, *hslot2;
  229. struct udp_table *udptable = sk->sk_prot->h.udp_table;
  230. int error = 1;
  231. struct net *net = sock_net(sk);
  232. if (!snum) {
  233. int low, high, remaining;
  234. unsigned int rand;
  235. unsigned short first, last;
  236. DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN);
  237. inet_get_local_port_range(net, &low, &high);
  238. remaining = (high - low) + 1;
  239. rand = prandom_u32();
  240. first = reciprocal_scale(rand, remaining) + low;
  241. /*
  242. * force rand to be an odd multiple of UDP_HTABLE_SIZE
  243. */
  244. rand = (rand | 1) * (udptable->mask + 1);
  245. last = first + udptable->mask + 1;
  246. do {
  247. hslot = udp_hashslot(udptable, net, first);
  248. bitmap_zero(bitmap, PORTS_PER_CHAIN);
  249. spin_lock_bh(&hslot->lock);
  250. udp_lib_lport_inuse(net, snum, hslot, bitmap, sk,
  251. udptable->log);
  252. snum = first;
  253. /*
  254. * Iterate on all possible values of snum for this hash.
  255. * Using steps of an odd multiple of UDP_HTABLE_SIZE
  256. * give us randomization and full range coverage.
  257. */
  258. do {
  259. if (low <= snum && snum <= high &&
  260. !test_bit(snum >> udptable->log, bitmap) &&
  261. !inet_is_local_reserved_port(net, snum))
  262. goto found;
  263. snum += rand;
  264. } while (snum != first);
  265. spin_unlock_bh(&hslot->lock);
  266. cond_resched();
  267. } while (++first != last);
  268. goto fail;
  269. } else {
  270. hslot = udp_hashslot(udptable, net, snum);
  271. spin_lock_bh(&hslot->lock);
  272. if (hslot->count > 10) {
  273. int exist;
  274. unsigned int slot2 = udp_sk(sk)->udp_portaddr_hash ^ snum;
  275. slot2 &= udptable->mask;
  276. hash2_nulladdr &= udptable->mask;
  277. hslot2 = udp_hashslot2(udptable, slot2);
  278. if (hslot->count < hslot2->count)
  279. goto scan_primary_hash;
  280. exist = udp_lib_lport_inuse2(net, snum, hslot2, sk);
  281. if (!exist && (hash2_nulladdr != slot2)) {
  282. hslot2 = udp_hashslot2(udptable, hash2_nulladdr);
  283. exist = udp_lib_lport_inuse2(net, snum, hslot2,
  284. sk);
  285. }
  286. if (exist)
  287. goto fail_unlock;
  288. else
  289. goto found;
  290. }
  291. scan_primary_hash:
  292. if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk, 0))
  293. goto fail_unlock;
  294. }
  295. found:
  296. inet_sk(sk)->inet_num = snum;
  297. udp_sk(sk)->udp_port_hash = snum;
  298. udp_sk(sk)->udp_portaddr_hash ^= snum;
  299. if (sk_unhashed(sk)) {
  300. if (sk->sk_reuseport &&
  301. udp_reuseport_add_sock(sk, hslot)) {
  302. inet_sk(sk)->inet_num = 0;
  303. udp_sk(sk)->udp_port_hash = 0;
  304. udp_sk(sk)->udp_portaddr_hash ^= snum;
  305. goto fail_unlock;
  306. }
  307. sk_add_node_rcu(sk, &hslot->head);
  308. hslot->count++;
  309. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  310. hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
  311. spin_lock(&hslot2->lock);
  312. if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
  313. sk->sk_family == AF_INET6)
  314. hlist_add_tail_rcu(&udp_sk(sk)->udp_portaddr_node,
  315. &hslot2->head);
  316. else
  317. hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
  318. &hslot2->head);
  319. hslot2->count++;
  320. spin_unlock(&hslot2->lock);
  321. }
  322. sock_set_flag(sk, SOCK_RCU_FREE);
  323. error = 0;
  324. fail_unlock:
  325. spin_unlock_bh(&hslot->lock);
  326. fail:
  327. return error;
  328. }
  329. EXPORT_SYMBOL(udp_lib_get_port);
  330. int udp_v4_get_port(struct sock *sk, unsigned short snum)
  331. {
  332. unsigned int hash2_nulladdr =
  333. ipv4_portaddr_hash(sock_net(sk), htonl(INADDR_ANY), snum);
  334. unsigned int hash2_partial =
  335. ipv4_portaddr_hash(sock_net(sk), inet_sk(sk)->inet_rcv_saddr, 0);
  336. /* precompute partial secondary hash */
  337. udp_sk(sk)->udp_portaddr_hash = hash2_partial;
  338. return udp_lib_get_port(sk, snum, hash2_nulladdr);
  339. }
  340. static int compute_score(struct sock *sk, struct net *net,
  341. __be32 saddr, __be16 sport,
  342. __be32 daddr, unsigned short hnum,
  343. int dif, int sdif, bool exact_dif)
  344. {
  345. int score;
  346. struct inet_sock *inet;
  347. if (!net_eq(sock_net(sk), net) ||
  348. udp_sk(sk)->udp_port_hash != hnum ||
  349. ipv6_only_sock(sk))
  350. return -1;
  351. score = (sk->sk_family == PF_INET) ? 2 : 1;
  352. inet = inet_sk(sk);
  353. if (inet->inet_rcv_saddr) {
  354. if (inet->inet_rcv_saddr != daddr)
  355. return -1;
  356. score += 4;
  357. }
  358. if (inet->inet_daddr) {
  359. if (inet->inet_daddr != saddr)
  360. return -1;
  361. score += 4;
  362. }
  363. if (inet->inet_dport) {
  364. if (inet->inet_dport != sport)
  365. return -1;
  366. score += 4;
  367. }
  368. if (sk->sk_bound_dev_if || exact_dif) {
  369. bool dev_match = (sk->sk_bound_dev_if == dif ||
  370. sk->sk_bound_dev_if == sdif);
  371. if (!dev_match)
  372. return -1;
  373. if (sk->sk_bound_dev_if)
  374. score += 4;
  375. }
  376. if (sk->sk_incoming_cpu == raw_smp_processor_id())
  377. score++;
  378. return score;
  379. }
  380. static u32 udp_ehashfn(const struct net *net, const __be32 laddr,
  381. const __u16 lport, const __be32 faddr,
  382. const __be16 fport)
  383. {
  384. static u32 udp_ehash_secret __read_mostly;
  385. net_get_random_once(&udp_ehash_secret, sizeof(udp_ehash_secret));
  386. return __inet_ehashfn(laddr, lport, faddr, fport,
  387. udp_ehash_secret + net_hash_mix(net));
  388. }
  389. /* called with rcu_read_lock() */
  390. static struct sock *udp4_lib_lookup2(struct net *net,
  391. __be32 saddr, __be16 sport,
  392. __be32 daddr, unsigned int hnum,
  393. int dif, int sdif, bool exact_dif,
  394. struct udp_hslot *hslot2,
  395. struct sk_buff *skb)
  396. {
  397. struct sock *sk, *result;
  398. int score, badness;
  399. u32 hash = 0;
  400. result = NULL;
  401. badness = 0;
  402. udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
  403. score = compute_score(sk, net, saddr, sport,
  404. daddr, hnum, dif, sdif, exact_dif);
  405. if (score > badness) {
  406. if (sk->sk_reuseport) {
  407. hash = udp_ehashfn(net, daddr, hnum,
  408. saddr, sport);
  409. result = reuseport_select_sock(sk, hash, skb,
  410. sizeof(struct udphdr));
  411. if (result)
  412. return result;
  413. }
  414. badness = score;
  415. result = sk;
  416. }
  417. }
  418. return result;
  419. }
  420. /* UDP is nearly always wildcards out the wazoo, it makes no sense to try
  421. * harder than this. -DaveM
  422. */
  423. struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,
  424. __be16 sport, __be32 daddr, __be16 dport, int dif,
  425. int sdif, struct udp_table *udptable, struct sk_buff *skb)
  426. {
  427. struct sock *sk, *result;
  428. unsigned short hnum = ntohs(dport);
  429. unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
  430. struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
  431. bool exact_dif = udp_lib_exact_dif_match(net, skb);
  432. int score, badness;
  433. u32 hash = 0;
  434. if (hslot->count > 10) {
  435. hash2 = ipv4_portaddr_hash(net, daddr, hnum);
  436. slot2 = hash2 & udptable->mask;
  437. hslot2 = &udptable->hash2[slot2];
  438. if (hslot->count < hslot2->count)
  439. goto begin;
  440. result = udp4_lib_lookup2(net, saddr, sport,
  441. daddr, hnum, dif, sdif,
  442. exact_dif, hslot2, skb);
  443. if (!result) {
  444. unsigned int old_slot2 = slot2;
  445. hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum);
  446. slot2 = hash2 & udptable->mask;
  447. /* avoid searching the same slot again. */
  448. if (unlikely(slot2 == old_slot2))
  449. return result;
  450. hslot2 = &udptable->hash2[slot2];
  451. if (hslot->count < hslot2->count)
  452. goto begin;
  453. result = udp4_lib_lookup2(net, saddr, sport,
  454. daddr, hnum, dif, sdif,
  455. exact_dif, hslot2, skb);
  456. }
  457. if (unlikely(IS_ERR(result)))
  458. return NULL;
  459. return result;
  460. }
  461. begin:
  462. result = NULL;
  463. badness = 0;
  464. sk_for_each_rcu(sk, &hslot->head) {
  465. score = compute_score(sk, net, saddr, sport,
  466. daddr, hnum, dif, sdif, exact_dif);
  467. if (score > badness) {
  468. if (sk->sk_reuseport) {
  469. hash = udp_ehashfn(net, daddr, hnum,
  470. saddr, sport);
  471. result = reuseport_select_sock(sk, hash, skb,
  472. sizeof(struct udphdr));
  473. if (unlikely(IS_ERR(result)))
  474. return NULL;
  475. if (result)
  476. return result;
  477. }
  478. result = sk;
  479. badness = score;
  480. }
  481. }
  482. return result;
  483. }
  484. EXPORT_SYMBOL_GPL(__udp4_lib_lookup);
  485. static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb,
  486. __be16 sport, __be16 dport,
  487. struct udp_table *udptable)
  488. {
  489. const struct iphdr *iph = ip_hdr(skb);
  490. return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport,
  491. iph->daddr, dport, inet_iif(skb),
  492. inet_sdif(skb), udptable, skb);
  493. }
  494. struct sock *udp4_lib_lookup_skb(struct sk_buff *skb,
  495. __be16 sport, __be16 dport)
  496. {
  497. return __udp4_lib_lookup_skb(skb, sport, dport, &udp_table);
  498. }
  499. EXPORT_SYMBOL_GPL(udp4_lib_lookup_skb);
  500. /* Must be called under rcu_read_lock().
  501. * Does increment socket refcount.
  502. */
  503. #if IS_ENABLED(CONFIG_NF_TPROXY_IPV4) || IS_ENABLED(CONFIG_NF_SOCKET_IPV4)
  504. struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
  505. __be32 daddr, __be16 dport, int dif)
  506. {
  507. struct sock *sk;
  508. sk = __udp4_lib_lookup(net, saddr, sport, daddr, dport,
  509. dif, 0, &udp_table, NULL);
  510. if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
  511. sk = NULL;
  512. return sk;
  513. }
  514. EXPORT_SYMBOL_GPL(udp4_lib_lookup);
  515. #endif
  516. static inline bool __udp_is_mcast_sock(struct net *net, struct sock *sk,
  517. __be16 loc_port, __be32 loc_addr,
  518. __be16 rmt_port, __be32 rmt_addr,
  519. int dif, int sdif, unsigned short hnum)
  520. {
  521. struct inet_sock *inet = inet_sk(sk);
  522. if (!net_eq(sock_net(sk), net) ||
  523. udp_sk(sk)->udp_port_hash != hnum ||
  524. (inet->inet_daddr && inet->inet_daddr != rmt_addr) ||
  525. (inet->inet_dport != rmt_port && inet->inet_dport) ||
  526. (inet->inet_rcv_saddr && inet->inet_rcv_saddr != loc_addr) ||
  527. ipv6_only_sock(sk) ||
  528. (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif &&
  529. sk->sk_bound_dev_if != sdif))
  530. return false;
  531. if (!ip_mc_sf_allow(sk, loc_addr, rmt_addr, dif, sdif))
  532. return false;
  533. return true;
  534. }
  535. /*
  536. * This routine is called by the ICMP module when it gets some
  537. * sort of error condition. If err < 0 then the socket should
  538. * be closed and the error returned to the user. If err > 0
  539. * it's just the icmp type << 8 | icmp code.
  540. * Header points to the ip header of the error packet. We move
  541. * on past this. Then (as it used to claim before adjustment)
  542. * header points to the first 8 bytes of the udp header. We need
  543. * to find the appropriate port.
  544. */
  545. void __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable)
  546. {
  547. struct inet_sock *inet;
  548. const struct iphdr *iph = (const struct iphdr *)skb->data;
  549. struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2));
  550. const int type = icmp_hdr(skb)->type;
  551. const int code = icmp_hdr(skb)->code;
  552. struct sock *sk;
  553. int harderr;
  554. int err;
  555. struct net *net = dev_net(skb->dev);
  556. sk = __udp4_lib_lookup(net, iph->daddr, uh->dest,
  557. iph->saddr, uh->source, skb->dev->ifindex,
  558. inet_sdif(skb), udptable, NULL);
  559. if (!sk) {
  560. __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
  561. return; /* No socket for error */
  562. }
  563. err = 0;
  564. harderr = 0;
  565. inet = inet_sk(sk);
  566. switch (type) {
  567. default:
  568. case ICMP_TIME_EXCEEDED:
  569. err = EHOSTUNREACH;
  570. break;
  571. case ICMP_SOURCE_QUENCH:
  572. goto out;
  573. case ICMP_PARAMETERPROB:
  574. err = EPROTO;
  575. harderr = 1;
  576. break;
  577. case ICMP_DEST_UNREACH:
  578. if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
  579. ipv4_sk_update_pmtu(skb, sk, info);
  580. if (inet->pmtudisc != IP_PMTUDISC_DONT) {
  581. err = EMSGSIZE;
  582. harderr = 1;
  583. break;
  584. }
  585. goto out;
  586. }
  587. err = EHOSTUNREACH;
  588. if (code <= NR_ICMP_UNREACH) {
  589. harderr = icmp_err_convert[code].fatal;
  590. err = icmp_err_convert[code].errno;
  591. }
  592. break;
  593. case ICMP_REDIRECT:
  594. ipv4_sk_redirect(skb, sk);
  595. goto out;
  596. }
  597. /*
  598. * RFC1122: OK. Passes ICMP errors back to application, as per
  599. * 4.1.3.3.
  600. */
  601. if (!inet->recverr) {
  602. if (!harderr || sk->sk_state != TCP_ESTABLISHED)
  603. goto out;
  604. } else
  605. ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1));
  606. sk->sk_err = err;
  607. sk->sk_error_report(sk);
  608. out:
  609. return;
  610. }
  611. void udp_err(struct sk_buff *skb, u32 info)
  612. {
  613. __udp4_lib_err(skb, info, &udp_table);
  614. }
  615. /*
  616. * Throw away all pending data and cancel the corking. Socket is locked.
  617. */
  618. void udp_flush_pending_frames(struct sock *sk)
  619. {
  620. struct udp_sock *up = udp_sk(sk);
  621. if (up->pending) {
  622. up->len = 0;
  623. up->pending = 0;
  624. ip_flush_pending_frames(sk);
  625. }
  626. }
  627. EXPORT_SYMBOL(udp_flush_pending_frames);
  628. /**
  629. * udp4_hwcsum - handle outgoing HW checksumming
  630. * @skb: sk_buff containing the filled-in UDP header
  631. * (checksum field must be zeroed out)
  632. * @src: source IP address
  633. * @dst: destination IP address
  634. */
  635. void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst)
  636. {
  637. struct udphdr *uh = udp_hdr(skb);
  638. int offset = skb_transport_offset(skb);
  639. int len = skb->len - offset;
  640. int hlen = len;
  641. __wsum csum = 0;
  642. if (!skb_has_frag_list(skb)) {
  643. /*
  644. * Only one fragment on the socket.
  645. */
  646. skb->csum_start = skb_transport_header(skb) - skb->head;
  647. skb->csum_offset = offsetof(struct udphdr, check);
  648. uh->check = ~csum_tcpudp_magic(src, dst, len,
  649. IPPROTO_UDP, 0);
  650. } else {
  651. struct sk_buff *frags;
  652. /*
  653. * HW-checksum won't work as there are two or more
  654. * fragments on the socket so that all csums of sk_buffs
  655. * should be together
  656. */
  657. skb_walk_frags(skb, frags) {
  658. csum = csum_add(csum, frags->csum);
  659. hlen -= frags->len;
  660. }
  661. csum = skb_checksum(skb, offset, hlen, csum);
  662. skb->ip_summed = CHECKSUM_NONE;
  663. uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum);
  664. if (uh->check == 0)
  665. uh->check = CSUM_MANGLED_0;
  666. }
  667. }
  668. EXPORT_SYMBOL_GPL(udp4_hwcsum);
  669. /* Function to set UDP checksum for an IPv4 UDP packet. This is intended
  670. * for the simple case like when setting the checksum for a UDP tunnel.
  671. */
  672. void udp_set_csum(bool nocheck, struct sk_buff *skb,
  673. __be32 saddr, __be32 daddr, int len)
  674. {
  675. struct udphdr *uh = udp_hdr(skb);
  676. if (nocheck) {
  677. uh->check = 0;
  678. } else if (skb_is_gso(skb)) {
  679. uh->check = ~udp_v4_check(len, saddr, daddr, 0);
  680. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  681. uh->check = 0;
  682. uh->check = udp_v4_check(len, saddr, daddr, lco_csum(skb));
  683. if (uh->check == 0)
  684. uh->check = CSUM_MANGLED_0;
  685. } else {
  686. skb->ip_summed = CHECKSUM_PARTIAL;
  687. skb->csum_start = skb_transport_header(skb) - skb->head;
  688. skb->csum_offset = offsetof(struct udphdr, check);
  689. uh->check = ~udp_v4_check(len, saddr, daddr, 0);
  690. }
  691. }
  692. EXPORT_SYMBOL(udp_set_csum);
  693. static int udp_send_skb(struct sk_buff *skb, struct flowi4 *fl4,
  694. struct inet_cork *cork)
  695. {
  696. struct sock *sk = skb->sk;
  697. struct inet_sock *inet = inet_sk(sk);
  698. struct udphdr *uh;
  699. int err = 0;
  700. int is_udplite = IS_UDPLITE(sk);
  701. int offset = skb_transport_offset(skb);
  702. int len = skb->len - offset;
  703. __wsum csum = 0;
  704. /*
  705. * Create a UDP header
  706. */
  707. uh = udp_hdr(skb);
  708. uh->source = inet->inet_sport;
  709. uh->dest = fl4->fl4_dport;
  710. uh->len = htons(len);
  711. uh->check = 0;
  712. if (cork->gso_size) {
  713. const int hlen = skb_network_header_len(skb) +
  714. sizeof(struct udphdr);
  715. if (hlen + cork->gso_size > cork->fragsize)
  716. return -EINVAL;
  717. if (skb->len > cork->gso_size * UDP_MAX_SEGMENTS)
  718. return -EINVAL;
  719. if (sk->sk_no_check_tx)
  720. return -EINVAL;
  721. if (skb->ip_summed != CHECKSUM_PARTIAL || is_udplite ||
  722. dst_xfrm(skb_dst(skb)))
  723. return -EIO;
  724. skb_shinfo(skb)->gso_size = cork->gso_size;
  725. skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4;
  726. skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(len - sizeof(uh),
  727. cork->gso_size);
  728. goto csum_partial;
  729. }
  730. if (is_udplite) /* UDP-Lite */
  731. csum = udplite_csum(skb);
  732. else if (sk->sk_no_check_tx) { /* UDP csum off */
  733. skb->ip_summed = CHECKSUM_NONE;
  734. goto send;
  735. } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
  736. csum_partial:
  737. udp4_hwcsum(skb, fl4->saddr, fl4->daddr);
  738. goto send;
  739. } else
  740. csum = udp_csum(skb);
  741. /* add protocol-dependent pseudo-header */
  742. uh->check = csum_tcpudp_magic(fl4->saddr, fl4->daddr, len,
  743. sk->sk_protocol, csum);
  744. if (uh->check == 0)
  745. uh->check = CSUM_MANGLED_0;
  746. send:
  747. err = ip_send_skb(sock_net(sk), skb);
  748. if (err) {
  749. if (err == -ENOBUFS && !inet->recverr) {
  750. UDP_INC_STATS(sock_net(sk),
  751. UDP_MIB_SNDBUFERRORS, is_udplite);
  752. err = 0;
  753. }
  754. } else
  755. UDP_INC_STATS(sock_net(sk),
  756. UDP_MIB_OUTDATAGRAMS, is_udplite);
  757. return err;
  758. }
  759. /*
  760. * Push out all pending data as one UDP datagram. Socket is locked.
  761. */
  762. int udp_push_pending_frames(struct sock *sk)
  763. {
  764. struct udp_sock *up = udp_sk(sk);
  765. struct inet_sock *inet = inet_sk(sk);
  766. struct flowi4 *fl4 = &inet->cork.fl.u.ip4;
  767. struct sk_buff *skb;
  768. int err = 0;
  769. skb = ip_finish_skb(sk, fl4);
  770. if (!skb)
  771. goto out;
  772. err = udp_send_skb(skb, fl4, &inet->cork.base);
  773. out:
  774. up->len = 0;
  775. up->pending = 0;
  776. return err;
  777. }
  778. EXPORT_SYMBOL(udp_push_pending_frames);
  779. static int __udp_cmsg_send(struct cmsghdr *cmsg, u16 *gso_size)
  780. {
  781. switch (cmsg->cmsg_type) {
  782. case UDP_SEGMENT:
  783. if (cmsg->cmsg_len != CMSG_LEN(sizeof(__u16)))
  784. return -EINVAL;
  785. *gso_size = *(__u16 *)CMSG_DATA(cmsg);
  786. return 0;
  787. default:
  788. return -EINVAL;
  789. }
  790. }
  791. int udp_cmsg_send(struct sock *sk, struct msghdr *msg, u16 *gso_size)
  792. {
  793. struct cmsghdr *cmsg;
  794. bool need_ip = false;
  795. int err;
  796. for_each_cmsghdr(cmsg, msg) {
  797. if (!CMSG_OK(msg, cmsg))
  798. return -EINVAL;
  799. if (cmsg->cmsg_level != SOL_UDP) {
  800. need_ip = true;
  801. continue;
  802. }
  803. err = __udp_cmsg_send(cmsg, gso_size);
  804. if (err)
  805. return err;
  806. }
  807. return need_ip;
  808. }
  809. EXPORT_SYMBOL_GPL(udp_cmsg_send);
  810. int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
  811. {
  812. struct inet_sock *inet = inet_sk(sk);
  813. struct udp_sock *up = udp_sk(sk);
  814. DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
  815. struct flowi4 fl4_stack;
  816. struct flowi4 *fl4;
  817. int ulen = len;
  818. struct ipcm_cookie ipc;
  819. struct rtable *rt = NULL;
  820. int free = 0;
  821. int connected = 0;
  822. __be32 daddr, faddr, saddr;
  823. __be16 dport;
  824. u8 tos;
  825. int err, is_udplite = IS_UDPLITE(sk);
  826. int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
  827. int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
  828. struct sk_buff *skb;
  829. struct ip_options_data opt_copy;
  830. if (len > 0xFFFF)
  831. return -EMSGSIZE;
  832. /*
  833. * Check the flags.
  834. */
  835. if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */
  836. return -EOPNOTSUPP;
  837. getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
  838. fl4 = &inet->cork.fl.u.ip4;
  839. if (up->pending) {
  840. /*
  841. * There are pending frames.
  842. * The socket lock must be held while it's corked.
  843. */
  844. lock_sock(sk);
  845. if (likely(up->pending)) {
  846. if (unlikely(up->pending != AF_INET)) {
  847. release_sock(sk);
  848. return -EINVAL;
  849. }
  850. goto do_append_data;
  851. }
  852. release_sock(sk);
  853. }
  854. ulen += sizeof(struct udphdr);
  855. /*
  856. * Get and verify the address.
  857. */
  858. if (usin) {
  859. if (msg->msg_namelen < sizeof(*usin))
  860. return -EINVAL;
  861. if (usin->sin_family != AF_INET) {
  862. if (usin->sin_family != AF_UNSPEC)
  863. return -EAFNOSUPPORT;
  864. }
  865. daddr = usin->sin_addr.s_addr;
  866. dport = usin->sin_port;
  867. if (dport == 0)
  868. return -EINVAL;
  869. } else {
  870. if (sk->sk_state != TCP_ESTABLISHED)
  871. return -EDESTADDRREQ;
  872. daddr = inet->inet_daddr;
  873. dport = inet->inet_dport;
  874. /* Open fast path for connected socket.
  875. Route will not be used, if at least one option is set.
  876. */
  877. connected = 1;
  878. }
  879. ipcm_init_sk(&ipc, inet);
  880. ipc.gso_size = up->gso_size;
  881. if (msg->msg_controllen) {
  882. err = udp_cmsg_send(sk, msg, &ipc.gso_size);
  883. if (err > 0)
  884. err = ip_cmsg_send(sk, msg, &ipc,
  885. sk->sk_family == AF_INET6);
  886. if (unlikely(err < 0)) {
  887. kfree(ipc.opt);
  888. return err;
  889. }
  890. if (ipc.opt)
  891. free = 1;
  892. connected = 0;
  893. }
  894. if (!ipc.opt) {
  895. struct ip_options_rcu *inet_opt;
  896. rcu_read_lock();
  897. inet_opt = rcu_dereference(inet->inet_opt);
  898. if (inet_opt) {
  899. memcpy(&opt_copy, inet_opt,
  900. sizeof(*inet_opt) + inet_opt->opt.optlen);
  901. ipc.opt = &opt_copy.opt;
  902. }
  903. rcu_read_unlock();
  904. }
  905. if (cgroup_bpf_enabled && !connected) {
  906. err = BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk,
  907. (struct sockaddr *)usin, &ipc.addr);
  908. if (err)
  909. goto out_free;
  910. if (usin) {
  911. if (usin->sin_port == 0) {
  912. /* BPF program set invalid port. Reject it. */
  913. err = -EINVAL;
  914. goto out_free;
  915. }
  916. daddr = usin->sin_addr.s_addr;
  917. dport = usin->sin_port;
  918. }
  919. }
  920. saddr = ipc.addr;
  921. ipc.addr = faddr = daddr;
  922. if (ipc.opt && ipc.opt->opt.srr) {
  923. if (!daddr) {
  924. err = -EINVAL;
  925. goto out_free;
  926. }
  927. faddr = ipc.opt->opt.faddr;
  928. connected = 0;
  929. }
  930. tos = get_rttos(&ipc, inet);
  931. if (sock_flag(sk, SOCK_LOCALROUTE) ||
  932. (msg->msg_flags & MSG_DONTROUTE) ||
  933. (ipc.opt && ipc.opt->opt.is_strictroute)) {
  934. tos |= RTO_ONLINK;
  935. connected = 0;
  936. }
  937. if (ipv4_is_multicast(daddr)) {
  938. if (!ipc.oif || netif_index_is_l3_master(sock_net(sk), ipc.oif))
  939. ipc.oif = inet->mc_index;
  940. if (!saddr)
  941. saddr = inet->mc_addr;
  942. connected = 0;
  943. } else if (!ipc.oif) {
  944. ipc.oif = inet->uc_index;
  945. } else if (ipv4_is_lbcast(daddr) && inet->uc_index) {
  946. /* oif is set, packet is to local broadcast and
  947. * and uc_index is set. oif is most likely set
  948. * by sk_bound_dev_if. If uc_index != oif check if the
  949. * oif is an L3 master and uc_index is an L3 slave.
  950. * If so, we want to allow the send using the uc_index.
  951. */
  952. if (ipc.oif != inet->uc_index &&
  953. ipc.oif == l3mdev_master_ifindex_by_index(sock_net(sk),
  954. inet->uc_index)) {
  955. ipc.oif = inet->uc_index;
  956. }
  957. }
  958. if (connected)
  959. rt = (struct rtable *)sk_dst_check(sk, 0);
  960. if (!rt) {
  961. struct net *net = sock_net(sk);
  962. __u8 flow_flags = inet_sk_flowi_flags(sk);
  963. fl4 = &fl4_stack;
  964. flowi4_init_output(fl4, ipc.oif, sk->sk_mark, tos,
  965. RT_SCOPE_UNIVERSE, sk->sk_protocol,
  966. flow_flags,
  967. faddr, saddr, dport, inet->inet_sport,
  968. sk->sk_uid);
  969. security_sk_classify_flow(sk, flowi4_to_flowi(fl4));
  970. rt = ip_route_output_flow(net, fl4, sk);
  971. if (IS_ERR(rt)) {
  972. err = PTR_ERR(rt);
  973. rt = NULL;
  974. if (err == -ENETUNREACH)
  975. IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
  976. goto out;
  977. }
  978. err = -EACCES;
  979. if ((rt->rt_flags & RTCF_BROADCAST) &&
  980. !sock_flag(sk, SOCK_BROADCAST))
  981. goto out;
  982. if (connected)
  983. sk_dst_set(sk, dst_clone(&rt->dst));
  984. }
  985. if (msg->msg_flags&MSG_CONFIRM)
  986. goto do_confirm;
  987. back_from_confirm:
  988. saddr = fl4->saddr;
  989. if (!ipc.addr)
  990. daddr = ipc.addr = fl4->daddr;
  991. /* Lockless fast path for the non-corking case. */
  992. if (!corkreq) {
  993. struct inet_cork cork;
  994. skb = ip_make_skb(sk, fl4, getfrag, msg, ulen,
  995. sizeof(struct udphdr), &ipc, &rt,
  996. &cork, msg->msg_flags);
  997. err = PTR_ERR(skb);
  998. if (!IS_ERR_OR_NULL(skb))
  999. err = udp_send_skb(skb, fl4, &cork);
  1000. goto out;
  1001. }
  1002. lock_sock(sk);
  1003. if (unlikely(up->pending)) {
  1004. /* The socket is already corked while preparing it. */
  1005. /* ... which is an evident application bug. --ANK */
  1006. release_sock(sk);
  1007. net_dbg_ratelimited("socket already corked\n");
  1008. err = -EINVAL;
  1009. goto out;
  1010. }
  1011. /*
  1012. * Now cork the socket to pend data.
  1013. */
  1014. fl4 = &inet->cork.fl.u.ip4;
  1015. fl4->daddr = daddr;
  1016. fl4->saddr = saddr;
  1017. fl4->fl4_dport = dport;
  1018. fl4->fl4_sport = inet->inet_sport;
  1019. up->pending = AF_INET;
  1020. do_append_data:
  1021. up->len += ulen;
  1022. err = ip_append_data(sk, fl4, getfrag, msg, ulen,
  1023. sizeof(struct udphdr), &ipc, &rt,
  1024. corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
  1025. if (err)
  1026. udp_flush_pending_frames(sk);
  1027. else if (!corkreq)
  1028. err = udp_push_pending_frames(sk);
  1029. else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
  1030. up->pending = 0;
  1031. release_sock(sk);
  1032. out:
  1033. ip_rt_put(rt);
  1034. out_free:
  1035. if (free)
  1036. kfree(ipc.opt);
  1037. if (!err)
  1038. return len;
  1039. /*
  1040. * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
  1041. * ENOBUFS might not be good (it's not tunable per se), but otherwise
  1042. * we don't have a good statistic (IpOutDiscards but it can be too many
  1043. * things). We could add another new stat but at least for now that
  1044. * seems like overkill.
  1045. */
  1046. if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
  1047. UDP_INC_STATS(sock_net(sk),
  1048. UDP_MIB_SNDBUFERRORS, is_udplite);
  1049. }
  1050. return err;
  1051. do_confirm:
  1052. if (msg->msg_flags & MSG_PROBE)
  1053. dst_confirm_neigh(&rt->dst, &fl4->daddr);
  1054. if (!(msg->msg_flags&MSG_PROBE) || len)
  1055. goto back_from_confirm;
  1056. err = 0;
  1057. goto out;
  1058. }
  1059. EXPORT_SYMBOL(udp_sendmsg);
  1060. int udp_sendpage(struct sock *sk, struct page *page, int offset,
  1061. size_t size, int flags)
  1062. {
  1063. struct inet_sock *inet = inet_sk(sk);
  1064. struct udp_sock *up = udp_sk(sk);
  1065. int ret;
  1066. if (flags & MSG_SENDPAGE_NOTLAST)
  1067. flags |= MSG_MORE;
  1068. if (!up->pending) {
  1069. struct msghdr msg = { .msg_flags = flags|MSG_MORE };
  1070. /* Call udp_sendmsg to specify destination address which
  1071. * sendpage interface can't pass.
  1072. * This will succeed only when the socket is connected.
  1073. */
  1074. ret = udp_sendmsg(sk, &msg, 0);
  1075. if (ret < 0)
  1076. return ret;
  1077. }
  1078. lock_sock(sk);
  1079. if (unlikely(!up->pending)) {
  1080. release_sock(sk);
  1081. net_dbg_ratelimited("cork failed\n");
  1082. return -EINVAL;
  1083. }
  1084. ret = ip_append_page(sk, &inet->cork.fl.u.ip4,
  1085. page, offset, size, flags);
  1086. if (ret == -EOPNOTSUPP) {
  1087. release_sock(sk);
  1088. return sock_no_sendpage(sk->sk_socket, page, offset,
  1089. size, flags);
  1090. }
  1091. if (ret < 0) {
  1092. udp_flush_pending_frames(sk);
  1093. goto out;
  1094. }
  1095. up->len += size;
  1096. if (!(up->corkflag || (flags&MSG_MORE)))
  1097. ret = udp_push_pending_frames(sk);
  1098. if (!ret)
  1099. ret = size;
  1100. out:
  1101. release_sock(sk);
  1102. return ret;
  1103. }
  1104. #define UDP_SKB_IS_STATELESS 0x80000000
  1105. static void udp_set_dev_scratch(struct sk_buff *skb)
  1106. {
  1107. struct udp_dev_scratch *scratch = udp_skb_scratch(skb);
  1108. BUILD_BUG_ON(sizeof(struct udp_dev_scratch) > sizeof(long));
  1109. scratch->_tsize_state = skb->truesize;
  1110. #if BITS_PER_LONG == 64
  1111. scratch->len = skb->len;
  1112. scratch->csum_unnecessary = !!skb_csum_unnecessary(skb);
  1113. scratch->is_linear = !skb_is_nonlinear(skb);
  1114. #endif
  1115. /* all head states execept sp (dst, sk, nf) are always cleared by
  1116. * udp_rcv() and we need to preserve secpath, if present, to eventually
  1117. * process IP_CMSG_PASSSEC at recvmsg() time
  1118. */
  1119. if (likely(!skb_sec_path(skb)))
  1120. scratch->_tsize_state |= UDP_SKB_IS_STATELESS;
  1121. }
  1122. static int udp_skb_truesize(struct sk_buff *skb)
  1123. {
  1124. return udp_skb_scratch(skb)->_tsize_state & ~UDP_SKB_IS_STATELESS;
  1125. }
  1126. static bool udp_skb_has_head_state(struct sk_buff *skb)
  1127. {
  1128. return !(udp_skb_scratch(skb)->_tsize_state & UDP_SKB_IS_STATELESS);
  1129. }
  1130. /* fully reclaim rmem/fwd memory allocated for skb */
  1131. static void udp_rmem_release(struct sock *sk, int size, int partial,
  1132. bool rx_queue_lock_held)
  1133. {
  1134. struct udp_sock *up = udp_sk(sk);
  1135. struct sk_buff_head *sk_queue;
  1136. int amt;
  1137. if (likely(partial)) {
  1138. up->forward_deficit += size;
  1139. size = up->forward_deficit;
  1140. if (size < (sk->sk_rcvbuf >> 2))
  1141. return;
  1142. } else {
  1143. size += up->forward_deficit;
  1144. }
  1145. up->forward_deficit = 0;
  1146. /* acquire the sk_receive_queue for fwd allocated memory scheduling,
  1147. * if the called don't held it already
  1148. */
  1149. sk_queue = &sk->sk_receive_queue;
  1150. if (!rx_queue_lock_held)
  1151. spin_lock(&sk_queue->lock);
  1152. sk->sk_forward_alloc += size;
  1153. amt = (sk->sk_forward_alloc - partial) & ~(SK_MEM_QUANTUM - 1);
  1154. sk->sk_forward_alloc -= amt;
  1155. if (amt)
  1156. __sk_mem_reduce_allocated(sk, amt >> SK_MEM_QUANTUM_SHIFT);
  1157. atomic_sub(size, &sk->sk_rmem_alloc);
  1158. /* this can save us from acquiring the rx queue lock on next receive */
  1159. skb_queue_splice_tail_init(sk_queue, &up->reader_queue);
  1160. if (!rx_queue_lock_held)
  1161. spin_unlock(&sk_queue->lock);
  1162. }
  1163. /* Note: called with reader_queue.lock held.
  1164. * Instead of using skb->truesize here, find a copy of it in skb->dev_scratch
  1165. * This avoids a cache line miss while receive_queue lock is held.
  1166. * Look at __udp_enqueue_schedule_skb() to find where this copy is done.
  1167. */
  1168. void udp_skb_destructor(struct sock *sk, struct sk_buff *skb)
  1169. {
  1170. prefetch(&skb->data);
  1171. udp_rmem_release(sk, udp_skb_truesize(skb), 1, false);
  1172. }
  1173. EXPORT_SYMBOL(udp_skb_destructor);
  1174. /* as above, but the caller held the rx queue lock, too */
  1175. static void udp_skb_dtor_locked(struct sock *sk, struct sk_buff *skb)
  1176. {
  1177. prefetch(&skb->data);
  1178. udp_rmem_release(sk, udp_skb_truesize(skb), 1, true);
  1179. }
  1180. /* Idea of busylocks is to let producers grab an extra spinlock
  1181. * to relieve pressure on the receive_queue spinlock shared by consumer.
  1182. * Under flood, this means that only one producer can be in line
  1183. * trying to acquire the receive_queue spinlock.
  1184. * These busylock can be allocated on a per cpu manner, instead of a
  1185. * per socket one (that would consume a cache line per socket)
  1186. */
  1187. static int udp_busylocks_log __read_mostly;
  1188. static spinlock_t *udp_busylocks __read_mostly;
  1189. static spinlock_t *busylock_acquire(void *ptr)
  1190. {
  1191. spinlock_t *busy;
  1192. busy = udp_busylocks + hash_ptr(ptr, udp_busylocks_log);
  1193. spin_lock(busy);
  1194. return busy;
  1195. }
  1196. static void busylock_release(spinlock_t *busy)
  1197. {
  1198. if (busy)
  1199. spin_unlock(busy);
  1200. }
  1201. int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb)
  1202. {
  1203. struct sk_buff_head *list = &sk->sk_receive_queue;
  1204. int rmem, delta, amt, err = -ENOMEM;
  1205. spinlock_t *busy = NULL;
  1206. int size;
  1207. /* try to avoid the costly atomic add/sub pair when the receive
  1208. * queue is full; always allow at least a packet
  1209. */
  1210. rmem = atomic_read(&sk->sk_rmem_alloc);
  1211. if (rmem > sk->sk_rcvbuf)
  1212. goto drop;
  1213. /* Under mem pressure, it might be helpful to help udp_recvmsg()
  1214. * having linear skbs :
  1215. * - Reduce memory overhead and thus increase receive queue capacity
  1216. * - Less cache line misses at copyout() time
  1217. * - Less work at consume_skb() (less alien page frag freeing)
  1218. */
  1219. if (rmem > (sk->sk_rcvbuf >> 1)) {
  1220. skb_condense(skb);
  1221. busy = busylock_acquire(sk);
  1222. }
  1223. size = skb->truesize;
  1224. udp_set_dev_scratch(skb);
  1225. /* we drop only if the receive buf is full and the receive
  1226. * queue contains some other skb
  1227. */
  1228. rmem = atomic_add_return(size, &sk->sk_rmem_alloc);
  1229. if (rmem > (size + sk->sk_rcvbuf))
  1230. goto uncharge_drop;
  1231. spin_lock(&list->lock);
  1232. if (size >= sk->sk_forward_alloc) {
  1233. amt = sk_mem_pages(size);
  1234. delta = amt << SK_MEM_QUANTUM_SHIFT;
  1235. if (!__sk_mem_raise_allocated(sk, delta, amt, SK_MEM_RECV)) {
  1236. err = -ENOBUFS;
  1237. spin_unlock(&list->lock);
  1238. goto uncharge_drop;
  1239. }
  1240. sk->sk_forward_alloc += delta;
  1241. }
  1242. sk->sk_forward_alloc -= size;
  1243. /* no need to setup a destructor, we will explicitly release the
  1244. * forward allocated memory on dequeue
  1245. */
  1246. sock_skb_set_dropcount(sk, skb);
  1247. __skb_queue_tail(list, skb);
  1248. spin_unlock(&list->lock);
  1249. if (!sock_flag(sk, SOCK_DEAD))
  1250. sk->sk_data_ready(sk);
  1251. busylock_release(busy);
  1252. return 0;
  1253. uncharge_drop:
  1254. atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
  1255. drop:
  1256. atomic_inc(&sk->sk_drops);
  1257. busylock_release(busy);
  1258. return err;
  1259. }
  1260. EXPORT_SYMBOL_GPL(__udp_enqueue_schedule_skb);
  1261. void udp_destruct_sock(struct sock *sk)
  1262. {
  1263. /* reclaim completely the forward allocated memory */
  1264. struct udp_sock *up = udp_sk(sk);
  1265. unsigned int total = 0;
  1266. struct sk_buff *skb;
  1267. skb_queue_splice_tail_init(&sk->sk_receive_queue, &up->reader_queue);
  1268. while ((skb = __skb_dequeue(&up->reader_queue)) != NULL) {
  1269. total += skb->truesize;
  1270. kfree_skb(skb);
  1271. }
  1272. udp_rmem_release(sk, total, 0, true);
  1273. inet_sock_destruct(sk);
  1274. }
  1275. EXPORT_SYMBOL_GPL(udp_destruct_sock);
  1276. int udp_init_sock(struct sock *sk)
  1277. {
  1278. skb_queue_head_init(&udp_sk(sk)->reader_queue);
  1279. sk->sk_destruct = udp_destruct_sock;
  1280. return 0;
  1281. }
  1282. EXPORT_SYMBOL_GPL(udp_init_sock);
  1283. void skb_consume_udp(struct sock *sk, struct sk_buff *skb, int len)
  1284. {
  1285. if (unlikely(READ_ONCE(sk->sk_peek_off) >= 0)) {
  1286. bool slow = lock_sock_fast(sk);
  1287. sk_peek_offset_bwd(sk, len);
  1288. unlock_sock_fast(sk, slow);
  1289. }
  1290. if (!skb_unref(skb))
  1291. return;
  1292. /* In the more common cases we cleared the head states previously,
  1293. * see __udp_queue_rcv_skb().
  1294. */
  1295. if (unlikely(udp_skb_has_head_state(skb)))
  1296. skb_release_head_state(skb);
  1297. __consume_stateless_skb(skb);
  1298. }
  1299. EXPORT_SYMBOL_GPL(skb_consume_udp);
  1300. static struct sk_buff *__first_packet_length(struct sock *sk,
  1301. struct sk_buff_head *rcvq,
  1302. int *total)
  1303. {
  1304. struct sk_buff *skb;
  1305. while ((skb = skb_peek(rcvq)) != NULL) {
  1306. if (udp_lib_checksum_complete(skb)) {
  1307. __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS,
  1308. IS_UDPLITE(sk));
  1309. __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS,
  1310. IS_UDPLITE(sk));
  1311. atomic_inc(&sk->sk_drops);
  1312. __skb_unlink(skb, rcvq);
  1313. *total += skb->truesize;
  1314. kfree_skb(skb);
  1315. } else {
  1316. /* the csum related bits could be changed, refresh
  1317. * the scratch area
  1318. */
  1319. udp_set_dev_scratch(skb);
  1320. break;
  1321. }
  1322. }
  1323. return skb;
  1324. }
  1325. /**
  1326. * first_packet_length - return length of first packet in receive queue
  1327. * @sk: socket
  1328. *
  1329. * Drops all bad checksum frames, until a valid one is found.
  1330. * Returns the length of found skb, or -1 if none is found.
  1331. */
  1332. static int first_packet_length(struct sock *sk)
  1333. {
  1334. struct sk_buff_head *rcvq = &udp_sk(sk)->reader_queue;
  1335. struct sk_buff_head *sk_queue = &sk->sk_receive_queue;
  1336. struct sk_buff *skb;
  1337. int total = 0;
  1338. int res;
  1339. spin_lock_bh(&rcvq->lock);
  1340. skb = __first_packet_length(sk, rcvq, &total);
  1341. if (!skb && !skb_queue_empty(sk_queue)) {
  1342. spin_lock(&sk_queue->lock);
  1343. skb_queue_splice_tail_init(sk_queue, rcvq);
  1344. spin_unlock(&sk_queue->lock);
  1345. skb = __first_packet_length(sk, rcvq, &total);
  1346. }
  1347. res = skb ? skb->len : -1;
  1348. if (total)
  1349. udp_rmem_release(sk, total, 1, false);
  1350. spin_unlock_bh(&rcvq->lock);
  1351. return res;
  1352. }
  1353. /*
  1354. * IOCTL requests applicable to the UDP protocol
  1355. */
  1356. int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  1357. {
  1358. switch (cmd) {
  1359. case SIOCOUTQ:
  1360. {
  1361. int amount = sk_wmem_alloc_get(sk);
  1362. return put_user(amount, (int __user *)arg);
  1363. }
  1364. case SIOCINQ:
  1365. {
  1366. int amount = max_t(int, 0, first_packet_length(sk));
  1367. return put_user(amount, (int __user *)arg);
  1368. }
  1369. default:
  1370. return -ENOIOCTLCMD;
  1371. }
  1372. return 0;
  1373. }
  1374. EXPORT_SYMBOL(udp_ioctl);
  1375. struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags,
  1376. int noblock, int *peeked, int *off, int *err)
  1377. {
  1378. struct sk_buff_head *sk_queue = &sk->sk_receive_queue;
  1379. struct sk_buff_head *queue;
  1380. struct sk_buff *last;
  1381. long timeo;
  1382. int error;
  1383. queue = &udp_sk(sk)->reader_queue;
  1384. flags |= noblock ? MSG_DONTWAIT : 0;
  1385. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1386. do {
  1387. struct sk_buff *skb;
  1388. error = sock_error(sk);
  1389. if (error)
  1390. break;
  1391. error = -EAGAIN;
  1392. *peeked = 0;
  1393. do {
  1394. spin_lock_bh(&queue->lock);
  1395. skb = __skb_try_recv_from_queue(sk, queue, flags,
  1396. udp_skb_destructor,
  1397. peeked, off, err,
  1398. &last);
  1399. if (skb) {
  1400. spin_unlock_bh(&queue->lock);
  1401. return skb;
  1402. }
  1403. if (skb_queue_empty(sk_queue)) {
  1404. spin_unlock_bh(&queue->lock);
  1405. goto busy_check;
  1406. }
  1407. /* refill the reader queue and walk it again
  1408. * keep both queues locked to avoid re-acquiring
  1409. * the sk_receive_queue lock if fwd memory scheduling
  1410. * is needed.
  1411. */
  1412. spin_lock(&sk_queue->lock);
  1413. skb_queue_splice_tail_init(sk_queue, queue);
  1414. skb = __skb_try_recv_from_queue(sk, queue, flags,
  1415. udp_skb_dtor_locked,
  1416. peeked, off, err,
  1417. &last);
  1418. spin_unlock(&sk_queue->lock);
  1419. spin_unlock_bh(&queue->lock);
  1420. if (skb)
  1421. return skb;
  1422. busy_check:
  1423. if (!sk_can_busy_loop(sk))
  1424. break;
  1425. sk_busy_loop(sk, flags & MSG_DONTWAIT);
  1426. } while (!skb_queue_empty(sk_queue));
  1427. /* sk_queue is empty, reader_queue may contain peeked packets */
  1428. } while (timeo &&
  1429. !__skb_wait_for_more_packets(sk, &error, &timeo,
  1430. (struct sk_buff *)sk_queue));
  1431. *err = error;
  1432. return NULL;
  1433. }
  1434. EXPORT_SYMBOL(__skb_recv_udp);
  1435. /*
  1436. * This should be easy, if there is something there we
  1437. * return it, otherwise we block.
  1438. */
  1439. int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock,
  1440. int flags, int *addr_len)
  1441. {
  1442. struct inet_sock *inet = inet_sk(sk);
  1443. DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
  1444. struct sk_buff *skb;
  1445. unsigned int ulen, copied;
  1446. int peeked, peeking, off;
  1447. int err;
  1448. int is_udplite = IS_UDPLITE(sk);
  1449. bool checksum_valid = false;
  1450. if (flags & MSG_ERRQUEUE)
  1451. return ip_recv_error(sk, msg, len, addr_len);
  1452. try_again:
  1453. peeking = flags & MSG_PEEK;
  1454. off = sk_peek_offset(sk, flags);
  1455. skb = __skb_recv_udp(sk, flags, noblock, &peeked, &off, &err);
  1456. if (!skb)
  1457. return err;
  1458. ulen = udp_skb_len(skb);
  1459. copied = len;
  1460. if (copied > ulen - off)
  1461. copied = ulen - off;
  1462. else if (copied < ulen)
  1463. msg->msg_flags |= MSG_TRUNC;
  1464. /*
  1465. * If checksum is needed at all, try to do it while copying the
  1466. * data. If the data is truncated, or if we only want a partial
  1467. * coverage checksum (UDP-Lite), do it before the copy.
  1468. */
  1469. if (copied < ulen || peeking ||
  1470. (is_udplite && UDP_SKB_CB(skb)->partial_cov)) {
  1471. checksum_valid = udp_skb_csum_unnecessary(skb) ||
  1472. !__udp_lib_checksum_complete(skb);
  1473. if (!checksum_valid)
  1474. goto csum_copy_err;
  1475. }
  1476. if (checksum_valid || udp_skb_csum_unnecessary(skb)) {
  1477. if (udp_skb_is_linear(skb))
  1478. err = copy_linear_skb(skb, copied, off, &msg->msg_iter);
  1479. else
  1480. err = skb_copy_datagram_msg(skb, off, msg, copied);
  1481. } else {
  1482. err = skb_copy_and_csum_datagram_msg(skb, off, msg);
  1483. if (err == -EINVAL)
  1484. goto csum_copy_err;
  1485. }
  1486. if (unlikely(err)) {
  1487. if (!peeked) {
  1488. atomic_inc(&sk->sk_drops);
  1489. UDP_INC_STATS(sock_net(sk),
  1490. UDP_MIB_INERRORS, is_udplite);
  1491. }
  1492. kfree_skb(skb);
  1493. return err;
  1494. }
  1495. if (!peeked)
  1496. UDP_INC_STATS(sock_net(sk),
  1497. UDP_MIB_INDATAGRAMS, is_udplite);
  1498. sock_recv_ts_and_drops(msg, sk, skb);
  1499. /* Copy the address. */
  1500. if (sin) {
  1501. sin->sin_family = AF_INET;
  1502. sin->sin_port = udp_hdr(skb)->source;
  1503. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  1504. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  1505. *addr_len = sizeof(*sin);
  1506. }
  1507. if (inet->cmsg_flags)
  1508. ip_cmsg_recv_offset(msg, sk, skb, sizeof(struct udphdr), off);
  1509. err = copied;
  1510. if (flags & MSG_TRUNC)
  1511. err = ulen;
  1512. skb_consume_udp(sk, skb, peeking ? -err : err);
  1513. return err;
  1514. csum_copy_err:
  1515. if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags,
  1516. udp_skb_destructor)) {
  1517. UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
  1518. UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  1519. }
  1520. kfree_skb(skb);
  1521. /* starting over for a new packet, but check if we need to yield */
  1522. cond_resched();
  1523. msg->msg_flags &= ~MSG_TRUNC;
  1524. goto try_again;
  1525. }
  1526. int udp_pre_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  1527. {
  1528. /* This check is replicated from __ip4_datagram_connect() and
  1529. * intended to prevent BPF program called below from accessing bytes
  1530. * that are out of the bound specified by user in addr_len.
  1531. */
  1532. if (addr_len < sizeof(struct sockaddr_in))
  1533. return -EINVAL;
  1534. return BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr);
  1535. }
  1536. EXPORT_SYMBOL(udp_pre_connect);
  1537. int __udp_disconnect(struct sock *sk, int flags)
  1538. {
  1539. struct inet_sock *inet = inet_sk(sk);
  1540. /*
  1541. * 1003.1g - break association.
  1542. */
  1543. sk->sk_state = TCP_CLOSE;
  1544. inet->inet_daddr = 0;
  1545. inet->inet_dport = 0;
  1546. sock_rps_reset_rxhash(sk);
  1547. sk->sk_bound_dev_if = 0;
  1548. if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
  1549. inet_reset_saddr(sk);
  1550. if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
  1551. sk->sk_prot->unhash(sk);
  1552. inet->inet_sport = 0;
  1553. }
  1554. sk_dst_reset(sk);
  1555. return 0;
  1556. }
  1557. EXPORT_SYMBOL(__udp_disconnect);
  1558. int udp_disconnect(struct sock *sk, int flags)
  1559. {
  1560. lock_sock(sk);
  1561. __udp_disconnect(sk, flags);
  1562. release_sock(sk);
  1563. return 0;
  1564. }
  1565. EXPORT_SYMBOL(udp_disconnect);
  1566. void udp_lib_unhash(struct sock *sk)
  1567. {
  1568. if (sk_hashed(sk)) {
  1569. struct udp_table *udptable = sk->sk_prot->h.udp_table;
  1570. struct udp_hslot *hslot, *hslot2;
  1571. hslot = udp_hashslot(udptable, sock_net(sk),
  1572. udp_sk(sk)->udp_port_hash);
  1573. hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
  1574. spin_lock_bh(&hslot->lock);
  1575. if (rcu_access_pointer(sk->sk_reuseport_cb))
  1576. reuseport_detach_sock(sk);
  1577. if (sk_del_node_init_rcu(sk)) {
  1578. hslot->count--;
  1579. inet_sk(sk)->inet_num = 0;
  1580. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  1581. spin_lock(&hslot2->lock);
  1582. hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
  1583. hslot2->count--;
  1584. spin_unlock(&hslot2->lock);
  1585. }
  1586. spin_unlock_bh(&hslot->lock);
  1587. }
  1588. }
  1589. EXPORT_SYMBOL(udp_lib_unhash);
  1590. /*
  1591. * inet_rcv_saddr was changed, we must rehash secondary hash
  1592. */
  1593. void udp_lib_rehash(struct sock *sk, u16 newhash)
  1594. {
  1595. if (sk_hashed(sk)) {
  1596. struct udp_table *udptable = sk->sk_prot->h.udp_table;
  1597. struct udp_hslot *hslot, *hslot2, *nhslot2;
  1598. hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
  1599. nhslot2 = udp_hashslot2(udptable, newhash);
  1600. udp_sk(sk)->udp_portaddr_hash = newhash;
  1601. if (hslot2 != nhslot2 ||
  1602. rcu_access_pointer(sk->sk_reuseport_cb)) {
  1603. hslot = udp_hashslot(udptable, sock_net(sk),
  1604. udp_sk(sk)->udp_port_hash);
  1605. /* we must lock primary chain too */
  1606. spin_lock_bh(&hslot->lock);
  1607. if (rcu_access_pointer(sk->sk_reuseport_cb))
  1608. reuseport_detach_sock(sk);
  1609. if (hslot2 != nhslot2) {
  1610. spin_lock(&hslot2->lock);
  1611. hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
  1612. hslot2->count--;
  1613. spin_unlock(&hslot2->lock);
  1614. spin_lock(&nhslot2->lock);
  1615. hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
  1616. &nhslot2->head);
  1617. nhslot2->count++;
  1618. spin_unlock(&nhslot2->lock);
  1619. }
  1620. spin_unlock_bh(&hslot->lock);
  1621. }
  1622. }
  1623. }
  1624. EXPORT_SYMBOL(udp_lib_rehash);
  1625. static void udp_v4_rehash(struct sock *sk)
  1626. {
  1627. u16 new_hash = ipv4_portaddr_hash(sock_net(sk),
  1628. inet_sk(sk)->inet_rcv_saddr,
  1629. inet_sk(sk)->inet_num);
  1630. udp_lib_rehash(sk, new_hash);
  1631. }
  1632. static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  1633. {
  1634. int rc;
  1635. if (inet_sk(sk)->inet_daddr) {
  1636. sock_rps_save_rxhash(sk, skb);
  1637. sk_mark_napi_id(sk, skb);
  1638. sk_incoming_cpu_update(sk);
  1639. } else {
  1640. sk_mark_napi_id_once(sk, skb);
  1641. }
  1642. rc = __udp_enqueue_schedule_skb(sk, skb);
  1643. if (rc < 0) {
  1644. int is_udplite = IS_UDPLITE(sk);
  1645. /* Note that an ENOMEM error is charged twice */
  1646. if (rc == -ENOMEM)
  1647. UDP_INC_STATS(sock_net(sk), UDP_MIB_RCVBUFERRORS,
  1648. is_udplite);
  1649. UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  1650. kfree_skb(skb);
  1651. trace_udp_fail_queue_rcv_skb(rc, sk);
  1652. return -1;
  1653. }
  1654. return 0;
  1655. }
  1656. DEFINE_STATIC_KEY_FALSE(udp_encap_needed_key);
  1657. void udp_encap_enable(void)
  1658. {
  1659. static_branch_enable(&udp_encap_needed_key);
  1660. }
  1661. EXPORT_SYMBOL(udp_encap_enable);
  1662. /* returns:
  1663. * -1: error
  1664. * 0: success
  1665. * >0: "udp encap" protocol resubmission
  1666. *
  1667. * Note that in the success and error cases, the skb is assumed to
  1668. * have either been requeued or freed.
  1669. */
  1670. static int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  1671. {
  1672. struct udp_sock *up = udp_sk(sk);
  1673. int is_udplite = IS_UDPLITE(sk);
  1674. /*
  1675. * Charge it to the socket, dropping if the queue is full.
  1676. */
  1677. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
  1678. goto drop;
  1679. nf_reset(skb);
  1680. if (static_branch_unlikely(&udp_encap_needed_key) && up->encap_type) {
  1681. int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
  1682. /*
  1683. * This is an encapsulation socket so pass the skb to
  1684. * the socket's udp_encap_rcv() hook. Otherwise, just
  1685. * fall through and pass this up the UDP socket.
  1686. * up->encap_rcv() returns the following value:
  1687. * =0 if skb was successfully passed to the encap
  1688. * handler or was discarded by it.
  1689. * >0 if skb should be passed on to UDP.
  1690. * <0 if skb should be resubmitted as proto -N
  1691. */
  1692. /* if we're overly short, let UDP handle it */
  1693. encap_rcv = READ_ONCE(up->encap_rcv);
  1694. if (encap_rcv) {
  1695. int ret;
  1696. /* Verify checksum before giving to encap */
  1697. if (udp_lib_checksum_complete(skb))
  1698. goto csum_error;
  1699. ret = encap_rcv(sk, skb);
  1700. if (ret <= 0) {
  1701. __UDP_INC_STATS(sock_net(sk),
  1702. UDP_MIB_INDATAGRAMS,
  1703. is_udplite);
  1704. return -ret;
  1705. }
  1706. }
  1707. /* FALLTHROUGH -- it's a UDP Packet */
  1708. }
  1709. /*
  1710. * UDP-Lite specific tests, ignored on UDP sockets
  1711. */
  1712. if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
  1713. /*
  1714. * MIB statistics other than incrementing the error count are
  1715. * disabled for the following two types of errors: these depend
  1716. * on the application settings, not on the functioning of the
  1717. * protocol stack as such.
  1718. *
  1719. * RFC 3828 here recommends (sec 3.3): "There should also be a
  1720. * way ... to ... at least let the receiving application block
  1721. * delivery of packets with coverage values less than a value
  1722. * provided by the application."
  1723. */
  1724. if (up->pcrlen == 0) { /* full coverage was set */
  1725. net_dbg_ratelimited("UDPLite: partial coverage %d while full coverage %d requested\n",
  1726. UDP_SKB_CB(skb)->cscov, skb->len);
  1727. goto drop;
  1728. }
  1729. /* The next case involves violating the min. coverage requested
  1730. * by the receiver. This is subtle: if receiver wants x and x is
  1731. * greater than the buffersize/MTU then receiver will complain
  1732. * that it wants x while sender emits packets of smaller size y.
  1733. * Therefore the above ...()->partial_cov statement is essential.
  1734. */
  1735. if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
  1736. net_dbg_ratelimited("UDPLite: coverage %d too small, need min %d\n",
  1737. UDP_SKB_CB(skb)->cscov, up->pcrlen);
  1738. goto drop;
  1739. }
  1740. }
  1741. prefetch(&sk->sk_rmem_alloc);
  1742. if (rcu_access_pointer(sk->sk_filter) &&
  1743. udp_lib_checksum_complete(skb))
  1744. goto csum_error;
  1745. if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr)))
  1746. goto drop;
  1747. udp_csum_pull_header(skb);
  1748. ipv4_pktinfo_prepare(sk, skb);
  1749. return __udp_queue_rcv_skb(sk, skb);
  1750. csum_error:
  1751. __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
  1752. drop:
  1753. __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  1754. atomic_inc(&sk->sk_drops);
  1755. kfree_skb(skb);
  1756. return -1;
  1757. }
  1758. /* For TCP sockets, sk_rx_dst is protected by socket lock
  1759. * For UDP, we use xchg() to guard against concurrent changes.
  1760. */
  1761. bool udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst)
  1762. {
  1763. struct dst_entry *old;
  1764. if (dst_hold_safe(dst)) {
  1765. old = xchg(&sk->sk_rx_dst, dst);
  1766. dst_release(old);
  1767. return old != dst;
  1768. }
  1769. return false;
  1770. }
  1771. EXPORT_SYMBOL(udp_sk_rx_dst_set);
  1772. /*
  1773. * Multicasts and broadcasts go to each listener.
  1774. *
  1775. * Note: called only from the BH handler context.
  1776. */
  1777. static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
  1778. struct udphdr *uh,
  1779. __be32 saddr, __be32 daddr,
  1780. struct udp_table *udptable,
  1781. int proto)
  1782. {
  1783. struct sock *sk, *first = NULL;
  1784. unsigned short hnum = ntohs(uh->dest);
  1785. struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
  1786. unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
  1787. unsigned int offset = offsetof(typeof(*sk), sk_node);
  1788. int dif = skb->dev->ifindex;
  1789. int sdif = inet_sdif(skb);
  1790. struct hlist_node *node;
  1791. struct sk_buff *nskb;
  1792. if (use_hash2) {
  1793. hash2_any = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum) &
  1794. udptable->mask;
  1795. hash2 = ipv4_portaddr_hash(net, daddr, hnum) & udptable->mask;
  1796. start_lookup:
  1797. hslot = &udptable->hash2[hash2];
  1798. offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
  1799. }
  1800. sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) {
  1801. if (!__udp_is_mcast_sock(net, sk, uh->dest, daddr,
  1802. uh->source, saddr, dif, sdif, hnum))
  1803. continue;
  1804. if (!first) {
  1805. first = sk;
  1806. continue;
  1807. }
  1808. nskb = skb_clone(skb, GFP_ATOMIC);
  1809. if (unlikely(!nskb)) {
  1810. atomic_inc(&sk->sk_drops);
  1811. __UDP_INC_STATS(net, UDP_MIB_RCVBUFERRORS,
  1812. IS_UDPLITE(sk));
  1813. __UDP_INC_STATS(net, UDP_MIB_INERRORS,
  1814. IS_UDPLITE(sk));
  1815. continue;
  1816. }
  1817. if (udp_queue_rcv_skb(sk, nskb) > 0)
  1818. consume_skb(nskb);
  1819. }
  1820. /* Also lookup *:port if we are using hash2 and haven't done so yet. */
  1821. if (use_hash2 && hash2 != hash2_any) {
  1822. hash2 = hash2_any;
  1823. goto start_lookup;
  1824. }
  1825. if (first) {
  1826. if (udp_queue_rcv_skb(first, skb) > 0)
  1827. consume_skb(skb);
  1828. } else {
  1829. kfree_skb(skb);
  1830. __UDP_INC_STATS(net, UDP_MIB_IGNOREDMULTI,
  1831. proto == IPPROTO_UDPLITE);
  1832. }
  1833. return 0;
  1834. }
  1835. /* Initialize UDP checksum. If exited with zero value (success),
  1836. * CHECKSUM_UNNECESSARY means, that no more checks are required.
  1837. * Otherwise, csum completion requires chacksumming packet body,
  1838. * including udp header and folding it to skb->csum.
  1839. */
  1840. static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh,
  1841. int proto)
  1842. {
  1843. int err;
  1844. UDP_SKB_CB(skb)->partial_cov = 0;
  1845. UDP_SKB_CB(skb)->cscov = skb->len;
  1846. if (proto == IPPROTO_UDPLITE) {
  1847. err = udplite_checksum_init(skb, uh);
  1848. if (err)
  1849. return err;
  1850. if (UDP_SKB_CB(skb)->partial_cov) {
  1851. skb->csum = inet_compute_pseudo(skb, proto);
  1852. return 0;
  1853. }
  1854. }
  1855. /* Note, we are only interested in != 0 or == 0, thus the
  1856. * force to int.
  1857. */
  1858. err = (__force int)skb_checksum_init_zero_check(skb, proto, uh->check,
  1859. inet_compute_pseudo);
  1860. if (err)
  1861. return err;
  1862. if (skb->ip_summed == CHECKSUM_COMPLETE && !skb->csum_valid) {
  1863. /* If SW calculated the value, we know it's bad */
  1864. if (skb->csum_complete_sw)
  1865. return 1;
  1866. /* HW says the value is bad. Let's validate that.
  1867. * skb->csum is no longer the full packet checksum,
  1868. * so don't treat it as such.
  1869. */
  1870. skb_checksum_complete_unset(skb);
  1871. }
  1872. return 0;
  1873. }
  1874. /* wrapper for udp_queue_rcv_skb tacking care of csum conversion and
  1875. * return code conversion for ip layer consumption
  1876. */
  1877. static int udp_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb,
  1878. struct udphdr *uh)
  1879. {
  1880. int ret;
  1881. if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
  1882. skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
  1883. inet_compute_pseudo);
  1884. ret = udp_queue_rcv_skb(sk, skb);
  1885. /* a return value > 0 means to resubmit the input, but
  1886. * it wants the return to be -protocol, or 0
  1887. */
  1888. if (ret > 0)
  1889. return -ret;
  1890. return 0;
  1891. }
  1892. /*
  1893. * All we need to do is get the socket, and then do a checksum.
  1894. */
  1895. int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
  1896. int proto)
  1897. {
  1898. struct sock *sk;
  1899. struct udphdr *uh;
  1900. unsigned short ulen;
  1901. struct rtable *rt = skb_rtable(skb);
  1902. __be32 saddr, daddr;
  1903. struct net *net = dev_net(skb->dev);
  1904. /*
  1905. * Validate the packet.
  1906. */
  1907. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  1908. goto drop; /* No space for header. */
  1909. uh = udp_hdr(skb);
  1910. ulen = ntohs(uh->len);
  1911. saddr = ip_hdr(skb)->saddr;
  1912. daddr = ip_hdr(skb)->daddr;
  1913. if (ulen > skb->len)
  1914. goto short_packet;
  1915. if (proto == IPPROTO_UDP) {
  1916. /* UDP validates ulen. */
  1917. if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen))
  1918. goto short_packet;
  1919. uh = udp_hdr(skb);
  1920. }
  1921. if (udp4_csum_init(skb, uh, proto))
  1922. goto csum_error;
  1923. sk = skb_steal_sock(skb);
  1924. if (sk) {
  1925. struct dst_entry *dst = skb_dst(skb);
  1926. int ret;
  1927. if (unlikely(sk->sk_rx_dst != dst))
  1928. udp_sk_rx_dst_set(sk, dst);
  1929. ret = udp_unicast_rcv_skb(sk, skb, uh);
  1930. sock_put(sk);
  1931. return ret;
  1932. }
  1933. if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
  1934. return __udp4_lib_mcast_deliver(net, skb, uh,
  1935. saddr, daddr, udptable, proto);
  1936. sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
  1937. if (sk)
  1938. return udp_unicast_rcv_skb(sk, skb, uh);
  1939. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  1940. goto drop;
  1941. nf_reset(skb);
  1942. /* No socket. Drop packet silently, if checksum is wrong */
  1943. if (udp_lib_checksum_complete(skb))
  1944. goto csum_error;
  1945. __UDP_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
  1946. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  1947. /*
  1948. * Hmm. We got an UDP packet to a port to which we
  1949. * don't wanna listen. Ignore it.
  1950. */
  1951. kfree_skb(skb);
  1952. return 0;
  1953. short_packet:
  1954. net_dbg_ratelimited("UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n",
  1955. proto == IPPROTO_UDPLITE ? "Lite" : "",
  1956. &saddr, ntohs(uh->source),
  1957. ulen, skb->len,
  1958. &daddr, ntohs(uh->dest));
  1959. goto drop;
  1960. csum_error:
  1961. /*
  1962. * RFC1122: OK. Discards the bad packet silently (as far as
  1963. * the network is concerned, anyway) as per 4.1.3.4 (MUST).
  1964. */
  1965. net_dbg_ratelimited("UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n",
  1966. proto == IPPROTO_UDPLITE ? "Lite" : "",
  1967. &saddr, ntohs(uh->source), &daddr, ntohs(uh->dest),
  1968. ulen);
  1969. __UDP_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
  1970. drop:
  1971. __UDP_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
  1972. kfree_skb(skb);
  1973. return 0;
  1974. }
  1975. /* We can only early demux multicast if there is a single matching socket.
  1976. * If more than one socket found returns NULL
  1977. */
  1978. static struct sock *__udp4_lib_mcast_demux_lookup(struct net *net,
  1979. __be16 loc_port, __be32 loc_addr,
  1980. __be16 rmt_port, __be32 rmt_addr,
  1981. int dif, int sdif)
  1982. {
  1983. struct sock *sk, *result;
  1984. unsigned short hnum = ntohs(loc_port);
  1985. unsigned int slot = udp_hashfn(net, hnum, udp_table.mask);
  1986. struct udp_hslot *hslot = &udp_table.hash[slot];
  1987. /* Do not bother scanning a too big list */
  1988. if (hslot->count > 10)
  1989. return NULL;
  1990. result = NULL;
  1991. sk_for_each_rcu(sk, &hslot->head) {
  1992. if (__udp_is_mcast_sock(net, sk, loc_port, loc_addr,
  1993. rmt_port, rmt_addr, dif, sdif, hnum)) {
  1994. if (result)
  1995. return NULL;
  1996. result = sk;
  1997. }
  1998. }
  1999. return result;
  2000. }
  2001. /* For unicast we should only early demux connected sockets or we can
  2002. * break forwarding setups. The chains here can be long so only check
  2003. * if the first socket is an exact match and if not move on.
  2004. */
  2005. static struct sock *__udp4_lib_demux_lookup(struct net *net,
  2006. __be16 loc_port, __be32 loc_addr,
  2007. __be16 rmt_port, __be32 rmt_addr,
  2008. int dif, int sdif)
  2009. {
  2010. unsigned short hnum = ntohs(loc_port);
  2011. unsigned int hash2 = ipv4_portaddr_hash(net, loc_addr, hnum);
  2012. unsigned int slot2 = hash2 & udp_table.mask;
  2013. struct udp_hslot *hslot2 = &udp_table.hash2[slot2];
  2014. INET_ADDR_COOKIE(acookie, rmt_addr, loc_addr);
  2015. const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum);
  2016. struct sock *sk;
  2017. udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
  2018. if (INET_MATCH(sk, net, acookie, rmt_addr,
  2019. loc_addr, ports, dif, sdif))
  2020. return sk;
  2021. /* Only check first socket in chain */
  2022. break;
  2023. }
  2024. return NULL;
  2025. }
  2026. int udp_v4_early_demux(struct sk_buff *skb)
  2027. {
  2028. struct net *net = dev_net(skb->dev);
  2029. struct in_device *in_dev = NULL;
  2030. const struct iphdr *iph;
  2031. const struct udphdr *uh;
  2032. struct sock *sk = NULL;
  2033. struct dst_entry *dst;
  2034. int dif = skb->dev->ifindex;
  2035. int sdif = inet_sdif(skb);
  2036. int ours;
  2037. /* validate the packet */
  2038. if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct udphdr)))
  2039. return 0;
  2040. iph = ip_hdr(skb);
  2041. uh = udp_hdr(skb);
  2042. if (skb->pkt_type == PACKET_MULTICAST) {
  2043. in_dev = __in_dev_get_rcu(skb->dev);
  2044. if (!in_dev)
  2045. return 0;
  2046. ours = ip_check_mc_rcu(in_dev, iph->daddr, iph->saddr,
  2047. iph->protocol);
  2048. if (!ours)
  2049. return 0;
  2050. sk = __udp4_lib_mcast_demux_lookup(net, uh->dest, iph->daddr,
  2051. uh->source, iph->saddr,
  2052. dif, sdif);
  2053. } else if (skb->pkt_type == PACKET_HOST) {
  2054. sk = __udp4_lib_demux_lookup(net, uh->dest, iph->daddr,
  2055. uh->source, iph->saddr, dif, sdif);
  2056. }
  2057. if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
  2058. return 0;
  2059. skb->sk = sk;
  2060. skb->destructor = sock_efree;
  2061. dst = READ_ONCE(sk->sk_rx_dst);
  2062. if (dst)
  2063. dst = dst_check(dst, 0);
  2064. if (dst) {
  2065. u32 itag = 0;
  2066. /* set noref for now.
  2067. * any place which wants to hold dst has to call
  2068. * dst_hold_safe()
  2069. */
  2070. skb_dst_set_noref(skb, dst);
  2071. /* for unconnected multicast sockets we need to validate
  2072. * the source on each packet
  2073. */
  2074. if (!inet_sk(sk)->inet_daddr && in_dev)
  2075. return ip_mc_validate_source(skb, iph->daddr,
  2076. iph->saddr, iph->tos,
  2077. skb->dev, in_dev, &itag);
  2078. }
  2079. return 0;
  2080. }
  2081. int udp_rcv(struct sk_buff *skb)
  2082. {
  2083. return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP);
  2084. }
  2085. void udp_destroy_sock(struct sock *sk)
  2086. {
  2087. struct udp_sock *up = udp_sk(sk);
  2088. bool slow = lock_sock_fast(sk);
  2089. udp_flush_pending_frames(sk);
  2090. unlock_sock_fast(sk, slow);
  2091. if (static_branch_unlikely(&udp_encap_needed_key) && up->encap_type) {
  2092. void (*encap_destroy)(struct sock *sk);
  2093. encap_destroy = READ_ONCE(up->encap_destroy);
  2094. if (encap_destroy)
  2095. encap_destroy(sk);
  2096. }
  2097. }
  2098. /*
  2099. * Socket option code for UDP
  2100. */
  2101. int udp_lib_setsockopt(struct sock *sk, int level, int optname,
  2102. char __user *optval, unsigned int optlen,
  2103. int (*push_pending_frames)(struct sock *))
  2104. {
  2105. struct udp_sock *up = udp_sk(sk);
  2106. int val, valbool;
  2107. int err = 0;
  2108. int is_udplite = IS_UDPLITE(sk);
  2109. if (optlen < sizeof(int))
  2110. return -EINVAL;
  2111. if (get_user(val, (int __user *)optval))
  2112. return -EFAULT;
  2113. valbool = val ? 1 : 0;
  2114. switch (optname) {
  2115. case UDP_CORK:
  2116. if (val != 0) {
  2117. up->corkflag = 1;
  2118. } else {
  2119. up->corkflag = 0;
  2120. lock_sock(sk);
  2121. push_pending_frames(sk);
  2122. release_sock(sk);
  2123. }
  2124. break;
  2125. case UDP_ENCAP:
  2126. switch (val) {
  2127. case 0:
  2128. case UDP_ENCAP_ESPINUDP:
  2129. case UDP_ENCAP_ESPINUDP_NON_IKE:
  2130. up->encap_rcv = xfrm4_udp_encap_rcv;
  2131. /* FALLTHROUGH */
  2132. case UDP_ENCAP_L2TPINUDP:
  2133. up->encap_type = val;
  2134. udp_encap_enable();
  2135. break;
  2136. default:
  2137. err = -ENOPROTOOPT;
  2138. break;
  2139. }
  2140. break;
  2141. case UDP_NO_CHECK6_TX:
  2142. up->no_check6_tx = valbool;
  2143. break;
  2144. case UDP_NO_CHECK6_RX:
  2145. up->no_check6_rx = valbool;
  2146. break;
  2147. case UDP_SEGMENT:
  2148. if (val < 0 || val > USHRT_MAX)
  2149. return -EINVAL;
  2150. up->gso_size = val;
  2151. break;
  2152. /*
  2153. * UDP-Lite's partial checksum coverage (RFC 3828).
  2154. */
  2155. /* The sender sets actual checksum coverage length via this option.
  2156. * The case coverage > packet length is handled by send module. */
  2157. case UDPLITE_SEND_CSCOV:
  2158. if (!is_udplite) /* Disable the option on UDP sockets */
  2159. return -ENOPROTOOPT;
  2160. if (val != 0 && val < 8) /* Illegal coverage: use default (8) */
  2161. val = 8;
  2162. else if (val > USHRT_MAX)
  2163. val = USHRT_MAX;
  2164. up->pcslen = val;
  2165. up->pcflag |= UDPLITE_SEND_CC;
  2166. break;
  2167. /* The receiver specifies a minimum checksum coverage value. To make
  2168. * sense, this should be set to at least 8 (as done below). If zero is
  2169. * used, this again means full checksum coverage. */
  2170. case UDPLITE_RECV_CSCOV:
  2171. if (!is_udplite) /* Disable the option on UDP sockets */
  2172. return -ENOPROTOOPT;
  2173. if (val != 0 && val < 8) /* Avoid silly minimal values. */
  2174. val = 8;
  2175. else if (val > USHRT_MAX)
  2176. val = USHRT_MAX;
  2177. up->pcrlen = val;
  2178. up->pcflag |= UDPLITE_RECV_CC;
  2179. break;
  2180. default:
  2181. err = -ENOPROTOOPT;
  2182. break;
  2183. }
  2184. return err;
  2185. }
  2186. EXPORT_SYMBOL(udp_lib_setsockopt);
  2187. int udp_setsockopt(struct sock *sk, int level, int optname,
  2188. char __user *optval, unsigned int optlen)
  2189. {
  2190. if (level == SOL_UDP || level == SOL_UDPLITE)
  2191. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  2192. udp_push_pending_frames);
  2193. return ip_setsockopt(sk, level, optname, optval, optlen);
  2194. }
  2195. #ifdef CONFIG_COMPAT
  2196. int compat_udp_setsockopt(struct sock *sk, int level, int optname,
  2197. char __user *optval, unsigned int optlen)
  2198. {
  2199. if (level == SOL_UDP || level == SOL_UDPLITE)
  2200. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  2201. udp_push_pending_frames);
  2202. return compat_ip_setsockopt(sk, level, optname, optval, optlen);
  2203. }
  2204. #endif
  2205. int udp_lib_getsockopt(struct sock *sk, int level, int optname,
  2206. char __user *optval, int __user *optlen)
  2207. {
  2208. struct udp_sock *up = udp_sk(sk);
  2209. int val, len;
  2210. if (get_user(len, optlen))
  2211. return -EFAULT;
  2212. len = min_t(unsigned int, len, sizeof(int));
  2213. if (len < 0)
  2214. return -EINVAL;
  2215. switch (optname) {
  2216. case UDP_CORK:
  2217. val = up->corkflag;
  2218. break;
  2219. case UDP_ENCAP:
  2220. val = up->encap_type;
  2221. break;
  2222. case UDP_NO_CHECK6_TX:
  2223. val = up->no_check6_tx;
  2224. break;
  2225. case UDP_NO_CHECK6_RX:
  2226. val = up->no_check6_rx;
  2227. break;
  2228. case UDP_SEGMENT:
  2229. val = up->gso_size;
  2230. break;
  2231. /* The following two cannot be changed on UDP sockets, the return is
  2232. * always 0 (which corresponds to the full checksum coverage of UDP). */
  2233. case UDPLITE_SEND_CSCOV:
  2234. val = up->pcslen;
  2235. break;
  2236. case UDPLITE_RECV_CSCOV:
  2237. val = up->pcrlen;
  2238. break;
  2239. default:
  2240. return -ENOPROTOOPT;
  2241. }
  2242. if (put_user(len, optlen))
  2243. return -EFAULT;
  2244. if (copy_to_user(optval, &val, len))
  2245. return -EFAULT;
  2246. return 0;
  2247. }
  2248. EXPORT_SYMBOL(udp_lib_getsockopt);
  2249. int udp_getsockopt(struct sock *sk, int level, int optname,
  2250. char __user *optval, int __user *optlen)
  2251. {
  2252. if (level == SOL_UDP || level == SOL_UDPLITE)
  2253. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  2254. return ip_getsockopt(sk, level, optname, optval, optlen);
  2255. }
  2256. #ifdef CONFIG_COMPAT
  2257. int compat_udp_getsockopt(struct sock *sk, int level, int optname,
  2258. char __user *optval, int __user *optlen)
  2259. {
  2260. if (level == SOL_UDP || level == SOL_UDPLITE)
  2261. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  2262. return compat_ip_getsockopt(sk, level, optname, optval, optlen);
  2263. }
  2264. #endif
  2265. /**
  2266. * udp_poll - wait for a UDP event.
  2267. * @file - file struct
  2268. * @sock - socket
  2269. * @wait - poll table
  2270. *
  2271. * This is same as datagram poll, except for the special case of
  2272. * blocking sockets. If application is using a blocking fd
  2273. * and a packet with checksum error is in the queue;
  2274. * then it could get return from select indicating data available
  2275. * but then block when reading it. Add special case code
  2276. * to work around these arguably broken applications.
  2277. */
  2278. __poll_t udp_poll(struct file *file, struct socket *sock, poll_table *wait)
  2279. {
  2280. __poll_t mask = datagram_poll(file, sock, wait);
  2281. struct sock *sk = sock->sk;
  2282. if (!skb_queue_empty(&udp_sk(sk)->reader_queue))
  2283. mask |= EPOLLIN | EPOLLRDNORM;
  2284. /* Check for false positives due to checksum errors */
  2285. if ((mask & EPOLLRDNORM) && !(file->f_flags & O_NONBLOCK) &&
  2286. !(sk->sk_shutdown & RCV_SHUTDOWN) && first_packet_length(sk) == -1)
  2287. mask &= ~(EPOLLIN | EPOLLRDNORM);
  2288. return mask;
  2289. }
  2290. EXPORT_SYMBOL(udp_poll);
  2291. int udp_abort(struct sock *sk, int err)
  2292. {
  2293. lock_sock(sk);
  2294. sk->sk_err = err;
  2295. sk->sk_error_report(sk);
  2296. __udp_disconnect(sk, 0);
  2297. release_sock(sk);
  2298. return 0;
  2299. }
  2300. EXPORT_SYMBOL_GPL(udp_abort);
  2301. struct proto udp_prot = {
  2302. .name = "UDP",
  2303. .owner = THIS_MODULE,
  2304. .close = udp_lib_close,
  2305. .pre_connect = udp_pre_connect,
  2306. .connect = ip4_datagram_connect,
  2307. .disconnect = udp_disconnect,
  2308. .ioctl = udp_ioctl,
  2309. .init = udp_init_sock,
  2310. .destroy = udp_destroy_sock,
  2311. .setsockopt = udp_setsockopt,
  2312. .getsockopt = udp_getsockopt,
  2313. .sendmsg = udp_sendmsg,
  2314. .recvmsg = udp_recvmsg,
  2315. .sendpage = udp_sendpage,
  2316. .release_cb = ip4_datagram_release_cb,
  2317. .hash = udp_lib_hash,
  2318. .unhash = udp_lib_unhash,
  2319. .rehash = udp_v4_rehash,
  2320. .get_port = udp_v4_get_port,
  2321. .memory_allocated = &udp_memory_allocated,
  2322. .sysctl_mem = sysctl_udp_mem,
  2323. .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_udp_wmem_min),
  2324. .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min),
  2325. .obj_size = sizeof(struct udp_sock),
  2326. .h.udp_table = &udp_table,
  2327. #ifdef CONFIG_COMPAT
  2328. .compat_setsockopt = compat_udp_setsockopt,
  2329. .compat_getsockopt = compat_udp_getsockopt,
  2330. #endif
  2331. .diag_destroy = udp_abort,
  2332. };
  2333. EXPORT_SYMBOL(udp_prot);
  2334. /* ------------------------------------------------------------------------ */
  2335. #ifdef CONFIG_PROC_FS
  2336. static struct sock *udp_get_first(struct seq_file *seq, int start)
  2337. {
  2338. struct sock *sk;
  2339. struct udp_seq_afinfo *afinfo = PDE_DATA(file_inode(seq->file));
  2340. struct udp_iter_state *state = seq->private;
  2341. struct net *net = seq_file_net(seq);
  2342. for (state->bucket = start; state->bucket <= afinfo->udp_table->mask;
  2343. ++state->bucket) {
  2344. struct udp_hslot *hslot = &afinfo->udp_table->hash[state->bucket];
  2345. if (hlist_empty(&hslot->head))
  2346. continue;
  2347. spin_lock_bh(&hslot->lock);
  2348. sk_for_each(sk, &hslot->head) {
  2349. if (!net_eq(sock_net(sk), net))
  2350. continue;
  2351. if (sk->sk_family == afinfo->family)
  2352. goto found;
  2353. }
  2354. spin_unlock_bh(&hslot->lock);
  2355. }
  2356. sk = NULL;
  2357. found:
  2358. return sk;
  2359. }
  2360. static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
  2361. {
  2362. struct udp_seq_afinfo *afinfo = PDE_DATA(file_inode(seq->file));
  2363. struct udp_iter_state *state = seq->private;
  2364. struct net *net = seq_file_net(seq);
  2365. do {
  2366. sk = sk_next(sk);
  2367. } while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != afinfo->family));
  2368. if (!sk) {
  2369. if (state->bucket <= afinfo->udp_table->mask)
  2370. spin_unlock_bh(&afinfo->udp_table->hash[state->bucket].lock);
  2371. return udp_get_first(seq, state->bucket + 1);
  2372. }
  2373. return sk;
  2374. }
  2375. static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
  2376. {
  2377. struct sock *sk = udp_get_first(seq, 0);
  2378. if (sk)
  2379. while (pos && (sk = udp_get_next(seq, sk)) != NULL)
  2380. --pos;
  2381. return pos ? NULL : sk;
  2382. }
  2383. void *udp_seq_start(struct seq_file *seq, loff_t *pos)
  2384. {
  2385. struct udp_iter_state *state = seq->private;
  2386. state->bucket = MAX_UDP_PORTS;
  2387. return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
  2388. }
  2389. EXPORT_SYMBOL(udp_seq_start);
  2390. void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2391. {
  2392. struct sock *sk;
  2393. if (v == SEQ_START_TOKEN)
  2394. sk = udp_get_idx(seq, 0);
  2395. else
  2396. sk = udp_get_next(seq, v);
  2397. ++*pos;
  2398. return sk;
  2399. }
  2400. EXPORT_SYMBOL(udp_seq_next);
  2401. void udp_seq_stop(struct seq_file *seq, void *v)
  2402. {
  2403. struct udp_seq_afinfo *afinfo = PDE_DATA(file_inode(seq->file));
  2404. struct udp_iter_state *state = seq->private;
  2405. if (state->bucket <= afinfo->udp_table->mask)
  2406. spin_unlock_bh(&afinfo->udp_table->hash[state->bucket].lock);
  2407. }
  2408. EXPORT_SYMBOL(udp_seq_stop);
  2409. /* ------------------------------------------------------------------------ */
  2410. static void udp4_format_sock(struct sock *sp, struct seq_file *f,
  2411. int bucket)
  2412. {
  2413. struct inet_sock *inet = inet_sk(sp);
  2414. __be32 dest = inet->inet_daddr;
  2415. __be32 src = inet->inet_rcv_saddr;
  2416. __u16 destp = ntohs(inet->inet_dport);
  2417. __u16 srcp = ntohs(inet->inet_sport);
  2418. seq_printf(f, "%5d: %08X:%04X %08X:%04X"
  2419. " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d",
  2420. bucket, src, srcp, dest, destp, sp->sk_state,
  2421. sk_wmem_alloc_get(sp),
  2422. udp_rqueue_get(sp),
  2423. 0, 0L, 0,
  2424. from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
  2425. 0, sock_i_ino(sp),
  2426. refcount_read(&sp->sk_refcnt), sp,
  2427. atomic_read(&sp->sk_drops));
  2428. }
  2429. int udp4_seq_show(struct seq_file *seq, void *v)
  2430. {
  2431. seq_setwidth(seq, 127);
  2432. if (v == SEQ_START_TOKEN)
  2433. seq_puts(seq, " sl local_address rem_address st tx_queue "
  2434. "rx_queue tr tm->when retrnsmt uid timeout "
  2435. "inode ref pointer drops");
  2436. else {
  2437. struct udp_iter_state *state = seq->private;
  2438. udp4_format_sock(v, seq, state->bucket);
  2439. }
  2440. seq_pad(seq, '\n');
  2441. return 0;
  2442. }
  2443. const struct seq_operations udp_seq_ops = {
  2444. .start = udp_seq_start,
  2445. .next = udp_seq_next,
  2446. .stop = udp_seq_stop,
  2447. .show = udp4_seq_show,
  2448. };
  2449. EXPORT_SYMBOL(udp_seq_ops);
  2450. static struct udp_seq_afinfo udp4_seq_afinfo = {
  2451. .family = AF_INET,
  2452. .udp_table = &udp_table,
  2453. };
  2454. static int __net_init udp4_proc_init_net(struct net *net)
  2455. {
  2456. if (!proc_create_net_data("udp", 0444, net->proc_net, &udp_seq_ops,
  2457. sizeof(struct udp_iter_state), &udp4_seq_afinfo))
  2458. return -ENOMEM;
  2459. return 0;
  2460. }
  2461. static void __net_exit udp4_proc_exit_net(struct net *net)
  2462. {
  2463. remove_proc_entry("udp", net->proc_net);
  2464. }
  2465. static struct pernet_operations udp4_net_ops = {
  2466. .init = udp4_proc_init_net,
  2467. .exit = udp4_proc_exit_net,
  2468. };
  2469. int __init udp4_proc_init(void)
  2470. {
  2471. return register_pernet_subsys(&udp4_net_ops);
  2472. }
  2473. void udp4_proc_exit(void)
  2474. {
  2475. unregister_pernet_subsys(&udp4_net_ops);
  2476. }
  2477. #endif /* CONFIG_PROC_FS */
  2478. static __initdata unsigned long uhash_entries;
  2479. static int __init set_uhash_entries(char *str)
  2480. {
  2481. ssize_t ret;
  2482. if (!str)
  2483. return 0;
  2484. ret = kstrtoul(str, 0, &uhash_entries);
  2485. if (ret)
  2486. return 0;
  2487. if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN)
  2488. uhash_entries = UDP_HTABLE_SIZE_MIN;
  2489. return 1;
  2490. }
  2491. __setup("uhash_entries=", set_uhash_entries);
  2492. void __init udp_table_init(struct udp_table *table, const char *name)
  2493. {
  2494. unsigned int i;
  2495. table->hash = alloc_large_system_hash(name,
  2496. 2 * sizeof(struct udp_hslot),
  2497. uhash_entries,
  2498. 21, /* one slot per 2 MB */
  2499. 0,
  2500. &table->log,
  2501. &table->mask,
  2502. UDP_HTABLE_SIZE_MIN,
  2503. 64 * 1024);
  2504. table->hash2 = table->hash + (table->mask + 1);
  2505. for (i = 0; i <= table->mask; i++) {
  2506. INIT_HLIST_HEAD(&table->hash[i].head);
  2507. table->hash[i].count = 0;
  2508. spin_lock_init(&table->hash[i].lock);
  2509. }
  2510. for (i = 0; i <= table->mask; i++) {
  2511. INIT_HLIST_HEAD(&table->hash2[i].head);
  2512. table->hash2[i].count = 0;
  2513. spin_lock_init(&table->hash2[i].lock);
  2514. }
  2515. }
  2516. u32 udp_flow_hashrnd(void)
  2517. {
  2518. static u32 hashrnd __read_mostly;
  2519. net_get_random_once(&hashrnd, sizeof(hashrnd));
  2520. return hashrnd;
  2521. }
  2522. EXPORT_SYMBOL(udp_flow_hashrnd);
  2523. static void __udp_sysctl_init(struct net *net)
  2524. {
  2525. net->ipv4.sysctl_udp_rmem_min = SK_MEM_QUANTUM;
  2526. net->ipv4.sysctl_udp_wmem_min = SK_MEM_QUANTUM;
  2527. #ifdef CONFIG_NET_L3_MASTER_DEV
  2528. net->ipv4.sysctl_udp_l3mdev_accept = 0;
  2529. #endif
  2530. }
  2531. static int __net_init udp_sysctl_init(struct net *net)
  2532. {
  2533. __udp_sysctl_init(net);
  2534. return 0;
  2535. }
  2536. static struct pernet_operations __net_initdata udp_sysctl_ops = {
  2537. .init = udp_sysctl_init,
  2538. };
  2539. void __init udp_init(void)
  2540. {
  2541. unsigned long limit;
  2542. unsigned int i;
  2543. udp_table_init(&udp_table, "UDP");
  2544. limit = nr_free_buffer_pages() / 8;
  2545. limit = max(limit, 128UL);
  2546. sysctl_udp_mem[0] = limit / 4 * 3;
  2547. sysctl_udp_mem[1] = limit;
  2548. sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2;
  2549. __udp_sysctl_init(&init_net);
  2550. /* 16 spinlocks per cpu */
  2551. udp_busylocks_log = ilog2(nr_cpu_ids) + 4;
  2552. udp_busylocks = kmalloc(sizeof(spinlock_t) << udp_busylocks_log,
  2553. GFP_KERNEL);
  2554. if (!udp_busylocks)
  2555. panic("UDP: failed to alloc udp_busylocks\n");
  2556. for (i = 0; i < (1U << udp_busylocks_log); i++)
  2557. spin_lock_init(udp_busylocks + i);
  2558. if (register_pernet_subsys(&udp_sysctl_ops))
  2559. panic("UDP: failed to init sysctl parameters.\n");
  2560. }