sock.c 83 KB

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