sock.c 82 KB

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