sock.c 81 KB

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