sock.c 76 KB

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