udp.c 76 KB

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