udp.c 76 KB

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