sock.c 85 KB

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