sock.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409
  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. * Generic socket support routines. Memory allocators, socket lock/release
  7. * handler for protocols to use and generic option handler.
  8. *
  9. *
  10. * Authors: Ross Biro
  11. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12. * Florian La Roche, <flla@stud.uni-sb.de>
  13. * Alan Cox, <A.Cox@swansea.ac.uk>
  14. *
  15. * Fixes:
  16. * Alan Cox : Numerous verify_area() problems
  17. * Alan Cox : Connecting on a connecting socket
  18. * now returns an error for tcp.
  19. * Alan Cox : sock->protocol is set correctly.
  20. * and is not sometimes left as 0.
  21. * Alan Cox : connect handles icmp errors on a
  22. * connect properly. Unfortunately there
  23. * is a restart syscall nasty there. I
  24. * can't match BSD without hacking the C
  25. * library. Ideas urgently sought!
  26. * Alan Cox : Disallow bind() to addresses that are
  27. * not ours - especially broadcast ones!!
  28. * Alan Cox : Socket 1024 _IS_ ok for users. (fencepost)
  29. * Alan Cox : sock_wfree/sock_rfree don't destroy sockets,
  30. * instead they leave that for the DESTROY timer.
  31. * Alan Cox : Clean up error flag in accept
  32. * Alan Cox : TCP ack handling is buggy, the DESTROY timer
  33. * was buggy. Put a remove_sock() in the handler
  34. * for memory when we hit 0. Also altered the timer
  35. * code. The ACK stuff can wait and needs major
  36. * TCP layer surgery.
  37. * Alan Cox : Fixed TCP ack bug, removed remove sock
  38. * and fixed timer/inet_bh race.
  39. * Alan Cox : Added zapped flag for TCP
  40. * Alan Cox : Move kfree_skb into skbuff.c and tidied up surplus code
  41. * Alan Cox : for new sk_buff allocations wmalloc/rmalloc now call alloc_skb
  42. * Alan Cox : kfree_s calls now are kfree_skbmem so we can track skb resources
  43. * Alan Cox : Supports socket option broadcast now as does udp. Packet and raw need fixing.
  44. * Alan Cox : Added RCVBUF,SNDBUF size setting. It suddenly occurred to me how easy it was so...
  45. * Rick Sladkey : Relaxed UDP rules for matching packets.
  46. * C.E.Hawkins : IFF_PROMISC/SIOCGHWADDR support
  47. * Pauline Middelink : identd support
  48. * Alan Cox : Fixed connect() taking signals I think.
  49. * Alan Cox : SO_LINGER supported
  50. * Alan Cox : Error reporting fixes
  51. * Anonymous : inet_create tidied up (sk->reuse setting)
  52. * Alan Cox : inet sockets don't set sk->type!
  53. * Alan Cox : Split socket option code
  54. * Alan Cox : Callbacks
  55. * Alan Cox : Nagle flag for Charles & Johannes stuff
  56. * Alex : Removed restriction on inet fioctl
  57. * Alan Cox : Splitting INET from NET core
  58. * Alan Cox : Fixed bogus SO_TYPE handling in getsockopt()
  59. * Adam Caldwell : Missing return in SO_DONTROUTE/SO_DEBUG code
  60. * Alan Cox : Split IP from generic code
  61. * Alan Cox : New kfree_skbmem()
  62. * Alan Cox : Make SO_DEBUG superuser only.
  63. * Alan Cox : Allow anyone to clear SO_DEBUG
  64. * (compatibility fix)
  65. * Alan Cox : Added optimistic memory grabbing for AF_UNIX throughput.
  66. * Alan Cox : Allocator for a socket is settable.
  67. * Alan Cox : SO_ERROR includes soft errors.
  68. * Alan Cox : Allow NULL arguments on some SO_ opts
  69. * Alan Cox : Generic socket allocation to make hooks
  70. * easier (suggested by Craig Metz).
  71. * Michael Pall : SO_ERROR returns positive errno again
  72. * Steve Whitehouse: Added default destructor to free
  73. * protocol private data.
  74. * Steve Whitehouse: Added various other default routines
  75. * common to several socket families.
  76. * Chris Evans : Call suser() check last on F_SETOWN
  77. * Jay Schulist : Added SO_ATTACH_FILTER and SO_DETACH_FILTER.
  78. * Andi Kleen : Add sock_kmalloc()/sock_kfree_s()
  79. * Andi Kleen : Fix write_space callback
  80. * Chris Evans : Security fixes - signedness again
  81. * Arnaldo C. Melo : cleanups, use skb_queue_purge
  82. *
  83. * To Fix:
  84. *
  85. *
  86. * This program is free software; you can redistribute it and/or
  87. * modify it under the terms of the GNU General Public License
  88. * as published by the Free Software Foundation; either version
  89. * 2 of the License, or (at your option) any later version.
  90. */
  91. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  92. #include <linux/capability.h>
  93. #include <linux/errno.h>
  94. #include <linux/errqueue.h>
  95. #include <linux/types.h>
  96. #include <linux/socket.h>
  97. #include <linux/in.h>
  98. #include <linux/kernel.h>
  99. #include <linux/module.h>
  100. #include <linux/proc_fs.h>
  101. #include <linux/seq_file.h>
  102. #include <linux/sched.h>
  103. #include <linux/sched/mm.h>
  104. #include <linux/timer.h>
  105. #include <linux/string.h>
  106. #include <linux/sockios.h>
  107. #include <linux/net.h>
  108. #include <linux/mm.h>
  109. #include <linux/slab.h>
  110. #include <linux/interrupt.h>
  111. #include <linux/poll.h>
  112. #include <linux/tcp.h>
  113. #include <linux/init.h>
  114. #include <linux/highmem.h>
  115. #include <linux/user_namespace.h>
  116. #include <linux/static_key.h>
  117. #include <linux/memcontrol.h>
  118. #include <linux/prefetch.h>
  119. #include <linux/uaccess.h>
  120. #include <linux/netdevice.h>
  121. #include <net/protocol.h>
  122. #include <linux/skbuff.h>
  123. #include <net/net_namespace.h>
  124. #include <net/request_sock.h>
  125. #include <net/sock.h>
  126. #include <linux/net_tstamp.h>
  127. #include <net/xfrm.h>
  128. #include <linux/ipsec.h>
  129. #include <net/cls_cgroup.h>
  130. #include <net/netprio_cgroup.h>
  131. #include <linux/sock_diag.h>
  132. #include <linux/filter.h>
  133. #include <net/sock_reuseport.h>
  134. #include <trace/events/sock.h>
  135. #include <net/tcp.h>
  136. #include <net/busy_poll.h>
  137. static DEFINE_MUTEX(proto_list_mutex);
  138. static LIST_HEAD(proto_list);
  139. static void sock_inuse_add(struct net *net, int val);
  140. /**
  141. * sk_ns_capable - General socket capability test
  142. * @sk: Socket to use a capability on or through
  143. * @user_ns: The user namespace of the capability to use
  144. * @cap: The capability to use
  145. *
  146. * Test to see if the opener of the socket had when the socket was
  147. * created and the current process has the capability @cap in the user
  148. * namespace @user_ns.
  149. */
  150. bool sk_ns_capable(const struct sock *sk,
  151. struct user_namespace *user_ns, int cap)
  152. {
  153. return file_ns_capable(sk->sk_socket->file, user_ns, cap) &&
  154. ns_capable(user_ns, cap);
  155. }
  156. EXPORT_SYMBOL(sk_ns_capable);
  157. /**
  158. * sk_capable - Socket global capability test
  159. * @sk: Socket to use a capability on or through
  160. * @cap: The global capability to use
  161. *
  162. * Test to see if the opener of the socket had when the socket was
  163. * created and the current process has the capability @cap in all user
  164. * namespaces.
  165. */
  166. bool sk_capable(const struct sock *sk, int cap)
  167. {
  168. return sk_ns_capable(sk, &init_user_ns, cap);
  169. }
  170. EXPORT_SYMBOL(sk_capable);
  171. /**
  172. * sk_net_capable - Network namespace socket capability test
  173. * @sk: Socket to use a capability on or through
  174. * @cap: The capability to use
  175. *
  176. * Test to see if the opener of the socket had when the socket was created
  177. * and the current process has the capability @cap over the network namespace
  178. * the socket is a member of.
  179. */
  180. bool sk_net_capable(const struct sock *sk, int cap)
  181. {
  182. return sk_ns_capable(sk, sock_net(sk)->user_ns, cap);
  183. }
  184. EXPORT_SYMBOL(sk_net_capable);
  185. /*
  186. * Each address family might have different locking rules, so we have
  187. * one slock key per address family and separate keys for internal and
  188. * userspace sockets.
  189. */
  190. static struct lock_class_key af_family_keys[AF_MAX];
  191. static struct lock_class_key af_family_kern_keys[AF_MAX];
  192. static struct lock_class_key af_family_slock_keys[AF_MAX];
  193. static struct lock_class_key af_family_kern_slock_keys[AF_MAX];
  194. /*
  195. * Make lock validator output more readable. (we pre-construct these
  196. * strings build-time, so that runtime initialization of socket
  197. * locks is fast):
  198. */
  199. #define _sock_locks(x) \
  200. x "AF_UNSPEC", x "AF_UNIX" , x "AF_INET" , \
  201. x "AF_AX25" , x "AF_IPX" , x "AF_APPLETALK", \
  202. x "AF_NETROM", x "AF_BRIDGE" , x "AF_ATMPVC" , \
  203. x "AF_X25" , x "AF_INET6" , x "AF_ROSE" , \
  204. x "AF_DECnet", x "AF_NETBEUI" , x "AF_SECURITY" , \
  205. x "AF_KEY" , x "AF_NETLINK" , x "AF_PACKET" , \
  206. x "AF_ASH" , x "AF_ECONET" , x "AF_ATMSVC" , \
  207. x "AF_RDS" , x "AF_SNA" , x "AF_IRDA" , \
  208. x "AF_PPPOX" , x "AF_WANPIPE" , x "AF_LLC" , \
  209. x "27" , x "28" , x "AF_CAN" , \
  210. x "AF_TIPC" , x "AF_BLUETOOTH", x "IUCV" , \
  211. x "AF_RXRPC" , x "AF_ISDN" , x "AF_PHONET" , \
  212. x "AF_IEEE802154", x "AF_CAIF" , x "AF_ALG" , \
  213. x "AF_NFC" , x "AF_VSOCK" , x "AF_KCM" , \
  214. x "AF_QIPCRTR", x "AF_SMC" , x "AF_MAX"
  215. static const char *const af_family_key_strings[AF_MAX+1] = {
  216. _sock_locks("sk_lock-")
  217. };
  218. static const char *const af_family_slock_key_strings[AF_MAX+1] = {
  219. _sock_locks("slock-")
  220. };
  221. static const char *const af_family_clock_key_strings[AF_MAX+1] = {
  222. _sock_locks("clock-")
  223. };
  224. static const char *const af_family_kern_key_strings[AF_MAX+1] = {
  225. _sock_locks("k-sk_lock-")
  226. };
  227. static const char *const af_family_kern_slock_key_strings[AF_MAX+1] = {
  228. _sock_locks("k-slock-")
  229. };
  230. static const char *const af_family_kern_clock_key_strings[AF_MAX+1] = {
  231. _sock_locks("k-clock-")
  232. };
  233. static const char *const af_family_rlock_key_strings[AF_MAX+1] = {
  234. "rlock-AF_UNSPEC", "rlock-AF_UNIX" , "rlock-AF_INET" ,
  235. "rlock-AF_AX25" , "rlock-AF_IPX" , "rlock-AF_APPLETALK",
  236. "rlock-AF_NETROM", "rlock-AF_BRIDGE" , "rlock-AF_ATMPVC" ,
  237. "rlock-AF_X25" , "rlock-AF_INET6" , "rlock-AF_ROSE" ,
  238. "rlock-AF_DECnet", "rlock-AF_NETBEUI" , "rlock-AF_SECURITY" ,
  239. "rlock-AF_KEY" , "rlock-AF_NETLINK" , "rlock-AF_PACKET" ,
  240. "rlock-AF_ASH" , "rlock-AF_ECONET" , "rlock-AF_ATMSVC" ,
  241. "rlock-AF_RDS" , "rlock-AF_SNA" , "rlock-AF_IRDA" ,
  242. "rlock-AF_PPPOX" , "rlock-AF_WANPIPE" , "rlock-AF_LLC" ,
  243. "rlock-27" , "rlock-28" , "rlock-AF_CAN" ,
  244. "rlock-AF_TIPC" , "rlock-AF_BLUETOOTH", "rlock-AF_IUCV" ,
  245. "rlock-AF_RXRPC" , "rlock-AF_ISDN" , "rlock-AF_PHONET" ,
  246. "rlock-AF_IEEE802154", "rlock-AF_CAIF" , "rlock-AF_ALG" ,
  247. "rlock-AF_NFC" , "rlock-AF_VSOCK" , "rlock-AF_KCM" ,
  248. "rlock-AF_QIPCRTR", "rlock-AF_SMC" , "rlock-AF_MAX"
  249. };
  250. static const char *const af_family_wlock_key_strings[AF_MAX+1] = {
  251. "wlock-AF_UNSPEC", "wlock-AF_UNIX" , "wlock-AF_INET" ,
  252. "wlock-AF_AX25" , "wlock-AF_IPX" , "wlock-AF_APPLETALK",
  253. "wlock-AF_NETROM", "wlock-AF_BRIDGE" , "wlock-AF_ATMPVC" ,
  254. "wlock-AF_X25" , "wlock-AF_INET6" , "wlock-AF_ROSE" ,
  255. "wlock-AF_DECnet", "wlock-AF_NETBEUI" , "wlock-AF_SECURITY" ,
  256. "wlock-AF_KEY" , "wlock-AF_NETLINK" , "wlock-AF_PACKET" ,
  257. "wlock-AF_ASH" , "wlock-AF_ECONET" , "wlock-AF_ATMSVC" ,
  258. "wlock-AF_RDS" , "wlock-AF_SNA" , "wlock-AF_IRDA" ,
  259. "wlock-AF_PPPOX" , "wlock-AF_WANPIPE" , "wlock-AF_LLC" ,
  260. "wlock-27" , "wlock-28" , "wlock-AF_CAN" ,
  261. "wlock-AF_TIPC" , "wlock-AF_BLUETOOTH", "wlock-AF_IUCV" ,
  262. "wlock-AF_RXRPC" , "wlock-AF_ISDN" , "wlock-AF_PHONET" ,
  263. "wlock-AF_IEEE802154", "wlock-AF_CAIF" , "wlock-AF_ALG" ,
  264. "wlock-AF_NFC" , "wlock-AF_VSOCK" , "wlock-AF_KCM" ,
  265. "wlock-AF_QIPCRTR", "wlock-AF_SMC" , "wlock-AF_MAX"
  266. };
  267. static const char *const af_family_elock_key_strings[AF_MAX+1] = {
  268. "elock-AF_UNSPEC", "elock-AF_UNIX" , "elock-AF_INET" ,
  269. "elock-AF_AX25" , "elock-AF_IPX" , "elock-AF_APPLETALK",
  270. "elock-AF_NETROM", "elock-AF_BRIDGE" , "elock-AF_ATMPVC" ,
  271. "elock-AF_X25" , "elock-AF_INET6" , "elock-AF_ROSE" ,
  272. "elock-AF_DECnet", "elock-AF_NETBEUI" , "elock-AF_SECURITY" ,
  273. "elock-AF_KEY" , "elock-AF_NETLINK" , "elock-AF_PACKET" ,
  274. "elock-AF_ASH" , "elock-AF_ECONET" , "elock-AF_ATMSVC" ,
  275. "elock-AF_RDS" , "elock-AF_SNA" , "elock-AF_IRDA" ,
  276. "elock-AF_PPPOX" , "elock-AF_WANPIPE" , "elock-AF_LLC" ,
  277. "elock-27" , "elock-28" , "elock-AF_CAN" ,
  278. "elock-AF_TIPC" , "elock-AF_BLUETOOTH", "elock-AF_IUCV" ,
  279. "elock-AF_RXRPC" , "elock-AF_ISDN" , "elock-AF_PHONET" ,
  280. "elock-AF_IEEE802154", "elock-AF_CAIF" , "elock-AF_ALG" ,
  281. "elock-AF_NFC" , "elock-AF_VSOCK" , "elock-AF_KCM" ,
  282. "elock-AF_QIPCRTR", "elock-AF_SMC" , "elock-AF_MAX"
  283. };
  284. /*
  285. * sk_callback_lock and sk queues locking rules are per-address-family,
  286. * so split the lock classes by using a per-AF key:
  287. */
  288. static struct lock_class_key af_callback_keys[AF_MAX];
  289. static struct lock_class_key af_rlock_keys[AF_MAX];
  290. static struct lock_class_key af_wlock_keys[AF_MAX];
  291. static struct lock_class_key af_elock_keys[AF_MAX];
  292. static struct lock_class_key af_kern_callback_keys[AF_MAX];
  293. /* Run time adjustable parameters. */
  294. __u32 sysctl_wmem_max __read_mostly = SK_WMEM_MAX;
  295. EXPORT_SYMBOL(sysctl_wmem_max);
  296. __u32 sysctl_rmem_max __read_mostly = SK_RMEM_MAX;
  297. EXPORT_SYMBOL(sysctl_rmem_max);
  298. __u32 sysctl_wmem_default __read_mostly = SK_WMEM_MAX;
  299. __u32 sysctl_rmem_default __read_mostly = SK_RMEM_MAX;
  300. /* Maximal space eaten by iovec or ancillary data plus some space */
  301. int sysctl_optmem_max __read_mostly = sizeof(unsigned long)*(2*UIO_MAXIOV+512);
  302. EXPORT_SYMBOL(sysctl_optmem_max);
  303. int sysctl_tstamp_allow_data __read_mostly = 1;
  304. struct static_key memalloc_socks = STATIC_KEY_INIT_FALSE;
  305. EXPORT_SYMBOL_GPL(memalloc_socks);
  306. /**
  307. * sk_set_memalloc - sets %SOCK_MEMALLOC
  308. * @sk: socket to set it on
  309. *
  310. * Set %SOCK_MEMALLOC on a socket for access to emergency reserves.
  311. * It's the responsibility of the admin to adjust min_free_kbytes
  312. * to meet the requirements
  313. */
  314. void sk_set_memalloc(struct sock *sk)
  315. {
  316. sock_set_flag(sk, SOCK_MEMALLOC);
  317. sk->sk_allocation |= __GFP_MEMALLOC;
  318. static_key_slow_inc(&memalloc_socks);
  319. }
  320. EXPORT_SYMBOL_GPL(sk_set_memalloc);
  321. void sk_clear_memalloc(struct sock *sk)
  322. {
  323. sock_reset_flag(sk, SOCK_MEMALLOC);
  324. sk->sk_allocation &= ~__GFP_MEMALLOC;
  325. static_key_slow_dec(&memalloc_socks);
  326. /*
  327. * SOCK_MEMALLOC is allowed to ignore rmem limits to ensure forward
  328. * progress of swapping. SOCK_MEMALLOC may be cleared while
  329. * it has rmem allocations due to the last swapfile being deactivated
  330. * but there is a risk that the socket is unusable due to exceeding
  331. * the rmem limits. Reclaim the reserves and obey rmem limits again.
  332. */
  333. sk_mem_reclaim(sk);
  334. }
  335. EXPORT_SYMBOL_GPL(sk_clear_memalloc);
  336. int __sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
  337. {
  338. int ret;
  339. unsigned int noreclaim_flag;
  340. /* these should have been dropped before queueing */
  341. BUG_ON(!sock_flag(sk, SOCK_MEMALLOC));
  342. noreclaim_flag = memalloc_noreclaim_save();
  343. ret = sk->sk_backlog_rcv(sk, skb);
  344. memalloc_noreclaim_restore(noreclaim_flag);
  345. return ret;
  346. }
  347. EXPORT_SYMBOL(__sk_backlog_rcv);
  348. static int sock_set_timeout(long *timeo_p, char __user *optval, int optlen)
  349. {
  350. struct timeval tv;
  351. if (optlen < sizeof(tv))
  352. return -EINVAL;
  353. if (copy_from_user(&tv, optval, sizeof(tv)))
  354. return -EFAULT;
  355. if (tv.tv_usec < 0 || tv.tv_usec >= USEC_PER_SEC)
  356. return -EDOM;
  357. if (tv.tv_sec < 0) {
  358. static int warned __read_mostly;
  359. *timeo_p = 0;
  360. if (warned < 10 && net_ratelimit()) {
  361. warned++;
  362. pr_info("%s: `%s' (pid %d) tries to set negative timeout\n",
  363. __func__, current->comm, task_pid_nr(current));
  364. }
  365. return 0;
  366. }
  367. *timeo_p = MAX_SCHEDULE_TIMEOUT;
  368. if (tv.tv_sec == 0 && tv.tv_usec == 0)
  369. return 0;
  370. if (tv.tv_sec < (MAX_SCHEDULE_TIMEOUT/HZ - 1))
  371. *timeo_p = tv.tv_sec * HZ + DIV_ROUND_UP(tv.tv_usec, USEC_PER_SEC / HZ);
  372. return 0;
  373. }
  374. static void sock_warn_obsolete_bsdism(const char *name)
  375. {
  376. static int warned;
  377. static char warncomm[TASK_COMM_LEN];
  378. if (strcmp(warncomm, current->comm) && warned < 5) {
  379. strcpy(warncomm, current->comm);
  380. pr_warn("process `%s' is using obsolete %s SO_BSDCOMPAT\n",
  381. warncomm, name);
  382. warned++;
  383. }
  384. }
  385. static bool sock_needs_netstamp(const struct sock *sk)
  386. {
  387. switch (sk->sk_family) {
  388. case AF_UNSPEC:
  389. case AF_UNIX:
  390. return false;
  391. default:
  392. return true;
  393. }
  394. }
  395. static void sock_disable_timestamp(struct sock *sk, unsigned long flags)
  396. {
  397. if (sk->sk_flags & flags) {
  398. sk->sk_flags &= ~flags;
  399. if (sock_needs_netstamp(sk) &&
  400. !(sk->sk_flags & SK_FLAGS_TIMESTAMP))
  401. net_disable_timestamp();
  402. }
  403. }
  404. int __sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  405. {
  406. unsigned long flags;
  407. struct sk_buff_head *list = &sk->sk_receive_queue;
  408. if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf) {
  409. atomic_inc(&sk->sk_drops);
  410. trace_sock_rcvqueue_full(sk, skb);
  411. return -ENOMEM;
  412. }
  413. if (!sk_rmem_schedule(sk, skb, skb->truesize)) {
  414. atomic_inc(&sk->sk_drops);
  415. return -ENOBUFS;
  416. }
  417. skb->dev = NULL;
  418. skb_set_owner_r(skb, sk);
  419. /* we escape from rcu protected region, make sure we dont leak
  420. * a norefcounted dst
  421. */
  422. skb_dst_force(skb);
  423. spin_lock_irqsave(&list->lock, flags);
  424. sock_skb_set_dropcount(sk, skb);
  425. __skb_queue_tail(list, skb);
  426. spin_unlock_irqrestore(&list->lock, flags);
  427. if (!sock_flag(sk, SOCK_DEAD))
  428. sk->sk_data_ready(sk);
  429. return 0;
  430. }
  431. EXPORT_SYMBOL(__sock_queue_rcv_skb);
  432. int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  433. {
  434. int err;
  435. err = sk_filter(sk, skb);
  436. if (err)
  437. return err;
  438. return __sock_queue_rcv_skb(sk, skb);
  439. }
  440. EXPORT_SYMBOL(sock_queue_rcv_skb);
  441. int __sk_receive_skb(struct sock *sk, struct sk_buff *skb,
  442. const int nested, unsigned int trim_cap, bool refcounted)
  443. {
  444. int rc = NET_RX_SUCCESS;
  445. if (sk_filter_trim_cap(sk, skb, trim_cap))
  446. goto discard_and_relse;
  447. skb->dev = NULL;
  448. if (sk_rcvqueues_full(sk, sk->sk_rcvbuf)) {
  449. atomic_inc(&sk->sk_drops);
  450. goto discard_and_relse;
  451. }
  452. if (nested)
  453. bh_lock_sock_nested(sk);
  454. else
  455. bh_lock_sock(sk);
  456. if (!sock_owned_by_user(sk)) {
  457. /*
  458. * trylock + unlock semantics:
  459. */
  460. mutex_acquire(&sk->sk_lock.dep_map, 0, 1, _RET_IP_);
  461. rc = sk_backlog_rcv(sk, skb);
  462. mutex_release(&sk->sk_lock.dep_map, 1, _RET_IP_);
  463. } else if (sk_add_backlog(sk, skb, sk->sk_rcvbuf)) {
  464. bh_unlock_sock(sk);
  465. atomic_inc(&sk->sk_drops);
  466. goto discard_and_relse;
  467. }
  468. bh_unlock_sock(sk);
  469. out:
  470. if (refcounted)
  471. sock_put(sk);
  472. return rc;
  473. discard_and_relse:
  474. kfree_skb(skb);
  475. goto out;
  476. }
  477. EXPORT_SYMBOL(__sk_receive_skb);
  478. struct dst_entry *__sk_dst_check(struct sock *sk, u32 cookie)
  479. {
  480. struct dst_entry *dst = __sk_dst_get(sk);
  481. if (dst && dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
  482. sk_tx_queue_clear(sk);
  483. sk->sk_dst_pending_confirm = 0;
  484. RCU_INIT_POINTER(sk->sk_dst_cache, NULL);
  485. dst_release(dst);
  486. return NULL;
  487. }
  488. return dst;
  489. }
  490. EXPORT_SYMBOL(__sk_dst_check);
  491. struct dst_entry *sk_dst_check(struct sock *sk, u32 cookie)
  492. {
  493. struct dst_entry *dst = sk_dst_get(sk);
  494. if (dst && dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
  495. sk_dst_reset(sk);
  496. dst_release(dst);
  497. return NULL;
  498. }
  499. return dst;
  500. }
  501. EXPORT_SYMBOL(sk_dst_check);
  502. static int sock_setbindtodevice(struct sock *sk, char __user *optval,
  503. int optlen)
  504. {
  505. int ret = -ENOPROTOOPT;
  506. #ifdef CONFIG_NETDEVICES
  507. struct net *net = sock_net(sk);
  508. char devname[IFNAMSIZ];
  509. int index;
  510. /* Sorry... */
  511. ret = -EPERM;
  512. if (!ns_capable(net->user_ns, CAP_NET_RAW))
  513. goto out;
  514. ret = -EINVAL;
  515. if (optlen < 0)
  516. goto out;
  517. /* Bind this socket to a particular device like "eth0",
  518. * as specified in the passed interface name. If the
  519. * name is "" or the option length is zero the socket
  520. * is not bound.
  521. */
  522. if (optlen > IFNAMSIZ - 1)
  523. optlen = IFNAMSIZ - 1;
  524. memset(devname, 0, sizeof(devname));
  525. ret = -EFAULT;
  526. if (copy_from_user(devname, optval, optlen))
  527. goto out;
  528. index = 0;
  529. if (devname[0] != '\0') {
  530. struct net_device *dev;
  531. rcu_read_lock();
  532. dev = dev_get_by_name_rcu(net, devname);
  533. if (dev)
  534. index = dev->ifindex;
  535. rcu_read_unlock();
  536. ret = -ENODEV;
  537. if (!dev)
  538. goto out;
  539. }
  540. lock_sock(sk);
  541. sk->sk_bound_dev_if = index;
  542. sk_dst_reset(sk);
  543. release_sock(sk);
  544. ret = 0;
  545. out:
  546. #endif
  547. return ret;
  548. }
  549. static int sock_getbindtodevice(struct sock *sk, char __user *optval,
  550. int __user *optlen, int len)
  551. {
  552. int ret = -ENOPROTOOPT;
  553. #ifdef CONFIG_NETDEVICES
  554. struct net *net = sock_net(sk);
  555. char devname[IFNAMSIZ];
  556. if (sk->sk_bound_dev_if == 0) {
  557. len = 0;
  558. goto zero;
  559. }
  560. ret = -EINVAL;
  561. if (len < IFNAMSIZ)
  562. goto out;
  563. ret = netdev_get_name(net, devname, sk->sk_bound_dev_if);
  564. if (ret)
  565. goto out;
  566. len = strlen(devname) + 1;
  567. ret = -EFAULT;
  568. if (copy_to_user(optval, devname, len))
  569. goto out;
  570. zero:
  571. ret = -EFAULT;
  572. if (put_user(len, optlen))
  573. goto out;
  574. ret = 0;
  575. out:
  576. #endif
  577. return ret;
  578. }
  579. static inline void sock_valbool_flag(struct sock *sk, int bit, int valbool)
  580. {
  581. if (valbool)
  582. sock_set_flag(sk, bit);
  583. else
  584. sock_reset_flag(sk, bit);
  585. }
  586. bool sk_mc_loop(struct sock *sk)
  587. {
  588. if (dev_recursion_level())
  589. return false;
  590. if (!sk)
  591. return true;
  592. switch (sk->sk_family) {
  593. case AF_INET:
  594. return inet_sk(sk)->mc_loop;
  595. #if IS_ENABLED(CONFIG_IPV6)
  596. case AF_INET6:
  597. return inet6_sk(sk)->mc_loop;
  598. #endif
  599. }
  600. WARN_ON(1);
  601. return true;
  602. }
  603. EXPORT_SYMBOL(sk_mc_loop);
  604. /*
  605. * This is meant for all protocols to use and covers goings on
  606. * at the socket level. Everything here is generic.
  607. */
  608. int sock_setsockopt(struct socket *sock, int level, int optname,
  609. char __user *optval, unsigned int optlen)
  610. {
  611. struct sock *sk = sock->sk;
  612. int val;
  613. int valbool;
  614. struct linger ling;
  615. int ret = 0;
  616. /*
  617. * Options without arguments
  618. */
  619. if (optname == SO_BINDTODEVICE)
  620. return sock_setbindtodevice(sk, optval, optlen);
  621. if (optlen < sizeof(int))
  622. return -EINVAL;
  623. if (get_user(val, (int __user *)optval))
  624. return -EFAULT;
  625. valbool = val ? 1 : 0;
  626. lock_sock(sk);
  627. switch (optname) {
  628. case SO_DEBUG:
  629. if (val && !capable(CAP_NET_ADMIN))
  630. ret = -EACCES;
  631. else
  632. sock_valbool_flag(sk, SOCK_DBG, valbool);
  633. break;
  634. case SO_REUSEADDR:
  635. sk->sk_reuse = (valbool ? SK_CAN_REUSE : SK_NO_REUSE);
  636. break;
  637. case SO_REUSEPORT:
  638. sk->sk_reuseport = valbool;
  639. break;
  640. case SO_TYPE:
  641. case SO_PROTOCOL:
  642. case SO_DOMAIN:
  643. case SO_ERROR:
  644. ret = -ENOPROTOOPT;
  645. break;
  646. case SO_DONTROUTE:
  647. sock_valbool_flag(sk, SOCK_LOCALROUTE, valbool);
  648. break;
  649. case SO_BROADCAST:
  650. sock_valbool_flag(sk, SOCK_BROADCAST, valbool);
  651. break;
  652. case SO_SNDBUF:
  653. /* Don't error on this BSD doesn't and if you think
  654. * about it this is right. Otherwise apps have to
  655. * play 'guess the biggest size' games. RCVBUF/SNDBUF
  656. * are treated in BSD as hints
  657. */
  658. val = min_t(u32, val, sysctl_wmem_max);
  659. set_sndbuf:
  660. sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
  661. sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
  662. /* Wake up sending tasks if we upped the value. */
  663. sk->sk_write_space(sk);
  664. break;
  665. case SO_SNDBUFFORCE:
  666. if (!capable(CAP_NET_ADMIN)) {
  667. ret = -EPERM;
  668. break;
  669. }
  670. goto set_sndbuf;
  671. case SO_RCVBUF:
  672. /* Don't error on this BSD doesn't and if you think
  673. * about it this is right. Otherwise apps have to
  674. * play 'guess the biggest size' games. RCVBUF/SNDBUF
  675. * are treated in BSD as hints
  676. */
  677. val = min_t(u32, val, sysctl_rmem_max);
  678. set_rcvbuf:
  679. sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
  680. /*
  681. * We double it on the way in to account for
  682. * "struct sk_buff" etc. overhead. Applications
  683. * assume that the SO_RCVBUF setting they make will
  684. * allow that much actual data to be received on that
  685. * socket.
  686. *
  687. * Applications are unaware that "struct sk_buff" and
  688. * other overheads allocate from the receive buffer
  689. * during socket buffer allocation.
  690. *
  691. * And after considering the possible alternatives,
  692. * returning the value we actually used in getsockopt
  693. * is the most desirable behavior.
  694. */
  695. sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
  696. break;
  697. case SO_RCVBUFFORCE:
  698. if (!capable(CAP_NET_ADMIN)) {
  699. ret = -EPERM;
  700. break;
  701. }
  702. goto set_rcvbuf;
  703. case SO_KEEPALIVE:
  704. if (sk->sk_prot->keepalive)
  705. sk->sk_prot->keepalive(sk, valbool);
  706. sock_valbool_flag(sk, SOCK_KEEPOPEN, valbool);
  707. break;
  708. case SO_OOBINLINE:
  709. sock_valbool_flag(sk, SOCK_URGINLINE, valbool);
  710. break;
  711. case SO_NO_CHECK:
  712. sk->sk_no_check_tx = valbool;
  713. break;
  714. case SO_PRIORITY:
  715. if ((val >= 0 && val <= 6) ||
  716. ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
  717. sk->sk_priority = val;
  718. else
  719. ret = -EPERM;
  720. break;
  721. case SO_LINGER:
  722. if (optlen < sizeof(ling)) {
  723. ret = -EINVAL; /* 1003.1g */
  724. break;
  725. }
  726. if (copy_from_user(&ling, optval, sizeof(ling))) {
  727. ret = -EFAULT;
  728. break;
  729. }
  730. if (!ling.l_onoff)
  731. sock_reset_flag(sk, SOCK_LINGER);
  732. else {
  733. #if (BITS_PER_LONG == 32)
  734. if ((unsigned int)ling.l_linger >= MAX_SCHEDULE_TIMEOUT/HZ)
  735. sk->sk_lingertime = MAX_SCHEDULE_TIMEOUT;
  736. else
  737. #endif
  738. sk->sk_lingertime = (unsigned int)ling.l_linger * HZ;
  739. sock_set_flag(sk, SOCK_LINGER);
  740. }
  741. break;
  742. case SO_BSDCOMPAT:
  743. sock_warn_obsolete_bsdism("setsockopt");
  744. break;
  745. case SO_PASSCRED:
  746. if (valbool)
  747. set_bit(SOCK_PASSCRED, &sock->flags);
  748. else
  749. clear_bit(SOCK_PASSCRED, &sock->flags);
  750. break;
  751. case SO_TIMESTAMP:
  752. case SO_TIMESTAMPNS:
  753. if (valbool) {
  754. if (optname == SO_TIMESTAMP)
  755. sock_reset_flag(sk, SOCK_RCVTSTAMPNS);
  756. else
  757. sock_set_flag(sk, SOCK_RCVTSTAMPNS);
  758. sock_set_flag(sk, SOCK_RCVTSTAMP);
  759. sock_enable_timestamp(sk, SOCK_TIMESTAMP);
  760. } else {
  761. sock_reset_flag(sk, SOCK_RCVTSTAMP);
  762. sock_reset_flag(sk, SOCK_RCVTSTAMPNS);
  763. }
  764. break;
  765. case SO_TIMESTAMPING:
  766. if (val & ~SOF_TIMESTAMPING_MASK) {
  767. ret = -EINVAL;
  768. break;
  769. }
  770. if (val & SOF_TIMESTAMPING_OPT_ID &&
  771. !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)) {
  772. if (sk->sk_protocol == IPPROTO_TCP &&
  773. sk->sk_type == SOCK_STREAM) {
  774. if ((1 << sk->sk_state) &
  775. (TCPF_CLOSE | TCPF_LISTEN)) {
  776. ret = -EINVAL;
  777. break;
  778. }
  779. sk->sk_tskey = tcp_sk(sk)->snd_una;
  780. } else {
  781. sk->sk_tskey = 0;
  782. }
  783. }
  784. if (val & SOF_TIMESTAMPING_OPT_STATS &&
  785. !(val & SOF_TIMESTAMPING_OPT_TSONLY)) {
  786. ret = -EINVAL;
  787. break;
  788. }
  789. sk->sk_tsflags = val;
  790. if (val & SOF_TIMESTAMPING_RX_SOFTWARE)
  791. sock_enable_timestamp(sk,
  792. SOCK_TIMESTAMPING_RX_SOFTWARE);
  793. else
  794. sock_disable_timestamp(sk,
  795. (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE));
  796. break;
  797. case SO_RCVLOWAT:
  798. if (val < 0)
  799. val = INT_MAX;
  800. sk->sk_rcvlowat = val ? : 1;
  801. break;
  802. case SO_RCVTIMEO:
  803. ret = sock_set_timeout(&sk->sk_rcvtimeo, optval, optlen);
  804. break;
  805. case SO_SNDTIMEO:
  806. ret = sock_set_timeout(&sk->sk_sndtimeo, optval, optlen);
  807. break;
  808. case SO_ATTACH_FILTER:
  809. ret = -EINVAL;
  810. if (optlen == sizeof(struct sock_fprog)) {
  811. struct sock_fprog fprog;
  812. ret = -EFAULT;
  813. if (copy_from_user(&fprog, optval, sizeof(fprog)))
  814. break;
  815. ret = sk_attach_filter(&fprog, sk);
  816. }
  817. break;
  818. case SO_ATTACH_BPF:
  819. ret = -EINVAL;
  820. if (optlen == sizeof(u32)) {
  821. u32 ufd;
  822. ret = -EFAULT;
  823. if (copy_from_user(&ufd, optval, sizeof(ufd)))
  824. break;
  825. ret = sk_attach_bpf(ufd, sk);
  826. }
  827. break;
  828. case SO_ATTACH_REUSEPORT_CBPF:
  829. ret = -EINVAL;
  830. if (optlen == sizeof(struct sock_fprog)) {
  831. struct sock_fprog fprog;
  832. ret = -EFAULT;
  833. if (copy_from_user(&fprog, optval, sizeof(fprog)))
  834. break;
  835. ret = sk_reuseport_attach_filter(&fprog, sk);
  836. }
  837. break;
  838. case SO_ATTACH_REUSEPORT_EBPF:
  839. ret = -EINVAL;
  840. if (optlen == sizeof(u32)) {
  841. u32 ufd;
  842. ret = -EFAULT;
  843. if (copy_from_user(&ufd, optval, sizeof(ufd)))
  844. break;
  845. ret = sk_reuseport_attach_bpf(ufd, sk);
  846. }
  847. break;
  848. case SO_DETACH_FILTER:
  849. ret = sk_detach_filter(sk);
  850. break;
  851. case SO_LOCK_FILTER:
  852. if (sock_flag(sk, SOCK_FILTER_LOCKED) && !valbool)
  853. ret = -EPERM;
  854. else
  855. sock_valbool_flag(sk, SOCK_FILTER_LOCKED, valbool);
  856. break;
  857. case SO_PASSSEC:
  858. if (valbool)
  859. set_bit(SOCK_PASSSEC, &sock->flags);
  860. else
  861. clear_bit(SOCK_PASSSEC, &sock->flags);
  862. break;
  863. case SO_MARK:
  864. if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
  865. ret = -EPERM;
  866. else
  867. sk->sk_mark = val;
  868. break;
  869. case SO_RXQ_OVFL:
  870. sock_valbool_flag(sk, SOCK_RXQ_OVFL, valbool);
  871. break;
  872. case SO_WIFI_STATUS:
  873. sock_valbool_flag(sk, SOCK_WIFI_STATUS, valbool);
  874. break;
  875. case SO_PEEK_OFF:
  876. if (sock->ops->set_peek_off)
  877. ret = sock->ops->set_peek_off(sk, val);
  878. else
  879. ret = -EOPNOTSUPP;
  880. break;
  881. case SO_NOFCS:
  882. sock_valbool_flag(sk, SOCK_NOFCS, valbool);
  883. break;
  884. case SO_SELECT_ERR_QUEUE:
  885. sock_valbool_flag(sk, SOCK_SELECT_ERR_QUEUE, valbool);
  886. break;
  887. #ifdef CONFIG_NET_RX_BUSY_POLL
  888. case SO_BUSY_POLL:
  889. /* allow unprivileged users to decrease the value */
  890. if ((val > sk->sk_ll_usec) && !capable(CAP_NET_ADMIN))
  891. ret = -EPERM;
  892. else {
  893. if (val < 0)
  894. ret = -EINVAL;
  895. else
  896. sk->sk_ll_usec = val;
  897. }
  898. break;
  899. #endif
  900. case SO_MAX_PACING_RATE:
  901. if (val != ~0U)
  902. cmpxchg(&sk->sk_pacing_status,
  903. SK_PACING_NONE,
  904. SK_PACING_NEEDED);
  905. sk->sk_max_pacing_rate = val;
  906. sk->sk_pacing_rate = min(sk->sk_pacing_rate,
  907. sk->sk_max_pacing_rate);
  908. break;
  909. case SO_INCOMING_CPU:
  910. sk->sk_incoming_cpu = val;
  911. break;
  912. case SO_CNX_ADVICE:
  913. if (val == 1)
  914. dst_negative_advice(sk);
  915. break;
  916. case SO_ZEROCOPY:
  917. if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
  918. ret = -ENOTSUPP;
  919. else if (sk->sk_protocol != IPPROTO_TCP)
  920. ret = -ENOTSUPP;
  921. else if (sk->sk_state != TCP_CLOSE)
  922. ret = -EBUSY;
  923. else if (val < 0 || val > 1)
  924. ret = -EINVAL;
  925. else
  926. sock_valbool_flag(sk, SOCK_ZEROCOPY, valbool);
  927. break;
  928. default:
  929. ret = -ENOPROTOOPT;
  930. break;
  931. }
  932. release_sock(sk);
  933. return ret;
  934. }
  935. EXPORT_SYMBOL(sock_setsockopt);
  936. static void cred_to_ucred(struct pid *pid, const struct cred *cred,
  937. struct ucred *ucred)
  938. {
  939. ucred->pid = pid_vnr(pid);
  940. ucred->uid = ucred->gid = -1;
  941. if (cred) {
  942. struct user_namespace *current_ns = current_user_ns();
  943. ucred->uid = from_kuid_munged(current_ns, cred->euid);
  944. ucred->gid = from_kgid_munged(current_ns, cred->egid);
  945. }
  946. }
  947. static int groups_to_user(gid_t __user *dst, const struct group_info *src)
  948. {
  949. struct user_namespace *user_ns = current_user_ns();
  950. int i;
  951. for (i = 0; i < src->ngroups; i++)
  952. if (put_user(from_kgid_munged(user_ns, src->gid[i]), dst + i))
  953. return -EFAULT;
  954. return 0;
  955. }
  956. int sock_getsockopt(struct socket *sock, int level, int optname,
  957. char __user *optval, int __user *optlen)
  958. {
  959. struct sock *sk = sock->sk;
  960. union {
  961. int val;
  962. u64 val64;
  963. struct linger ling;
  964. struct timeval tm;
  965. } v;
  966. int lv = sizeof(int);
  967. int len;
  968. if (get_user(len, optlen))
  969. return -EFAULT;
  970. if (len < 0)
  971. return -EINVAL;
  972. memset(&v, 0, sizeof(v));
  973. switch (optname) {
  974. case SO_DEBUG:
  975. v.val = sock_flag(sk, SOCK_DBG);
  976. break;
  977. case SO_DONTROUTE:
  978. v.val = sock_flag(sk, SOCK_LOCALROUTE);
  979. break;
  980. case SO_BROADCAST:
  981. v.val = sock_flag(sk, SOCK_BROADCAST);
  982. break;
  983. case SO_SNDBUF:
  984. v.val = sk->sk_sndbuf;
  985. break;
  986. case SO_RCVBUF:
  987. v.val = sk->sk_rcvbuf;
  988. break;
  989. case SO_REUSEADDR:
  990. v.val = sk->sk_reuse;
  991. break;
  992. case SO_REUSEPORT:
  993. v.val = sk->sk_reuseport;
  994. break;
  995. case SO_KEEPALIVE:
  996. v.val = sock_flag(sk, SOCK_KEEPOPEN);
  997. break;
  998. case SO_TYPE:
  999. v.val = sk->sk_type;
  1000. break;
  1001. case SO_PROTOCOL:
  1002. v.val = sk->sk_protocol;
  1003. break;
  1004. case SO_DOMAIN:
  1005. v.val = sk->sk_family;
  1006. break;
  1007. case SO_ERROR:
  1008. v.val = -sock_error(sk);
  1009. if (v.val == 0)
  1010. v.val = xchg(&sk->sk_err_soft, 0);
  1011. break;
  1012. case SO_OOBINLINE:
  1013. v.val = sock_flag(sk, SOCK_URGINLINE);
  1014. break;
  1015. case SO_NO_CHECK:
  1016. v.val = sk->sk_no_check_tx;
  1017. break;
  1018. case SO_PRIORITY:
  1019. v.val = sk->sk_priority;
  1020. break;
  1021. case SO_LINGER:
  1022. lv = sizeof(v.ling);
  1023. v.ling.l_onoff = sock_flag(sk, SOCK_LINGER);
  1024. v.ling.l_linger = sk->sk_lingertime / HZ;
  1025. break;
  1026. case SO_BSDCOMPAT:
  1027. sock_warn_obsolete_bsdism("getsockopt");
  1028. break;
  1029. case SO_TIMESTAMP:
  1030. v.val = sock_flag(sk, SOCK_RCVTSTAMP) &&
  1031. !sock_flag(sk, SOCK_RCVTSTAMPNS);
  1032. break;
  1033. case SO_TIMESTAMPNS:
  1034. v.val = sock_flag(sk, SOCK_RCVTSTAMPNS);
  1035. break;
  1036. case SO_TIMESTAMPING:
  1037. v.val = sk->sk_tsflags;
  1038. break;
  1039. case SO_RCVTIMEO:
  1040. lv = sizeof(struct timeval);
  1041. if (sk->sk_rcvtimeo == MAX_SCHEDULE_TIMEOUT) {
  1042. v.tm.tv_sec = 0;
  1043. v.tm.tv_usec = 0;
  1044. } else {
  1045. v.tm.tv_sec = sk->sk_rcvtimeo / HZ;
  1046. v.tm.tv_usec = ((sk->sk_rcvtimeo % HZ) * USEC_PER_SEC) / HZ;
  1047. }
  1048. break;
  1049. case SO_SNDTIMEO:
  1050. lv = sizeof(struct timeval);
  1051. if (sk->sk_sndtimeo == MAX_SCHEDULE_TIMEOUT) {
  1052. v.tm.tv_sec = 0;
  1053. v.tm.tv_usec = 0;
  1054. } else {
  1055. v.tm.tv_sec = sk->sk_sndtimeo / HZ;
  1056. v.tm.tv_usec = ((sk->sk_sndtimeo % HZ) * USEC_PER_SEC) / HZ;
  1057. }
  1058. break;
  1059. case SO_RCVLOWAT:
  1060. v.val = sk->sk_rcvlowat;
  1061. break;
  1062. case SO_SNDLOWAT:
  1063. v.val = 1;
  1064. break;
  1065. case SO_PASSCRED:
  1066. v.val = !!test_bit(SOCK_PASSCRED, &sock->flags);
  1067. break;
  1068. case SO_PEERCRED:
  1069. {
  1070. struct ucred peercred;
  1071. if (len > sizeof(peercred))
  1072. len = sizeof(peercred);
  1073. cred_to_ucred(sk->sk_peer_pid, sk->sk_peer_cred, &peercred);
  1074. if (copy_to_user(optval, &peercred, len))
  1075. return -EFAULT;
  1076. goto lenout;
  1077. }
  1078. case SO_PEERGROUPS:
  1079. {
  1080. int ret, n;
  1081. if (!sk->sk_peer_cred)
  1082. return -ENODATA;
  1083. n = sk->sk_peer_cred->group_info->ngroups;
  1084. if (len < n * sizeof(gid_t)) {
  1085. len = n * sizeof(gid_t);
  1086. return put_user(len, optlen) ? -EFAULT : -ERANGE;
  1087. }
  1088. len = n * sizeof(gid_t);
  1089. ret = groups_to_user((gid_t __user *)optval,
  1090. sk->sk_peer_cred->group_info);
  1091. if (ret)
  1092. return ret;
  1093. goto lenout;
  1094. }
  1095. case SO_PEERNAME:
  1096. {
  1097. char address[128];
  1098. lv = sock->ops->getname(sock, (struct sockaddr *)address, 2);
  1099. if (lv < 0)
  1100. return -ENOTCONN;
  1101. if (lv < len)
  1102. return -EINVAL;
  1103. if (copy_to_user(optval, address, len))
  1104. return -EFAULT;
  1105. goto lenout;
  1106. }
  1107. /* Dubious BSD thing... Probably nobody even uses it, but
  1108. * the UNIX standard wants it for whatever reason... -DaveM
  1109. */
  1110. case SO_ACCEPTCONN:
  1111. v.val = sk->sk_state == TCP_LISTEN;
  1112. break;
  1113. case SO_PASSSEC:
  1114. v.val = !!test_bit(SOCK_PASSSEC, &sock->flags);
  1115. break;
  1116. case SO_PEERSEC:
  1117. return security_socket_getpeersec_stream(sock, optval, optlen, len);
  1118. case SO_MARK:
  1119. v.val = sk->sk_mark;
  1120. break;
  1121. case SO_RXQ_OVFL:
  1122. v.val = sock_flag(sk, SOCK_RXQ_OVFL);
  1123. break;
  1124. case SO_WIFI_STATUS:
  1125. v.val = sock_flag(sk, SOCK_WIFI_STATUS);
  1126. break;
  1127. case SO_PEEK_OFF:
  1128. if (!sock->ops->set_peek_off)
  1129. return -EOPNOTSUPP;
  1130. v.val = sk->sk_peek_off;
  1131. break;
  1132. case SO_NOFCS:
  1133. v.val = sock_flag(sk, SOCK_NOFCS);
  1134. break;
  1135. case SO_BINDTODEVICE:
  1136. return sock_getbindtodevice(sk, optval, optlen, len);
  1137. case SO_GET_FILTER:
  1138. len = sk_get_filter(sk, (struct sock_filter __user *)optval, len);
  1139. if (len < 0)
  1140. return len;
  1141. goto lenout;
  1142. case SO_LOCK_FILTER:
  1143. v.val = sock_flag(sk, SOCK_FILTER_LOCKED);
  1144. break;
  1145. case SO_BPF_EXTENSIONS:
  1146. v.val = bpf_tell_extensions();
  1147. break;
  1148. case SO_SELECT_ERR_QUEUE:
  1149. v.val = sock_flag(sk, SOCK_SELECT_ERR_QUEUE);
  1150. break;
  1151. #ifdef CONFIG_NET_RX_BUSY_POLL
  1152. case SO_BUSY_POLL:
  1153. v.val = sk->sk_ll_usec;
  1154. break;
  1155. #endif
  1156. case SO_MAX_PACING_RATE:
  1157. v.val = sk->sk_max_pacing_rate;
  1158. break;
  1159. case SO_INCOMING_CPU:
  1160. v.val = sk->sk_incoming_cpu;
  1161. break;
  1162. case SO_MEMINFO:
  1163. {
  1164. u32 meminfo[SK_MEMINFO_VARS];
  1165. if (get_user(len, optlen))
  1166. return -EFAULT;
  1167. sk_get_meminfo(sk, meminfo);
  1168. len = min_t(unsigned int, len, sizeof(meminfo));
  1169. if (copy_to_user(optval, &meminfo, len))
  1170. return -EFAULT;
  1171. goto lenout;
  1172. }
  1173. #ifdef CONFIG_NET_RX_BUSY_POLL
  1174. case SO_INCOMING_NAPI_ID:
  1175. v.val = READ_ONCE(sk->sk_napi_id);
  1176. /* aggregate non-NAPI IDs down to 0 */
  1177. if (v.val < MIN_NAPI_ID)
  1178. v.val = 0;
  1179. break;
  1180. #endif
  1181. case SO_COOKIE:
  1182. lv = sizeof(u64);
  1183. if (len < lv)
  1184. return -EINVAL;
  1185. v.val64 = sock_gen_cookie(sk);
  1186. break;
  1187. case SO_ZEROCOPY:
  1188. v.val = sock_flag(sk, SOCK_ZEROCOPY);
  1189. break;
  1190. default:
  1191. /* We implement the SO_SNDLOWAT etc to not be settable
  1192. * (1003.1g 7).
  1193. */
  1194. return -ENOPROTOOPT;
  1195. }
  1196. if (len > lv)
  1197. len = lv;
  1198. if (copy_to_user(optval, &v, len))
  1199. return -EFAULT;
  1200. lenout:
  1201. if (put_user(len, optlen))
  1202. return -EFAULT;
  1203. return 0;
  1204. }
  1205. /*
  1206. * Initialize an sk_lock.
  1207. *
  1208. * (We also register the sk_lock with the lock validator.)
  1209. */
  1210. static inline void sock_lock_init(struct sock *sk)
  1211. {
  1212. if (sk->sk_kern_sock)
  1213. sock_lock_init_class_and_name(
  1214. sk,
  1215. af_family_kern_slock_key_strings[sk->sk_family],
  1216. af_family_kern_slock_keys + sk->sk_family,
  1217. af_family_kern_key_strings[sk->sk_family],
  1218. af_family_kern_keys + sk->sk_family);
  1219. else
  1220. sock_lock_init_class_and_name(
  1221. sk,
  1222. af_family_slock_key_strings[sk->sk_family],
  1223. af_family_slock_keys + sk->sk_family,
  1224. af_family_key_strings[sk->sk_family],
  1225. af_family_keys + sk->sk_family);
  1226. }
  1227. /*
  1228. * Copy all fields from osk to nsk but nsk->sk_refcnt must not change yet,
  1229. * even temporarly, because of RCU lookups. sk_node should also be left as is.
  1230. * We must not copy fields between sk_dontcopy_begin and sk_dontcopy_end
  1231. */
  1232. static void sock_copy(struct sock *nsk, const struct sock *osk)
  1233. {
  1234. #ifdef CONFIG_SECURITY_NETWORK
  1235. void *sptr = nsk->sk_security;
  1236. #endif
  1237. memcpy(nsk, osk, offsetof(struct sock, sk_dontcopy_begin));
  1238. memcpy(&nsk->sk_dontcopy_end, &osk->sk_dontcopy_end,
  1239. osk->sk_prot->obj_size - offsetof(struct sock, sk_dontcopy_end));
  1240. #ifdef CONFIG_SECURITY_NETWORK
  1241. nsk->sk_security = sptr;
  1242. security_sk_clone(osk, nsk);
  1243. #endif
  1244. }
  1245. static struct sock *sk_prot_alloc(struct proto *prot, gfp_t priority,
  1246. int family)
  1247. {
  1248. struct sock *sk;
  1249. struct kmem_cache *slab;
  1250. slab = prot->slab;
  1251. if (slab != NULL) {
  1252. sk = kmem_cache_alloc(slab, priority & ~__GFP_ZERO);
  1253. if (!sk)
  1254. return sk;
  1255. if (priority & __GFP_ZERO)
  1256. sk_prot_clear_nulls(sk, prot->obj_size);
  1257. } else
  1258. sk = kmalloc(prot->obj_size, priority);
  1259. if (sk != NULL) {
  1260. if (security_sk_alloc(sk, family, priority))
  1261. goto out_free;
  1262. if (!try_module_get(prot->owner))
  1263. goto out_free_sec;
  1264. sk_tx_queue_clear(sk);
  1265. }
  1266. return sk;
  1267. out_free_sec:
  1268. security_sk_free(sk);
  1269. out_free:
  1270. if (slab != NULL)
  1271. kmem_cache_free(slab, sk);
  1272. else
  1273. kfree(sk);
  1274. return NULL;
  1275. }
  1276. static void sk_prot_free(struct proto *prot, struct sock *sk)
  1277. {
  1278. struct kmem_cache *slab;
  1279. struct module *owner;
  1280. owner = prot->owner;
  1281. slab = prot->slab;
  1282. cgroup_sk_free(&sk->sk_cgrp_data);
  1283. mem_cgroup_sk_free(sk);
  1284. security_sk_free(sk);
  1285. if (slab != NULL)
  1286. kmem_cache_free(slab, sk);
  1287. else
  1288. kfree(sk);
  1289. module_put(owner);
  1290. }
  1291. /**
  1292. * sk_alloc - All socket objects are allocated here
  1293. * @net: the applicable net namespace
  1294. * @family: protocol family
  1295. * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
  1296. * @prot: struct proto associated with this new sock instance
  1297. * @kern: is this to be a kernel socket?
  1298. */
  1299. struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
  1300. struct proto *prot, int kern)
  1301. {
  1302. struct sock *sk;
  1303. sk = sk_prot_alloc(prot, priority | __GFP_ZERO, family);
  1304. if (sk) {
  1305. sk->sk_family = family;
  1306. /*
  1307. * See comment in struct sock definition to understand
  1308. * why we need sk_prot_creator -acme
  1309. */
  1310. sk->sk_prot = sk->sk_prot_creator = prot;
  1311. sk->sk_kern_sock = kern;
  1312. sock_lock_init(sk);
  1313. sk->sk_net_refcnt = kern ? 0 : 1;
  1314. if (likely(sk->sk_net_refcnt)) {
  1315. get_net(net);
  1316. sock_inuse_add(net, 1);
  1317. }
  1318. sock_net_set(sk, net);
  1319. refcount_set(&sk->sk_wmem_alloc, 1);
  1320. mem_cgroup_sk_alloc(sk);
  1321. cgroup_sk_alloc(&sk->sk_cgrp_data);
  1322. sock_update_classid(&sk->sk_cgrp_data);
  1323. sock_update_netprioidx(&sk->sk_cgrp_data);
  1324. }
  1325. return sk;
  1326. }
  1327. EXPORT_SYMBOL(sk_alloc);
  1328. /* Sockets having SOCK_RCU_FREE will call this function after one RCU
  1329. * grace period. This is the case for UDP sockets and TCP listeners.
  1330. */
  1331. static void __sk_destruct(struct rcu_head *head)
  1332. {
  1333. struct sock *sk = container_of(head, struct sock, sk_rcu);
  1334. struct sk_filter *filter;
  1335. if (sk->sk_destruct)
  1336. sk->sk_destruct(sk);
  1337. filter = rcu_dereference_check(sk->sk_filter,
  1338. refcount_read(&sk->sk_wmem_alloc) == 0);
  1339. if (filter) {
  1340. sk_filter_uncharge(sk, filter);
  1341. RCU_INIT_POINTER(sk->sk_filter, NULL);
  1342. }
  1343. if (rcu_access_pointer(sk->sk_reuseport_cb))
  1344. reuseport_detach_sock(sk);
  1345. sock_disable_timestamp(sk, SK_FLAGS_TIMESTAMP);
  1346. if (atomic_read(&sk->sk_omem_alloc))
  1347. pr_debug("%s: optmem leakage (%d bytes) detected\n",
  1348. __func__, atomic_read(&sk->sk_omem_alloc));
  1349. if (sk->sk_frag.page) {
  1350. put_page(sk->sk_frag.page);
  1351. sk->sk_frag.page = NULL;
  1352. }
  1353. if (sk->sk_peer_cred)
  1354. put_cred(sk->sk_peer_cred);
  1355. put_pid(sk->sk_peer_pid);
  1356. if (likely(sk->sk_net_refcnt))
  1357. put_net(sock_net(sk));
  1358. sk_prot_free(sk->sk_prot_creator, sk);
  1359. }
  1360. void sk_destruct(struct sock *sk)
  1361. {
  1362. if (sock_flag(sk, SOCK_RCU_FREE))
  1363. call_rcu(&sk->sk_rcu, __sk_destruct);
  1364. else
  1365. __sk_destruct(&sk->sk_rcu);
  1366. }
  1367. static void __sk_free(struct sock *sk)
  1368. {
  1369. if (likely(sk->sk_net_refcnt))
  1370. sock_inuse_add(sock_net(sk), -1);
  1371. if (unlikely(sock_diag_has_destroy_listeners(sk) && sk->sk_net_refcnt))
  1372. sock_diag_broadcast_destroy(sk);
  1373. else
  1374. sk_destruct(sk);
  1375. }
  1376. void sk_free(struct sock *sk)
  1377. {
  1378. /*
  1379. * We subtract one from sk_wmem_alloc and can know if
  1380. * some packets are still in some tx queue.
  1381. * If not null, sock_wfree() will call __sk_free(sk) later
  1382. */
  1383. if (refcount_dec_and_test(&sk->sk_wmem_alloc))
  1384. __sk_free(sk);
  1385. }
  1386. EXPORT_SYMBOL(sk_free);
  1387. static void sk_init_common(struct sock *sk)
  1388. {
  1389. skb_queue_head_init(&sk->sk_receive_queue);
  1390. skb_queue_head_init(&sk->sk_write_queue);
  1391. skb_queue_head_init(&sk->sk_error_queue);
  1392. rwlock_init(&sk->sk_callback_lock);
  1393. lockdep_set_class_and_name(&sk->sk_receive_queue.lock,
  1394. af_rlock_keys + sk->sk_family,
  1395. af_family_rlock_key_strings[sk->sk_family]);
  1396. lockdep_set_class_and_name(&sk->sk_write_queue.lock,
  1397. af_wlock_keys + sk->sk_family,
  1398. af_family_wlock_key_strings[sk->sk_family]);
  1399. lockdep_set_class_and_name(&sk->sk_error_queue.lock,
  1400. af_elock_keys + sk->sk_family,
  1401. af_family_elock_key_strings[sk->sk_family]);
  1402. lockdep_set_class_and_name(&sk->sk_callback_lock,
  1403. af_callback_keys + sk->sk_family,
  1404. af_family_clock_key_strings[sk->sk_family]);
  1405. }
  1406. /**
  1407. * sk_clone_lock - clone a socket, and lock its clone
  1408. * @sk: the socket to clone
  1409. * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
  1410. *
  1411. * Caller must unlock socket even in error path (bh_unlock_sock(newsk))
  1412. */
  1413. struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
  1414. {
  1415. struct sock *newsk;
  1416. bool is_charged = true;
  1417. newsk = sk_prot_alloc(sk->sk_prot, priority, sk->sk_family);
  1418. if (newsk != NULL) {
  1419. struct sk_filter *filter;
  1420. sock_copy(newsk, sk);
  1421. newsk->sk_prot_creator = sk->sk_prot;
  1422. /* SANITY */
  1423. if (likely(newsk->sk_net_refcnt))
  1424. get_net(sock_net(newsk));
  1425. sk_node_init(&newsk->sk_node);
  1426. sock_lock_init(newsk);
  1427. bh_lock_sock(newsk);
  1428. newsk->sk_backlog.head = newsk->sk_backlog.tail = NULL;
  1429. newsk->sk_backlog.len = 0;
  1430. atomic_set(&newsk->sk_rmem_alloc, 0);
  1431. /*
  1432. * sk_wmem_alloc set to one (see sk_free() and sock_wfree())
  1433. */
  1434. refcount_set(&newsk->sk_wmem_alloc, 1);
  1435. atomic_set(&newsk->sk_omem_alloc, 0);
  1436. sk_init_common(newsk);
  1437. newsk->sk_dst_cache = NULL;
  1438. newsk->sk_dst_pending_confirm = 0;
  1439. newsk->sk_wmem_queued = 0;
  1440. newsk->sk_forward_alloc = 0;
  1441. atomic_set(&newsk->sk_drops, 0);
  1442. newsk->sk_send_head = NULL;
  1443. newsk->sk_userlocks = sk->sk_userlocks & ~SOCK_BINDPORT_LOCK;
  1444. atomic_set(&newsk->sk_zckey, 0);
  1445. sock_reset_flag(newsk, SOCK_DONE);
  1446. mem_cgroup_sk_alloc(newsk);
  1447. cgroup_sk_alloc(&newsk->sk_cgrp_data);
  1448. rcu_read_lock();
  1449. filter = rcu_dereference(sk->sk_filter);
  1450. if (filter != NULL)
  1451. /* though it's an empty new sock, the charging may fail
  1452. * if sysctl_optmem_max was changed between creation of
  1453. * original socket and cloning
  1454. */
  1455. is_charged = sk_filter_charge(newsk, filter);
  1456. RCU_INIT_POINTER(newsk->sk_filter, filter);
  1457. rcu_read_unlock();
  1458. if (unlikely(!is_charged || xfrm_sk_clone_policy(newsk, sk))) {
  1459. /* We need to make sure that we don't uncharge the new
  1460. * socket if we couldn't charge it in the first place
  1461. * as otherwise we uncharge the parent's filter.
  1462. */
  1463. if (!is_charged)
  1464. RCU_INIT_POINTER(newsk->sk_filter, NULL);
  1465. sk_free_unlock_clone(newsk);
  1466. newsk = NULL;
  1467. goto out;
  1468. }
  1469. RCU_INIT_POINTER(newsk->sk_reuseport_cb, NULL);
  1470. newsk->sk_err = 0;
  1471. newsk->sk_err_soft = 0;
  1472. newsk->sk_priority = 0;
  1473. newsk->sk_incoming_cpu = raw_smp_processor_id();
  1474. atomic64_set(&newsk->sk_cookie, 0);
  1475. if (likely(newsk->sk_net_refcnt))
  1476. sock_inuse_add(sock_net(newsk), 1);
  1477. /*
  1478. * Before updating sk_refcnt, we must commit prior changes to memory
  1479. * (Documentation/RCU/rculist_nulls.txt for details)
  1480. */
  1481. smp_wmb();
  1482. refcount_set(&newsk->sk_refcnt, 2);
  1483. /*
  1484. * Increment the counter in the same struct proto as the master
  1485. * sock (sk_refcnt_debug_inc uses newsk->sk_prot->socks, that
  1486. * is the same as sk->sk_prot->socks, as this field was copied
  1487. * with memcpy).
  1488. *
  1489. * This _changes_ the previous behaviour, where
  1490. * tcp_create_openreq_child always was incrementing the
  1491. * equivalent to tcp_prot->socks (inet_sock_nr), so this have
  1492. * to be taken into account in all callers. -acme
  1493. */
  1494. sk_refcnt_debug_inc(newsk);
  1495. sk_set_socket(newsk, NULL);
  1496. newsk->sk_wq = NULL;
  1497. if (newsk->sk_prot->sockets_allocated)
  1498. sk_sockets_allocated_inc(newsk);
  1499. if (sock_needs_netstamp(sk) &&
  1500. newsk->sk_flags & SK_FLAGS_TIMESTAMP)
  1501. net_enable_timestamp();
  1502. }
  1503. out:
  1504. return newsk;
  1505. }
  1506. EXPORT_SYMBOL_GPL(sk_clone_lock);
  1507. void sk_free_unlock_clone(struct sock *sk)
  1508. {
  1509. /* It is still raw copy of parent, so invalidate
  1510. * destructor and make plain sk_free() */
  1511. sk->sk_destruct = NULL;
  1512. bh_unlock_sock(sk);
  1513. sk_free(sk);
  1514. }
  1515. EXPORT_SYMBOL_GPL(sk_free_unlock_clone);
  1516. void sk_setup_caps(struct sock *sk, struct dst_entry *dst)
  1517. {
  1518. u32 max_segs = 1;
  1519. sk_dst_set(sk, dst);
  1520. sk->sk_route_caps = dst->dev->features;
  1521. if (sk->sk_route_caps & NETIF_F_GSO)
  1522. sk->sk_route_caps |= NETIF_F_GSO_SOFTWARE;
  1523. sk->sk_route_caps &= ~sk->sk_route_nocaps;
  1524. if (sk_can_gso(sk)) {
  1525. if (dst->header_len && !xfrm_dst_offload_ok(dst)) {
  1526. sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
  1527. } else {
  1528. sk->sk_route_caps |= NETIF_F_SG | NETIF_F_HW_CSUM;
  1529. sk->sk_gso_max_size = dst->dev->gso_max_size;
  1530. max_segs = max_t(u32, dst->dev->gso_max_segs, 1);
  1531. }
  1532. }
  1533. sk->sk_gso_max_segs = max_segs;
  1534. }
  1535. EXPORT_SYMBOL_GPL(sk_setup_caps);
  1536. /*
  1537. * Simple resource managers for sockets.
  1538. */
  1539. /*
  1540. * Write buffer destructor automatically called from kfree_skb.
  1541. */
  1542. void sock_wfree(struct sk_buff *skb)
  1543. {
  1544. struct sock *sk = skb->sk;
  1545. unsigned int len = skb->truesize;
  1546. if (!sock_flag(sk, SOCK_USE_WRITE_QUEUE)) {
  1547. /*
  1548. * Keep a reference on sk_wmem_alloc, this will be released
  1549. * after sk_write_space() call
  1550. */
  1551. WARN_ON(refcount_sub_and_test(len - 1, &sk->sk_wmem_alloc));
  1552. sk->sk_write_space(sk);
  1553. len = 1;
  1554. }
  1555. /*
  1556. * if sk_wmem_alloc reaches 0, we must finish what sk_free()
  1557. * could not do because of in-flight packets
  1558. */
  1559. if (refcount_sub_and_test(len, &sk->sk_wmem_alloc))
  1560. __sk_free(sk);
  1561. }
  1562. EXPORT_SYMBOL(sock_wfree);
  1563. /* This variant of sock_wfree() is used by TCP,
  1564. * since it sets SOCK_USE_WRITE_QUEUE.
  1565. */
  1566. void __sock_wfree(struct sk_buff *skb)
  1567. {
  1568. struct sock *sk = skb->sk;
  1569. if (refcount_sub_and_test(skb->truesize, &sk->sk_wmem_alloc))
  1570. __sk_free(sk);
  1571. }
  1572. void skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
  1573. {
  1574. skb_orphan(skb);
  1575. skb->sk = sk;
  1576. #ifdef CONFIG_INET
  1577. if (unlikely(!sk_fullsock(sk))) {
  1578. skb->destructor = sock_edemux;
  1579. sock_hold(sk);
  1580. return;
  1581. }
  1582. #endif
  1583. skb->destructor = sock_wfree;
  1584. skb_set_hash_from_sk(skb, sk);
  1585. /*
  1586. * We used to take a refcount on sk, but following operation
  1587. * is enough to guarantee sk_free() wont free this sock until
  1588. * all in-flight packets are completed
  1589. */
  1590. refcount_add(skb->truesize, &sk->sk_wmem_alloc);
  1591. }
  1592. EXPORT_SYMBOL(skb_set_owner_w);
  1593. /* This helper is used by netem, as it can hold packets in its
  1594. * delay queue. We want to allow the owner socket to send more
  1595. * packets, as if they were already TX completed by a typical driver.
  1596. * But we also want to keep skb->sk set because some packet schedulers
  1597. * rely on it (sch_fq for example).
  1598. */
  1599. void skb_orphan_partial(struct sk_buff *skb)
  1600. {
  1601. if (skb_is_tcp_pure_ack(skb))
  1602. return;
  1603. if (skb->destructor == sock_wfree
  1604. #ifdef CONFIG_INET
  1605. || skb->destructor == tcp_wfree
  1606. #endif
  1607. ) {
  1608. struct sock *sk = skb->sk;
  1609. if (refcount_inc_not_zero(&sk->sk_refcnt)) {
  1610. WARN_ON(refcount_sub_and_test(skb->truesize, &sk->sk_wmem_alloc));
  1611. skb->destructor = sock_efree;
  1612. }
  1613. } else {
  1614. skb_orphan(skb);
  1615. }
  1616. }
  1617. EXPORT_SYMBOL(skb_orphan_partial);
  1618. /*
  1619. * Read buffer destructor automatically called from kfree_skb.
  1620. */
  1621. void sock_rfree(struct sk_buff *skb)
  1622. {
  1623. struct sock *sk = skb->sk;
  1624. unsigned int len = skb->truesize;
  1625. atomic_sub(len, &sk->sk_rmem_alloc);
  1626. sk_mem_uncharge(sk, len);
  1627. }
  1628. EXPORT_SYMBOL(sock_rfree);
  1629. /*
  1630. * Buffer destructor for skbs that are not used directly in read or write
  1631. * path, e.g. for error handler skbs. Automatically called from kfree_skb.
  1632. */
  1633. void sock_efree(struct sk_buff *skb)
  1634. {
  1635. sock_put(skb->sk);
  1636. }
  1637. EXPORT_SYMBOL(sock_efree);
  1638. kuid_t sock_i_uid(struct sock *sk)
  1639. {
  1640. kuid_t uid;
  1641. read_lock_bh(&sk->sk_callback_lock);
  1642. uid = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_uid : GLOBAL_ROOT_UID;
  1643. read_unlock_bh(&sk->sk_callback_lock);
  1644. return uid;
  1645. }
  1646. EXPORT_SYMBOL(sock_i_uid);
  1647. unsigned long sock_i_ino(struct sock *sk)
  1648. {
  1649. unsigned long ino;
  1650. read_lock_bh(&sk->sk_callback_lock);
  1651. ino = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_ino : 0;
  1652. read_unlock_bh(&sk->sk_callback_lock);
  1653. return ino;
  1654. }
  1655. EXPORT_SYMBOL(sock_i_ino);
  1656. /*
  1657. * Allocate a skb from the socket's send buffer.
  1658. */
  1659. struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force,
  1660. gfp_t priority)
  1661. {
  1662. if (force || refcount_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf) {
  1663. struct sk_buff *skb = alloc_skb(size, priority);
  1664. if (skb) {
  1665. skb_set_owner_w(skb, sk);
  1666. return skb;
  1667. }
  1668. }
  1669. return NULL;
  1670. }
  1671. EXPORT_SYMBOL(sock_wmalloc);
  1672. static void sock_ofree(struct sk_buff *skb)
  1673. {
  1674. struct sock *sk = skb->sk;
  1675. atomic_sub(skb->truesize, &sk->sk_omem_alloc);
  1676. }
  1677. struct sk_buff *sock_omalloc(struct sock *sk, unsigned long size,
  1678. gfp_t priority)
  1679. {
  1680. struct sk_buff *skb;
  1681. /* small safe race: SKB_TRUESIZE may differ from final skb->truesize */
  1682. if (atomic_read(&sk->sk_omem_alloc) + SKB_TRUESIZE(size) >
  1683. sysctl_optmem_max)
  1684. return NULL;
  1685. skb = alloc_skb(size, priority);
  1686. if (!skb)
  1687. return NULL;
  1688. atomic_add(skb->truesize, &sk->sk_omem_alloc);
  1689. skb->sk = sk;
  1690. skb->destructor = sock_ofree;
  1691. return skb;
  1692. }
  1693. /*
  1694. * Allocate a memory block from the socket's option memory buffer.
  1695. */
  1696. void *sock_kmalloc(struct sock *sk, int size, gfp_t priority)
  1697. {
  1698. if ((unsigned int)size <= sysctl_optmem_max &&
  1699. atomic_read(&sk->sk_omem_alloc) + size < sysctl_optmem_max) {
  1700. void *mem;
  1701. /* First do the add, to avoid the race if kmalloc
  1702. * might sleep.
  1703. */
  1704. atomic_add(size, &sk->sk_omem_alloc);
  1705. mem = kmalloc(size, priority);
  1706. if (mem)
  1707. return mem;
  1708. atomic_sub(size, &sk->sk_omem_alloc);
  1709. }
  1710. return NULL;
  1711. }
  1712. EXPORT_SYMBOL(sock_kmalloc);
  1713. /* Free an option memory block. Note, we actually want the inline
  1714. * here as this allows gcc to detect the nullify and fold away the
  1715. * condition entirely.
  1716. */
  1717. static inline void __sock_kfree_s(struct sock *sk, void *mem, int size,
  1718. const bool nullify)
  1719. {
  1720. if (WARN_ON_ONCE(!mem))
  1721. return;
  1722. if (nullify)
  1723. kzfree(mem);
  1724. else
  1725. kfree(mem);
  1726. atomic_sub(size, &sk->sk_omem_alloc);
  1727. }
  1728. void sock_kfree_s(struct sock *sk, void *mem, int size)
  1729. {
  1730. __sock_kfree_s(sk, mem, size, false);
  1731. }
  1732. EXPORT_SYMBOL(sock_kfree_s);
  1733. void sock_kzfree_s(struct sock *sk, void *mem, int size)
  1734. {
  1735. __sock_kfree_s(sk, mem, size, true);
  1736. }
  1737. EXPORT_SYMBOL(sock_kzfree_s);
  1738. /* It is almost wait_for_tcp_memory minus release_sock/lock_sock.
  1739. I think, these locks should be removed for datagram sockets.
  1740. */
  1741. static long sock_wait_for_wmem(struct sock *sk, long timeo)
  1742. {
  1743. DEFINE_WAIT(wait);
  1744. sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  1745. for (;;) {
  1746. if (!timeo)
  1747. break;
  1748. if (signal_pending(current))
  1749. break;
  1750. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  1751. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1752. if (refcount_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf)
  1753. break;
  1754. if (sk->sk_shutdown & SEND_SHUTDOWN)
  1755. break;
  1756. if (sk->sk_err)
  1757. break;
  1758. timeo = schedule_timeout(timeo);
  1759. }
  1760. finish_wait(sk_sleep(sk), &wait);
  1761. return timeo;
  1762. }
  1763. /*
  1764. * Generic send/receive buffer handlers
  1765. */
  1766. struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
  1767. unsigned long data_len, int noblock,
  1768. int *errcode, int max_page_order)
  1769. {
  1770. struct sk_buff *skb;
  1771. long timeo;
  1772. int err;
  1773. timeo = sock_sndtimeo(sk, noblock);
  1774. for (;;) {
  1775. err = sock_error(sk);
  1776. if (err != 0)
  1777. goto failure;
  1778. err = -EPIPE;
  1779. if (sk->sk_shutdown & SEND_SHUTDOWN)
  1780. goto failure;
  1781. if (sk_wmem_alloc_get(sk) < sk->sk_sndbuf)
  1782. break;
  1783. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  1784. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  1785. err = -EAGAIN;
  1786. if (!timeo)
  1787. goto failure;
  1788. if (signal_pending(current))
  1789. goto interrupted;
  1790. timeo = sock_wait_for_wmem(sk, timeo);
  1791. }
  1792. skb = alloc_skb_with_frags(header_len, data_len, max_page_order,
  1793. errcode, sk->sk_allocation);
  1794. if (skb)
  1795. skb_set_owner_w(skb, sk);
  1796. return skb;
  1797. interrupted:
  1798. err = sock_intr_errno(timeo);
  1799. failure:
  1800. *errcode = err;
  1801. return NULL;
  1802. }
  1803. EXPORT_SYMBOL(sock_alloc_send_pskb);
  1804. struct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size,
  1805. int noblock, int *errcode)
  1806. {
  1807. return sock_alloc_send_pskb(sk, size, 0, noblock, errcode, 0);
  1808. }
  1809. EXPORT_SYMBOL(sock_alloc_send_skb);
  1810. int __sock_cmsg_send(struct sock *sk, struct msghdr *msg, struct cmsghdr *cmsg,
  1811. struct sockcm_cookie *sockc)
  1812. {
  1813. u32 tsflags;
  1814. switch (cmsg->cmsg_type) {
  1815. case SO_MARK:
  1816. if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
  1817. return -EPERM;
  1818. if (cmsg->cmsg_len != CMSG_LEN(sizeof(u32)))
  1819. return -EINVAL;
  1820. sockc->mark = *(u32 *)CMSG_DATA(cmsg);
  1821. break;
  1822. case SO_TIMESTAMPING:
  1823. if (cmsg->cmsg_len != CMSG_LEN(sizeof(u32)))
  1824. return -EINVAL;
  1825. tsflags = *(u32 *)CMSG_DATA(cmsg);
  1826. if (tsflags & ~SOF_TIMESTAMPING_TX_RECORD_MASK)
  1827. return -EINVAL;
  1828. sockc->tsflags &= ~SOF_TIMESTAMPING_TX_RECORD_MASK;
  1829. sockc->tsflags |= tsflags;
  1830. break;
  1831. /* SCM_RIGHTS and SCM_CREDENTIALS are semantically in SOL_UNIX. */
  1832. case SCM_RIGHTS:
  1833. case SCM_CREDENTIALS:
  1834. break;
  1835. default:
  1836. return -EINVAL;
  1837. }
  1838. return 0;
  1839. }
  1840. EXPORT_SYMBOL(__sock_cmsg_send);
  1841. int sock_cmsg_send(struct sock *sk, struct msghdr *msg,
  1842. struct sockcm_cookie *sockc)
  1843. {
  1844. struct cmsghdr *cmsg;
  1845. int ret;
  1846. for_each_cmsghdr(cmsg, msg) {
  1847. if (!CMSG_OK(msg, cmsg))
  1848. return -EINVAL;
  1849. if (cmsg->cmsg_level != SOL_SOCKET)
  1850. continue;
  1851. ret = __sock_cmsg_send(sk, msg, cmsg, sockc);
  1852. if (ret)
  1853. return ret;
  1854. }
  1855. return 0;
  1856. }
  1857. EXPORT_SYMBOL(sock_cmsg_send);
  1858. static void sk_enter_memory_pressure(struct sock *sk)
  1859. {
  1860. if (!sk->sk_prot->enter_memory_pressure)
  1861. return;
  1862. sk->sk_prot->enter_memory_pressure(sk);
  1863. }
  1864. static void sk_leave_memory_pressure(struct sock *sk)
  1865. {
  1866. if (sk->sk_prot->leave_memory_pressure) {
  1867. sk->sk_prot->leave_memory_pressure(sk);
  1868. } else {
  1869. unsigned long *memory_pressure = sk->sk_prot->memory_pressure;
  1870. if (memory_pressure && *memory_pressure)
  1871. *memory_pressure = 0;
  1872. }
  1873. }
  1874. /* On 32bit arches, an skb frag is limited to 2^15 */
  1875. #define SKB_FRAG_PAGE_ORDER get_order(32768)
  1876. /**
  1877. * skb_page_frag_refill - check that a page_frag contains enough room
  1878. * @sz: minimum size of the fragment we want to get
  1879. * @pfrag: pointer to page_frag
  1880. * @gfp: priority for memory allocation
  1881. *
  1882. * Note: While this allocator tries to use high order pages, there is
  1883. * no guarantee that allocations succeed. Therefore, @sz MUST be
  1884. * less or equal than PAGE_SIZE.
  1885. */
  1886. bool skb_page_frag_refill(unsigned int sz, struct page_frag *pfrag, gfp_t gfp)
  1887. {
  1888. if (pfrag->page) {
  1889. if (page_ref_count(pfrag->page) == 1) {
  1890. pfrag->offset = 0;
  1891. return true;
  1892. }
  1893. if (pfrag->offset + sz <= pfrag->size)
  1894. return true;
  1895. put_page(pfrag->page);
  1896. }
  1897. pfrag->offset = 0;
  1898. if (SKB_FRAG_PAGE_ORDER) {
  1899. /* Avoid direct reclaim but allow kswapd to wake */
  1900. pfrag->page = alloc_pages((gfp & ~__GFP_DIRECT_RECLAIM) |
  1901. __GFP_COMP | __GFP_NOWARN |
  1902. __GFP_NORETRY,
  1903. SKB_FRAG_PAGE_ORDER);
  1904. if (likely(pfrag->page)) {
  1905. pfrag->size = PAGE_SIZE << SKB_FRAG_PAGE_ORDER;
  1906. return true;
  1907. }
  1908. }
  1909. pfrag->page = alloc_page(gfp);
  1910. if (likely(pfrag->page)) {
  1911. pfrag->size = PAGE_SIZE;
  1912. return true;
  1913. }
  1914. return false;
  1915. }
  1916. EXPORT_SYMBOL(skb_page_frag_refill);
  1917. bool sk_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
  1918. {
  1919. if (likely(skb_page_frag_refill(32U, pfrag, sk->sk_allocation)))
  1920. return true;
  1921. sk_enter_memory_pressure(sk);
  1922. sk_stream_moderate_sndbuf(sk);
  1923. return false;
  1924. }
  1925. EXPORT_SYMBOL(sk_page_frag_refill);
  1926. static void __lock_sock(struct sock *sk)
  1927. __releases(&sk->sk_lock.slock)
  1928. __acquires(&sk->sk_lock.slock)
  1929. {
  1930. DEFINE_WAIT(wait);
  1931. for (;;) {
  1932. prepare_to_wait_exclusive(&sk->sk_lock.wq, &wait,
  1933. TASK_UNINTERRUPTIBLE);
  1934. spin_unlock_bh(&sk->sk_lock.slock);
  1935. schedule();
  1936. spin_lock_bh(&sk->sk_lock.slock);
  1937. if (!sock_owned_by_user(sk))
  1938. break;
  1939. }
  1940. finish_wait(&sk->sk_lock.wq, &wait);
  1941. }
  1942. static void __release_sock(struct sock *sk)
  1943. __releases(&sk->sk_lock.slock)
  1944. __acquires(&sk->sk_lock.slock)
  1945. {
  1946. struct sk_buff *skb, *next;
  1947. while ((skb = sk->sk_backlog.head) != NULL) {
  1948. sk->sk_backlog.head = sk->sk_backlog.tail = NULL;
  1949. spin_unlock_bh(&sk->sk_lock.slock);
  1950. do {
  1951. next = skb->next;
  1952. prefetch(next);
  1953. WARN_ON_ONCE(skb_dst_is_noref(skb));
  1954. skb->next = NULL;
  1955. sk_backlog_rcv(sk, skb);
  1956. cond_resched();
  1957. skb = next;
  1958. } while (skb != NULL);
  1959. spin_lock_bh(&sk->sk_lock.slock);
  1960. }
  1961. /*
  1962. * Doing the zeroing here guarantee we can not loop forever
  1963. * while a wild producer attempts to flood us.
  1964. */
  1965. sk->sk_backlog.len = 0;
  1966. }
  1967. void __sk_flush_backlog(struct sock *sk)
  1968. {
  1969. spin_lock_bh(&sk->sk_lock.slock);
  1970. __release_sock(sk);
  1971. spin_unlock_bh(&sk->sk_lock.slock);
  1972. }
  1973. /**
  1974. * sk_wait_data - wait for data to arrive at sk_receive_queue
  1975. * @sk: sock to wait on
  1976. * @timeo: for how long
  1977. * @skb: last skb seen on sk_receive_queue
  1978. *
  1979. * Now socket state including sk->sk_err is changed only under lock,
  1980. * hence we may omit checks after joining wait queue.
  1981. * We check receive queue before schedule() only as optimization;
  1982. * it is very likely that release_sock() added new data.
  1983. */
  1984. int sk_wait_data(struct sock *sk, long *timeo, const struct sk_buff *skb)
  1985. {
  1986. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  1987. int rc;
  1988. add_wait_queue(sk_sleep(sk), &wait);
  1989. sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  1990. rc = sk_wait_event(sk, timeo, skb_peek_tail(&sk->sk_receive_queue) != skb, &wait);
  1991. sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  1992. remove_wait_queue(sk_sleep(sk), &wait);
  1993. return rc;
  1994. }
  1995. EXPORT_SYMBOL(sk_wait_data);
  1996. /**
  1997. * __sk_mem_raise_allocated - increase memory_allocated
  1998. * @sk: socket
  1999. * @size: memory size to allocate
  2000. * @amt: pages to allocate
  2001. * @kind: allocation type
  2002. *
  2003. * Similar to __sk_mem_schedule(), but does not update sk_forward_alloc
  2004. */
  2005. int __sk_mem_raise_allocated(struct sock *sk, int size, int amt, int kind)
  2006. {
  2007. struct proto *prot = sk->sk_prot;
  2008. long allocated = sk_memory_allocated_add(sk, amt);
  2009. if (mem_cgroup_sockets_enabled && sk->sk_memcg &&
  2010. !mem_cgroup_charge_skmem(sk->sk_memcg, amt))
  2011. goto suppress_allocation;
  2012. /* Under limit. */
  2013. if (allocated <= sk_prot_mem_limits(sk, 0)) {
  2014. sk_leave_memory_pressure(sk);
  2015. return 1;
  2016. }
  2017. /* Under pressure. */
  2018. if (allocated > sk_prot_mem_limits(sk, 1))
  2019. sk_enter_memory_pressure(sk);
  2020. /* Over hard limit. */
  2021. if (allocated > sk_prot_mem_limits(sk, 2))
  2022. goto suppress_allocation;
  2023. /* guarantee minimum buffer size under pressure */
  2024. if (kind == SK_MEM_RECV) {
  2025. if (atomic_read(&sk->sk_rmem_alloc) < sk_get_rmem0(sk, prot))
  2026. return 1;
  2027. } else { /* SK_MEM_SEND */
  2028. int wmem0 = sk_get_wmem0(sk, prot);
  2029. if (sk->sk_type == SOCK_STREAM) {
  2030. if (sk->sk_wmem_queued < wmem0)
  2031. return 1;
  2032. } else if (refcount_read(&sk->sk_wmem_alloc) < wmem0) {
  2033. return 1;
  2034. }
  2035. }
  2036. if (sk_has_memory_pressure(sk)) {
  2037. int alloc;
  2038. if (!sk_under_memory_pressure(sk))
  2039. return 1;
  2040. alloc = sk_sockets_allocated_read_positive(sk);
  2041. if (sk_prot_mem_limits(sk, 2) > alloc *
  2042. sk_mem_pages(sk->sk_wmem_queued +
  2043. atomic_read(&sk->sk_rmem_alloc) +
  2044. sk->sk_forward_alloc))
  2045. return 1;
  2046. }
  2047. suppress_allocation:
  2048. if (kind == SK_MEM_SEND && sk->sk_type == SOCK_STREAM) {
  2049. sk_stream_moderate_sndbuf(sk);
  2050. /* Fail only if socket is _under_ its sndbuf.
  2051. * In this case we cannot block, so that we have to fail.
  2052. */
  2053. if (sk->sk_wmem_queued + size >= sk->sk_sndbuf)
  2054. return 1;
  2055. }
  2056. trace_sock_exceed_buf_limit(sk, prot, allocated);
  2057. sk_memory_allocated_sub(sk, amt);
  2058. if (mem_cgroup_sockets_enabled && sk->sk_memcg)
  2059. mem_cgroup_uncharge_skmem(sk->sk_memcg, amt);
  2060. return 0;
  2061. }
  2062. EXPORT_SYMBOL(__sk_mem_raise_allocated);
  2063. /**
  2064. * __sk_mem_schedule - increase sk_forward_alloc and memory_allocated
  2065. * @sk: socket
  2066. * @size: memory size to allocate
  2067. * @kind: allocation type
  2068. *
  2069. * If kind is SK_MEM_SEND, it means wmem allocation. Otherwise it means
  2070. * rmem allocation. This function assumes that protocols which have
  2071. * memory_pressure use sk_wmem_queued as write buffer accounting.
  2072. */
  2073. int __sk_mem_schedule(struct sock *sk, int size, int kind)
  2074. {
  2075. int ret, amt = sk_mem_pages(size);
  2076. sk->sk_forward_alloc += amt << SK_MEM_QUANTUM_SHIFT;
  2077. ret = __sk_mem_raise_allocated(sk, size, amt, kind);
  2078. if (!ret)
  2079. sk->sk_forward_alloc -= amt << SK_MEM_QUANTUM_SHIFT;
  2080. return ret;
  2081. }
  2082. EXPORT_SYMBOL(__sk_mem_schedule);
  2083. /**
  2084. * __sk_mem_reduce_allocated - reclaim memory_allocated
  2085. * @sk: socket
  2086. * @amount: number of quanta
  2087. *
  2088. * Similar to __sk_mem_reclaim(), but does not update sk_forward_alloc
  2089. */
  2090. void __sk_mem_reduce_allocated(struct sock *sk, int amount)
  2091. {
  2092. sk_memory_allocated_sub(sk, amount);
  2093. if (mem_cgroup_sockets_enabled && sk->sk_memcg)
  2094. mem_cgroup_uncharge_skmem(sk->sk_memcg, amount);
  2095. if (sk_under_memory_pressure(sk) &&
  2096. (sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0)))
  2097. sk_leave_memory_pressure(sk);
  2098. }
  2099. EXPORT_SYMBOL(__sk_mem_reduce_allocated);
  2100. /**
  2101. * __sk_mem_reclaim - reclaim sk_forward_alloc and memory_allocated
  2102. * @sk: socket
  2103. * @amount: number of bytes (rounded down to a SK_MEM_QUANTUM multiple)
  2104. */
  2105. void __sk_mem_reclaim(struct sock *sk, int amount)
  2106. {
  2107. amount >>= SK_MEM_QUANTUM_SHIFT;
  2108. sk->sk_forward_alloc -= amount << SK_MEM_QUANTUM_SHIFT;
  2109. __sk_mem_reduce_allocated(sk, amount);
  2110. }
  2111. EXPORT_SYMBOL(__sk_mem_reclaim);
  2112. int sk_set_peek_off(struct sock *sk, int val)
  2113. {
  2114. sk->sk_peek_off = val;
  2115. return 0;
  2116. }
  2117. EXPORT_SYMBOL_GPL(sk_set_peek_off);
  2118. /*
  2119. * Set of default routines for initialising struct proto_ops when
  2120. * the protocol does not support a particular function. In certain
  2121. * cases where it makes no sense for a protocol to have a "do nothing"
  2122. * function, some default processing is provided.
  2123. */
  2124. int sock_no_bind(struct socket *sock, struct sockaddr *saddr, int len)
  2125. {
  2126. return -EOPNOTSUPP;
  2127. }
  2128. EXPORT_SYMBOL(sock_no_bind);
  2129. int sock_no_connect(struct socket *sock, struct sockaddr *saddr,
  2130. int len, int flags)
  2131. {
  2132. return -EOPNOTSUPP;
  2133. }
  2134. EXPORT_SYMBOL(sock_no_connect);
  2135. int sock_no_socketpair(struct socket *sock1, struct socket *sock2)
  2136. {
  2137. return -EOPNOTSUPP;
  2138. }
  2139. EXPORT_SYMBOL(sock_no_socketpair);
  2140. int sock_no_accept(struct socket *sock, struct socket *newsock, int flags,
  2141. bool kern)
  2142. {
  2143. return -EOPNOTSUPP;
  2144. }
  2145. EXPORT_SYMBOL(sock_no_accept);
  2146. int sock_no_getname(struct socket *sock, struct sockaddr *saddr,
  2147. int peer)
  2148. {
  2149. return -EOPNOTSUPP;
  2150. }
  2151. EXPORT_SYMBOL(sock_no_getname);
  2152. __poll_t sock_no_poll(struct file *file, struct socket *sock, poll_table *pt)
  2153. {
  2154. return 0;
  2155. }
  2156. EXPORT_SYMBOL(sock_no_poll);
  2157. int sock_no_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  2158. {
  2159. return -EOPNOTSUPP;
  2160. }
  2161. EXPORT_SYMBOL(sock_no_ioctl);
  2162. int sock_no_listen(struct socket *sock, int backlog)
  2163. {
  2164. return -EOPNOTSUPP;
  2165. }
  2166. EXPORT_SYMBOL(sock_no_listen);
  2167. int sock_no_shutdown(struct socket *sock, int how)
  2168. {
  2169. return -EOPNOTSUPP;
  2170. }
  2171. EXPORT_SYMBOL(sock_no_shutdown);
  2172. int sock_no_setsockopt(struct socket *sock, int level, int optname,
  2173. char __user *optval, unsigned int optlen)
  2174. {
  2175. return -EOPNOTSUPP;
  2176. }
  2177. EXPORT_SYMBOL(sock_no_setsockopt);
  2178. int sock_no_getsockopt(struct socket *sock, int level, int optname,
  2179. char __user *optval, int __user *optlen)
  2180. {
  2181. return -EOPNOTSUPP;
  2182. }
  2183. EXPORT_SYMBOL(sock_no_getsockopt);
  2184. int sock_no_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
  2185. {
  2186. return -EOPNOTSUPP;
  2187. }
  2188. EXPORT_SYMBOL(sock_no_sendmsg);
  2189. int sock_no_sendmsg_locked(struct sock *sk, struct msghdr *m, size_t len)
  2190. {
  2191. return -EOPNOTSUPP;
  2192. }
  2193. EXPORT_SYMBOL(sock_no_sendmsg_locked);
  2194. int sock_no_recvmsg(struct socket *sock, struct msghdr *m, size_t len,
  2195. int flags)
  2196. {
  2197. return -EOPNOTSUPP;
  2198. }
  2199. EXPORT_SYMBOL(sock_no_recvmsg);
  2200. int sock_no_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
  2201. {
  2202. /* Mirror missing mmap method error code */
  2203. return -ENODEV;
  2204. }
  2205. EXPORT_SYMBOL(sock_no_mmap);
  2206. ssize_t sock_no_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
  2207. {
  2208. ssize_t res;
  2209. struct msghdr msg = {.msg_flags = flags};
  2210. struct kvec iov;
  2211. char *kaddr = kmap(page);
  2212. iov.iov_base = kaddr + offset;
  2213. iov.iov_len = size;
  2214. res = kernel_sendmsg(sock, &msg, &iov, 1, size);
  2215. kunmap(page);
  2216. return res;
  2217. }
  2218. EXPORT_SYMBOL(sock_no_sendpage);
  2219. ssize_t sock_no_sendpage_locked(struct sock *sk, struct page *page,
  2220. int offset, size_t size, int flags)
  2221. {
  2222. ssize_t res;
  2223. struct msghdr msg = {.msg_flags = flags};
  2224. struct kvec iov;
  2225. char *kaddr = kmap(page);
  2226. iov.iov_base = kaddr + offset;
  2227. iov.iov_len = size;
  2228. res = kernel_sendmsg_locked(sk, &msg, &iov, 1, size);
  2229. kunmap(page);
  2230. return res;
  2231. }
  2232. EXPORT_SYMBOL(sock_no_sendpage_locked);
  2233. /*
  2234. * Default Socket Callbacks
  2235. */
  2236. static void sock_def_wakeup(struct sock *sk)
  2237. {
  2238. struct socket_wq *wq;
  2239. rcu_read_lock();
  2240. wq = rcu_dereference(sk->sk_wq);
  2241. if (skwq_has_sleeper(wq))
  2242. wake_up_interruptible_all(&wq->wait);
  2243. rcu_read_unlock();
  2244. }
  2245. static void sock_def_error_report(struct sock *sk)
  2246. {
  2247. struct socket_wq *wq;
  2248. rcu_read_lock();
  2249. wq = rcu_dereference(sk->sk_wq);
  2250. if (skwq_has_sleeper(wq))
  2251. wake_up_interruptible_poll(&wq->wait, EPOLLERR);
  2252. sk_wake_async(sk, SOCK_WAKE_IO, POLL_ERR);
  2253. rcu_read_unlock();
  2254. }
  2255. static void sock_def_readable(struct sock *sk)
  2256. {
  2257. struct socket_wq *wq;
  2258. rcu_read_lock();
  2259. wq = rcu_dereference(sk->sk_wq);
  2260. if (skwq_has_sleeper(wq))
  2261. wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN | EPOLLPRI |
  2262. EPOLLRDNORM | EPOLLRDBAND);
  2263. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
  2264. rcu_read_unlock();
  2265. }
  2266. static void sock_def_write_space(struct sock *sk)
  2267. {
  2268. struct socket_wq *wq;
  2269. rcu_read_lock();
  2270. /* Do not wake up a writer until he can make "significant"
  2271. * progress. --DaveM
  2272. */
  2273. if ((refcount_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf) {
  2274. wq = rcu_dereference(sk->sk_wq);
  2275. if (skwq_has_sleeper(wq))
  2276. wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
  2277. EPOLLWRNORM | EPOLLWRBAND);
  2278. /* Should agree with poll, otherwise some programs break */
  2279. if (sock_writeable(sk))
  2280. sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
  2281. }
  2282. rcu_read_unlock();
  2283. }
  2284. static void sock_def_destruct(struct sock *sk)
  2285. {
  2286. }
  2287. void sk_send_sigurg(struct sock *sk)
  2288. {
  2289. if (sk->sk_socket && sk->sk_socket->file)
  2290. if (send_sigurg(&sk->sk_socket->file->f_owner))
  2291. sk_wake_async(sk, SOCK_WAKE_URG, POLL_PRI);
  2292. }
  2293. EXPORT_SYMBOL(sk_send_sigurg);
  2294. void sk_reset_timer(struct sock *sk, struct timer_list* timer,
  2295. unsigned long expires)
  2296. {
  2297. if (!mod_timer(timer, expires))
  2298. sock_hold(sk);
  2299. }
  2300. EXPORT_SYMBOL(sk_reset_timer);
  2301. void sk_stop_timer(struct sock *sk, struct timer_list* timer)
  2302. {
  2303. if (del_timer(timer))
  2304. __sock_put(sk);
  2305. }
  2306. EXPORT_SYMBOL(sk_stop_timer);
  2307. void sock_init_data(struct socket *sock, struct sock *sk)
  2308. {
  2309. sk_init_common(sk);
  2310. sk->sk_send_head = NULL;
  2311. timer_setup(&sk->sk_timer, NULL, 0);
  2312. sk->sk_allocation = GFP_KERNEL;
  2313. sk->sk_rcvbuf = sysctl_rmem_default;
  2314. sk->sk_sndbuf = sysctl_wmem_default;
  2315. sk->sk_state = TCP_CLOSE;
  2316. sk_set_socket(sk, sock);
  2317. sock_set_flag(sk, SOCK_ZAPPED);
  2318. if (sock) {
  2319. sk->sk_type = sock->type;
  2320. sk->sk_wq = sock->wq;
  2321. sock->sk = sk;
  2322. sk->sk_uid = SOCK_INODE(sock)->i_uid;
  2323. } else {
  2324. sk->sk_wq = NULL;
  2325. sk->sk_uid = make_kuid(sock_net(sk)->user_ns, 0);
  2326. }
  2327. rwlock_init(&sk->sk_callback_lock);
  2328. if (sk->sk_kern_sock)
  2329. lockdep_set_class_and_name(
  2330. &sk->sk_callback_lock,
  2331. af_kern_callback_keys + sk->sk_family,
  2332. af_family_kern_clock_key_strings[sk->sk_family]);
  2333. else
  2334. lockdep_set_class_and_name(
  2335. &sk->sk_callback_lock,
  2336. af_callback_keys + sk->sk_family,
  2337. af_family_clock_key_strings[sk->sk_family]);
  2338. sk->sk_state_change = sock_def_wakeup;
  2339. sk->sk_data_ready = sock_def_readable;
  2340. sk->sk_write_space = sock_def_write_space;
  2341. sk->sk_error_report = sock_def_error_report;
  2342. sk->sk_destruct = sock_def_destruct;
  2343. sk->sk_frag.page = NULL;
  2344. sk->sk_frag.offset = 0;
  2345. sk->sk_peek_off = -1;
  2346. sk->sk_peer_pid = NULL;
  2347. sk->sk_peer_cred = NULL;
  2348. sk->sk_write_pending = 0;
  2349. sk->sk_rcvlowat = 1;
  2350. sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
  2351. sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
  2352. sk->sk_stamp = SK_DEFAULT_STAMP;
  2353. atomic_set(&sk->sk_zckey, 0);
  2354. #ifdef CONFIG_NET_RX_BUSY_POLL
  2355. sk->sk_napi_id = 0;
  2356. sk->sk_ll_usec = sysctl_net_busy_read;
  2357. #endif
  2358. sk->sk_max_pacing_rate = ~0U;
  2359. sk->sk_pacing_rate = ~0U;
  2360. sk->sk_pacing_shift = 10;
  2361. sk->sk_incoming_cpu = -1;
  2362. /*
  2363. * Before updating sk_refcnt, we must commit prior changes to memory
  2364. * (Documentation/RCU/rculist_nulls.txt for details)
  2365. */
  2366. smp_wmb();
  2367. refcount_set(&sk->sk_refcnt, 1);
  2368. atomic_set(&sk->sk_drops, 0);
  2369. }
  2370. EXPORT_SYMBOL(sock_init_data);
  2371. void lock_sock_nested(struct sock *sk, int subclass)
  2372. {
  2373. might_sleep();
  2374. spin_lock_bh(&sk->sk_lock.slock);
  2375. if (sk->sk_lock.owned)
  2376. __lock_sock(sk);
  2377. sk->sk_lock.owned = 1;
  2378. spin_unlock(&sk->sk_lock.slock);
  2379. /*
  2380. * The sk_lock has mutex_lock() semantics here:
  2381. */
  2382. mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_);
  2383. local_bh_enable();
  2384. }
  2385. EXPORT_SYMBOL(lock_sock_nested);
  2386. void release_sock(struct sock *sk)
  2387. {
  2388. spin_lock_bh(&sk->sk_lock.slock);
  2389. if (sk->sk_backlog.tail)
  2390. __release_sock(sk);
  2391. /* Warning : release_cb() might need to release sk ownership,
  2392. * ie call sock_release_ownership(sk) before us.
  2393. */
  2394. if (sk->sk_prot->release_cb)
  2395. sk->sk_prot->release_cb(sk);
  2396. sock_release_ownership(sk);
  2397. if (waitqueue_active(&sk->sk_lock.wq))
  2398. wake_up(&sk->sk_lock.wq);
  2399. spin_unlock_bh(&sk->sk_lock.slock);
  2400. }
  2401. EXPORT_SYMBOL(release_sock);
  2402. /**
  2403. * lock_sock_fast - fast version of lock_sock
  2404. * @sk: socket
  2405. *
  2406. * This version should be used for very small section, where process wont block
  2407. * return false if fast path is taken:
  2408. *
  2409. * sk_lock.slock locked, owned = 0, BH disabled
  2410. *
  2411. * return true if slow path is taken:
  2412. *
  2413. * sk_lock.slock unlocked, owned = 1, BH enabled
  2414. */
  2415. bool lock_sock_fast(struct sock *sk)
  2416. {
  2417. might_sleep();
  2418. spin_lock_bh(&sk->sk_lock.slock);
  2419. if (!sk->sk_lock.owned)
  2420. /*
  2421. * Note : We must disable BH
  2422. */
  2423. return false;
  2424. __lock_sock(sk);
  2425. sk->sk_lock.owned = 1;
  2426. spin_unlock(&sk->sk_lock.slock);
  2427. /*
  2428. * The sk_lock has mutex_lock() semantics here:
  2429. */
  2430. mutex_acquire(&sk->sk_lock.dep_map, 0, 0, _RET_IP_);
  2431. local_bh_enable();
  2432. return true;
  2433. }
  2434. EXPORT_SYMBOL(lock_sock_fast);
  2435. int sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
  2436. {
  2437. struct timeval tv;
  2438. if (!sock_flag(sk, SOCK_TIMESTAMP))
  2439. sock_enable_timestamp(sk, SOCK_TIMESTAMP);
  2440. tv = ktime_to_timeval(sk->sk_stamp);
  2441. if (tv.tv_sec == -1)
  2442. return -ENOENT;
  2443. if (tv.tv_sec == 0) {
  2444. sk->sk_stamp = ktime_get_real();
  2445. tv = ktime_to_timeval(sk->sk_stamp);
  2446. }
  2447. return copy_to_user(userstamp, &tv, sizeof(tv)) ? -EFAULT : 0;
  2448. }
  2449. EXPORT_SYMBOL(sock_get_timestamp);
  2450. int sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
  2451. {
  2452. struct timespec ts;
  2453. if (!sock_flag(sk, SOCK_TIMESTAMP))
  2454. sock_enable_timestamp(sk, SOCK_TIMESTAMP);
  2455. ts = ktime_to_timespec(sk->sk_stamp);
  2456. if (ts.tv_sec == -1)
  2457. return -ENOENT;
  2458. if (ts.tv_sec == 0) {
  2459. sk->sk_stamp = ktime_get_real();
  2460. ts = ktime_to_timespec(sk->sk_stamp);
  2461. }
  2462. return copy_to_user(userstamp, &ts, sizeof(ts)) ? -EFAULT : 0;
  2463. }
  2464. EXPORT_SYMBOL(sock_get_timestampns);
  2465. void sock_enable_timestamp(struct sock *sk, int flag)
  2466. {
  2467. if (!sock_flag(sk, flag)) {
  2468. unsigned long previous_flags = sk->sk_flags;
  2469. sock_set_flag(sk, flag);
  2470. /*
  2471. * we just set one of the two flags which require net
  2472. * time stamping, but time stamping might have been on
  2473. * already because of the other one
  2474. */
  2475. if (sock_needs_netstamp(sk) &&
  2476. !(previous_flags & SK_FLAGS_TIMESTAMP))
  2477. net_enable_timestamp();
  2478. }
  2479. }
  2480. int sock_recv_errqueue(struct sock *sk, struct msghdr *msg, int len,
  2481. int level, int type)
  2482. {
  2483. struct sock_exterr_skb *serr;
  2484. struct sk_buff *skb;
  2485. int copied, err;
  2486. err = -EAGAIN;
  2487. skb = sock_dequeue_err_skb(sk);
  2488. if (skb == NULL)
  2489. goto out;
  2490. copied = skb->len;
  2491. if (copied > len) {
  2492. msg->msg_flags |= MSG_TRUNC;
  2493. copied = len;
  2494. }
  2495. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  2496. if (err)
  2497. goto out_free_skb;
  2498. sock_recv_timestamp(msg, sk, skb);
  2499. serr = SKB_EXT_ERR(skb);
  2500. put_cmsg(msg, level, type, sizeof(serr->ee), &serr->ee);
  2501. msg->msg_flags |= MSG_ERRQUEUE;
  2502. err = copied;
  2503. out_free_skb:
  2504. kfree_skb(skb);
  2505. out:
  2506. return err;
  2507. }
  2508. EXPORT_SYMBOL(sock_recv_errqueue);
  2509. /*
  2510. * Get a socket option on an socket.
  2511. *
  2512. * FIX: POSIX 1003.1g is very ambiguous here. It states that
  2513. * asynchronous errors should be reported by getsockopt. We assume
  2514. * this means if you specify SO_ERROR (otherwise whats the point of it).
  2515. */
  2516. int sock_common_getsockopt(struct socket *sock, int level, int optname,
  2517. char __user *optval, int __user *optlen)
  2518. {
  2519. struct sock *sk = sock->sk;
  2520. return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
  2521. }
  2522. EXPORT_SYMBOL(sock_common_getsockopt);
  2523. #ifdef CONFIG_COMPAT
  2524. int compat_sock_common_getsockopt(struct socket *sock, int level, int optname,
  2525. char __user *optval, int __user *optlen)
  2526. {
  2527. struct sock *sk = sock->sk;
  2528. if (sk->sk_prot->compat_getsockopt != NULL)
  2529. return sk->sk_prot->compat_getsockopt(sk, level, optname,
  2530. optval, optlen);
  2531. return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
  2532. }
  2533. EXPORT_SYMBOL(compat_sock_common_getsockopt);
  2534. #endif
  2535. int sock_common_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
  2536. int flags)
  2537. {
  2538. struct sock *sk = sock->sk;
  2539. int addr_len = 0;
  2540. int err;
  2541. err = sk->sk_prot->recvmsg(sk, msg, size, flags & MSG_DONTWAIT,
  2542. flags & ~MSG_DONTWAIT, &addr_len);
  2543. if (err >= 0)
  2544. msg->msg_namelen = addr_len;
  2545. return err;
  2546. }
  2547. EXPORT_SYMBOL(sock_common_recvmsg);
  2548. /*
  2549. * Set socket options on an inet socket.
  2550. */
  2551. int sock_common_setsockopt(struct socket *sock, int level, int optname,
  2552. char __user *optval, unsigned int optlen)
  2553. {
  2554. struct sock *sk = sock->sk;
  2555. return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
  2556. }
  2557. EXPORT_SYMBOL(sock_common_setsockopt);
  2558. #ifdef CONFIG_COMPAT
  2559. int compat_sock_common_setsockopt(struct socket *sock, int level, int optname,
  2560. char __user *optval, unsigned int optlen)
  2561. {
  2562. struct sock *sk = sock->sk;
  2563. if (sk->sk_prot->compat_setsockopt != NULL)
  2564. return sk->sk_prot->compat_setsockopt(sk, level, optname,
  2565. optval, optlen);
  2566. return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
  2567. }
  2568. EXPORT_SYMBOL(compat_sock_common_setsockopt);
  2569. #endif
  2570. void sk_common_release(struct sock *sk)
  2571. {
  2572. if (sk->sk_prot->destroy)
  2573. sk->sk_prot->destroy(sk);
  2574. /*
  2575. * Observation: when sock_common_release is called, processes have
  2576. * no access to socket. But net still has.
  2577. * Step one, detach it from networking:
  2578. *
  2579. * A. Remove from hash tables.
  2580. */
  2581. sk->sk_prot->unhash(sk);
  2582. /*
  2583. * In this point socket cannot receive new packets, but it is possible
  2584. * that some packets are in flight because some CPU runs receiver and
  2585. * did hash table lookup before we unhashed socket. They will achieve
  2586. * receive queue and will be purged by socket destructor.
  2587. *
  2588. * Also we still have packets pending on receive queue and probably,
  2589. * our own packets waiting in device queues. sock_destroy will drain
  2590. * receive queue, but transmitted packets will delay socket destruction
  2591. * until the last reference will be released.
  2592. */
  2593. sock_orphan(sk);
  2594. xfrm_sk_free_policy(sk);
  2595. sk_refcnt_debug_release(sk);
  2596. sock_put(sk);
  2597. }
  2598. EXPORT_SYMBOL(sk_common_release);
  2599. void sk_get_meminfo(const struct sock *sk, u32 *mem)
  2600. {
  2601. memset(mem, 0, sizeof(*mem) * SK_MEMINFO_VARS);
  2602. mem[SK_MEMINFO_RMEM_ALLOC] = sk_rmem_alloc_get(sk);
  2603. mem[SK_MEMINFO_RCVBUF] = sk->sk_rcvbuf;
  2604. mem[SK_MEMINFO_WMEM_ALLOC] = sk_wmem_alloc_get(sk);
  2605. mem[SK_MEMINFO_SNDBUF] = sk->sk_sndbuf;
  2606. mem[SK_MEMINFO_FWD_ALLOC] = sk->sk_forward_alloc;
  2607. mem[SK_MEMINFO_WMEM_QUEUED] = sk->sk_wmem_queued;
  2608. mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc);
  2609. mem[SK_MEMINFO_BACKLOG] = sk->sk_backlog.len;
  2610. mem[SK_MEMINFO_DROPS] = atomic_read(&sk->sk_drops);
  2611. }
  2612. #ifdef CONFIG_PROC_FS
  2613. #define PROTO_INUSE_NR 64 /* should be enough for the first time */
  2614. struct prot_inuse {
  2615. int val[PROTO_INUSE_NR];
  2616. };
  2617. static DECLARE_BITMAP(proto_inuse_idx, PROTO_INUSE_NR);
  2618. void sock_prot_inuse_add(struct net *net, struct proto *prot, int val)
  2619. {
  2620. __this_cpu_add(net->core.prot_inuse->val[prot->inuse_idx], val);
  2621. }
  2622. EXPORT_SYMBOL_GPL(sock_prot_inuse_add);
  2623. int sock_prot_inuse_get(struct net *net, struct proto *prot)
  2624. {
  2625. int cpu, idx = prot->inuse_idx;
  2626. int res = 0;
  2627. for_each_possible_cpu(cpu)
  2628. res += per_cpu_ptr(net->core.prot_inuse, cpu)->val[idx];
  2629. return res >= 0 ? res : 0;
  2630. }
  2631. EXPORT_SYMBOL_GPL(sock_prot_inuse_get);
  2632. static void sock_inuse_add(struct net *net, int val)
  2633. {
  2634. this_cpu_add(*net->core.sock_inuse, val);
  2635. }
  2636. int sock_inuse_get(struct net *net)
  2637. {
  2638. int cpu, res = 0;
  2639. for_each_possible_cpu(cpu)
  2640. res += *per_cpu_ptr(net->core.sock_inuse, cpu);
  2641. return res;
  2642. }
  2643. EXPORT_SYMBOL_GPL(sock_inuse_get);
  2644. static int __net_init sock_inuse_init_net(struct net *net)
  2645. {
  2646. net->core.prot_inuse = alloc_percpu(struct prot_inuse);
  2647. if (net->core.prot_inuse == NULL)
  2648. return -ENOMEM;
  2649. net->core.sock_inuse = alloc_percpu(int);
  2650. if (net->core.sock_inuse == NULL)
  2651. goto out;
  2652. return 0;
  2653. out:
  2654. free_percpu(net->core.prot_inuse);
  2655. return -ENOMEM;
  2656. }
  2657. static void __net_exit sock_inuse_exit_net(struct net *net)
  2658. {
  2659. free_percpu(net->core.prot_inuse);
  2660. free_percpu(net->core.sock_inuse);
  2661. }
  2662. static struct pernet_operations net_inuse_ops = {
  2663. .init = sock_inuse_init_net,
  2664. .exit = sock_inuse_exit_net,
  2665. .async = true,
  2666. };
  2667. static __init int net_inuse_init(void)
  2668. {
  2669. if (register_pernet_subsys(&net_inuse_ops))
  2670. panic("Cannot initialize net inuse counters");
  2671. return 0;
  2672. }
  2673. core_initcall(net_inuse_init);
  2674. static void assign_proto_idx(struct proto *prot)
  2675. {
  2676. prot->inuse_idx = find_first_zero_bit(proto_inuse_idx, PROTO_INUSE_NR);
  2677. if (unlikely(prot->inuse_idx == PROTO_INUSE_NR - 1)) {
  2678. pr_err("PROTO_INUSE_NR exhausted\n");
  2679. return;
  2680. }
  2681. set_bit(prot->inuse_idx, proto_inuse_idx);
  2682. }
  2683. static void release_proto_idx(struct proto *prot)
  2684. {
  2685. if (prot->inuse_idx != PROTO_INUSE_NR - 1)
  2686. clear_bit(prot->inuse_idx, proto_inuse_idx);
  2687. }
  2688. #else
  2689. static inline void assign_proto_idx(struct proto *prot)
  2690. {
  2691. }
  2692. static inline void release_proto_idx(struct proto *prot)
  2693. {
  2694. }
  2695. static void sock_inuse_add(struct net *net, int val)
  2696. {
  2697. }
  2698. #endif
  2699. static void req_prot_cleanup(struct request_sock_ops *rsk_prot)
  2700. {
  2701. if (!rsk_prot)
  2702. return;
  2703. kfree(rsk_prot->slab_name);
  2704. rsk_prot->slab_name = NULL;
  2705. kmem_cache_destroy(rsk_prot->slab);
  2706. rsk_prot->slab = NULL;
  2707. }
  2708. static int req_prot_init(const struct proto *prot)
  2709. {
  2710. struct request_sock_ops *rsk_prot = prot->rsk_prot;
  2711. if (!rsk_prot)
  2712. return 0;
  2713. rsk_prot->slab_name = kasprintf(GFP_KERNEL, "request_sock_%s",
  2714. prot->name);
  2715. if (!rsk_prot->slab_name)
  2716. return -ENOMEM;
  2717. rsk_prot->slab = kmem_cache_create(rsk_prot->slab_name,
  2718. rsk_prot->obj_size, 0,
  2719. prot->slab_flags, NULL);
  2720. if (!rsk_prot->slab) {
  2721. pr_crit("%s: Can't create request sock SLAB cache!\n",
  2722. prot->name);
  2723. return -ENOMEM;
  2724. }
  2725. return 0;
  2726. }
  2727. int proto_register(struct proto *prot, int alloc_slab)
  2728. {
  2729. if (alloc_slab) {
  2730. prot->slab = kmem_cache_create_usercopy(prot->name,
  2731. prot->obj_size, 0,
  2732. SLAB_HWCACHE_ALIGN | prot->slab_flags,
  2733. prot->useroffset, prot->usersize,
  2734. NULL);
  2735. if (prot->slab == NULL) {
  2736. pr_crit("%s: Can't create sock SLAB cache!\n",
  2737. prot->name);
  2738. goto out;
  2739. }
  2740. if (req_prot_init(prot))
  2741. goto out_free_request_sock_slab;
  2742. if (prot->twsk_prot != NULL) {
  2743. prot->twsk_prot->twsk_slab_name = kasprintf(GFP_KERNEL, "tw_sock_%s", prot->name);
  2744. if (prot->twsk_prot->twsk_slab_name == NULL)
  2745. goto out_free_request_sock_slab;
  2746. prot->twsk_prot->twsk_slab =
  2747. kmem_cache_create(prot->twsk_prot->twsk_slab_name,
  2748. prot->twsk_prot->twsk_obj_size,
  2749. 0,
  2750. prot->slab_flags,
  2751. NULL);
  2752. if (prot->twsk_prot->twsk_slab == NULL)
  2753. goto out_free_timewait_sock_slab_name;
  2754. }
  2755. }
  2756. mutex_lock(&proto_list_mutex);
  2757. list_add(&prot->node, &proto_list);
  2758. assign_proto_idx(prot);
  2759. mutex_unlock(&proto_list_mutex);
  2760. return 0;
  2761. out_free_timewait_sock_slab_name:
  2762. kfree(prot->twsk_prot->twsk_slab_name);
  2763. out_free_request_sock_slab:
  2764. req_prot_cleanup(prot->rsk_prot);
  2765. kmem_cache_destroy(prot->slab);
  2766. prot->slab = NULL;
  2767. out:
  2768. return -ENOBUFS;
  2769. }
  2770. EXPORT_SYMBOL(proto_register);
  2771. void proto_unregister(struct proto *prot)
  2772. {
  2773. mutex_lock(&proto_list_mutex);
  2774. release_proto_idx(prot);
  2775. list_del(&prot->node);
  2776. mutex_unlock(&proto_list_mutex);
  2777. kmem_cache_destroy(prot->slab);
  2778. prot->slab = NULL;
  2779. req_prot_cleanup(prot->rsk_prot);
  2780. if (prot->twsk_prot != NULL && prot->twsk_prot->twsk_slab != NULL) {
  2781. kmem_cache_destroy(prot->twsk_prot->twsk_slab);
  2782. kfree(prot->twsk_prot->twsk_slab_name);
  2783. prot->twsk_prot->twsk_slab = NULL;
  2784. }
  2785. }
  2786. EXPORT_SYMBOL(proto_unregister);
  2787. #ifdef CONFIG_PROC_FS
  2788. static void *proto_seq_start(struct seq_file *seq, loff_t *pos)
  2789. __acquires(proto_list_mutex)
  2790. {
  2791. mutex_lock(&proto_list_mutex);
  2792. return seq_list_start_head(&proto_list, *pos);
  2793. }
  2794. static void *proto_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2795. {
  2796. return seq_list_next(v, &proto_list, pos);
  2797. }
  2798. static void proto_seq_stop(struct seq_file *seq, void *v)
  2799. __releases(proto_list_mutex)
  2800. {
  2801. mutex_unlock(&proto_list_mutex);
  2802. }
  2803. static char proto_method_implemented(const void *method)
  2804. {
  2805. return method == NULL ? 'n' : 'y';
  2806. }
  2807. static long sock_prot_memory_allocated(struct proto *proto)
  2808. {
  2809. return proto->memory_allocated != NULL ? proto_memory_allocated(proto) : -1L;
  2810. }
  2811. static char *sock_prot_memory_pressure(struct proto *proto)
  2812. {
  2813. return proto->memory_pressure != NULL ?
  2814. proto_memory_pressure(proto) ? "yes" : "no" : "NI";
  2815. }
  2816. static void proto_seq_printf(struct seq_file *seq, struct proto *proto)
  2817. {
  2818. seq_printf(seq, "%-9s %4u %6d %6ld %-3s %6u %-3s %-10s "
  2819. "%2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c\n",
  2820. proto->name,
  2821. proto->obj_size,
  2822. sock_prot_inuse_get(seq_file_net(seq), proto),
  2823. sock_prot_memory_allocated(proto),
  2824. sock_prot_memory_pressure(proto),
  2825. proto->max_header,
  2826. proto->slab == NULL ? "no" : "yes",
  2827. module_name(proto->owner),
  2828. proto_method_implemented(proto->close),
  2829. proto_method_implemented(proto->connect),
  2830. proto_method_implemented(proto->disconnect),
  2831. proto_method_implemented(proto->accept),
  2832. proto_method_implemented(proto->ioctl),
  2833. proto_method_implemented(proto->init),
  2834. proto_method_implemented(proto->destroy),
  2835. proto_method_implemented(proto->shutdown),
  2836. proto_method_implemented(proto->setsockopt),
  2837. proto_method_implemented(proto->getsockopt),
  2838. proto_method_implemented(proto->sendmsg),
  2839. proto_method_implemented(proto->recvmsg),
  2840. proto_method_implemented(proto->sendpage),
  2841. proto_method_implemented(proto->bind),
  2842. proto_method_implemented(proto->backlog_rcv),
  2843. proto_method_implemented(proto->hash),
  2844. proto_method_implemented(proto->unhash),
  2845. proto_method_implemented(proto->get_port),
  2846. proto_method_implemented(proto->enter_memory_pressure));
  2847. }
  2848. static int proto_seq_show(struct seq_file *seq, void *v)
  2849. {
  2850. if (v == &proto_list)
  2851. seq_printf(seq, "%-9s %-4s %-8s %-6s %-5s %-7s %-4s %-10s %s",
  2852. "protocol",
  2853. "size",
  2854. "sockets",
  2855. "memory",
  2856. "press",
  2857. "maxhdr",
  2858. "slab",
  2859. "module",
  2860. "cl co di ac io in de sh ss gs se re sp bi br ha uh gp em\n");
  2861. else
  2862. proto_seq_printf(seq, list_entry(v, struct proto, node));
  2863. return 0;
  2864. }
  2865. static const struct seq_operations proto_seq_ops = {
  2866. .start = proto_seq_start,
  2867. .next = proto_seq_next,
  2868. .stop = proto_seq_stop,
  2869. .show = proto_seq_show,
  2870. };
  2871. static int proto_seq_open(struct inode *inode, struct file *file)
  2872. {
  2873. return seq_open_net(inode, file, &proto_seq_ops,
  2874. sizeof(struct seq_net_private));
  2875. }
  2876. static const struct file_operations proto_seq_fops = {
  2877. .open = proto_seq_open,
  2878. .read = seq_read,
  2879. .llseek = seq_lseek,
  2880. .release = seq_release_net,
  2881. };
  2882. static __net_init int proto_init_net(struct net *net)
  2883. {
  2884. if (!proc_create("protocols", S_IRUGO, net->proc_net, &proto_seq_fops))
  2885. return -ENOMEM;
  2886. return 0;
  2887. }
  2888. static __net_exit void proto_exit_net(struct net *net)
  2889. {
  2890. remove_proc_entry("protocols", net->proc_net);
  2891. }
  2892. static __net_initdata struct pernet_operations proto_net_ops = {
  2893. .init = proto_init_net,
  2894. .exit = proto_exit_net,
  2895. };
  2896. static int __init proto_init(void)
  2897. {
  2898. return register_pernet_subsys(&proto_net_ops);
  2899. }
  2900. subsys_initcall(proto_init);
  2901. #endif /* PROC_FS */
  2902. #ifdef CONFIG_NET_RX_BUSY_POLL
  2903. bool sk_busy_loop_end(void *p, unsigned long start_time)
  2904. {
  2905. struct sock *sk = p;
  2906. return !skb_queue_empty(&sk->sk_receive_queue) ||
  2907. sk_busy_loop_timeout(sk, start_time);
  2908. }
  2909. EXPORT_SYMBOL(sk_busy_loop_end);
  2910. #endif /* CONFIG_NET_RX_BUSY_POLL */