sock.c 82 KB

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