sock.c 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * INET An implementation of the TCP/IP protocol suite for the LINUX
  4. * operating system. INET is implemented using the BSD Socket
  5. * interface as the means of communication with the user level.
  6. *
  7. * Generic socket support routines. Memory allocators, socket lock/release
  8. * handler for protocols to use and generic option handler.
  9. *
  10. * Authors: Ross Biro
  11. * Fred N. van Kempen, <[email protected]>
  12. * Florian La Roche, <[email protected]>
  13. * Alan Cox, <[email protected]>
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  86. #include <asm/unaligned.h>
  87. #include <linux/capability.h>
  88. #include <linux/errno.h>
  89. #include <linux/errqueue.h>
  90. #include <linux/types.h>
  91. #include <linux/socket.h>
  92. #include <linux/in.h>
  93. #include <linux/kernel.h>
  94. #include <linux/module.h>
  95. #include <linux/proc_fs.h>
  96. #include <linux/seq_file.h>
  97. #include <linux/sched.h>
  98. #include <linux/sched/mm.h>
  99. #include <linux/timer.h>
  100. #include <linux/string.h>
  101. #include <linux/sockios.h>
  102. #include <linux/net.h>
  103. #include <linux/mm.h>
  104. #include <linux/slab.h>
  105. #include <linux/interrupt.h>
  106. #include <linux/poll.h>
  107. #include <linux/tcp.h>
  108. #include <linux/init.h>
  109. #include <linux/highmem.h>
  110. #include <linux/user_namespace.h>
  111. #include <linux/static_key.h>
  112. #include <linux/memcontrol.h>
  113. #include <linux/prefetch.h>
  114. #include <linux/compat.h>
  115. #include <linux/uaccess.h>
  116. #include <linux/netdevice.h>
  117. #include <net/protocol.h>
  118. #include <linux/skbuff.h>
  119. #include <net/net_namespace.h>
  120. #include <net/request_sock.h>
  121. #include <net/sock.h>
  122. #include <linux/net_tstamp.h>
  123. #include <net/xfrm.h>
  124. #include <linux/ipsec.h>
  125. #include <net/cls_cgroup.h>
  126. #include <net/netprio_cgroup.h>
  127. #include <linux/sock_diag.h>
  128. #include <linux/filter.h>
  129. #include <net/sock_reuseport.h>
  130. #include <net/bpf_sk_storage.h>
  131. #include <trace/events/sock.h>
  132. #include <trace/hooks/sched.h>
  133. #include <trace/hooks/net.h>
  134. #include <net/tcp.h>
  135. #include <net/busy_poll.h>
  136. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA {
  137. #ifdef CONFIG_KNOX_NCM
  138. #include <linux/pid.h>
  139. #define PROCESS_NAME_LEN_NAP 128
  140. #define DOMAIN_NAME_LEN_NAP 255
  141. #endif
  142. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA }
  143. #include <linux/ethtool.h>
  144. #include "dev.h"
  145. static DEFINE_MUTEX(proto_list_mutex);
  146. static LIST_HEAD(proto_list);
  147. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA {
  148. #ifdef CONFIG_KNOX_NCM
  149. extern unsigned int check_ncm_flag(void);
  150. #endif
  151. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA }
  152. static void sock_def_write_space_wfree(struct sock *sk);
  153. static void sock_def_write_space(struct sock *sk);
  154. /**
  155. * sk_ns_capable - General socket capability test
  156. * @sk: Socket to use a capability on or through
  157. * @user_ns: The user namespace of the capability to use
  158. * @cap: The capability to use
  159. *
  160. * Test to see if the opener of the socket had when the socket was
  161. * created and the current process has the capability @cap in the user
  162. * namespace @user_ns.
  163. */
  164. bool sk_ns_capable(const struct sock *sk,
  165. struct user_namespace *user_ns, int cap)
  166. {
  167. return file_ns_capable(sk->sk_socket->file, user_ns, cap) &&
  168. ns_capable(user_ns, cap);
  169. }
  170. EXPORT_SYMBOL(sk_ns_capable);
  171. /**
  172. * sk_capable - Socket global capability test
  173. * @sk: Socket to use a capability on or through
  174. * @cap: The global capability to use
  175. *
  176. * Test to see if the opener of the socket had when the socket was
  177. * created and the current process has the capability @cap in all user
  178. * namespaces.
  179. */
  180. bool sk_capable(const struct sock *sk, int cap)
  181. {
  182. return sk_ns_capable(sk, &init_user_ns, cap);
  183. }
  184. EXPORT_SYMBOL(sk_capable);
  185. /**
  186. * sk_net_capable - Network namespace socket capability test
  187. * @sk: Socket to use a capability on or through
  188. * @cap: The capability to use
  189. *
  190. * Test to see if the opener of the socket had when the socket was created
  191. * and the current process has the capability @cap over the network namespace
  192. * the socket is a member of.
  193. */
  194. bool sk_net_capable(const struct sock *sk, int cap)
  195. {
  196. return sk_ns_capable(sk, sock_net(sk)->user_ns, cap);
  197. }
  198. EXPORT_SYMBOL(sk_net_capable);
  199. /*
  200. * Each address family might have different locking rules, so we have
  201. * one slock key per address family and separate keys for internal and
  202. * userspace sockets.
  203. */
  204. static struct lock_class_key af_family_keys[AF_MAX];
  205. static struct lock_class_key af_family_kern_keys[AF_MAX];
  206. static struct lock_class_key af_family_slock_keys[AF_MAX];
  207. static struct lock_class_key af_family_kern_slock_keys[AF_MAX];
  208. /*
  209. * Make lock validator output more readable. (we pre-construct these
  210. * strings build-time, so that runtime initialization of socket
  211. * locks is fast):
  212. */
  213. #define _sock_locks(x) \
  214. x "AF_UNSPEC", x "AF_UNIX" , x "AF_INET" , \
  215. x "AF_AX25" , x "AF_IPX" , x "AF_APPLETALK", \
  216. x "AF_NETROM", x "AF_BRIDGE" , x "AF_ATMPVC" , \
  217. x "AF_X25" , x "AF_INET6" , x "AF_ROSE" , \
  218. x "AF_DECnet", x "AF_NETBEUI" , x "AF_SECURITY" , \
  219. x "AF_KEY" , x "AF_NETLINK" , x "AF_PACKET" , \
  220. x "AF_ASH" , x "AF_ECONET" , x "AF_ATMSVC" , \
  221. x "AF_RDS" , x "AF_SNA" , x "AF_IRDA" , \
  222. x "AF_PPPOX" , x "AF_WANPIPE" , x "AF_LLC" , \
  223. x "27" , x "28" , x "AF_CAN" , \
  224. x "AF_TIPC" , x "AF_BLUETOOTH", x "IUCV" , \
  225. x "AF_RXRPC" , x "AF_ISDN" , x "AF_PHONET" , \
  226. x "AF_IEEE802154", x "AF_CAIF" , x "AF_ALG" , \
  227. x "AF_NFC" , x "AF_VSOCK" , x "AF_KCM" , \
  228. x "AF_QIPCRTR", x "AF_SMC" , x "AF_XDP" , \
  229. x "AF_MCTP" , \
  230. x "AF_MAX"
  231. static const char *const af_family_key_strings[AF_MAX+1] = {
  232. _sock_locks("sk_lock-")
  233. };
  234. static const char *const af_family_slock_key_strings[AF_MAX+1] = {
  235. _sock_locks("slock-")
  236. };
  237. static const char *const af_family_clock_key_strings[AF_MAX+1] = {
  238. _sock_locks("clock-")
  239. };
  240. static const char *const af_family_kern_key_strings[AF_MAX+1] = {
  241. _sock_locks("k-sk_lock-")
  242. };
  243. static const char *const af_family_kern_slock_key_strings[AF_MAX+1] = {
  244. _sock_locks("k-slock-")
  245. };
  246. static const char *const af_family_kern_clock_key_strings[AF_MAX+1] = {
  247. _sock_locks("k-clock-")
  248. };
  249. static const char *const af_family_rlock_key_strings[AF_MAX+1] = {
  250. _sock_locks("rlock-")
  251. };
  252. static const char *const af_family_wlock_key_strings[AF_MAX+1] = {
  253. _sock_locks("wlock-")
  254. };
  255. static const char *const af_family_elock_key_strings[AF_MAX+1] = {
  256. _sock_locks("elock-")
  257. };
  258. /*
  259. * sk_callback_lock and sk queues locking rules are per-address-family,
  260. * so split the lock classes by using a per-AF key:
  261. */
  262. static struct lock_class_key af_callback_keys[AF_MAX];
  263. static struct lock_class_key af_rlock_keys[AF_MAX];
  264. static struct lock_class_key af_wlock_keys[AF_MAX];
  265. static struct lock_class_key af_elock_keys[AF_MAX];
  266. static struct lock_class_key af_kern_callback_keys[AF_MAX];
  267. /* Run time adjustable parameters. */
  268. __u32 sysctl_wmem_max __read_mostly = SK_WMEM_MAX;
  269. EXPORT_SYMBOL(sysctl_wmem_max);
  270. __u32 sysctl_rmem_max __read_mostly = SK_RMEM_MAX;
  271. EXPORT_SYMBOL(sysctl_rmem_max);
  272. __u32 sysctl_wmem_default __read_mostly = SK_WMEM_MAX;
  273. __u32 sysctl_rmem_default __read_mostly = SK_RMEM_MAX;
  274. /* Maximal space eaten by iovec or ancillary data plus some space */
  275. int sysctl_optmem_max __read_mostly = sizeof(unsigned long)*(2*UIO_MAXIOV+512);
  276. EXPORT_SYMBOL(sysctl_optmem_max);
  277. int sysctl_tstamp_allow_data __read_mostly = 1;
  278. DEFINE_STATIC_KEY_FALSE(memalloc_socks_key);
  279. EXPORT_SYMBOL_GPL(memalloc_socks_key);
  280. /**
  281. * sk_set_memalloc - sets %SOCK_MEMALLOC
  282. * @sk: socket to set it on
  283. *
  284. * Set %SOCK_MEMALLOC on a socket for access to emergency reserves.
  285. * It's the responsibility of the admin to adjust min_free_kbytes
  286. * to meet the requirements
  287. */
  288. void sk_set_memalloc(struct sock *sk)
  289. {
  290. sock_set_flag(sk, SOCK_MEMALLOC);
  291. sk->sk_allocation |= __GFP_MEMALLOC;
  292. static_branch_inc(&memalloc_socks_key);
  293. }
  294. EXPORT_SYMBOL_GPL(sk_set_memalloc);
  295. void sk_clear_memalloc(struct sock *sk)
  296. {
  297. sock_reset_flag(sk, SOCK_MEMALLOC);
  298. sk->sk_allocation &= ~__GFP_MEMALLOC;
  299. static_branch_dec(&memalloc_socks_key);
  300. /*
  301. * SOCK_MEMALLOC is allowed to ignore rmem limits to ensure forward
  302. * progress of swapping. SOCK_MEMALLOC may be cleared while
  303. * it has rmem allocations due to the last swapfile being deactivated
  304. * but there is a risk that the socket is unusable due to exceeding
  305. * the rmem limits. Reclaim the reserves and obey rmem limits again.
  306. */
  307. sk_mem_reclaim(sk);
  308. }
  309. EXPORT_SYMBOL_GPL(sk_clear_memalloc);
  310. int __sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
  311. {
  312. int ret;
  313. unsigned int noreclaim_flag;
  314. /* these should have been dropped before queueing */
  315. BUG_ON(!sock_flag(sk, SOCK_MEMALLOC));
  316. noreclaim_flag = memalloc_noreclaim_save();
  317. ret = INDIRECT_CALL_INET(sk->sk_backlog_rcv,
  318. tcp_v6_do_rcv,
  319. tcp_v4_do_rcv,
  320. sk, skb);
  321. memalloc_noreclaim_restore(noreclaim_flag);
  322. return ret;
  323. }
  324. EXPORT_SYMBOL(__sk_backlog_rcv);
  325. void sk_error_report(struct sock *sk)
  326. {
  327. sk->sk_error_report(sk);
  328. switch (sk->sk_family) {
  329. case AF_INET:
  330. fallthrough;
  331. case AF_INET6:
  332. trace_inet_sk_error_report(sk);
  333. break;
  334. default:
  335. break;
  336. }
  337. }
  338. EXPORT_SYMBOL(sk_error_report);
  339. int sock_get_timeout(long timeo, void *optval, bool old_timeval)
  340. {
  341. struct __kernel_sock_timeval tv;
  342. if (timeo == MAX_SCHEDULE_TIMEOUT) {
  343. tv.tv_sec = 0;
  344. tv.tv_usec = 0;
  345. } else {
  346. tv.tv_sec = timeo / HZ;
  347. tv.tv_usec = ((timeo % HZ) * USEC_PER_SEC) / HZ;
  348. }
  349. if (old_timeval && in_compat_syscall() && !COMPAT_USE_64BIT_TIME) {
  350. struct old_timeval32 tv32 = { tv.tv_sec, tv.tv_usec };
  351. *(struct old_timeval32 *)optval = tv32;
  352. return sizeof(tv32);
  353. }
  354. if (old_timeval) {
  355. struct __kernel_old_timeval old_tv;
  356. old_tv.tv_sec = tv.tv_sec;
  357. old_tv.tv_usec = tv.tv_usec;
  358. *(struct __kernel_old_timeval *)optval = old_tv;
  359. return sizeof(old_tv);
  360. }
  361. *(struct __kernel_sock_timeval *)optval = tv;
  362. return sizeof(tv);
  363. }
  364. EXPORT_SYMBOL(sock_get_timeout);
  365. int sock_copy_user_timeval(struct __kernel_sock_timeval *tv,
  366. sockptr_t optval, int optlen, bool old_timeval)
  367. {
  368. if (old_timeval && in_compat_syscall() && !COMPAT_USE_64BIT_TIME) {
  369. struct old_timeval32 tv32;
  370. if (optlen < sizeof(tv32))
  371. return -EINVAL;
  372. if (copy_from_sockptr(&tv32, optval, sizeof(tv32)))
  373. return -EFAULT;
  374. tv->tv_sec = tv32.tv_sec;
  375. tv->tv_usec = tv32.tv_usec;
  376. } else if (old_timeval) {
  377. struct __kernel_old_timeval old_tv;
  378. if (optlen < sizeof(old_tv))
  379. return -EINVAL;
  380. if (copy_from_sockptr(&old_tv, optval, sizeof(old_tv)))
  381. return -EFAULT;
  382. tv->tv_sec = old_tv.tv_sec;
  383. tv->tv_usec = old_tv.tv_usec;
  384. } else {
  385. if (optlen < sizeof(*tv))
  386. return -EINVAL;
  387. if (copy_from_sockptr(tv, optval, sizeof(*tv)))
  388. return -EFAULT;
  389. }
  390. return 0;
  391. }
  392. EXPORT_SYMBOL(sock_copy_user_timeval);
  393. static int sock_set_timeout(long *timeo_p, sockptr_t optval, int optlen,
  394. bool old_timeval)
  395. {
  396. struct __kernel_sock_timeval tv;
  397. int err = sock_copy_user_timeval(&tv, optval, optlen, old_timeval);
  398. long val;
  399. if (err)
  400. return err;
  401. if (tv.tv_usec < 0 || tv.tv_usec >= USEC_PER_SEC)
  402. return -EDOM;
  403. if (tv.tv_sec < 0) {
  404. static int warned __read_mostly;
  405. WRITE_ONCE(*timeo_p, 0);
  406. if (warned < 10 && net_ratelimit()) {
  407. warned++;
  408. pr_info("%s: `%s' (pid %d) tries to set negative timeout\n",
  409. __func__, current->comm, task_pid_nr(current));
  410. }
  411. return 0;
  412. }
  413. val = MAX_SCHEDULE_TIMEOUT;
  414. if ((tv.tv_sec || tv.tv_usec) &&
  415. (tv.tv_sec < (MAX_SCHEDULE_TIMEOUT / HZ - 1)))
  416. val = tv.tv_sec * HZ + DIV_ROUND_UP((unsigned long)tv.tv_usec,
  417. USEC_PER_SEC / HZ);
  418. WRITE_ONCE(*timeo_p, val);
  419. return 0;
  420. }
  421. static bool sock_needs_netstamp(const struct sock *sk)
  422. {
  423. switch (sk->sk_family) {
  424. case AF_UNSPEC:
  425. case AF_UNIX:
  426. return false;
  427. default:
  428. return true;
  429. }
  430. }
  431. static void sock_disable_timestamp(struct sock *sk, unsigned long flags)
  432. {
  433. if (sk->sk_flags & flags) {
  434. sk->sk_flags &= ~flags;
  435. if (sock_needs_netstamp(sk) &&
  436. !(sk->sk_flags & SK_FLAGS_TIMESTAMP))
  437. net_disable_timestamp();
  438. }
  439. }
  440. int __sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  441. {
  442. unsigned long flags;
  443. struct sk_buff_head *list = &sk->sk_receive_queue;
  444. if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf) {
  445. atomic_inc(&sk->sk_drops);
  446. trace_sock_rcvqueue_full(sk, skb);
  447. return -ENOMEM;
  448. }
  449. if (!sk_rmem_schedule(sk, skb, skb->truesize)) {
  450. atomic_inc(&sk->sk_drops);
  451. return -ENOBUFS;
  452. }
  453. skb->dev = NULL;
  454. skb_set_owner_r(skb, sk);
  455. /* we escape from rcu protected region, make sure we dont leak
  456. * a norefcounted dst
  457. */
  458. skb_dst_force(skb);
  459. spin_lock_irqsave(&list->lock, flags);
  460. sock_skb_set_dropcount(sk, skb);
  461. __skb_queue_tail(list, skb);
  462. spin_unlock_irqrestore(&list->lock, flags);
  463. if (!sock_flag(sk, SOCK_DEAD))
  464. sk->sk_data_ready(sk);
  465. return 0;
  466. }
  467. EXPORT_SYMBOL(__sock_queue_rcv_skb);
  468. int sock_queue_rcv_skb_reason(struct sock *sk, struct sk_buff *skb,
  469. enum skb_drop_reason *reason)
  470. {
  471. enum skb_drop_reason drop_reason;
  472. int err;
  473. err = sk_filter(sk, skb);
  474. if (err) {
  475. drop_reason = SKB_DROP_REASON_SOCKET_FILTER;
  476. goto out;
  477. }
  478. err = __sock_queue_rcv_skb(sk, skb);
  479. switch (err) {
  480. case -ENOMEM:
  481. drop_reason = SKB_DROP_REASON_SOCKET_RCVBUFF;
  482. break;
  483. case -ENOBUFS:
  484. drop_reason = SKB_DROP_REASON_PROTO_MEM;
  485. break;
  486. default:
  487. drop_reason = SKB_NOT_DROPPED_YET;
  488. break;
  489. }
  490. out:
  491. if (reason)
  492. *reason = drop_reason;
  493. return err;
  494. }
  495. EXPORT_SYMBOL(sock_queue_rcv_skb_reason);
  496. int __sk_receive_skb(struct sock *sk, struct sk_buff *skb,
  497. const int nested, unsigned int trim_cap, bool refcounted)
  498. {
  499. int rc = NET_RX_SUCCESS;
  500. if (sk_filter_trim_cap(sk, skb, trim_cap))
  501. goto discard_and_relse;
  502. skb->dev = NULL;
  503. if (sk_rcvqueues_full(sk, sk->sk_rcvbuf)) {
  504. atomic_inc(&sk->sk_drops);
  505. goto discard_and_relse;
  506. }
  507. if (nested)
  508. bh_lock_sock_nested(sk);
  509. else
  510. bh_lock_sock(sk);
  511. if (!sock_owned_by_user(sk)) {
  512. /*
  513. * trylock + unlock semantics:
  514. */
  515. mutex_acquire(&sk->sk_lock.dep_map, 0, 1, _RET_IP_);
  516. rc = sk_backlog_rcv(sk, skb);
  517. mutex_release(&sk->sk_lock.dep_map, _RET_IP_);
  518. } else if (sk_add_backlog(sk, skb, READ_ONCE(sk->sk_rcvbuf))) {
  519. bh_unlock_sock(sk);
  520. atomic_inc(&sk->sk_drops);
  521. goto discard_and_relse;
  522. }
  523. bh_unlock_sock(sk);
  524. out:
  525. if (refcounted)
  526. sock_put(sk);
  527. return rc;
  528. discard_and_relse:
  529. kfree_skb(skb);
  530. goto out;
  531. }
  532. EXPORT_SYMBOL(__sk_receive_skb);
  533. INDIRECT_CALLABLE_DECLARE(struct dst_entry *ip6_dst_check(struct dst_entry *,
  534. u32));
  535. INDIRECT_CALLABLE_DECLARE(struct dst_entry *ipv4_dst_check(struct dst_entry *,
  536. u32));
  537. struct dst_entry *__sk_dst_check(struct sock *sk, u32 cookie)
  538. {
  539. struct dst_entry *dst = __sk_dst_get(sk);
  540. if (dst && dst->obsolete &&
  541. INDIRECT_CALL_INET(dst->ops->check, ip6_dst_check, ipv4_dst_check,
  542. dst, cookie) == NULL) {
  543. sk_tx_queue_clear(sk);
  544. WRITE_ONCE(sk->sk_dst_pending_confirm, 0);
  545. RCU_INIT_POINTER(sk->sk_dst_cache, NULL);
  546. dst_release(dst);
  547. return NULL;
  548. }
  549. return dst;
  550. }
  551. EXPORT_SYMBOL(__sk_dst_check);
  552. struct dst_entry *sk_dst_check(struct sock *sk, u32 cookie)
  553. {
  554. struct dst_entry *dst = sk_dst_get(sk);
  555. if (dst && dst->obsolete &&
  556. INDIRECT_CALL_INET(dst->ops->check, ip6_dst_check, ipv4_dst_check,
  557. dst, cookie) == NULL) {
  558. sk_dst_reset(sk);
  559. dst_release(dst);
  560. return NULL;
  561. }
  562. return dst;
  563. }
  564. EXPORT_SYMBOL(sk_dst_check);
  565. static int sock_bindtoindex_locked(struct sock *sk, int ifindex)
  566. {
  567. int ret = -ENOPROTOOPT;
  568. #ifdef CONFIG_NETDEVICES
  569. struct net *net = sock_net(sk);
  570. /* Sorry... */
  571. ret = -EPERM;
  572. if (sk->sk_bound_dev_if && !ns_capable(net->user_ns, CAP_NET_RAW))
  573. goto out;
  574. ret = -EINVAL;
  575. if (ifindex < 0)
  576. goto out;
  577. /* Paired with all READ_ONCE() done locklessly. */
  578. WRITE_ONCE(sk->sk_bound_dev_if, ifindex);
  579. if (sk->sk_prot->rehash)
  580. sk->sk_prot->rehash(sk);
  581. sk_dst_reset(sk);
  582. ret = 0;
  583. out:
  584. #endif
  585. return ret;
  586. }
  587. int sock_bindtoindex(struct sock *sk, int ifindex, bool lock_sk)
  588. {
  589. int ret;
  590. if (lock_sk)
  591. lock_sock(sk);
  592. ret = sock_bindtoindex_locked(sk, ifindex);
  593. if (lock_sk)
  594. release_sock(sk);
  595. return ret;
  596. }
  597. EXPORT_SYMBOL(sock_bindtoindex);
  598. static int sock_setbindtodevice(struct sock *sk, sockptr_t optval, int optlen)
  599. {
  600. int ret = -ENOPROTOOPT;
  601. #ifdef CONFIG_NETDEVICES
  602. struct net *net = sock_net(sk);
  603. char devname[IFNAMSIZ];
  604. int index;
  605. ret = -EINVAL;
  606. if (optlen < 0)
  607. goto out;
  608. /* Bind this socket to a particular device like "eth0",
  609. * as specified in the passed interface name. If the
  610. * name is "" or the option length is zero the socket
  611. * is not bound.
  612. */
  613. if (optlen > IFNAMSIZ - 1)
  614. optlen = IFNAMSIZ - 1;
  615. memset(devname, 0, sizeof(devname));
  616. ret = -EFAULT;
  617. if (copy_from_sockptr(devname, optval, optlen))
  618. goto out;
  619. index = 0;
  620. if (devname[0] != '\0') {
  621. struct net_device *dev;
  622. rcu_read_lock();
  623. dev = dev_get_by_name_rcu(net, devname);
  624. if (dev)
  625. index = dev->ifindex;
  626. rcu_read_unlock();
  627. ret = -ENODEV;
  628. if (!dev)
  629. goto out;
  630. }
  631. sockopt_lock_sock(sk);
  632. ret = sock_bindtoindex_locked(sk, index);
  633. sockopt_release_sock(sk);
  634. out:
  635. #endif
  636. return ret;
  637. }
  638. static int sock_getbindtodevice(struct sock *sk, sockptr_t optval,
  639. sockptr_t optlen, int len)
  640. {
  641. int ret = -ENOPROTOOPT;
  642. #ifdef CONFIG_NETDEVICES
  643. int bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
  644. struct net *net = sock_net(sk);
  645. char devname[IFNAMSIZ];
  646. if (bound_dev_if == 0) {
  647. len = 0;
  648. goto zero;
  649. }
  650. ret = -EINVAL;
  651. if (len < IFNAMSIZ)
  652. goto out;
  653. ret = netdev_get_name(net, devname, bound_dev_if);
  654. if (ret)
  655. goto out;
  656. len = strlen(devname) + 1;
  657. ret = -EFAULT;
  658. if (copy_to_sockptr(optval, devname, len))
  659. goto out;
  660. zero:
  661. ret = -EFAULT;
  662. if (copy_to_sockptr(optlen, &len, sizeof(int)))
  663. goto out;
  664. ret = 0;
  665. out:
  666. #endif
  667. return ret;
  668. }
  669. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA {
  670. #ifdef CONFIG_KNOX_NCM
  671. /** The function sets the domain name associated with the socket. **/
  672. static int sock_set_domain_name(struct sock *sk, sockptr_t optval,
  673. int optlen)
  674. {
  675. int ret = -EADDRNOTAVAIL;
  676. char domain[DOMAIN_NAME_LEN_NAP];
  677. ret = -EINVAL;
  678. if (optlen < 0)
  679. goto out;
  680. if (optlen > DOMAIN_NAME_LEN_NAP - 1)
  681. optlen = DOMAIN_NAME_LEN_NAP - 1;
  682. memset(domain, 0, sizeof(domain));
  683. ret = -EFAULT;
  684. if (copy_from_sockptr(domain, optval, optlen))
  685. goto out;
  686. if (SOCK_NPA_VENDOR_DATA_GET(sk)) {
  687. memcpy(SOCK_NPA_VENDOR_DATA_GET(sk)->domain_name, domain, sizeof(SOCK_NPA_VENDOR_DATA_GET(sk)->domain_name)-1);
  688. ret = 0;
  689. }
  690. out:
  691. return ret;
  692. }
  693. /** The function sets the uid associated with the dns socket. **/
  694. static int sock_set_dns_uid(struct sock *sk, sockptr_t optval, int optlen)
  695. {
  696. int ret = -EADDRNOTAVAIL;
  697. if (optlen < 0)
  698. goto out;
  699. if (optlen == sizeof(uid_t)) {
  700. uid_t dns_uid;
  701. ret = -EFAULT;
  702. if (copy_from_sockptr(&dns_uid, optval, sizeof(dns_uid)))
  703. goto out;
  704. if (SOCK_NPA_VENDOR_DATA_GET(sk)) {
  705. memcpy(&SOCK_NPA_VENDOR_DATA_GET(sk)->knox_dns_uid, &dns_uid, sizeof(SOCK_NPA_VENDOR_DATA_GET(sk)->knox_dns_uid));
  706. ret = 0;
  707. }
  708. }
  709. out:
  710. return ret;
  711. }
  712. /** The function sets the pid and the process name associated with the dns socket. **/
  713. static int sock_set_dns_pid(struct sock *sk, sockptr_t optval, int optlen)
  714. {
  715. int ret = -EADDRNOTAVAIL;
  716. struct pid *pid_struct = NULL;
  717. struct task_struct *task = NULL;
  718. int process_returnValue = -1;
  719. char full_process_name[PROCESS_NAME_LEN_NAP] = {0};
  720. if (optlen < 0)
  721. goto out;
  722. if (optlen == sizeof(pid_t)) {
  723. pid_t dns_pid;
  724. ret = -EFAULT;
  725. if (copy_from_sockptr(&dns_pid, optval, sizeof(dns_pid)))
  726. goto out;
  727. if (SOCK_NPA_VENDOR_DATA_GET(sk)) {
  728. memcpy(&SOCK_NPA_VENDOR_DATA_GET(sk)->knox_dns_pid, &dns_pid, sizeof(SOCK_NPA_VENDOR_DATA_GET(sk)->knox_dns_pid));
  729. if(check_ncm_flag()) {
  730. pid_struct = find_get_pid(dns_pid);
  731. if (pid_struct != NULL) {
  732. task = pid_task(pid_struct,PIDTYPE_PID);
  733. if (task != NULL) {
  734. process_returnValue = get_cmdline(task, full_process_name, sizeof(full_process_name)-1);
  735. if (process_returnValue > 0) {
  736. memcpy(SOCK_NPA_VENDOR_DATA_GET(sk)->dns_process_name, full_process_name, sizeof(SOCK_NPA_VENDOR_DATA_GET(sk)->dns_process_name)-1);
  737. } else {
  738. memcpy(SOCK_NPA_VENDOR_DATA_GET(sk)->dns_process_name, task->comm, sizeof(task->comm)-1);
  739. }
  740. }
  741. }
  742. }
  743. ret = 0;
  744. }
  745. }
  746. out:
  747. return ret;
  748. }
  749. #endif
  750. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA }
  751. bool sk_mc_loop(struct sock *sk)
  752. {
  753. if (dev_recursion_level())
  754. return false;
  755. if (!sk)
  756. return true;
  757. /* IPV6_ADDRFORM can change sk->sk_family under us. */
  758. switch (READ_ONCE(sk->sk_family)) {
  759. case AF_INET:
  760. return inet_sk(sk)->mc_loop;
  761. #if IS_ENABLED(CONFIG_IPV6)
  762. case AF_INET6:
  763. return inet6_sk(sk)->mc_loop;
  764. #endif
  765. }
  766. WARN_ON_ONCE(1);
  767. return true;
  768. }
  769. EXPORT_SYMBOL(sk_mc_loop);
  770. void sock_set_reuseaddr(struct sock *sk)
  771. {
  772. lock_sock(sk);
  773. sk->sk_reuse = SK_CAN_REUSE;
  774. release_sock(sk);
  775. }
  776. EXPORT_SYMBOL(sock_set_reuseaddr);
  777. void sock_set_reuseport(struct sock *sk)
  778. {
  779. lock_sock(sk);
  780. sk->sk_reuseport = true;
  781. release_sock(sk);
  782. }
  783. EXPORT_SYMBOL(sock_set_reuseport);
  784. void sock_no_linger(struct sock *sk)
  785. {
  786. lock_sock(sk);
  787. WRITE_ONCE(sk->sk_lingertime, 0);
  788. sock_set_flag(sk, SOCK_LINGER);
  789. release_sock(sk);
  790. }
  791. EXPORT_SYMBOL(sock_no_linger);
  792. void sock_set_priority(struct sock *sk, u32 priority)
  793. {
  794. lock_sock(sk);
  795. WRITE_ONCE(sk->sk_priority, priority);
  796. release_sock(sk);
  797. }
  798. EXPORT_SYMBOL(sock_set_priority);
  799. void sock_set_sndtimeo(struct sock *sk, s64 secs)
  800. {
  801. lock_sock(sk);
  802. if (secs && secs < MAX_SCHEDULE_TIMEOUT / HZ - 1)
  803. WRITE_ONCE(sk->sk_sndtimeo, secs * HZ);
  804. else
  805. WRITE_ONCE(sk->sk_sndtimeo, MAX_SCHEDULE_TIMEOUT);
  806. release_sock(sk);
  807. }
  808. EXPORT_SYMBOL(sock_set_sndtimeo);
  809. static void __sock_set_timestamps(struct sock *sk, bool val, bool new, bool ns)
  810. {
  811. if (val) {
  812. sock_valbool_flag(sk, SOCK_TSTAMP_NEW, new);
  813. sock_valbool_flag(sk, SOCK_RCVTSTAMPNS, ns);
  814. sock_set_flag(sk, SOCK_RCVTSTAMP);
  815. sock_enable_timestamp(sk, SOCK_TIMESTAMP);
  816. } else {
  817. sock_reset_flag(sk, SOCK_RCVTSTAMP);
  818. sock_reset_flag(sk, SOCK_RCVTSTAMPNS);
  819. }
  820. }
  821. void sock_enable_timestamps(struct sock *sk)
  822. {
  823. lock_sock(sk);
  824. __sock_set_timestamps(sk, true, false, true);
  825. release_sock(sk);
  826. }
  827. EXPORT_SYMBOL(sock_enable_timestamps);
  828. void sock_set_timestamp(struct sock *sk, int optname, bool valbool)
  829. {
  830. switch (optname) {
  831. case SO_TIMESTAMP_OLD:
  832. __sock_set_timestamps(sk, valbool, false, false);
  833. break;
  834. case SO_TIMESTAMP_NEW:
  835. __sock_set_timestamps(sk, valbool, true, false);
  836. break;
  837. case SO_TIMESTAMPNS_OLD:
  838. __sock_set_timestamps(sk, valbool, false, true);
  839. break;
  840. case SO_TIMESTAMPNS_NEW:
  841. __sock_set_timestamps(sk, valbool, true, true);
  842. break;
  843. }
  844. }
  845. static int sock_timestamping_bind_phc(struct sock *sk, int phc_index)
  846. {
  847. struct net *net = sock_net(sk);
  848. struct net_device *dev = NULL;
  849. bool match = false;
  850. int *vclock_index;
  851. int i, num;
  852. if (sk->sk_bound_dev_if)
  853. dev = dev_get_by_index(net, sk->sk_bound_dev_if);
  854. if (!dev) {
  855. pr_err("%s: sock not bind to device\n", __func__);
  856. return -EOPNOTSUPP;
  857. }
  858. num = ethtool_get_phc_vclocks(dev, &vclock_index);
  859. dev_put(dev);
  860. for (i = 0; i < num; i++) {
  861. if (*(vclock_index + i) == phc_index) {
  862. match = true;
  863. break;
  864. }
  865. }
  866. if (num > 0)
  867. kfree(vclock_index);
  868. if (!match)
  869. return -EINVAL;
  870. sk->sk_bind_phc = phc_index;
  871. return 0;
  872. }
  873. int sock_set_timestamping(struct sock *sk, int optname,
  874. struct so_timestamping timestamping)
  875. {
  876. int val = timestamping.flags;
  877. int ret;
  878. if (val & ~SOF_TIMESTAMPING_MASK)
  879. return -EINVAL;
  880. if (val & SOF_TIMESTAMPING_OPT_ID &&
  881. !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)) {
  882. if (sk_is_tcp(sk)) {
  883. if ((1 << sk->sk_state) &
  884. (TCPF_CLOSE | TCPF_LISTEN))
  885. return -EINVAL;
  886. atomic_set(&sk->sk_tskey, tcp_sk(sk)->snd_una);
  887. } else {
  888. atomic_set(&sk->sk_tskey, 0);
  889. }
  890. }
  891. if (val & SOF_TIMESTAMPING_OPT_STATS &&
  892. !(val & SOF_TIMESTAMPING_OPT_TSONLY))
  893. return -EINVAL;
  894. if (val & SOF_TIMESTAMPING_BIND_PHC) {
  895. ret = sock_timestamping_bind_phc(sk, timestamping.bind_phc);
  896. if (ret)
  897. return ret;
  898. }
  899. sk->sk_tsflags = val;
  900. sock_valbool_flag(sk, SOCK_TSTAMP_NEW, optname == SO_TIMESTAMPING_NEW);
  901. if (val & SOF_TIMESTAMPING_RX_SOFTWARE)
  902. sock_enable_timestamp(sk,
  903. SOCK_TIMESTAMPING_RX_SOFTWARE);
  904. else
  905. sock_disable_timestamp(sk,
  906. (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE));
  907. return 0;
  908. }
  909. void sock_set_keepalive(struct sock *sk)
  910. {
  911. lock_sock(sk);
  912. if (sk->sk_prot->keepalive)
  913. sk->sk_prot->keepalive(sk, true);
  914. sock_valbool_flag(sk, SOCK_KEEPOPEN, true);
  915. release_sock(sk);
  916. }
  917. EXPORT_SYMBOL(sock_set_keepalive);
  918. static void __sock_set_rcvbuf(struct sock *sk, int val)
  919. {
  920. /* Ensure val * 2 fits into an int, to prevent max_t() from treating it
  921. * as a negative value.
  922. */
  923. val = min_t(int, val, INT_MAX / 2);
  924. sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
  925. /* We double it on the way in to account for "struct sk_buff" etc.
  926. * overhead. Applications assume that the SO_RCVBUF setting they make
  927. * will allow that much actual data to be received on that socket.
  928. *
  929. * Applications are unaware that "struct sk_buff" and other overheads
  930. * allocate from the receive buffer during socket buffer allocation.
  931. *
  932. * And after considering the possible alternatives, returning the value
  933. * we actually used in getsockopt is the most desirable behavior.
  934. */
  935. WRITE_ONCE(sk->sk_rcvbuf, max_t(int, val * 2, SOCK_MIN_RCVBUF));
  936. }
  937. void sock_set_rcvbuf(struct sock *sk, int val)
  938. {
  939. lock_sock(sk);
  940. __sock_set_rcvbuf(sk, val);
  941. release_sock(sk);
  942. }
  943. EXPORT_SYMBOL(sock_set_rcvbuf);
  944. static void __sock_set_mark(struct sock *sk, u32 val)
  945. {
  946. if (val != sk->sk_mark) {
  947. WRITE_ONCE(sk->sk_mark, val);
  948. sk_dst_reset(sk);
  949. }
  950. }
  951. void sock_set_mark(struct sock *sk, u32 val)
  952. {
  953. lock_sock(sk);
  954. __sock_set_mark(sk, val);
  955. release_sock(sk);
  956. }
  957. EXPORT_SYMBOL(sock_set_mark);
  958. static void sock_release_reserved_memory(struct sock *sk, int bytes)
  959. {
  960. /* Round down bytes to multiple of pages */
  961. bytes = round_down(bytes, PAGE_SIZE);
  962. WARN_ON(bytes > sk->sk_reserved_mem);
  963. WRITE_ONCE(sk->sk_reserved_mem, sk->sk_reserved_mem - bytes);
  964. sk_mem_reclaim(sk);
  965. }
  966. static int sock_reserve_memory(struct sock *sk, int bytes)
  967. {
  968. long allocated;
  969. bool charged;
  970. int pages;
  971. if (!mem_cgroup_sockets_enabled || !sk->sk_memcg || !sk_has_account(sk))
  972. return -EOPNOTSUPP;
  973. if (!bytes)
  974. return 0;
  975. pages = sk_mem_pages(bytes);
  976. /* pre-charge to memcg */
  977. charged = mem_cgroup_charge_skmem(sk->sk_memcg, pages,
  978. GFP_KERNEL | __GFP_RETRY_MAYFAIL);
  979. if (!charged)
  980. return -ENOMEM;
  981. /* pre-charge to forward_alloc */
  982. sk_memory_allocated_add(sk, pages);
  983. allocated = sk_memory_allocated(sk);
  984. /* If the system goes into memory pressure with this
  985. * precharge, give up and return error.
  986. */
  987. if (allocated > sk_prot_mem_limits(sk, 1)) {
  988. sk_memory_allocated_sub(sk, pages);
  989. mem_cgroup_uncharge_skmem(sk->sk_memcg, pages);
  990. return -ENOMEM;
  991. }
  992. sk_forward_alloc_add(sk, pages << PAGE_SHIFT);
  993. WRITE_ONCE(sk->sk_reserved_mem,
  994. sk->sk_reserved_mem + (pages << PAGE_SHIFT));
  995. return 0;
  996. }
  997. void sockopt_lock_sock(struct sock *sk)
  998. {
  999. /* When current->bpf_ctx is set, the setsockopt is called from
  1000. * a bpf prog. bpf has ensured the sk lock has been
  1001. * acquired before calling setsockopt().
  1002. */
  1003. if (has_current_bpf_ctx())
  1004. return;
  1005. lock_sock(sk);
  1006. }
  1007. EXPORT_SYMBOL(sockopt_lock_sock);
  1008. void sockopt_release_sock(struct sock *sk)
  1009. {
  1010. if (has_current_bpf_ctx())
  1011. return;
  1012. release_sock(sk);
  1013. }
  1014. EXPORT_SYMBOL(sockopt_release_sock);
  1015. bool sockopt_ns_capable(struct user_namespace *ns, int cap)
  1016. {
  1017. return has_current_bpf_ctx() || ns_capable(ns, cap);
  1018. }
  1019. EXPORT_SYMBOL(sockopt_ns_capable);
  1020. bool sockopt_capable(int cap)
  1021. {
  1022. return has_current_bpf_ctx() || capable(cap);
  1023. }
  1024. EXPORT_SYMBOL(sockopt_capable);
  1025. /*
  1026. * This is meant for all protocols to use and covers goings on
  1027. * at the socket level. Everything here is generic.
  1028. */
  1029. int sk_setsockopt(struct sock *sk, int level, int optname,
  1030. sockptr_t optval, unsigned int optlen)
  1031. {
  1032. struct so_timestamping timestamping;
  1033. struct socket *sock = sk->sk_socket;
  1034. struct sock_txtime sk_txtime;
  1035. int val;
  1036. int valbool;
  1037. struct linger ling;
  1038. int ret = 0;
  1039. /*
  1040. * Options without arguments
  1041. */
  1042. if (optname == SO_BINDTODEVICE)
  1043. return sock_setbindtodevice(sk, optval, optlen);
  1044. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA {
  1045. #ifdef CONFIG_KNOX_NCM
  1046. if (optname == SO_SET_DOMAIN_NAME)
  1047. return sock_set_domain_name(sk, optval, optlen);
  1048. if (optname == SO_SET_DNS_UID)
  1049. return sock_set_dns_uid(sk, optval, optlen);
  1050. if (optname == SO_SET_DNS_PID)
  1051. return sock_set_dns_pid(sk, optval, optlen);
  1052. #endif
  1053. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA }
  1054. if (optlen < sizeof(int))
  1055. return -EINVAL;
  1056. if (copy_from_sockptr(&val, optval, sizeof(val)))
  1057. return -EFAULT;
  1058. valbool = val ? 1 : 0;
  1059. sockopt_lock_sock(sk);
  1060. switch (optname) {
  1061. case SO_DEBUG:
  1062. if (val && !sockopt_capable(CAP_NET_ADMIN))
  1063. ret = -EACCES;
  1064. else
  1065. sock_valbool_flag(sk, SOCK_DBG, valbool);
  1066. break;
  1067. case SO_REUSEADDR:
  1068. sk->sk_reuse = (valbool ? SK_CAN_REUSE : SK_NO_REUSE);
  1069. break;
  1070. case SO_REUSEPORT:
  1071. sk->sk_reuseport = valbool;
  1072. break;
  1073. case SO_TYPE:
  1074. case SO_PROTOCOL:
  1075. case SO_DOMAIN:
  1076. case SO_ERROR:
  1077. ret = -ENOPROTOOPT;
  1078. break;
  1079. case SO_DONTROUTE:
  1080. sock_valbool_flag(sk, SOCK_LOCALROUTE, valbool);
  1081. sk_dst_reset(sk);
  1082. break;
  1083. case SO_BROADCAST:
  1084. sock_valbool_flag(sk, SOCK_BROADCAST, valbool);
  1085. break;
  1086. case SO_SNDBUF:
  1087. /* Don't error on this BSD doesn't and if you think
  1088. * about it this is right. Otherwise apps have to
  1089. * play 'guess the biggest size' games. RCVBUF/SNDBUF
  1090. * are treated in BSD as hints
  1091. */
  1092. val = min_t(u32, val, READ_ONCE(sysctl_wmem_max));
  1093. set_sndbuf:
  1094. /* Ensure val * 2 fits into an int, to prevent max_t()
  1095. * from treating it as a negative value.
  1096. */
  1097. val = min_t(int, val, INT_MAX / 2);
  1098. sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
  1099. WRITE_ONCE(sk->sk_sndbuf,
  1100. max_t(int, val * 2, SOCK_MIN_SNDBUF));
  1101. /* Wake up sending tasks if we upped the value. */
  1102. sk->sk_write_space(sk);
  1103. break;
  1104. case SO_SNDBUFFORCE:
  1105. if (!sockopt_capable(CAP_NET_ADMIN)) {
  1106. ret = -EPERM;
  1107. break;
  1108. }
  1109. /* No negative values (to prevent underflow, as val will be
  1110. * multiplied by 2).
  1111. */
  1112. if (val < 0)
  1113. val = 0;
  1114. goto set_sndbuf;
  1115. case SO_RCVBUF:
  1116. /* Don't error on this BSD doesn't and if you think
  1117. * about it this is right. Otherwise apps have to
  1118. * play 'guess the biggest size' games. RCVBUF/SNDBUF
  1119. * are treated in BSD as hints
  1120. */
  1121. __sock_set_rcvbuf(sk, min_t(u32, val, READ_ONCE(sysctl_rmem_max)));
  1122. break;
  1123. case SO_RCVBUFFORCE:
  1124. if (!sockopt_capable(CAP_NET_ADMIN)) {
  1125. ret = -EPERM;
  1126. break;
  1127. }
  1128. /* No negative values (to prevent underflow, as val will be
  1129. * multiplied by 2).
  1130. */
  1131. __sock_set_rcvbuf(sk, max(val, 0));
  1132. break;
  1133. case SO_KEEPALIVE:
  1134. if (sk->sk_prot->keepalive)
  1135. sk->sk_prot->keepalive(sk, valbool);
  1136. sock_valbool_flag(sk, SOCK_KEEPOPEN, valbool);
  1137. break;
  1138. case SO_OOBINLINE:
  1139. sock_valbool_flag(sk, SOCK_URGINLINE, valbool);
  1140. break;
  1141. case SO_NO_CHECK:
  1142. sk->sk_no_check_tx = valbool;
  1143. break;
  1144. case SO_PRIORITY:
  1145. if ((val >= 0 && val <= 6) ||
  1146. sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) ||
  1147. sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
  1148. WRITE_ONCE(sk->sk_priority, val);
  1149. else
  1150. ret = -EPERM;
  1151. break;
  1152. case SO_LINGER:
  1153. if (optlen < sizeof(ling)) {
  1154. ret = -EINVAL; /* 1003.1g */
  1155. break;
  1156. }
  1157. if (copy_from_sockptr(&ling, optval, sizeof(ling))) {
  1158. ret = -EFAULT;
  1159. break;
  1160. }
  1161. if (!ling.l_onoff) {
  1162. sock_reset_flag(sk, SOCK_LINGER);
  1163. } else {
  1164. unsigned long t_sec = ling.l_linger;
  1165. if (t_sec >= MAX_SCHEDULE_TIMEOUT / HZ)
  1166. WRITE_ONCE(sk->sk_lingertime, MAX_SCHEDULE_TIMEOUT);
  1167. else
  1168. WRITE_ONCE(sk->sk_lingertime, t_sec * HZ);
  1169. sock_set_flag(sk, SOCK_LINGER);
  1170. }
  1171. break;
  1172. case SO_BSDCOMPAT:
  1173. break;
  1174. case SO_PASSCRED:
  1175. if (valbool)
  1176. set_bit(SOCK_PASSCRED, &sock->flags);
  1177. else
  1178. clear_bit(SOCK_PASSCRED, &sock->flags);
  1179. break;
  1180. case SO_TIMESTAMP_OLD:
  1181. case SO_TIMESTAMP_NEW:
  1182. case SO_TIMESTAMPNS_OLD:
  1183. case SO_TIMESTAMPNS_NEW:
  1184. sock_set_timestamp(sk, optname, valbool);
  1185. break;
  1186. case SO_TIMESTAMPING_NEW:
  1187. case SO_TIMESTAMPING_OLD:
  1188. if (optlen == sizeof(timestamping)) {
  1189. if (copy_from_sockptr(&timestamping, optval,
  1190. sizeof(timestamping))) {
  1191. ret = -EFAULT;
  1192. break;
  1193. }
  1194. } else {
  1195. memset(&timestamping, 0, sizeof(timestamping));
  1196. timestamping.flags = val;
  1197. }
  1198. ret = sock_set_timestamping(sk, optname, timestamping);
  1199. break;
  1200. case SO_RCVLOWAT:
  1201. if (val < 0)
  1202. val = INT_MAX;
  1203. if (sock && sock->ops->set_rcvlowat)
  1204. ret = sock->ops->set_rcvlowat(sk, val);
  1205. else
  1206. WRITE_ONCE(sk->sk_rcvlowat, val ? : 1);
  1207. break;
  1208. case SO_RCVTIMEO_OLD:
  1209. case SO_RCVTIMEO_NEW:
  1210. ret = sock_set_timeout(&sk->sk_rcvtimeo, optval,
  1211. optlen, optname == SO_RCVTIMEO_OLD);
  1212. break;
  1213. case SO_SNDTIMEO_OLD:
  1214. case SO_SNDTIMEO_NEW:
  1215. ret = sock_set_timeout(&sk->sk_sndtimeo, optval,
  1216. optlen, optname == SO_SNDTIMEO_OLD);
  1217. break;
  1218. case SO_ATTACH_FILTER: {
  1219. struct sock_fprog fprog;
  1220. ret = copy_bpf_fprog_from_user(&fprog, optval, optlen);
  1221. if (!ret)
  1222. ret = sk_attach_filter(&fprog, sk);
  1223. break;
  1224. }
  1225. case SO_ATTACH_BPF:
  1226. ret = -EINVAL;
  1227. if (optlen == sizeof(u32)) {
  1228. u32 ufd;
  1229. ret = -EFAULT;
  1230. if (copy_from_sockptr(&ufd, optval, sizeof(ufd)))
  1231. break;
  1232. ret = sk_attach_bpf(ufd, sk);
  1233. }
  1234. break;
  1235. case SO_ATTACH_REUSEPORT_CBPF: {
  1236. struct sock_fprog fprog;
  1237. ret = copy_bpf_fprog_from_user(&fprog, optval, optlen);
  1238. if (!ret)
  1239. ret = sk_reuseport_attach_filter(&fprog, sk);
  1240. break;
  1241. }
  1242. case SO_ATTACH_REUSEPORT_EBPF:
  1243. ret = -EINVAL;
  1244. if (optlen == sizeof(u32)) {
  1245. u32 ufd;
  1246. ret = -EFAULT;
  1247. if (copy_from_sockptr(&ufd, optval, sizeof(ufd)))
  1248. break;
  1249. ret = sk_reuseport_attach_bpf(ufd, sk);
  1250. }
  1251. break;
  1252. case SO_DETACH_REUSEPORT_BPF:
  1253. ret = reuseport_detach_prog(sk);
  1254. break;
  1255. case SO_DETACH_FILTER:
  1256. ret = sk_detach_filter(sk);
  1257. break;
  1258. case SO_LOCK_FILTER:
  1259. if (sock_flag(sk, SOCK_FILTER_LOCKED) && !valbool)
  1260. ret = -EPERM;
  1261. else
  1262. sock_valbool_flag(sk, SOCK_FILTER_LOCKED, valbool);
  1263. break;
  1264. case SO_PASSSEC:
  1265. if (valbool)
  1266. set_bit(SOCK_PASSSEC, &sock->flags);
  1267. else
  1268. clear_bit(SOCK_PASSSEC, &sock->flags);
  1269. break;
  1270. case SO_MARK:
  1271. if (!sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
  1272. !sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
  1273. ret = -EPERM;
  1274. break;
  1275. }
  1276. __sock_set_mark(sk, val);
  1277. break;
  1278. case SO_RCVMARK:
  1279. sock_valbool_flag(sk, SOCK_RCVMARK, valbool);
  1280. break;
  1281. case SO_RXQ_OVFL:
  1282. sock_valbool_flag(sk, SOCK_RXQ_OVFL, valbool);
  1283. break;
  1284. case SO_WIFI_STATUS:
  1285. sock_valbool_flag(sk, SOCK_WIFI_STATUS, valbool);
  1286. break;
  1287. case SO_PEEK_OFF:
  1288. if (sock->ops->set_peek_off)
  1289. ret = sock->ops->set_peek_off(sk, val);
  1290. else
  1291. ret = -EOPNOTSUPP;
  1292. break;
  1293. case SO_NOFCS:
  1294. sock_valbool_flag(sk, SOCK_NOFCS, valbool);
  1295. break;
  1296. case SO_SELECT_ERR_QUEUE:
  1297. sock_valbool_flag(sk, SOCK_SELECT_ERR_QUEUE, valbool);
  1298. break;
  1299. #ifdef CONFIG_NET_RX_BUSY_POLL
  1300. case SO_BUSY_POLL:
  1301. /* allow unprivileged users to decrease the value */
  1302. if ((val > sk->sk_ll_usec) && !sockopt_capable(CAP_NET_ADMIN))
  1303. ret = -EPERM;
  1304. else {
  1305. if (val < 0)
  1306. ret = -EINVAL;
  1307. else
  1308. WRITE_ONCE(sk->sk_ll_usec, val);
  1309. }
  1310. break;
  1311. case SO_PREFER_BUSY_POLL:
  1312. if (valbool && !sockopt_capable(CAP_NET_ADMIN))
  1313. ret = -EPERM;
  1314. else
  1315. WRITE_ONCE(sk->sk_prefer_busy_poll, valbool);
  1316. break;
  1317. case SO_BUSY_POLL_BUDGET:
  1318. if (val > READ_ONCE(sk->sk_busy_poll_budget) && !sockopt_capable(CAP_NET_ADMIN)) {
  1319. ret = -EPERM;
  1320. } else {
  1321. if (val < 0 || val > U16_MAX)
  1322. ret = -EINVAL;
  1323. else
  1324. WRITE_ONCE(sk->sk_busy_poll_budget, val);
  1325. }
  1326. break;
  1327. #endif
  1328. case SO_MAX_PACING_RATE:
  1329. {
  1330. unsigned long ulval = (val == ~0U) ? ~0UL : (unsigned int)val;
  1331. if (sizeof(ulval) != sizeof(val) &&
  1332. optlen >= sizeof(ulval) &&
  1333. copy_from_sockptr(&ulval, optval, sizeof(ulval))) {
  1334. ret = -EFAULT;
  1335. break;
  1336. }
  1337. if (ulval != ~0UL)
  1338. cmpxchg(&sk->sk_pacing_status,
  1339. SK_PACING_NONE,
  1340. SK_PACING_NEEDED);
  1341. /* Pairs with READ_ONCE() from sk_getsockopt() */
  1342. WRITE_ONCE(sk->sk_max_pacing_rate, ulval);
  1343. sk->sk_pacing_rate = min(sk->sk_pacing_rate, ulval);
  1344. break;
  1345. }
  1346. case SO_INCOMING_CPU:
  1347. reuseport_update_incoming_cpu(sk, val);
  1348. break;
  1349. case SO_CNX_ADVICE:
  1350. if (val == 1)
  1351. dst_negative_advice(sk);
  1352. break;
  1353. case SO_ZEROCOPY:
  1354. if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6) {
  1355. if (!(sk_is_tcp(sk) ||
  1356. (sk->sk_type == SOCK_DGRAM &&
  1357. sk->sk_protocol == IPPROTO_UDP)))
  1358. ret = -EOPNOTSUPP;
  1359. } else if (sk->sk_family != PF_RDS) {
  1360. ret = -EOPNOTSUPP;
  1361. }
  1362. if (!ret) {
  1363. if (val < 0 || val > 1)
  1364. ret = -EINVAL;
  1365. else
  1366. sock_valbool_flag(sk, SOCK_ZEROCOPY, valbool);
  1367. }
  1368. break;
  1369. case SO_TXTIME:
  1370. if (optlen != sizeof(struct sock_txtime)) {
  1371. ret = -EINVAL;
  1372. break;
  1373. } else if (copy_from_sockptr(&sk_txtime, optval,
  1374. sizeof(struct sock_txtime))) {
  1375. ret = -EFAULT;
  1376. break;
  1377. } else if (sk_txtime.flags & ~SOF_TXTIME_FLAGS_MASK) {
  1378. ret = -EINVAL;
  1379. break;
  1380. }
  1381. /* CLOCK_MONOTONIC is only used by sch_fq, and this packet
  1382. * scheduler has enough safe guards.
  1383. */
  1384. if (sk_txtime.clockid != CLOCK_MONOTONIC &&
  1385. !sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
  1386. ret = -EPERM;
  1387. break;
  1388. }
  1389. sock_valbool_flag(sk, SOCK_TXTIME, true);
  1390. sk->sk_clockid = sk_txtime.clockid;
  1391. sk->sk_txtime_deadline_mode =
  1392. !!(sk_txtime.flags & SOF_TXTIME_DEADLINE_MODE);
  1393. sk->sk_txtime_report_errors =
  1394. !!(sk_txtime.flags & SOF_TXTIME_REPORT_ERRORS);
  1395. break;
  1396. case SO_BINDTOIFINDEX:
  1397. ret = sock_bindtoindex_locked(sk, val);
  1398. break;
  1399. case SO_BUF_LOCK:
  1400. if (val & ~SOCK_BUF_LOCK_MASK) {
  1401. ret = -EINVAL;
  1402. break;
  1403. }
  1404. sk->sk_userlocks = val | (sk->sk_userlocks &
  1405. ~SOCK_BUF_LOCK_MASK);
  1406. break;
  1407. case SO_RESERVE_MEM:
  1408. {
  1409. int delta;
  1410. if (val < 0) {
  1411. ret = -EINVAL;
  1412. break;
  1413. }
  1414. delta = val - sk->sk_reserved_mem;
  1415. if (delta < 0)
  1416. sock_release_reserved_memory(sk, -delta);
  1417. else
  1418. ret = sock_reserve_memory(sk, delta);
  1419. break;
  1420. }
  1421. case SO_TXREHASH:
  1422. if (val < -1 || val > 1) {
  1423. ret = -EINVAL;
  1424. break;
  1425. }
  1426. if ((u8)val == SOCK_TXREHASH_DEFAULT)
  1427. val = READ_ONCE(sock_net(sk)->core.sysctl_txrehash);
  1428. /* Paired with READ_ONCE() in tcp_rtx_synack()
  1429. * and sk_getsockopt().
  1430. */
  1431. WRITE_ONCE(sk->sk_txrehash, (u8)val);
  1432. break;
  1433. default:
  1434. ret = -ENOPROTOOPT;
  1435. break;
  1436. }
  1437. sockopt_release_sock(sk);
  1438. return ret;
  1439. }
  1440. int sock_setsockopt(struct socket *sock, int level, int optname,
  1441. sockptr_t optval, unsigned int optlen)
  1442. {
  1443. return sk_setsockopt(sock->sk, level, optname,
  1444. optval, optlen);
  1445. }
  1446. EXPORT_SYMBOL(sock_setsockopt);
  1447. static const struct cred *sk_get_peer_cred(struct sock *sk)
  1448. {
  1449. const struct cred *cred;
  1450. spin_lock(&sk->sk_peer_lock);
  1451. cred = get_cred(sk->sk_peer_cred);
  1452. spin_unlock(&sk->sk_peer_lock);
  1453. return cred;
  1454. }
  1455. static void cred_to_ucred(struct pid *pid, const struct cred *cred,
  1456. struct ucred *ucred)
  1457. {
  1458. ucred->pid = pid_vnr(pid);
  1459. ucred->uid = ucred->gid = -1;
  1460. if (cred) {
  1461. struct user_namespace *current_ns = current_user_ns();
  1462. ucred->uid = from_kuid_munged(current_ns, cred->euid);
  1463. ucred->gid = from_kgid_munged(current_ns, cred->egid);
  1464. }
  1465. }
  1466. static int groups_to_user(sockptr_t dst, const struct group_info *src)
  1467. {
  1468. struct user_namespace *user_ns = current_user_ns();
  1469. int i;
  1470. for (i = 0; i < src->ngroups; i++) {
  1471. gid_t gid = from_kgid_munged(user_ns, src->gid[i]);
  1472. if (copy_to_sockptr_offset(dst, i * sizeof(gid), &gid, sizeof(gid)))
  1473. return -EFAULT;
  1474. }
  1475. return 0;
  1476. }
  1477. int sk_getsockopt(struct sock *sk, int level, int optname,
  1478. sockptr_t optval, sockptr_t optlen)
  1479. {
  1480. struct socket *sock = sk->sk_socket;
  1481. union {
  1482. int val;
  1483. u64 val64;
  1484. unsigned long ulval;
  1485. struct linger ling;
  1486. struct old_timeval32 tm32;
  1487. struct __kernel_old_timeval tm;
  1488. struct __kernel_sock_timeval stm;
  1489. struct sock_txtime txtime;
  1490. struct so_timestamping timestamping;
  1491. } v;
  1492. int lv = sizeof(int);
  1493. int len;
  1494. if (copy_from_sockptr(&len, optlen, sizeof(int)))
  1495. return -EFAULT;
  1496. if (len < 0)
  1497. return -EINVAL;
  1498. memset(&v, 0, sizeof(v));
  1499. switch (optname) {
  1500. case SO_DEBUG:
  1501. v.val = sock_flag(sk, SOCK_DBG);
  1502. break;
  1503. case SO_DONTROUTE:
  1504. v.val = sock_flag(sk, SOCK_LOCALROUTE);
  1505. break;
  1506. case SO_BROADCAST:
  1507. v.val = sock_flag(sk, SOCK_BROADCAST);
  1508. break;
  1509. case SO_SNDBUF:
  1510. v.val = READ_ONCE(sk->sk_sndbuf);
  1511. break;
  1512. case SO_RCVBUF:
  1513. v.val = READ_ONCE(sk->sk_rcvbuf);
  1514. break;
  1515. case SO_REUSEADDR:
  1516. v.val = sk->sk_reuse;
  1517. break;
  1518. case SO_REUSEPORT:
  1519. v.val = sk->sk_reuseport;
  1520. break;
  1521. case SO_KEEPALIVE:
  1522. v.val = sock_flag(sk, SOCK_KEEPOPEN);
  1523. break;
  1524. case SO_TYPE:
  1525. v.val = sk->sk_type;
  1526. break;
  1527. case SO_PROTOCOL:
  1528. v.val = sk->sk_protocol;
  1529. break;
  1530. case SO_DOMAIN:
  1531. v.val = sk->sk_family;
  1532. break;
  1533. case SO_ERROR:
  1534. v.val = -sock_error(sk);
  1535. if (v.val == 0)
  1536. v.val = xchg(&sk->sk_err_soft, 0);
  1537. break;
  1538. case SO_OOBINLINE:
  1539. v.val = sock_flag(sk, SOCK_URGINLINE);
  1540. break;
  1541. case SO_NO_CHECK:
  1542. v.val = sk->sk_no_check_tx;
  1543. break;
  1544. case SO_PRIORITY:
  1545. v.val = READ_ONCE(sk->sk_priority);
  1546. break;
  1547. case SO_LINGER:
  1548. lv = sizeof(v.ling);
  1549. v.ling.l_onoff = sock_flag(sk, SOCK_LINGER);
  1550. v.ling.l_linger = READ_ONCE(sk->sk_lingertime) / HZ;
  1551. break;
  1552. case SO_BSDCOMPAT:
  1553. break;
  1554. case SO_TIMESTAMP_OLD:
  1555. v.val = sock_flag(sk, SOCK_RCVTSTAMP) &&
  1556. !sock_flag(sk, SOCK_TSTAMP_NEW) &&
  1557. !sock_flag(sk, SOCK_RCVTSTAMPNS);
  1558. break;
  1559. case SO_TIMESTAMPNS_OLD:
  1560. v.val = sock_flag(sk, SOCK_RCVTSTAMPNS) && !sock_flag(sk, SOCK_TSTAMP_NEW);
  1561. break;
  1562. case SO_TIMESTAMP_NEW:
  1563. v.val = sock_flag(sk, SOCK_RCVTSTAMP) && sock_flag(sk, SOCK_TSTAMP_NEW);
  1564. break;
  1565. case SO_TIMESTAMPNS_NEW:
  1566. v.val = sock_flag(sk, SOCK_RCVTSTAMPNS) && sock_flag(sk, SOCK_TSTAMP_NEW);
  1567. break;
  1568. case SO_TIMESTAMPING_OLD:
  1569. lv = sizeof(v.timestamping);
  1570. v.timestamping.flags = sk->sk_tsflags;
  1571. v.timestamping.bind_phc = sk->sk_bind_phc;
  1572. break;
  1573. case SO_RCVTIMEO_OLD:
  1574. case SO_RCVTIMEO_NEW:
  1575. lv = sock_get_timeout(READ_ONCE(sk->sk_rcvtimeo), &v,
  1576. SO_RCVTIMEO_OLD == optname);
  1577. break;
  1578. case SO_SNDTIMEO_OLD:
  1579. case SO_SNDTIMEO_NEW:
  1580. lv = sock_get_timeout(READ_ONCE(sk->sk_sndtimeo), &v,
  1581. SO_SNDTIMEO_OLD == optname);
  1582. break;
  1583. case SO_RCVLOWAT:
  1584. v.val = READ_ONCE(sk->sk_rcvlowat);
  1585. break;
  1586. case SO_SNDLOWAT:
  1587. v.val = 1;
  1588. break;
  1589. case SO_PASSCRED:
  1590. v.val = !!test_bit(SOCK_PASSCRED, &sock->flags);
  1591. break;
  1592. case SO_PEERCRED:
  1593. {
  1594. struct ucred peercred;
  1595. if (len > sizeof(peercred))
  1596. len = sizeof(peercred);
  1597. spin_lock(&sk->sk_peer_lock);
  1598. cred_to_ucred(sk->sk_peer_pid, sk->sk_peer_cred, &peercred);
  1599. spin_unlock(&sk->sk_peer_lock);
  1600. if (copy_to_sockptr(optval, &peercred, len))
  1601. return -EFAULT;
  1602. goto lenout;
  1603. }
  1604. case SO_PEERGROUPS:
  1605. {
  1606. const struct cred *cred;
  1607. int ret, n;
  1608. cred = sk_get_peer_cred(sk);
  1609. if (!cred)
  1610. return -ENODATA;
  1611. n = cred->group_info->ngroups;
  1612. if (len < n * sizeof(gid_t)) {
  1613. len = n * sizeof(gid_t);
  1614. put_cred(cred);
  1615. return copy_to_sockptr(optlen, &len, sizeof(int)) ? -EFAULT : -ERANGE;
  1616. }
  1617. len = n * sizeof(gid_t);
  1618. ret = groups_to_user(optval, cred->group_info);
  1619. put_cred(cred);
  1620. if (ret)
  1621. return ret;
  1622. goto lenout;
  1623. }
  1624. case SO_PEERNAME:
  1625. {
  1626. struct sockaddr_storage address;
  1627. lv = sock->ops->getname(sock, (struct sockaddr *)&address, 2);
  1628. if (lv < 0)
  1629. return -ENOTCONN;
  1630. if (lv < len)
  1631. return -EINVAL;
  1632. if (copy_to_sockptr(optval, &address, len))
  1633. return -EFAULT;
  1634. goto lenout;
  1635. }
  1636. /* Dubious BSD thing... Probably nobody even uses it, but
  1637. * the UNIX standard wants it for whatever reason... -DaveM
  1638. */
  1639. case SO_ACCEPTCONN:
  1640. v.val = sk->sk_state == TCP_LISTEN;
  1641. break;
  1642. case SO_PASSSEC:
  1643. v.val = !!test_bit(SOCK_PASSSEC, &sock->flags);
  1644. break;
  1645. case SO_PEERSEC:
  1646. return security_socket_getpeersec_stream(sock, optval.user, optlen.user, len);
  1647. case SO_MARK:
  1648. v.val = READ_ONCE(sk->sk_mark);
  1649. break;
  1650. case SO_RCVMARK:
  1651. v.val = sock_flag(sk, SOCK_RCVMARK);
  1652. break;
  1653. case SO_RXQ_OVFL:
  1654. v.val = sock_flag(sk, SOCK_RXQ_OVFL);
  1655. break;
  1656. case SO_WIFI_STATUS:
  1657. v.val = sock_flag(sk, SOCK_WIFI_STATUS);
  1658. break;
  1659. case SO_PEEK_OFF:
  1660. if (!sock->ops->set_peek_off)
  1661. return -EOPNOTSUPP;
  1662. v.val = READ_ONCE(sk->sk_peek_off);
  1663. break;
  1664. case SO_NOFCS:
  1665. v.val = sock_flag(sk, SOCK_NOFCS);
  1666. break;
  1667. case SO_BINDTODEVICE:
  1668. return sock_getbindtodevice(sk, optval, optlen, len);
  1669. case SO_GET_FILTER:
  1670. len = sk_get_filter(sk, optval, len);
  1671. if (len < 0)
  1672. return len;
  1673. goto lenout;
  1674. case SO_LOCK_FILTER:
  1675. v.val = sock_flag(sk, SOCK_FILTER_LOCKED);
  1676. break;
  1677. case SO_BPF_EXTENSIONS:
  1678. v.val = bpf_tell_extensions();
  1679. break;
  1680. case SO_SELECT_ERR_QUEUE:
  1681. v.val = sock_flag(sk, SOCK_SELECT_ERR_QUEUE);
  1682. break;
  1683. #ifdef CONFIG_NET_RX_BUSY_POLL
  1684. case SO_BUSY_POLL:
  1685. v.val = READ_ONCE(sk->sk_ll_usec);
  1686. break;
  1687. case SO_PREFER_BUSY_POLL:
  1688. v.val = READ_ONCE(sk->sk_prefer_busy_poll);
  1689. break;
  1690. #endif
  1691. case SO_MAX_PACING_RATE:
  1692. /* The READ_ONCE() pair with the WRITE_ONCE() in sk_setsockopt() */
  1693. if (sizeof(v.ulval) != sizeof(v.val) && len >= sizeof(v.ulval)) {
  1694. lv = sizeof(v.ulval);
  1695. v.ulval = READ_ONCE(sk->sk_max_pacing_rate);
  1696. } else {
  1697. /* 32bit version */
  1698. v.val = min_t(unsigned long, ~0U,
  1699. READ_ONCE(sk->sk_max_pacing_rate));
  1700. }
  1701. break;
  1702. case SO_INCOMING_CPU:
  1703. v.val = READ_ONCE(sk->sk_incoming_cpu);
  1704. break;
  1705. case SO_MEMINFO:
  1706. {
  1707. u32 meminfo[SK_MEMINFO_VARS];
  1708. sk_get_meminfo(sk, meminfo);
  1709. len = min_t(unsigned int, len, sizeof(meminfo));
  1710. if (copy_to_sockptr(optval, &meminfo, len))
  1711. return -EFAULT;
  1712. goto lenout;
  1713. }
  1714. #ifdef CONFIG_NET_RX_BUSY_POLL
  1715. case SO_INCOMING_NAPI_ID:
  1716. v.val = READ_ONCE(sk->sk_napi_id);
  1717. /* aggregate non-NAPI IDs down to 0 */
  1718. if (v.val < MIN_NAPI_ID)
  1719. v.val = 0;
  1720. break;
  1721. #endif
  1722. case SO_COOKIE:
  1723. lv = sizeof(u64);
  1724. if (len < lv)
  1725. return -EINVAL;
  1726. v.val64 = sock_gen_cookie(sk);
  1727. break;
  1728. case SO_ZEROCOPY:
  1729. v.val = sock_flag(sk, SOCK_ZEROCOPY);
  1730. break;
  1731. case SO_TXTIME:
  1732. lv = sizeof(v.txtime);
  1733. v.txtime.clockid = sk->sk_clockid;
  1734. v.txtime.flags |= sk->sk_txtime_deadline_mode ?
  1735. SOF_TXTIME_DEADLINE_MODE : 0;
  1736. v.txtime.flags |= sk->sk_txtime_report_errors ?
  1737. SOF_TXTIME_REPORT_ERRORS : 0;
  1738. break;
  1739. case SO_BINDTOIFINDEX:
  1740. v.val = READ_ONCE(sk->sk_bound_dev_if);
  1741. break;
  1742. case SO_NETNS_COOKIE:
  1743. lv = sizeof(u64);
  1744. if (len != lv)
  1745. return -EINVAL;
  1746. v.val64 = sock_net(sk)->net_cookie;
  1747. break;
  1748. case SO_BUF_LOCK:
  1749. v.val = sk->sk_userlocks & SOCK_BUF_LOCK_MASK;
  1750. break;
  1751. case SO_RESERVE_MEM:
  1752. v.val = READ_ONCE(sk->sk_reserved_mem);
  1753. break;
  1754. case SO_TXREHASH:
  1755. /* Paired with WRITE_ONCE() in sk_setsockopt() */
  1756. v.val = READ_ONCE(sk->sk_txrehash);
  1757. break;
  1758. default:
  1759. /* We implement the SO_SNDLOWAT etc to not be settable
  1760. * (1003.1g 7).
  1761. */
  1762. return -ENOPROTOOPT;
  1763. }
  1764. if (len > lv)
  1765. len = lv;
  1766. if (copy_to_sockptr(optval, &v, len))
  1767. return -EFAULT;
  1768. lenout:
  1769. if (copy_to_sockptr(optlen, &len, sizeof(int)))
  1770. return -EFAULT;
  1771. return 0;
  1772. }
  1773. int sock_getsockopt(struct socket *sock, int level, int optname,
  1774. char __user *optval, int __user *optlen)
  1775. {
  1776. return sk_getsockopt(sock->sk, level, optname,
  1777. USER_SOCKPTR(optval),
  1778. USER_SOCKPTR(optlen));
  1779. }
  1780. /*
  1781. * Initialize an sk_lock.
  1782. *
  1783. * (We also register the sk_lock with the lock validator.)
  1784. */
  1785. static inline void sock_lock_init(struct sock *sk)
  1786. {
  1787. if (sk->sk_kern_sock)
  1788. sock_lock_init_class_and_name(
  1789. sk,
  1790. af_family_kern_slock_key_strings[sk->sk_family],
  1791. af_family_kern_slock_keys + sk->sk_family,
  1792. af_family_kern_key_strings[sk->sk_family],
  1793. af_family_kern_keys + sk->sk_family);
  1794. else
  1795. sock_lock_init_class_and_name(
  1796. sk,
  1797. af_family_slock_key_strings[sk->sk_family],
  1798. af_family_slock_keys + sk->sk_family,
  1799. af_family_key_strings[sk->sk_family],
  1800. af_family_keys + sk->sk_family);
  1801. }
  1802. /*
  1803. * Copy all fields from osk to nsk but nsk->sk_refcnt must not change yet,
  1804. * even temporarly, because of RCU lookups. sk_node should also be left as is.
  1805. * We must not copy fields between sk_dontcopy_begin and sk_dontcopy_end
  1806. */
  1807. static void sock_copy(struct sock *nsk, const struct sock *osk)
  1808. {
  1809. const struct proto *prot = READ_ONCE(osk->sk_prot);
  1810. #ifdef CONFIG_SECURITY_NETWORK
  1811. void *sptr = nsk->sk_security;
  1812. #endif
  1813. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA {
  1814. #ifdef CONFIG_KNOX_NCM
  1815. u64 android_vendor_data_npa = nsk->android_oem_data1;
  1816. #endif
  1817. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA }
  1818. /* If we move sk_tx_queue_mapping out of the private section,
  1819. * we must check if sk_tx_queue_clear() is called after
  1820. * sock_copy() in sk_clone_lock().
  1821. */
  1822. BUILD_BUG_ON(offsetof(struct sock, sk_tx_queue_mapping) <
  1823. offsetof(struct sock, sk_dontcopy_begin) ||
  1824. offsetof(struct sock, sk_tx_queue_mapping) >=
  1825. offsetof(struct sock, sk_dontcopy_end));
  1826. memcpy(nsk, osk, offsetof(struct sock, sk_dontcopy_begin));
  1827. memcpy(&nsk->sk_dontcopy_end, &osk->sk_dontcopy_end,
  1828. prot->obj_size - offsetof(struct sock, sk_dontcopy_end));
  1829. #ifdef CONFIG_SECURITY_NETWORK
  1830. nsk->sk_security = sptr;
  1831. security_sk_clone(osk, nsk);
  1832. #endif
  1833. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA {
  1834. #ifdef CONFIG_KNOX_NCM
  1835. nsk->android_oem_data1 = android_vendor_data_npa;
  1836. #endif
  1837. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA }
  1838. }
  1839. static struct sock *sk_prot_alloc(struct proto *prot, gfp_t priority,
  1840. int family)
  1841. {
  1842. struct sock *sk;
  1843. struct kmem_cache *slab;
  1844. slab = prot->slab;
  1845. if (slab != NULL) {
  1846. sk = kmem_cache_alloc(slab, priority & ~__GFP_ZERO);
  1847. if (!sk)
  1848. return sk;
  1849. if (want_init_on_alloc(priority))
  1850. sk_prot_clear_nulls(sk, prot->obj_size);
  1851. } else
  1852. sk = kmalloc(prot->obj_size, priority);
  1853. if (sk != NULL) {
  1854. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA {
  1855. #ifdef CONFIG_KNOX_NCM
  1856. sk->android_oem_data1 = (u64)NULL;
  1857. #endif
  1858. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA }
  1859. if (security_sk_alloc(sk, family, priority))
  1860. goto out_free;
  1861. trace_android_rvh_sk_alloc(sk);
  1862. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA {
  1863. #ifdef CONFIG_KNOX_NCM
  1864. sk->android_oem_data1 = (u64)kzalloc(sizeof(struct sock_npa_vendor_data), GFP_NOWAIT);
  1865. #endif
  1866. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA }
  1867. if (!try_module_get(prot->owner))
  1868. goto out_free_sec;
  1869. }
  1870. return sk;
  1871. out_free_sec:
  1872. security_sk_free(sk);
  1873. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA {
  1874. #ifdef CONFIG_KNOX_NCM
  1875. if (SOCK_NPA_VENDOR_DATA_GET(sk)) {
  1876. kfree(SOCK_NPA_VENDOR_DATA_GET(sk));
  1877. sk->android_oem_data1 = (u64)NULL;
  1878. }
  1879. #endif
  1880. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA }
  1881. trace_android_rvh_sk_free(sk);
  1882. out_free:
  1883. if (slab != NULL)
  1884. kmem_cache_free(slab, sk);
  1885. else
  1886. kfree(sk);
  1887. return NULL;
  1888. }
  1889. static void sk_prot_free(struct proto *prot, struct sock *sk)
  1890. {
  1891. struct kmem_cache *slab;
  1892. struct module *owner;
  1893. owner = prot->owner;
  1894. slab = prot->slab;
  1895. cgroup_sk_free(&sk->sk_cgrp_data);
  1896. mem_cgroup_sk_free(sk);
  1897. security_sk_free(sk);
  1898. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA {
  1899. #ifdef CONFIG_KNOX_NCM
  1900. if (SOCK_NPA_VENDOR_DATA_GET(sk)) {
  1901. kfree(SOCK_NPA_VENDOR_DATA_GET(sk));
  1902. sk->android_oem_data1 = (u64)NULL;
  1903. }
  1904. #endif
  1905. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA }
  1906. trace_android_rvh_sk_free(sk);
  1907. if (slab != NULL)
  1908. kmem_cache_free(slab, sk);
  1909. else
  1910. kfree(sk);
  1911. module_put(owner);
  1912. }
  1913. /**
  1914. * sk_alloc - All socket objects are allocated here
  1915. * @net: the applicable net namespace
  1916. * @family: protocol family
  1917. * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
  1918. * @prot: struct proto associated with this new sock instance
  1919. * @kern: is this to be a kernel socket?
  1920. */
  1921. struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
  1922. struct proto *prot, int kern)
  1923. {
  1924. struct sock *sk;
  1925. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA {
  1926. #ifdef CONFIG_KNOX_NCM
  1927. struct pid *pid_struct = NULL;
  1928. struct task_struct *task = NULL;
  1929. int process_returnValue = -1;
  1930. char full_process_name[PROCESS_NAME_LEN_NAP] = {0};
  1931. struct pid *parent_pid_struct = NULL;
  1932. struct task_struct *parent_task = NULL;
  1933. int parent_returnValue = -1;
  1934. char full_parent_process_name[PROCESS_NAME_LEN_NAP] = {0};
  1935. #endif
  1936. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA }
  1937. sk = sk_prot_alloc(prot, priority | __GFP_ZERO, family);
  1938. if (sk) {
  1939. sk->sk_family = family;
  1940. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA {
  1941. #ifdef CONFIG_KNOX_NCM
  1942. if (SOCK_NPA_VENDOR_DATA_GET(sk)) {
  1943. SOCK_NPA_VENDOR_DATA_GET(sk)->knox_uid = current->cred->uid.val;
  1944. SOCK_NPA_VENDOR_DATA_GET(sk)->knox_pid = current->tgid;
  1945. if (check_ncm_flag()) {
  1946. pid_struct = find_get_pid(current->tgid);
  1947. if (pid_struct != NULL) {
  1948. task = pid_task(pid_struct, PIDTYPE_PID);
  1949. if (task != NULL) {
  1950. process_returnValue = get_cmdline(task, full_process_name, sizeof(full_process_name)-1);
  1951. if (process_returnValue > 0) {
  1952. memcpy(SOCK_NPA_VENDOR_DATA_GET(sk)->process_name, full_process_name, sizeof(SOCK_NPA_VENDOR_DATA_GET(sk)->process_name)-1);
  1953. } else {
  1954. memcpy(SOCK_NPA_VENDOR_DATA_GET(sk)->process_name, task->comm, sizeof(task->comm)-1);
  1955. }
  1956. if (task->parent != NULL) {
  1957. parent_pid_struct = find_get_pid(task->parent->tgid);
  1958. if (parent_pid_struct != NULL) {
  1959. parent_task = pid_task(parent_pid_struct, PIDTYPE_PID);
  1960. if (parent_task != NULL) {
  1961. parent_returnValue = get_cmdline(parent_task, full_parent_process_name, sizeof(full_parent_process_name)-1);
  1962. if (parent_returnValue > 0) {
  1963. memcpy(SOCK_NPA_VENDOR_DATA_GET(sk)->parent_process_name, full_parent_process_name, sizeof(SOCK_NPA_VENDOR_DATA_GET(sk)->parent_process_name)-1);
  1964. } else {
  1965. memcpy(SOCK_NPA_VENDOR_DATA_GET(sk)->parent_process_name, parent_task->comm, sizeof(parent_task->comm)-1);
  1966. }
  1967. SOCK_NPA_VENDOR_DATA_GET(sk)->knox_puid = parent_task->cred->uid.val;
  1968. SOCK_NPA_VENDOR_DATA_GET(sk)->knox_ppid = parent_task->tgid;
  1969. }
  1970. }
  1971. }
  1972. }
  1973. }
  1974. }
  1975. }
  1976. #endif
  1977. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA }
  1978. /*
  1979. * See comment in struct sock definition to understand
  1980. * why we need sk_prot_creator -acme
  1981. */
  1982. sk->sk_prot = sk->sk_prot_creator = prot;
  1983. sk->sk_kern_sock = kern;
  1984. sock_lock_init(sk);
  1985. sk->sk_net_refcnt = kern ? 0 : 1;
  1986. if (likely(sk->sk_net_refcnt)) {
  1987. get_net_track(net, &sk->ns_tracker, priority);
  1988. sock_inuse_add(net, 1);
  1989. }
  1990. sock_net_set(sk, net);
  1991. refcount_set(&sk->sk_wmem_alloc, 1);
  1992. mem_cgroup_sk_alloc(sk);
  1993. cgroup_sk_alloc(&sk->sk_cgrp_data);
  1994. sock_update_classid(&sk->sk_cgrp_data);
  1995. sock_update_netprioidx(&sk->sk_cgrp_data);
  1996. sk_tx_queue_clear(sk);
  1997. }
  1998. return sk;
  1999. }
  2000. EXPORT_SYMBOL(sk_alloc);
  2001. /* Sockets having SOCK_RCU_FREE will call this function after one RCU
  2002. * grace period. This is the case for UDP sockets and TCP listeners.
  2003. */
  2004. static void __sk_destruct(struct rcu_head *head)
  2005. {
  2006. struct sock *sk = container_of(head, struct sock, sk_rcu);
  2007. struct sk_filter *filter;
  2008. if (sk->sk_destruct)
  2009. sk->sk_destruct(sk);
  2010. filter = rcu_dereference_check(sk->sk_filter,
  2011. refcount_read(&sk->sk_wmem_alloc) == 0);
  2012. if (filter) {
  2013. sk_filter_uncharge(sk, filter);
  2014. RCU_INIT_POINTER(sk->sk_filter, NULL);
  2015. }
  2016. sock_disable_timestamp(sk, SK_FLAGS_TIMESTAMP);
  2017. #ifdef CONFIG_BPF_SYSCALL
  2018. bpf_sk_storage_free(sk);
  2019. #endif
  2020. if (atomic_read(&sk->sk_omem_alloc))
  2021. pr_debug("%s: optmem leakage (%d bytes) detected\n",
  2022. __func__, atomic_read(&sk->sk_omem_alloc));
  2023. if (sk->sk_frag.page) {
  2024. put_page(sk->sk_frag.page);
  2025. sk->sk_frag.page = NULL;
  2026. }
  2027. /* We do not need to acquire sk->sk_peer_lock, we are the last user. */
  2028. put_cred(sk->sk_peer_cred);
  2029. put_pid(sk->sk_peer_pid);
  2030. if (likely(sk->sk_net_refcnt))
  2031. put_net_track(sock_net(sk), &sk->ns_tracker);
  2032. sk_prot_free(sk->sk_prot_creator, sk);
  2033. }
  2034. void sk_destruct(struct sock *sk)
  2035. {
  2036. bool use_call_rcu = sock_flag(sk, SOCK_RCU_FREE);
  2037. if (rcu_access_pointer(sk->sk_reuseport_cb)) {
  2038. reuseport_detach_sock(sk);
  2039. use_call_rcu = true;
  2040. }
  2041. if (use_call_rcu)
  2042. call_rcu(&sk->sk_rcu, __sk_destruct);
  2043. else
  2044. __sk_destruct(&sk->sk_rcu);
  2045. }
  2046. static void __sk_free(struct sock *sk)
  2047. {
  2048. if (likely(sk->sk_net_refcnt))
  2049. sock_inuse_add(sock_net(sk), -1);
  2050. if (unlikely(sk->sk_net_refcnt && sock_diag_has_destroy_listeners(sk)))
  2051. sock_diag_broadcast_destroy(sk);
  2052. else
  2053. sk_destruct(sk);
  2054. }
  2055. void sk_free(struct sock *sk)
  2056. {
  2057. /*
  2058. * We subtract one from sk_wmem_alloc and can know if
  2059. * some packets are still in some tx queue.
  2060. * If not null, sock_wfree() will call __sk_free(sk) later
  2061. */
  2062. if (refcount_dec_and_test(&sk->sk_wmem_alloc))
  2063. __sk_free(sk);
  2064. }
  2065. EXPORT_SYMBOL(sk_free);
  2066. static void sk_init_common(struct sock *sk)
  2067. {
  2068. skb_queue_head_init(&sk->sk_receive_queue);
  2069. skb_queue_head_init(&sk->sk_write_queue);
  2070. skb_queue_head_init(&sk->sk_error_queue);
  2071. rwlock_init(&sk->sk_callback_lock);
  2072. lockdep_set_class_and_name(&sk->sk_receive_queue.lock,
  2073. af_rlock_keys + sk->sk_family,
  2074. af_family_rlock_key_strings[sk->sk_family]);
  2075. lockdep_set_class_and_name(&sk->sk_write_queue.lock,
  2076. af_wlock_keys + sk->sk_family,
  2077. af_family_wlock_key_strings[sk->sk_family]);
  2078. lockdep_set_class_and_name(&sk->sk_error_queue.lock,
  2079. af_elock_keys + sk->sk_family,
  2080. af_family_elock_key_strings[sk->sk_family]);
  2081. lockdep_set_class_and_name(&sk->sk_callback_lock,
  2082. af_callback_keys + sk->sk_family,
  2083. af_family_clock_key_strings[sk->sk_family]);
  2084. }
  2085. /**
  2086. * sk_clone_lock - clone a socket, and lock its clone
  2087. * @sk: the socket to clone
  2088. * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
  2089. *
  2090. * Caller must unlock socket even in error path (bh_unlock_sock(newsk))
  2091. */
  2092. struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
  2093. {
  2094. struct proto *prot = READ_ONCE(sk->sk_prot);
  2095. struct sk_filter *filter;
  2096. bool is_charged = true;
  2097. struct sock *newsk;
  2098. newsk = sk_prot_alloc(prot, priority, sk->sk_family);
  2099. if (!newsk)
  2100. goto out;
  2101. sock_copy(newsk, sk);
  2102. newsk->sk_prot_creator = prot;
  2103. /* SANITY */
  2104. if (likely(newsk->sk_net_refcnt)) {
  2105. get_net_track(sock_net(newsk), &newsk->ns_tracker, priority);
  2106. sock_inuse_add(sock_net(newsk), 1);
  2107. }
  2108. sk_node_init(&newsk->sk_node);
  2109. sock_lock_init(newsk);
  2110. bh_lock_sock(newsk);
  2111. newsk->sk_backlog.head = newsk->sk_backlog.tail = NULL;
  2112. newsk->sk_backlog.len = 0;
  2113. atomic_set(&newsk->sk_rmem_alloc, 0);
  2114. /* sk_wmem_alloc set to one (see sk_free() and sock_wfree()) */
  2115. refcount_set(&newsk->sk_wmem_alloc, 1);
  2116. atomic_set(&newsk->sk_omem_alloc, 0);
  2117. sk_init_common(newsk);
  2118. newsk->sk_dst_cache = NULL;
  2119. newsk->sk_dst_pending_confirm = 0;
  2120. newsk->sk_wmem_queued = 0;
  2121. newsk->sk_forward_alloc = 0;
  2122. newsk->sk_reserved_mem = 0;
  2123. atomic_set(&newsk->sk_drops, 0);
  2124. newsk->sk_send_head = NULL;
  2125. newsk->sk_userlocks = sk->sk_userlocks & ~SOCK_BINDPORT_LOCK;
  2126. atomic_set(&newsk->sk_zckey, 0);
  2127. sock_reset_flag(newsk, SOCK_DONE);
  2128. /* sk->sk_memcg will be populated at accept() time */
  2129. newsk->sk_memcg = NULL;
  2130. cgroup_sk_clone(&newsk->sk_cgrp_data);
  2131. rcu_read_lock();
  2132. filter = rcu_dereference(sk->sk_filter);
  2133. if (filter != NULL)
  2134. /* though it's an empty new sock, the charging may fail
  2135. * if sysctl_optmem_max was changed between creation of
  2136. * original socket and cloning
  2137. */
  2138. is_charged = sk_filter_charge(newsk, filter);
  2139. RCU_INIT_POINTER(newsk->sk_filter, filter);
  2140. rcu_read_unlock();
  2141. if (unlikely(!is_charged || xfrm_sk_clone_policy(newsk, sk))) {
  2142. /* We need to make sure that we don't uncharge the new
  2143. * socket if we couldn't charge it in the first place
  2144. * as otherwise we uncharge the parent's filter.
  2145. */
  2146. if (!is_charged)
  2147. RCU_INIT_POINTER(newsk->sk_filter, NULL);
  2148. sk_free_unlock_clone(newsk);
  2149. newsk = NULL;
  2150. goto out;
  2151. }
  2152. RCU_INIT_POINTER(newsk->sk_reuseport_cb, NULL);
  2153. if (bpf_sk_storage_clone(sk, newsk)) {
  2154. sk_free_unlock_clone(newsk);
  2155. newsk = NULL;
  2156. goto out;
  2157. }
  2158. /* Clear sk_user_data if parent had the pointer tagged
  2159. * as not suitable for copying when cloning.
  2160. */
  2161. if (sk_user_data_is_nocopy(newsk))
  2162. newsk->sk_user_data = NULL;
  2163. newsk->sk_err = 0;
  2164. newsk->sk_err_soft = 0;
  2165. newsk->sk_priority = 0;
  2166. newsk->sk_incoming_cpu = raw_smp_processor_id();
  2167. /* Before updating sk_refcnt, we must commit prior changes to memory
  2168. * (Documentation/RCU/rculist_nulls.rst for details)
  2169. */
  2170. smp_wmb();
  2171. refcount_set(&newsk->sk_refcnt, 2);
  2172. /* Increment the counter in the same struct proto as the master
  2173. * sock (sk_refcnt_debug_inc uses newsk->sk_prot->socks, that
  2174. * is the same as sk->sk_prot->socks, as this field was copied
  2175. * with memcpy).
  2176. *
  2177. * This _changes_ the previous behaviour, where
  2178. * tcp_create_openreq_child always was incrementing the
  2179. * equivalent to tcp_prot->socks (inet_sock_nr), so this have
  2180. * to be taken into account in all callers. -acme
  2181. */
  2182. sk_refcnt_debug_inc(newsk);
  2183. sk_set_socket(newsk, NULL);
  2184. sk_tx_queue_clear(newsk);
  2185. RCU_INIT_POINTER(newsk->sk_wq, NULL);
  2186. if (newsk->sk_prot->sockets_allocated)
  2187. sk_sockets_allocated_inc(newsk);
  2188. if (sock_needs_netstamp(sk) && newsk->sk_flags & SK_FLAGS_TIMESTAMP)
  2189. net_enable_timestamp();
  2190. out:
  2191. return newsk;
  2192. }
  2193. EXPORT_SYMBOL_GPL(sk_clone_lock);
  2194. void sk_free_unlock_clone(struct sock *sk)
  2195. {
  2196. /* It is still raw copy of parent, so invalidate
  2197. * destructor and make plain sk_free() */
  2198. sk->sk_destruct = NULL;
  2199. bh_unlock_sock(sk);
  2200. sk_free(sk);
  2201. }
  2202. EXPORT_SYMBOL_GPL(sk_free_unlock_clone);
  2203. static void sk_trim_gso_size(struct sock *sk)
  2204. {
  2205. if (sk->sk_gso_max_size <= GSO_LEGACY_MAX_SIZE)
  2206. return;
  2207. #if IS_ENABLED(CONFIG_IPV6)
  2208. if (sk->sk_family == AF_INET6 &&
  2209. sk_is_tcp(sk) &&
  2210. !ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr))
  2211. return;
  2212. #endif
  2213. sk->sk_gso_max_size = GSO_LEGACY_MAX_SIZE;
  2214. }
  2215. void sk_setup_caps(struct sock *sk, struct dst_entry *dst)
  2216. {
  2217. u32 max_segs = 1;
  2218. sk->sk_route_caps = dst->dev->features;
  2219. if (sk_is_tcp(sk))
  2220. sk->sk_route_caps |= NETIF_F_GSO;
  2221. if (sk->sk_route_caps & NETIF_F_GSO)
  2222. sk->sk_route_caps |= NETIF_F_GSO_SOFTWARE;
  2223. if (unlikely(sk->sk_gso_disabled))
  2224. sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
  2225. if (sk_can_gso(sk)) {
  2226. if (dst->header_len && !xfrm_dst_offload_ok(dst)) {
  2227. sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
  2228. } else {
  2229. sk->sk_route_caps |= NETIF_F_SG | NETIF_F_HW_CSUM;
  2230. /* pairs with the WRITE_ONCE() in netif_set_gso_max_size() */
  2231. sk->sk_gso_max_size = READ_ONCE(dst->dev->gso_max_size);
  2232. sk_trim_gso_size(sk);
  2233. sk->sk_gso_max_size -= (MAX_TCP_HEADER + 1);
  2234. /* pairs with the WRITE_ONCE() in netif_set_gso_max_segs() */
  2235. max_segs = max_t(u32, READ_ONCE(dst->dev->gso_max_segs), 1);
  2236. }
  2237. }
  2238. sk->sk_gso_max_segs = max_segs;
  2239. sk_dst_set(sk, dst);
  2240. }
  2241. EXPORT_SYMBOL_GPL(sk_setup_caps);
  2242. /*
  2243. * Simple resource managers for sockets.
  2244. */
  2245. /*
  2246. * Write buffer destructor automatically called from kfree_skb.
  2247. */
  2248. void sock_wfree(struct sk_buff *skb)
  2249. {
  2250. struct sock *sk = skb->sk;
  2251. unsigned int len = skb->truesize;
  2252. bool free;
  2253. if (!sock_flag(sk, SOCK_USE_WRITE_QUEUE)) {
  2254. if (sock_flag(sk, SOCK_RCU_FREE) &&
  2255. sk->sk_write_space == sock_def_write_space) {
  2256. rcu_read_lock();
  2257. free = refcount_sub_and_test(len, &sk->sk_wmem_alloc);
  2258. sock_def_write_space_wfree(sk);
  2259. rcu_read_unlock();
  2260. if (unlikely(free))
  2261. __sk_free(sk);
  2262. return;
  2263. }
  2264. /*
  2265. * Keep a reference on sk_wmem_alloc, this will be released
  2266. * after sk_write_space() call
  2267. */
  2268. WARN_ON(refcount_sub_and_test(len - 1, &sk->sk_wmem_alloc));
  2269. sk->sk_write_space(sk);
  2270. len = 1;
  2271. }
  2272. /*
  2273. * if sk_wmem_alloc reaches 0, we must finish what sk_free()
  2274. * could not do because of in-flight packets
  2275. */
  2276. if (refcount_sub_and_test(len, &sk->sk_wmem_alloc))
  2277. __sk_free(sk);
  2278. }
  2279. EXPORT_SYMBOL(sock_wfree);
  2280. /* This variant of sock_wfree() is used by TCP,
  2281. * since it sets SOCK_USE_WRITE_QUEUE.
  2282. */
  2283. void __sock_wfree(struct sk_buff *skb)
  2284. {
  2285. struct sock *sk = skb->sk;
  2286. if (refcount_sub_and_test(skb->truesize, &sk->sk_wmem_alloc))
  2287. __sk_free(sk);
  2288. }
  2289. void skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
  2290. {
  2291. skb_orphan(skb);
  2292. skb->sk = sk;
  2293. #ifdef CONFIG_INET
  2294. if (unlikely(!sk_fullsock(sk))) {
  2295. skb->destructor = sock_edemux;
  2296. sock_hold(sk);
  2297. return;
  2298. }
  2299. #endif
  2300. skb->destructor = sock_wfree;
  2301. skb_set_hash_from_sk(skb, sk);
  2302. /*
  2303. * We used to take a refcount on sk, but following operation
  2304. * is enough to guarantee sk_free() wont free this sock until
  2305. * all in-flight packets are completed
  2306. */
  2307. refcount_add(skb->truesize, &sk->sk_wmem_alloc);
  2308. }
  2309. EXPORT_SYMBOL(skb_set_owner_w);
  2310. static bool can_skb_orphan_partial(const struct sk_buff *skb)
  2311. {
  2312. #ifdef CONFIG_TLS_DEVICE
  2313. /* Drivers depend on in-order delivery for crypto offload,
  2314. * partial orphan breaks out-of-order-OK logic.
  2315. */
  2316. if (skb->decrypted)
  2317. return false;
  2318. #endif
  2319. return (skb->destructor == sock_wfree ||
  2320. (IS_ENABLED(CONFIG_INET) && skb->destructor == tcp_wfree));
  2321. }
  2322. /* This helper is used by netem, as it can hold packets in its
  2323. * delay queue. We want to allow the owner socket to send more
  2324. * packets, as if they were already TX completed by a typical driver.
  2325. * But we also want to keep skb->sk set because some packet schedulers
  2326. * rely on it (sch_fq for example).
  2327. */
  2328. void skb_orphan_partial(struct sk_buff *skb)
  2329. {
  2330. if (skb_is_tcp_pure_ack(skb))
  2331. return;
  2332. if (can_skb_orphan_partial(skb) && skb_set_owner_sk_safe(skb, skb->sk))
  2333. return;
  2334. skb_orphan(skb);
  2335. }
  2336. EXPORT_SYMBOL(skb_orphan_partial);
  2337. /*
  2338. * Read buffer destructor automatically called from kfree_skb.
  2339. */
  2340. void sock_rfree(struct sk_buff *skb)
  2341. {
  2342. struct sock *sk = skb->sk;
  2343. unsigned int len = skb->truesize;
  2344. atomic_sub(len, &sk->sk_rmem_alloc);
  2345. sk_mem_uncharge(sk, len);
  2346. }
  2347. EXPORT_SYMBOL(sock_rfree);
  2348. /*
  2349. * Buffer destructor for skbs that are not used directly in read or write
  2350. * path, e.g. for error handler skbs. Automatically called from kfree_skb.
  2351. */
  2352. void sock_efree(struct sk_buff *skb)
  2353. {
  2354. sock_put(skb->sk);
  2355. }
  2356. EXPORT_SYMBOL(sock_efree);
  2357. /* Buffer destructor for prefetch/receive path where reference count may
  2358. * not be held, e.g. for listen sockets.
  2359. */
  2360. #ifdef CONFIG_INET
  2361. void sock_pfree(struct sk_buff *skb)
  2362. {
  2363. if (sk_is_refcounted(skb->sk))
  2364. sock_gen_put(skb->sk);
  2365. }
  2366. EXPORT_SYMBOL(sock_pfree);
  2367. #endif /* CONFIG_INET */
  2368. kuid_t sock_i_uid(struct sock *sk)
  2369. {
  2370. kuid_t uid;
  2371. read_lock_bh(&sk->sk_callback_lock);
  2372. uid = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_uid : GLOBAL_ROOT_UID;
  2373. read_unlock_bh(&sk->sk_callback_lock);
  2374. return uid;
  2375. }
  2376. EXPORT_SYMBOL(sock_i_uid);
  2377. unsigned long __sock_i_ino(struct sock *sk)
  2378. {
  2379. unsigned long ino;
  2380. read_lock(&sk->sk_callback_lock);
  2381. ino = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_ino : 0;
  2382. read_unlock(&sk->sk_callback_lock);
  2383. return ino;
  2384. }
  2385. EXPORT_SYMBOL(__sock_i_ino);
  2386. unsigned long sock_i_ino(struct sock *sk)
  2387. {
  2388. unsigned long ino;
  2389. local_bh_disable();
  2390. ino = __sock_i_ino(sk);
  2391. local_bh_enable();
  2392. return ino;
  2393. }
  2394. EXPORT_SYMBOL(sock_i_ino);
  2395. /*
  2396. * Allocate a skb from the socket's send buffer.
  2397. */
  2398. struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force,
  2399. gfp_t priority)
  2400. {
  2401. if (force ||
  2402. refcount_read(&sk->sk_wmem_alloc) < READ_ONCE(sk->sk_sndbuf)) {
  2403. struct sk_buff *skb = alloc_skb(size, priority);
  2404. if (skb) {
  2405. skb_set_owner_w(skb, sk);
  2406. return skb;
  2407. }
  2408. }
  2409. return NULL;
  2410. }
  2411. EXPORT_SYMBOL(sock_wmalloc);
  2412. static void sock_ofree(struct sk_buff *skb)
  2413. {
  2414. struct sock *sk = skb->sk;
  2415. atomic_sub(skb->truesize, &sk->sk_omem_alloc);
  2416. }
  2417. struct sk_buff *sock_omalloc(struct sock *sk, unsigned long size,
  2418. gfp_t priority)
  2419. {
  2420. struct sk_buff *skb;
  2421. /* small safe race: SKB_TRUESIZE may differ from final skb->truesize */
  2422. if (atomic_read(&sk->sk_omem_alloc) + SKB_TRUESIZE(size) >
  2423. READ_ONCE(sysctl_optmem_max))
  2424. return NULL;
  2425. skb = alloc_skb(size, priority);
  2426. if (!skb)
  2427. return NULL;
  2428. atomic_add(skb->truesize, &sk->sk_omem_alloc);
  2429. skb->sk = sk;
  2430. skb->destructor = sock_ofree;
  2431. return skb;
  2432. }
  2433. /*
  2434. * Allocate a memory block from the socket's option memory buffer.
  2435. */
  2436. void *sock_kmalloc(struct sock *sk, int size, gfp_t priority)
  2437. {
  2438. int optmem_max = READ_ONCE(sysctl_optmem_max);
  2439. if ((unsigned int)size <= optmem_max &&
  2440. atomic_read(&sk->sk_omem_alloc) + size < optmem_max) {
  2441. void *mem;
  2442. /* First do the add, to avoid the race if kmalloc
  2443. * might sleep.
  2444. */
  2445. atomic_add(size, &sk->sk_omem_alloc);
  2446. mem = kmalloc(size, priority);
  2447. if (mem)
  2448. return mem;
  2449. atomic_sub(size, &sk->sk_omem_alloc);
  2450. }
  2451. return NULL;
  2452. }
  2453. EXPORT_SYMBOL(sock_kmalloc);
  2454. /* Free an option memory block. Note, we actually want the inline
  2455. * here as this allows gcc to detect the nullify and fold away the
  2456. * condition entirely.
  2457. */
  2458. static inline void __sock_kfree_s(struct sock *sk, void *mem, int size,
  2459. const bool nullify)
  2460. {
  2461. if (WARN_ON_ONCE(!mem))
  2462. return;
  2463. if (nullify)
  2464. kfree_sensitive(mem);
  2465. else
  2466. kfree(mem);
  2467. atomic_sub(size, &sk->sk_omem_alloc);
  2468. }
  2469. void sock_kfree_s(struct sock *sk, void *mem, int size)
  2470. {
  2471. __sock_kfree_s(sk, mem, size, false);
  2472. }
  2473. EXPORT_SYMBOL(sock_kfree_s);
  2474. void sock_kzfree_s(struct sock *sk, void *mem, int size)
  2475. {
  2476. __sock_kfree_s(sk, mem, size, true);
  2477. }
  2478. EXPORT_SYMBOL(sock_kzfree_s);
  2479. /* It is almost wait_for_tcp_memory minus release_sock/lock_sock.
  2480. I think, these locks should be removed for datagram sockets.
  2481. */
  2482. static long sock_wait_for_wmem(struct sock *sk, long timeo)
  2483. {
  2484. DEFINE_WAIT(wait);
  2485. sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  2486. for (;;) {
  2487. if (!timeo)
  2488. break;
  2489. if (signal_pending(current))
  2490. break;
  2491. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  2492. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  2493. if (refcount_read(&sk->sk_wmem_alloc) < READ_ONCE(sk->sk_sndbuf))
  2494. break;
  2495. if (READ_ONCE(sk->sk_shutdown) & SEND_SHUTDOWN)
  2496. break;
  2497. if (READ_ONCE(sk->sk_err))
  2498. break;
  2499. timeo = schedule_timeout(timeo);
  2500. }
  2501. finish_wait(sk_sleep(sk), &wait);
  2502. return timeo;
  2503. }
  2504. /*
  2505. * Generic send/receive buffer handlers
  2506. */
  2507. struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
  2508. unsigned long data_len, int noblock,
  2509. int *errcode, int max_page_order)
  2510. {
  2511. struct sk_buff *skb;
  2512. long timeo;
  2513. int err;
  2514. timeo = sock_sndtimeo(sk, noblock);
  2515. for (;;) {
  2516. err = sock_error(sk);
  2517. if (err != 0)
  2518. goto failure;
  2519. err = -EPIPE;
  2520. if (READ_ONCE(sk->sk_shutdown) & SEND_SHUTDOWN)
  2521. goto failure;
  2522. if (sk_wmem_alloc_get(sk) < READ_ONCE(sk->sk_sndbuf))
  2523. break;
  2524. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  2525. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  2526. err = -EAGAIN;
  2527. if (!timeo)
  2528. goto failure;
  2529. if (signal_pending(current))
  2530. goto interrupted;
  2531. timeo = sock_wait_for_wmem(sk, timeo);
  2532. }
  2533. skb = alloc_skb_with_frags(header_len, data_len, max_page_order,
  2534. errcode, sk->sk_allocation);
  2535. if (skb)
  2536. skb_set_owner_w(skb, sk);
  2537. return skb;
  2538. interrupted:
  2539. err = sock_intr_errno(timeo);
  2540. failure:
  2541. *errcode = err;
  2542. return NULL;
  2543. }
  2544. EXPORT_SYMBOL(sock_alloc_send_pskb);
  2545. int __sock_cmsg_send(struct sock *sk, struct msghdr *msg, struct cmsghdr *cmsg,
  2546. struct sockcm_cookie *sockc)
  2547. {
  2548. u32 tsflags;
  2549. switch (cmsg->cmsg_type) {
  2550. case SO_MARK:
  2551. if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
  2552. !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
  2553. return -EPERM;
  2554. if (cmsg->cmsg_len != CMSG_LEN(sizeof(u32)))
  2555. return -EINVAL;
  2556. sockc->mark = *(u32 *)CMSG_DATA(cmsg);
  2557. break;
  2558. case SO_TIMESTAMPING_OLD:
  2559. if (cmsg->cmsg_len != CMSG_LEN(sizeof(u32)))
  2560. return -EINVAL;
  2561. tsflags = *(u32 *)CMSG_DATA(cmsg);
  2562. if (tsflags & ~SOF_TIMESTAMPING_TX_RECORD_MASK)
  2563. return -EINVAL;
  2564. sockc->tsflags &= ~SOF_TIMESTAMPING_TX_RECORD_MASK;
  2565. sockc->tsflags |= tsflags;
  2566. break;
  2567. case SCM_TXTIME:
  2568. if (!sock_flag(sk, SOCK_TXTIME))
  2569. return -EINVAL;
  2570. if (cmsg->cmsg_len != CMSG_LEN(sizeof(u64)))
  2571. return -EINVAL;
  2572. sockc->transmit_time = get_unaligned((u64 *)CMSG_DATA(cmsg));
  2573. break;
  2574. /* SCM_RIGHTS and SCM_CREDENTIALS are semantically in SOL_UNIX. */
  2575. case SCM_RIGHTS:
  2576. case SCM_CREDENTIALS:
  2577. break;
  2578. default:
  2579. return -EINVAL;
  2580. }
  2581. return 0;
  2582. }
  2583. EXPORT_SYMBOL(__sock_cmsg_send);
  2584. int sock_cmsg_send(struct sock *sk, struct msghdr *msg,
  2585. struct sockcm_cookie *sockc)
  2586. {
  2587. struct cmsghdr *cmsg;
  2588. int ret;
  2589. for_each_cmsghdr(cmsg, msg) {
  2590. if (!CMSG_OK(msg, cmsg))
  2591. return -EINVAL;
  2592. if (cmsg->cmsg_level != SOL_SOCKET)
  2593. continue;
  2594. ret = __sock_cmsg_send(sk, msg, cmsg, sockc);
  2595. if (ret)
  2596. return ret;
  2597. }
  2598. return 0;
  2599. }
  2600. EXPORT_SYMBOL(sock_cmsg_send);
  2601. static void sk_enter_memory_pressure(struct sock *sk)
  2602. {
  2603. if (!sk->sk_prot->enter_memory_pressure)
  2604. return;
  2605. sk->sk_prot->enter_memory_pressure(sk);
  2606. }
  2607. static void sk_leave_memory_pressure(struct sock *sk)
  2608. {
  2609. if (sk->sk_prot->leave_memory_pressure) {
  2610. INDIRECT_CALL_INET_1(sk->sk_prot->leave_memory_pressure,
  2611. tcp_leave_memory_pressure, sk);
  2612. } else {
  2613. unsigned long *memory_pressure = sk->sk_prot->memory_pressure;
  2614. if (memory_pressure && READ_ONCE(*memory_pressure))
  2615. WRITE_ONCE(*memory_pressure, 0);
  2616. }
  2617. }
  2618. DEFINE_STATIC_KEY_FALSE(net_high_order_alloc_disable_key);
  2619. /**
  2620. * skb_page_frag_refill - check that a page_frag contains enough room
  2621. * @sz: minimum size of the fragment we want to get
  2622. * @pfrag: pointer to page_frag
  2623. * @gfp: priority for memory allocation
  2624. *
  2625. * Note: While this allocator tries to use high order pages, there is
  2626. * no guarantee that allocations succeed. Therefore, @sz MUST be
  2627. * less or equal than PAGE_SIZE.
  2628. */
  2629. bool skb_page_frag_refill(unsigned int sz, struct page_frag *pfrag, gfp_t gfp)
  2630. {
  2631. if (pfrag->page) {
  2632. if (page_ref_count(pfrag->page) == 1) {
  2633. pfrag->offset = 0;
  2634. return true;
  2635. }
  2636. if (pfrag->offset + sz <= pfrag->size)
  2637. return true;
  2638. put_page(pfrag->page);
  2639. }
  2640. pfrag->offset = 0;
  2641. if (SKB_FRAG_PAGE_ORDER &&
  2642. !static_branch_unlikely(&net_high_order_alloc_disable_key)) {
  2643. /* Avoid direct reclaim but allow kswapd to wake */
  2644. pfrag->page = alloc_pages((gfp & ~__GFP_DIRECT_RECLAIM) |
  2645. __GFP_COMP | __GFP_NOWARN |
  2646. __GFP_NORETRY,
  2647. SKB_FRAG_PAGE_ORDER);
  2648. if (likely(pfrag->page)) {
  2649. pfrag->size = PAGE_SIZE << SKB_FRAG_PAGE_ORDER;
  2650. return true;
  2651. }
  2652. }
  2653. pfrag->page = alloc_page(gfp);
  2654. if (likely(pfrag->page)) {
  2655. pfrag->size = PAGE_SIZE;
  2656. return true;
  2657. }
  2658. return false;
  2659. }
  2660. EXPORT_SYMBOL(skb_page_frag_refill);
  2661. bool sk_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
  2662. {
  2663. if (likely(skb_page_frag_refill(32U, pfrag, sk->sk_allocation)))
  2664. return true;
  2665. sk_enter_memory_pressure(sk);
  2666. sk_stream_moderate_sndbuf(sk);
  2667. return false;
  2668. }
  2669. EXPORT_SYMBOL(sk_page_frag_refill);
  2670. void __lock_sock(struct sock *sk)
  2671. __releases(&sk->sk_lock.slock)
  2672. __acquires(&sk->sk_lock.slock)
  2673. {
  2674. DEFINE_WAIT(wait);
  2675. for (;;) {
  2676. prepare_to_wait_exclusive(&sk->sk_lock.wq, &wait,
  2677. TASK_UNINTERRUPTIBLE);
  2678. spin_unlock_bh(&sk->sk_lock.slock);
  2679. schedule();
  2680. spin_lock_bh(&sk->sk_lock.slock);
  2681. if (!sock_owned_by_user(sk))
  2682. break;
  2683. }
  2684. finish_wait(&sk->sk_lock.wq, &wait);
  2685. }
  2686. void __release_sock(struct sock *sk)
  2687. __releases(&sk->sk_lock.slock)
  2688. __acquires(&sk->sk_lock.slock)
  2689. {
  2690. struct sk_buff *skb, *next;
  2691. while ((skb = sk->sk_backlog.head) != NULL) {
  2692. sk->sk_backlog.head = sk->sk_backlog.tail = NULL;
  2693. spin_unlock_bh(&sk->sk_lock.slock);
  2694. do {
  2695. next = skb->next;
  2696. prefetch(next);
  2697. DEBUG_NET_WARN_ON_ONCE(skb_dst_is_noref(skb));
  2698. skb_mark_not_on_list(skb);
  2699. sk_backlog_rcv(sk, skb);
  2700. cond_resched();
  2701. skb = next;
  2702. } while (skb != NULL);
  2703. spin_lock_bh(&sk->sk_lock.slock);
  2704. }
  2705. /*
  2706. * Doing the zeroing here guarantee we can not loop forever
  2707. * while a wild producer attempts to flood us.
  2708. */
  2709. sk->sk_backlog.len = 0;
  2710. }
  2711. void __sk_flush_backlog(struct sock *sk)
  2712. {
  2713. spin_lock_bh(&sk->sk_lock.slock);
  2714. __release_sock(sk);
  2715. spin_unlock_bh(&sk->sk_lock.slock);
  2716. }
  2717. EXPORT_SYMBOL_GPL(__sk_flush_backlog);
  2718. /**
  2719. * sk_wait_data - wait for data to arrive at sk_receive_queue
  2720. * @sk: sock to wait on
  2721. * @timeo: for how long
  2722. * @skb: last skb seen on sk_receive_queue
  2723. *
  2724. * Now socket state including sk->sk_err is changed only under lock,
  2725. * hence we may omit checks after joining wait queue.
  2726. * We check receive queue before schedule() only as optimization;
  2727. * it is very likely that release_sock() added new data.
  2728. */
  2729. int sk_wait_data(struct sock *sk, long *timeo, const struct sk_buff *skb)
  2730. {
  2731. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  2732. int rc;
  2733. add_wait_queue(sk_sleep(sk), &wait);
  2734. sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  2735. rc = sk_wait_event(sk, timeo, skb_peek_tail(&sk->sk_receive_queue) != skb, &wait);
  2736. sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  2737. remove_wait_queue(sk_sleep(sk), &wait);
  2738. return rc;
  2739. }
  2740. EXPORT_SYMBOL(sk_wait_data);
  2741. /**
  2742. * __sk_mem_raise_allocated - increase memory_allocated
  2743. * @sk: socket
  2744. * @size: memory size to allocate
  2745. * @amt: pages to allocate
  2746. * @kind: allocation type
  2747. *
  2748. * Similar to __sk_mem_schedule(), but does not update sk_forward_alloc
  2749. */
  2750. int __sk_mem_raise_allocated(struct sock *sk, int size, int amt, int kind)
  2751. {
  2752. bool memcg_charge = mem_cgroup_sockets_enabled && sk->sk_memcg;
  2753. struct proto *prot = sk->sk_prot;
  2754. bool charged = true;
  2755. long allocated;
  2756. sk_memory_allocated_add(sk, amt);
  2757. allocated = sk_memory_allocated(sk);
  2758. if (memcg_charge &&
  2759. !(charged = mem_cgroup_charge_skmem(sk->sk_memcg, amt,
  2760. gfp_memcg_charge())))
  2761. goto suppress_allocation;
  2762. /* Under limit. */
  2763. if (allocated <= sk_prot_mem_limits(sk, 0)) {
  2764. sk_leave_memory_pressure(sk);
  2765. return 1;
  2766. }
  2767. /* Under pressure. */
  2768. if (allocated > sk_prot_mem_limits(sk, 1))
  2769. sk_enter_memory_pressure(sk);
  2770. /* Over hard limit. */
  2771. if (allocated > sk_prot_mem_limits(sk, 2))
  2772. goto suppress_allocation;
  2773. /* guarantee minimum buffer size under pressure */
  2774. if (kind == SK_MEM_RECV) {
  2775. if (atomic_read(&sk->sk_rmem_alloc) < sk_get_rmem0(sk, prot))
  2776. return 1;
  2777. } else { /* SK_MEM_SEND */
  2778. int wmem0 = sk_get_wmem0(sk, prot);
  2779. if (sk->sk_type == SOCK_STREAM) {
  2780. if (sk->sk_wmem_queued < wmem0)
  2781. return 1;
  2782. } else if (refcount_read(&sk->sk_wmem_alloc) < wmem0) {
  2783. return 1;
  2784. }
  2785. }
  2786. if (sk_has_memory_pressure(sk)) {
  2787. u64 alloc;
  2788. if (!sk_under_memory_pressure(sk))
  2789. return 1;
  2790. alloc = sk_sockets_allocated_read_positive(sk);
  2791. if (sk_prot_mem_limits(sk, 2) > alloc *
  2792. sk_mem_pages(sk->sk_wmem_queued +
  2793. atomic_read(&sk->sk_rmem_alloc) +
  2794. sk->sk_forward_alloc))
  2795. return 1;
  2796. }
  2797. suppress_allocation:
  2798. if (kind == SK_MEM_SEND && sk->sk_type == SOCK_STREAM) {
  2799. sk_stream_moderate_sndbuf(sk);
  2800. /* Fail only if socket is _under_ its sndbuf.
  2801. * In this case we cannot block, so that we have to fail.
  2802. */
  2803. if (sk->sk_wmem_queued + size >= sk->sk_sndbuf) {
  2804. /* Force charge with __GFP_NOFAIL */
  2805. if (memcg_charge && !charged) {
  2806. mem_cgroup_charge_skmem(sk->sk_memcg, amt,
  2807. gfp_memcg_charge() | __GFP_NOFAIL);
  2808. }
  2809. return 1;
  2810. }
  2811. }
  2812. if (kind == SK_MEM_SEND || (kind == SK_MEM_RECV && charged))
  2813. trace_sock_exceed_buf_limit(sk, prot, allocated, kind);
  2814. sk_memory_allocated_sub(sk, amt);
  2815. if (memcg_charge && charged)
  2816. mem_cgroup_uncharge_skmem(sk->sk_memcg, amt);
  2817. return 0;
  2818. }
  2819. /**
  2820. * __sk_mem_schedule - increase sk_forward_alloc and memory_allocated
  2821. * @sk: socket
  2822. * @size: memory size to allocate
  2823. * @kind: allocation type
  2824. *
  2825. * If kind is SK_MEM_SEND, it means wmem allocation. Otherwise it means
  2826. * rmem allocation. This function assumes that protocols which have
  2827. * memory_pressure use sk_wmem_queued as write buffer accounting.
  2828. */
  2829. int __sk_mem_schedule(struct sock *sk, int size, int kind)
  2830. {
  2831. int ret, amt = sk_mem_pages(size);
  2832. sk_forward_alloc_add(sk, amt << PAGE_SHIFT);
  2833. ret = __sk_mem_raise_allocated(sk, size, amt, kind);
  2834. if (!ret)
  2835. sk_forward_alloc_add(sk, -(amt << PAGE_SHIFT));
  2836. return ret;
  2837. }
  2838. EXPORT_SYMBOL(__sk_mem_schedule);
  2839. /**
  2840. * __sk_mem_reduce_allocated - reclaim memory_allocated
  2841. * @sk: socket
  2842. * @amount: number of quanta
  2843. *
  2844. * Similar to __sk_mem_reclaim(), but does not update sk_forward_alloc
  2845. */
  2846. void __sk_mem_reduce_allocated(struct sock *sk, int amount)
  2847. {
  2848. sk_memory_allocated_sub(sk, amount);
  2849. if (mem_cgroup_sockets_enabled && sk->sk_memcg)
  2850. mem_cgroup_uncharge_skmem(sk->sk_memcg, amount);
  2851. if (sk_under_global_memory_pressure(sk) &&
  2852. (sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0)))
  2853. sk_leave_memory_pressure(sk);
  2854. }
  2855. /**
  2856. * __sk_mem_reclaim - reclaim sk_forward_alloc and memory_allocated
  2857. * @sk: socket
  2858. * @amount: number of bytes (rounded down to a PAGE_SIZE multiple)
  2859. */
  2860. void __sk_mem_reclaim(struct sock *sk, int amount)
  2861. {
  2862. amount >>= PAGE_SHIFT;
  2863. sk_forward_alloc_add(sk, -(amount << PAGE_SHIFT));
  2864. __sk_mem_reduce_allocated(sk, amount);
  2865. }
  2866. EXPORT_SYMBOL(__sk_mem_reclaim);
  2867. int sk_set_peek_off(struct sock *sk, int val)
  2868. {
  2869. WRITE_ONCE(sk->sk_peek_off, val);
  2870. return 0;
  2871. }
  2872. EXPORT_SYMBOL_GPL(sk_set_peek_off);
  2873. /*
  2874. * Set of default routines for initialising struct proto_ops when
  2875. * the protocol does not support a particular function. In certain
  2876. * cases where it makes no sense for a protocol to have a "do nothing"
  2877. * function, some default processing is provided.
  2878. */
  2879. int sock_no_bind(struct socket *sock, struct sockaddr *saddr, int len)
  2880. {
  2881. return -EOPNOTSUPP;
  2882. }
  2883. EXPORT_SYMBOL(sock_no_bind);
  2884. int sock_no_connect(struct socket *sock, struct sockaddr *saddr,
  2885. int len, int flags)
  2886. {
  2887. return -EOPNOTSUPP;
  2888. }
  2889. EXPORT_SYMBOL(sock_no_connect);
  2890. int sock_no_socketpair(struct socket *sock1, struct socket *sock2)
  2891. {
  2892. return -EOPNOTSUPP;
  2893. }
  2894. EXPORT_SYMBOL(sock_no_socketpair);
  2895. int sock_no_accept(struct socket *sock, struct socket *newsock, int flags,
  2896. bool kern)
  2897. {
  2898. return -EOPNOTSUPP;
  2899. }
  2900. EXPORT_SYMBOL(sock_no_accept);
  2901. int sock_no_getname(struct socket *sock, struct sockaddr *saddr,
  2902. int peer)
  2903. {
  2904. return -EOPNOTSUPP;
  2905. }
  2906. EXPORT_SYMBOL(sock_no_getname);
  2907. int sock_no_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  2908. {
  2909. return -EOPNOTSUPP;
  2910. }
  2911. EXPORT_SYMBOL(sock_no_ioctl);
  2912. int sock_no_listen(struct socket *sock, int backlog)
  2913. {
  2914. return -EOPNOTSUPP;
  2915. }
  2916. EXPORT_SYMBOL(sock_no_listen);
  2917. int sock_no_shutdown(struct socket *sock, int how)
  2918. {
  2919. return -EOPNOTSUPP;
  2920. }
  2921. EXPORT_SYMBOL(sock_no_shutdown);
  2922. int sock_no_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
  2923. {
  2924. return -EOPNOTSUPP;
  2925. }
  2926. EXPORT_SYMBOL(sock_no_sendmsg);
  2927. int sock_no_sendmsg_locked(struct sock *sk, struct msghdr *m, size_t len)
  2928. {
  2929. return -EOPNOTSUPP;
  2930. }
  2931. EXPORT_SYMBOL(sock_no_sendmsg_locked);
  2932. int sock_no_recvmsg(struct socket *sock, struct msghdr *m, size_t len,
  2933. int flags)
  2934. {
  2935. return -EOPNOTSUPP;
  2936. }
  2937. EXPORT_SYMBOL(sock_no_recvmsg);
  2938. int sock_no_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
  2939. {
  2940. /* Mirror missing mmap method error code */
  2941. return -ENODEV;
  2942. }
  2943. EXPORT_SYMBOL(sock_no_mmap);
  2944. /*
  2945. * When a file is received (via SCM_RIGHTS, etc), we must bump the
  2946. * various sock-based usage counts.
  2947. */
  2948. void __receive_sock(struct file *file)
  2949. {
  2950. struct socket *sock;
  2951. sock = sock_from_file(file);
  2952. if (sock) {
  2953. sock_update_netprioidx(&sock->sk->sk_cgrp_data);
  2954. sock_update_classid(&sock->sk->sk_cgrp_data);
  2955. }
  2956. }
  2957. ssize_t sock_no_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
  2958. {
  2959. ssize_t res;
  2960. struct msghdr msg = {.msg_flags = flags};
  2961. struct kvec iov;
  2962. char *kaddr = kmap(page);
  2963. iov.iov_base = kaddr + offset;
  2964. iov.iov_len = size;
  2965. res = kernel_sendmsg(sock, &msg, &iov, 1, size);
  2966. kunmap(page);
  2967. return res;
  2968. }
  2969. EXPORT_SYMBOL(sock_no_sendpage);
  2970. ssize_t sock_no_sendpage_locked(struct sock *sk, struct page *page,
  2971. int offset, size_t size, int flags)
  2972. {
  2973. ssize_t res;
  2974. struct msghdr msg = {.msg_flags = flags};
  2975. struct kvec iov;
  2976. char *kaddr = kmap(page);
  2977. iov.iov_base = kaddr + offset;
  2978. iov.iov_len = size;
  2979. res = kernel_sendmsg_locked(sk, &msg, &iov, 1, size);
  2980. kunmap(page);
  2981. return res;
  2982. }
  2983. EXPORT_SYMBOL(sock_no_sendpage_locked);
  2984. /*
  2985. * Default Socket Callbacks
  2986. */
  2987. static void sock_def_wakeup(struct sock *sk)
  2988. {
  2989. struct socket_wq *wq;
  2990. rcu_read_lock();
  2991. wq = rcu_dereference(sk->sk_wq);
  2992. if (skwq_has_sleeper(wq))
  2993. wake_up_interruptible_all(&wq->wait);
  2994. rcu_read_unlock();
  2995. }
  2996. static void sock_def_error_report(struct sock *sk)
  2997. {
  2998. struct socket_wq *wq;
  2999. rcu_read_lock();
  3000. wq = rcu_dereference(sk->sk_wq);
  3001. if (skwq_has_sleeper(wq))
  3002. wake_up_interruptible_poll(&wq->wait, EPOLLERR);
  3003. sk_wake_async(sk, SOCK_WAKE_IO, POLL_ERR);
  3004. rcu_read_unlock();
  3005. }
  3006. void sock_def_readable(struct sock *sk)
  3007. {
  3008. struct socket_wq *wq;
  3009. rcu_read_lock();
  3010. wq = rcu_dereference(sk->sk_wq);
  3011. if (skwq_has_sleeper(wq)) {
  3012. int done = 0;
  3013. trace_android_vh_do_wake_up_sync(&wq->wait, &done, sk);
  3014. if (done)
  3015. goto out;
  3016. wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN | EPOLLPRI |
  3017. EPOLLRDNORM | EPOLLRDBAND);
  3018. }
  3019. out:
  3020. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
  3021. rcu_read_unlock();
  3022. }
  3023. static void sock_def_write_space(struct sock *sk)
  3024. {
  3025. struct socket_wq *wq;
  3026. rcu_read_lock();
  3027. /* Do not wake up a writer until he can make "significant"
  3028. * progress. --DaveM
  3029. */
  3030. if (sock_writeable(sk)) {
  3031. wq = rcu_dereference(sk->sk_wq);
  3032. if (skwq_has_sleeper(wq))
  3033. wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
  3034. EPOLLWRNORM | EPOLLWRBAND);
  3035. /* Should agree with poll, otherwise some programs break */
  3036. sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
  3037. }
  3038. rcu_read_unlock();
  3039. }
  3040. /* An optimised version of sock_def_write_space(), should only be called
  3041. * for SOCK_RCU_FREE sockets under RCU read section and after putting
  3042. * ->sk_wmem_alloc.
  3043. */
  3044. static void sock_def_write_space_wfree(struct sock *sk)
  3045. {
  3046. /* Do not wake up a writer until he can make "significant"
  3047. * progress. --DaveM
  3048. */
  3049. if (sock_writeable(sk)) {
  3050. struct socket_wq *wq = rcu_dereference(sk->sk_wq);
  3051. /* rely on refcount_sub from sock_wfree() */
  3052. smp_mb__after_atomic();
  3053. if (wq && waitqueue_active(&wq->wait))
  3054. wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
  3055. EPOLLWRNORM | EPOLLWRBAND);
  3056. /* Should agree with poll, otherwise some programs break */
  3057. sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
  3058. }
  3059. }
  3060. static void sock_def_destruct(struct sock *sk)
  3061. {
  3062. }
  3063. void sk_send_sigurg(struct sock *sk)
  3064. {
  3065. if (sk->sk_socket && sk->sk_socket->file)
  3066. if (send_sigurg(&sk->sk_socket->file->f_owner))
  3067. sk_wake_async(sk, SOCK_WAKE_URG, POLL_PRI);
  3068. }
  3069. EXPORT_SYMBOL(sk_send_sigurg);
  3070. void sk_reset_timer(struct sock *sk, struct timer_list* timer,
  3071. unsigned long expires)
  3072. {
  3073. if (!mod_timer(timer, expires))
  3074. sock_hold(sk);
  3075. }
  3076. EXPORT_SYMBOL(sk_reset_timer);
  3077. void sk_stop_timer(struct sock *sk, struct timer_list* timer)
  3078. {
  3079. if (del_timer(timer))
  3080. __sock_put(sk);
  3081. }
  3082. EXPORT_SYMBOL(sk_stop_timer);
  3083. void sk_stop_timer_sync(struct sock *sk, struct timer_list *timer)
  3084. {
  3085. if (del_timer_sync(timer))
  3086. __sock_put(sk);
  3087. }
  3088. EXPORT_SYMBOL(sk_stop_timer_sync);
  3089. void sock_init_data_uid(struct socket *sock, struct sock *sk, kuid_t uid)
  3090. {
  3091. sk_init_common(sk);
  3092. sk->sk_send_head = NULL;
  3093. timer_setup(&sk->sk_timer, NULL, 0);
  3094. sk->sk_allocation = GFP_KERNEL;
  3095. sk->sk_rcvbuf = READ_ONCE(sysctl_rmem_default);
  3096. sk->sk_sndbuf = READ_ONCE(sysctl_wmem_default);
  3097. sk->sk_state = TCP_CLOSE;
  3098. sk_set_socket(sk, sock);
  3099. sock_set_flag(sk, SOCK_ZAPPED);
  3100. if (sock) {
  3101. sk->sk_type = sock->type;
  3102. RCU_INIT_POINTER(sk->sk_wq, &sock->wq);
  3103. sock->sk = sk;
  3104. } else {
  3105. RCU_INIT_POINTER(sk->sk_wq, NULL);
  3106. }
  3107. sk->sk_uid = uid;
  3108. rwlock_init(&sk->sk_callback_lock);
  3109. if (sk->sk_kern_sock)
  3110. lockdep_set_class_and_name(
  3111. &sk->sk_callback_lock,
  3112. af_kern_callback_keys + sk->sk_family,
  3113. af_family_kern_clock_key_strings[sk->sk_family]);
  3114. else
  3115. lockdep_set_class_and_name(
  3116. &sk->sk_callback_lock,
  3117. af_callback_keys + sk->sk_family,
  3118. af_family_clock_key_strings[sk->sk_family]);
  3119. sk->sk_state_change = sock_def_wakeup;
  3120. sk->sk_data_ready = sock_def_readable;
  3121. sk->sk_write_space = sock_def_write_space;
  3122. sk->sk_error_report = sock_def_error_report;
  3123. sk->sk_destruct = sock_def_destruct;
  3124. sk->sk_frag.page = NULL;
  3125. sk->sk_frag.offset = 0;
  3126. sk->sk_peek_off = -1;
  3127. sk->sk_peer_pid = NULL;
  3128. sk->sk_peer_cred = NULL;
  3129. spin_lock_init(&sk->sk_peer_lock);
  3130. sk->sk_write_pending = 0;
  3131. sk->sk_rcvlowat = 1;
  3132. sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
  3133. sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
  3134. sk->sk_stamp = SK_DEFAULT_STAMP;
  3135. #if BITS_PER_LONG==32
  3136. seqlock_init(&sk->sk_stamp_seq);
  3137. #endif
  3138. atomic_set(&sk->sk_zckey, 0);
  3139. #ifdef CONFIG_NET_RX_BUSY_POLL
  3140. sk->sk_napi_id = 0;
  3141. sk->sk_ll_usec = READ_ONCE(sysctl_net_busy_read);
  3142. #endif
  3143. sk->sk_max_pacing_rate = ~0UL;
  3144. sk->sk_pacing_rate = ~0UL;
  3145. WRITE_ONCE(sk->sk_pacing_shift, 10);
  3146. sk->sk_incoming_cpu = -1;
  3147. sk_rx_queue_clear(sk);
  3148. /*
  3149. * Before updating sk_refcnt, we must commit prior changes to memory
  3150. * (Documentation/RCU/rculist_nulls.rst for details)
  3151. */
  3152. smp_wmb();
  3153. refcount_set(&sk->sk_refcnt, 1);
  3154. atomic_set(&sk->sk_drops, 0);
  3155. }
  3156. EXPORT_SYMBOL(sock_init_data_uid);
  3157. void sock_init_data(struct socket *sock, struct sock *sk)
  3158. {
  3159. kuid_t uid = sock ?
  3160. SOCK_INODE(sock)->i_uid :
  3161. make_kuid(sock_net(sk)->user_ns, 0);
  3162. sock_init_data_uid(sock, sk, uid);
  3163. }
  3164. EXPORT_SYMBOL(sock_init_data);
  3165. void lock_sock_nested(struct sock *sk, int subclass)
  3166. {
  3167. /* The sk_lock has mutex_lock() semantics here. */
  3168. mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_);
  3169. might_sleep();
  3170. spin_lock_bh(&sk->sk_lock.slock);
  3171. if (sock_owned_by_user_nocheck(sk))
  3172. __lock_sock(sk);
  3173. sk->sk_lock.owned = 1;
  3174. spin_unlock_bh(&sk->sk_lock.slock);
  3175. }
  3176. EXPORT_SYMBOL(lock_sock_nested);
  3177. void release_sock(struct sock *sk)
  3178. {
  3179. spin_lock_bh(&sk->sk_lock.slock);
  3180. if (sk->sk_backlog.tail)
  3181. __release_sock(sk);
  3182. /* Warning : release_cb() might need to release sk ownership,
  3183. * ie call sock_release_ownership(sk) before us.
  3184. */
  3185. if (sk->sk_prot->release_cb)
  3186. sk->sk_prot->release_cb(sk);
  3187. sock_release_ownership(sk);
  3188. if (waitqueue_active(&sk->sk_lock.wq))
  3189. wake_up(&sk->sk_lock.wq);
  3190. spin_unlock_bh(&sk->sk_lock.slock);
  3191. }
  3192. EXPORT_SYMBOL(release_sock);
  3193. bool __lock_sock_fast(struct sock *sk) __acquires(&sk->sk_lock.slock)
  3194. {
  3195. might_sleep();
  3196. spin_lock_bh(&sk->sk_lock.slock);
  3197. if (!sock_owned_by_user_nocheck(sk)) {
  3198. /*
  3199. * Fast path return with bottom halves disabled and
  3200. * sock::sk_lock.slock held.
  3201. *
  3202. * The 'mutex' is not contended and holding
  3203. * sock::sk_lock.slock prevents all other lockers to
  3204. * proceed so the corresponding unlock_sock_fast() can
  3205. * avoid the slow path of release_sock() completely and
  3206. * just release slock.
  3207. *
  3208. * From a semantical POV this is equivalent to 'acquiring'
  3209. * the 'mutex', hence the corresponding lockdep
  3210. * mutex_release() has to happen in the fast path of
  3211. * unlock_sock_fast().
  3212. */
  3213. return false;
  3214. }
  3215. __lock_sock(sk);
  3216. sk->sk_lock.owned = 1;
  3217. __acquire(&sk->sk_lock.slock);
  3218. spin_unlock_bh(&sk->sk_lock.slock);
  3219. return true;
  3220. }
  3221. EXPORT_SYMBOL(__lock_sock_fast);
  3222. int sock_gettstamp(struct socket *sock, void __user *userstamp,
  3223. bool timeval, bool time32)
  3224. {
  3225. struct sock *sk = sock->sk;
  3226. struct timespec64 ts;
  3227. sock_enable_timestamp(sk, SOCK_TIMESTAMP);
  3228. ts = ktime_to_timespec64(sock_read_timestamp(sk));
  3229. if (ts.tv_sec == -1)
  3230. return -ENOENT;
  3231. if (ts.tv_sec == 0) {
  3232. ktime_t kt = ktime_get_real();
  3233. sock_write_timestamp(sk, kt);
  3234. ts = ktime_to_timespec64(kt);
  3235. }
  3236. if (timeval)
  3237. ts.tv_nsec /= 1000;
  3238. #ifdef CONFIG_COMPAT_32BIT_TIME
  3239. if (time32)
  3240. return put_old_timespec32(&ts, userstamp);
  3241. #endif
  3242. #ifdef CONFIG_SPARC64
  3243. /* beware of padding in sparc64 timeval */
  3244. if (timeval && !in_compat_syscall()) {
  3245. struct __kernel_old_timeval __user tv = {
  3246. .tv_sec = ts.tv_sec,
  3247. .tv_usec = ts.tv_nsec,
  3248. };
  3249. if (copy_to_user(userstamp, &tv, sizeof(tv)))
  3250. return -EFAULT;
  3251. return 0;
  3252. }
  3253. #endif
  3254. return put_timespec64(&ts, userstamp);
  3255. }
  3256. EXPORT_SYMBOL(sock_gettstamp);
  3257. void sock_enable_timestamp(struct sock *sk, enum sock_flags flag)
  3258. {
  3259. if (!sock_flag(sk, flag)) {
  3260. unsigned long previous_flags = sk->sk_flags;
  3261. sock_set_flag(sk, flag);
  3262. /*
  3263. * we just set one of the two flags which require net
  3264. * time stamping, but time stamping might have been on
  3265. * already because of the other one
  3266. */
  3267. if (sock_needs_netstamp(sk) &&
  3268. !(previous_flags & SK_FLAGS_TIMESTAMP))
  3269. net_enable_timestamp();
  3270. }
  3271. }
  3272. int sock_recv_errqueue(struct sock *sk, struct msghdr *msg, int len,
  3273. int level, int type)
  3274. {
  3275. struct sock_exterr_skb *serr;
  3276. struct sk_buff *skb;
  3277. int copied, err;
  3278. err = -EAGAIN;
  3279. skb = sock_dequeue_err_skb(sk);
  3280. if (skb == NULL)
  3281. goto out;
  3282. copied = skb->len;
  3283. if (copied > len) {
  3284. msg->msg_flags |= MSG_TRUNC;
  3285. copied = len;
  3286. }
  3287. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  3288. if (err)
  3289. goto out_free_skb;
  3290. sock_recv_timestamp(msg, sk, skb);
  3291. serr = SKB_EXT_ERR(skb);
  3292. put_cmsg(msg, level, type, sizeof(serr->ee), &serr->ee);
  3293. msg->msg_flags |= MSG_ERRQUEUE;
  3294. err = copied;
  3295. out_free_skb:
  3296. kfree_skb(skb);
  3297. out:
  3298. return err;
  3299. }
  3300. EXPORT_SYMBOL(sock_recv_errqueue);
  3301. /*
  3302. * Get a socket option on an socket.
  3303. *
  3304. * FIX: POSIX 1003.1g is very ambiguous here. It states that
  3305. * asynchronous errors should be reported by getsockopt. We assume
  3306. * this means if you specify SO_ERROR (otherwise whats the point of it).
  3307. */
  3308. int sock_common_getsockopt(struct socket *sock, int level, int optname,
  3309. char __user *optval, int __user *optlen)
  3310. {
  3311. struct sock *sk = sock->sk;
  3312. /* IPV6_ADDRFORM can change sk->sk_prot under us. */
  3313. return READ_ONCE(sk->sk_prot)->getsockopt(sk, level, optname, optval, optlen);
  3314. }
  3315. EXPORT_SYMBOL(sock_common_getsockopt);
  3316. int sock_common_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
  3317. int flags)
  3318. {
  3319. struct sock *sk = sock->sk;
  3320. int addr_len = 0;
  3321. int err;
  3322. err = sk->sk_prot->recvmsg(sk, msg, size, flags, &addr_len);
  3323. if (err >= 0)
  3324. msg->msg_namelen = addr_len;
  3325. return err;
  3326. }
  3327. EXPORT_SYMBOL(sock_common_recvmsg);
  3328. /*
  3329. * Set socket options on an inet socket.
  3330. */
  3331. int sock_common_setsockopt(struct socket *sock, int level, int optname,
  3332. sockptr_t optval, unsigned int optlen)
  3333. {
  3334. struct sock *sk = sock->sk;
  3335. /* IPV6_ADDRFORM can change sk->sk_prot under us. */
  3336. return READ_ONCE(sk->sk_prot)->setsockopt(sk, level, optname, optval, optlen);
  3337. }
  3338. EXPORT_SYMBOL(sock_common_setsockopt);
  3339. void sk_common_release(struct sock *sk)
  3340. {
  3341. if (sk->sk_prot->destroy)
  3342. sk->sk_prot->destroy(sk);
  3343. /*
  3344. * Observation: when sk_common_release is called, processes have
  3345. * no access to socket. But net still has.
  3346. * Step one, detach it from networking:
  3347. *
  3348. * A. Remove from hash tables.
  3349. */
  3350. sk->sk_prot->unhash(sk);
  3351. /*
  3352. * In this point socket cannot receive new packets, but it is possible
  3353. * that some packets are in flight because some CPU runs receiver and
  3354. * did hash table lookup before we unhashed socket. They will achieve
  3355. * receive queue and will be purged by socket destructor.
  3356. *
  3357. * Also we still have packets pending on receive queue and probably,
  3358. * our own packets waiting in device queues. sock_destroy will drain
  3359. * receive queue, but transmitted packets will delay socket destruction
  3360. * until the last reference will be released.
  3361. */
  3362. sock_orphan(sk);
  3363. xfrm_sk_free_policy(sk);
  3364. sk_refcnt_debug_release(sk);
  3365. sock_put(sk);
  3366. }
  3367. EXPORT_SYMBOL(sk_common_release);
  3368. void sk_get_meminfo(const struct sock *sk, u32 *mem)
  3369. {
  3370. memset(mem, 0, sizeof(*mem) * SK_MEMINFO_VARS);
  3371. mem[SK_MEMINFO_RMEM_ALLOC] = sk_rmem_alloc_get(sk);
  3372. mem[SK_MEMINFO_RCVBUF] = READ_ONCE(sk->sk_rcvbuf);
  3373. mem[SK_MEMINFO_WMEM_ALLOC] = sk_wmem_alloc_get(sk);
  3374. mem[SK_MEMINFO_SNDBUF] = READ_ONCE(sk->sk_sndbuf);
  3375. mem[SK_MEMINFO_FWD_ALLOC] = sk_forward_alloc_get(sk);
  3376. mem[SK_MEMINFO_WMEM_QUEUED] = READ_ONCE(sk->sk_wmem_queued);
  3377. mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc);
  3378. mem[SK_MEMINFO_BACKLOG] = READ_ONCE(sk->sk_backlog.len);
  3379. mem[SK_MEMINFO_DROPS] = atomic_read(&sk->sk_drops);
  3380. }
  3381. #ifdef CONFIG_PROC_FS
  3382. static DECLARE_BITMAP(proto_inuse_idx, PROTO_INUSE_NR);
  3383. int sock_prot_inuse_get(struct net *net, struct proto *prot)
  3384. {
  3385. int cpu, idx = prot->inuse_idx;
  3386. int res = 0;
  3387. for_each_possible_cpu(cpu)
  3388. res += per_cpu_ptr(net->core.prot_inuse, cpu)->val[idx];
  3389. return res >= 0 ? res : 0;
  3390. }
  3391. EXPORT_SYMBOL_GPL(sock_prot_inuse_get);
  3392. int sock_inuse_get(struct net *net)
  3393. {
  3394. int cpu, res = 0;
  3395. for_each_possible_cpu(cpu)
  3396. res += per_cpu_ptr(net->core.prot_inuse, cpu)->all;
  3397. return res;
  3398. }
  3399. EXPORT_SYMBOL_GPL(sock_inuse_get);
  3400. static int __net_init sock_inuse_init_net(struct net *net)
  3401. {
  3402. net->core.prot_inuse = alloc_percpu(struct prot_inuse);
  3403. if (net->core.prot_inuse == NULL)
  3404. return -ENOMEM;
  3405. return 0;
  3406. }
  3407. static void __net_exit sock_inuse_exit_net(struct net *net)
  3408. {
  3409. free_percpu(net->core.prot_inuse);
  3410. }
  3411. static struct pernet_operations net_inuse_ops = {
  3412. .init = sock_inuse_init_net,
  3413. .exit = sock_inuse_exit_net,
  3414. };
  3415. static __init int net_inuse_init(void)
  3416. {
  3417. if (register_pernet_subsys(&net_inuse_ops))
  3418. panic("Cannot initialize net inuse counters");
  3419. return 0;
  3420. }
  3421. core_initcall(net_inuse_init);
  3422. static int assign_proto_idx(struct proto *prot)
  3423. {
  3424. prot->inuse_idx = find_first_zero_bit(proto_inuse_idx, PROTO_INUSE_NR);
  3425. if (unlikely(prot->inuse_idx == PROTO_INUSE_NR - 1)) {
  3426. pr_err("PROTO_INUSE_NR exhausted\n");
  3427. return -ENOSPC;
  3428. }
  3429. set_bit(prot->inuse_idx, proto_inuse_idx);
  3430. return 0;
  3431. }
  3432. static void release_proto_idx(struct proto *prot)
  3433. {
  3434. if (prot->inuse_idx != PROTO_INUSE_NR - 1)
  3435. clear_bit(prot->inuse_idx, proto_inuse_idx);
  3436. }
  3437. #else
  3438. static inline int assign_proto_idx(struct proto *prot)
  3439. {
  3440. return 0;
  3441. }
  3442. static inline void release_proto_idx(struct proto *prot)
  3443. {
  3444. }
  3445. #endif
  3446. static void tw_prot_cleanup(struct timewait_sock_ops *twsk_prot)
  3447. {
  3448. if (!twsk_prot)
  3449. return;
  3450. kfree(twsk_prot->twsk_slab_name);
  3451. twsk_prot->twsk_slab_name = NULL;
  3452. kmem_cache_destroy(twsk_prot->twsk_slab);
  3453. twsk_prot->twsk_slab = NULL;
  3454. }
  3455. static int tw_prot_init(const struct proto *prot)
  3456. {
  3457. struct timewait_sock_ops *twsk_prot = prot->twsk_prot;
  3458. if (!twsk_prot)
  3459. return 0;
  3460. twsk_prot->twsk_slab_name = kasprintf(GFP_KERNEL, "tw_sock_%s",
  3461. prot->name);
  3462. if (!twsk_prot->twsk_slab_name)
  3463. return -ENOMEM;
  3464. twsk_prot->twsk_slab =
  3465. kmem_cache_create(twsk_prot->twsk_slab_name,
  3466. twsk_prot->twsk_obj_size, 0,
  3467. SLAB_ACCOUNT | prot->slab_flags,
  3468. NULL);
  3469. if (!twsk_prot->twsk_slab) {
  3470. pr_crit("%s: Can't create timewait sock SLAB cache!\n",
  3471. prot->name);
  3472. return -ENOMEM;
  3473. }
  3474. return 0;
  3475. }
  3476. static void req_prot_cleanup(struct request_sock_ops *rsk_prot)
  3477. {
  3478. if (!rsk_prot)
  3479. return;
  3480. kfree(rsk_prot->slab_name);
  3481. rsk_prot->slab_name = NULL;
  3482. kmem_cache_destroy(rsk_prot->slab);
  3483. rsk_prot->slab = NULL;
  3484. }
  3485. static int req_prot_init(const struct proto *prot)
  3486. {
  3487. struct request_sock_ops *rsk_prot = prot->rsk_prot;
  3488. if (!rsk_prot)
  3489. return 0;
  3490. rsk_prot->slab_name = kasprintf(GFP_KERNEL, "request_sock_%s",
  3491. prot->name);
  3492. if (!rsk_prot->slab_name)
  3493. return -ENOMEM;
  3494. rsk_prot->slab = kmem_cache_create(rsk_prot->slab_name,
  3495. rsk_prot->obj_size, 0,
  3496. SLAB_ACCOUNT | prot->slab_flags,
  3497. NULL);
  3498. if (!rsk_prot->slab) {
  3499. pr_crit("%s: Can't create request sock SLAB cache!\n",
  3500. prot->name);
  3501. return -ENOMEM;
  3502. }
  3503. return 0;
  3504. }
  3505. int proto_register(struct proto *prot, int alloc_slab)
  3506. {
  3507. int ret = -ENOBUFS;
  3508. if (prot->memory_allocated && !prot->sysctl_mem) {
  3509. pr_err("%s: missing sysctl_mem\n", prot->name);
  3510. return -EINVAL;
  3511. }
  3512. if (prot->memory_allocated && !prot->per_cpu_fw_alloc) {
  3513. pr_err("%s: missing per_cpu_fw_alloc\n", prot->name);
  3514. return -EINVAL;
  3515. }
  3516. if (alloc_slab) {
  3517. prot->slab = kmem_cache_create_usercopy(prot->name,
  3518. prot->obj_size, 0,
  3519. SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT |
  3520. prot->slab_flags,
  3521. prot->useroffset, prot->usersize,
  3522. NULL);
  3523. if (prot->slab == NULL) {
  3524. pr_crit("%s: Can't create sock SLAB cache!\n",
  3525. prot->name);
  3526. goto out;
  3527. }
  3528. if (req_prot_init(prot))
  3529. goto out_free_request_sock_slab;
  3530. if (tw_prot_init(prot))
  3531. goto out_free_timewait_sock_slab;
  3532. }
  3533. mutex_lock(&proto_list_mutex);
  3534. ret = assign_proto_idx(prot);
  3535. if (ret) {
  3536. mutex_unlock(&proto_list_mutex);
  3537. goto out_free_timewait_sock_slab;
  3538. }
  3539. list_add(&prot->node, &proto_list);
  3540. mutex_unlock(&proto_list_mutex);
  3541. return ret;
  3542. out_free_timewait_sock_slab:
  3543. if (alloc_slab)
  3544. tw_prot_cleanup(prot->twsk_prot);
  3545. out_free_request_sock_slab:
  3546. if (alloc_slab) {
  3547. req_prot_cleanup(prot->rsk_prot);
  3548. kmem_cache_destroy(prot->slab);
  3549. prot->slab = NULL;
  3550. }
  3551. out:
  3552. return ret;
  3553. }
  3554. EXPORT_SYMBOL(proto_register);
  3555. void proto_unregister(struct proto *prot)
  3556. {
  3557. mutex_lock(&proto_list_mutex);
  3558. release_proto_idx(prot);
  3559. list_del(&prot->node);
  3560. mutex_unlock(&proto_list_mutex);
  3561. kmem_cache_destroy(prot->slab);
  3562. prot->slab = NULL;
  3563. req_prot_cleanup(prot->rsk_prot);
  3564. tw_prot_cleanup(prot->twsk_prot);
  3565. }
  3566. EXPORT_SYMBOL(proto_unregister);
  3567. int sock_load_diag_module(int family, int protocol)
  3568. {
  3569. if (!protocol) {
  3570. if (!sock_is_registered(family))
  3571. return -ENOENT;
  3572. return request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
  3573. NETLINK_SOCK_DIAG, family);
  3574. }
  3575. #ifdef CONFIG_INET
  3576. if (family == AF_INET &&
  3577. protocol != IPPROTO_RAW &&
  3578. protocol < MAX_INET_PROTOS &&
  3579. !rcu_access_pointer(inet_protos[protocol]))
  3580. return -ENOENT;
  3581. #endif
  3582. return request_module("net-pf-%d-proto-%d-type-%d-%d", PF_NETLINK,
  3583. NETLINK_SOCK_DIAG, family, protocol);
  3584. }
  3585. EXPORT_SYMBOL(sock_load_diag_module);
  3586. #ifdef CONFIG_PROC_FS
  3587. static void *proto_seq_start(struct seq_file *seq, loff_t *pos)
  3588. __acquires(proto_list_mutex)
  3589. {
  3590. mutex_lock(&proto_list_mutex);
  3591. return seq_list_start_head(&proto_list, *pos);
  3592. }
  3593. static void *proto_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  3594. {
  3595. return seq_list_next(v, &proto_list, pos);
  3596. }
  3597. static void proto_seq_stop(struct seq_file *seq, void *v)
  3598. __releases(proto_list_mutex)
  3599. {
  3600. mutex_unlock(&proto_list_mutex);
  3601. }
  3602. static char proto_method_implemented(const void *method)
  3603. {
  3604. return method == NULL ? 'n' : 'y';
  3605. }
  3606. static long sock_prot_memory_allocated(struct proto *proto)
  3607. {
  3608. return proto->memory_allocated != NULL ? proto_memory_allocated(proto) : -1L;
  3609. }
  3610. static const char *sock_prot_memory_pressure(struct proto *proto)
  3611. {
  3612. return proto->memory_pressure != NULL ?
  3613. proto_memory_pressure(proto) ? "yes" : "no" : "NI";
  3614. }
  3615. static void proto_seq_printf(struct seq_file *seq, struct proto *proto)
  3616. {
  3617. seq_printf(seq, "%-9s %4u %6d %6ld %-3s %6u %-3s %-10s "
  3618. "%2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c %2c\n",
  3619. proto->name,
  3620. proto->obj_size,
  3621. sock_prot_inuse_get(seq_file_net(seq), proto),
  3622. sock_prot_memory_allocated(proto),
  3623. sock_prot_memory_pressure(proto),
  3624. proto->max_header,
  3625. proto->slab == NULL ? "no" : "yes",
  3626. module_name(proto->owner),
  3627. proto_method_implemented(proto->close),
  3628. proto_method_implemented(proto->connect),
  3629. proto_method_implemented(proto->disconnect),
  3630. proto_method_implemented(proto->accept),
  3631. proto_method_implemented(proto->ioctl),
  3632. proto_method_implemented(proto->init),
  3633. proto_method_implemented(proto->destroy),
  3634. proto_method_implemented(proto->shutdown),
  3635. proto_method_implemented(proto->setsockopt),
  3636. proto_method_implemented(proto->getsockopt),
  3637. proto_method_implemented(proto->sendmsg),
  3638. proto_method_implemented(proto->recvmsg),
  3639. proto_method_implemented(proto->sendpage),
  3640. proto_method_implemented(proto->bind),
  3641. proto_method_implemented(proto->backlog_rcv),
  3642. proto_method_implemented(proto->hash),
  3643. proto_method_implemented(proto->unhash),
  3644. proto_method_implemented(proto->get_port),
  3645. proto_method_implemented(proto->enter_memory_pressure));
  3646. }
  3647. static int proto_seq_show(struct seq_file *seq, void *v)
  3648. {
  3649. if (v == &proto_list)
  3650. seq_printf(seq, "%-9s %-4s %-8s %-6s %-5s %-7s %-4s %-10s %s",
  3651. "protocol",
  3652. "size",
  3653. "sockets",
  3654. "memory",
  3655. "press",
  3656. "maxhdr",
  3657. "slab",
  3658. "module",
  3659. "cl co di ac io in de sh ss gs se re sp bi br ha uh gp em\n");
  3660. else
  3661. proto_seq_printf(seq, list_entry(v, struct proto, node));
  3662. return 0;
  3663. }
  3664. static const struct seq_operations proto_seq_ops = {
  3665. .start = proto_seq_start,
  3666. .next = proto_seq_next,
  3667. .stop = proto_seq_stop,
  3668. .show = proto_seq_show,
  3669. };
  3670. static __net_init int proto_init_net(struct net *net)
  3671. {
  3672. if (!proc_create_net("protocols", 0444, net->proc_net, &proto_seq_ops,
  3673. sizeof(struct seq_net_private)))
  3674. return -ENOMEM;
  3675. return 0;
  3676. }
  3677. static __net_exit void proto_exit_net(struct net *net)
  3678. {
  3679. remove_proc_entry("protocols", net->proc_net);
  3680. }
  3681. static __net_initdata struct pernet_operations proto_net_ops = {
  3682. .init = proto_init_net,
  3683. .exit = proto_exit_net,
  3684. };
  3685. static int __init proto_init(void)
  3686. {
  3687. return register_pernet_subsys(&proto_net_ops);
  3688. }
  3689. subsys_initcall(proto_init);
  3690. #endif /* PROC_FS */
  3691. #ifdef CONFIG_NET_RX_BUSY_POLL
  3692. bool sk_busy_loop_end(void *p, unsigned long start_time)
  3693. {
  3694. struct sock *sk = p;
  3695. return !skb_queue_empty_lockless(&sk->sk_receive_queue) ||
  3696. sk_busy_loop_timeout(sk, start_time);
  3697. }
  3698. EXPORT_SYMBOL(sk_busy_loop_end);
  3699. #endif /* CONFIG_NET_RX_BUSY_POLL */
  3700. int sock_bind_add(struct sock *sk, struct sockaddr *addr, int addr_len)
  3701. {
  3702. if (!sk->sk_prot->bind_add)
  3703. return -EOPNOTSUPP;
  3704. return sk->sk_prot->bind_add(sk, addr, addr_len);
  3705. }
  3706. EXPORT_SYMBOL(sock_bind_add);