sock.c 77 KB

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