sock.c 72 KB

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