sock.c 85 KB

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